/* * Copyright (C) 2014 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #import "config.h" #import "ServicesController.h" #if ENABLE(SERVICE_CONTROLS) #import "WebContext.h" #import "WebProcessMessages.h" #import #if __has_include() #import #else typedef enum { NSSharingServicePickerStyleMenu = 0, NSSharingServicePickerStyleRollover = 1, NSSharingServicePickerStyleTextSelection = 2, NSSharingServicePickerStyleDataDetector = 3 } NSSharingServicePickerStyle; @interface NSSharingServicePicker (Details) @property NSSharingServicePickerStyle style; - (NSMenu *)menu; @end #endif namespace WebKit { ServicesController& ServicesController::shared() { static NeverDestroyed sharedController; return sharedController; } ServicesController::ServicesController() : m_refreshExistingServicesTimer(RunLoop::main(), this, &ServicesController::refreshExistingServicesTimerFired) , m_hasImageServices(false) , m_hasSelectionServices(false) { m_refreshExistingServicesTimer.startOneShot(0); } void ServicesController::refreshExistingServices(WebContext* context) { ASSERT(context); m_contextsToNotify.add(context); m_refreshExistingServicesTimer.startOneShot(0); } void ServicesController::refreshExistingServicesTimerFired() { static NeverDestroyed image([[NSImage alloc] init]); RetainPtr picker = adoptNS([[NSSharingServicePicker alloc] initWithItems:@[ image ]]); [picker setStyle:NSSharingServicePickerStyleRollover]; bool hasImageServices = picker.get().menu; static NeverDestroyed attributedString([[NSAttributedString alloc] initWithString:@"a"]); picker = adoptNS([[NSSharingServicePicker alloc] initWithItems:@[ attributedString ]]); [picker setStyle:NSSharingServicePickerStyleTextSelection]; bool hasSelectionServices = picker.get().menu; bool notifyContexts = (hasImageServices != m_hasImageServices) || (hasSelectionServices != m_hasSelectionServices); m_hasSelectionServices = hasSelectionServices; m_hasImageServices = hasImageServices; if (notifyContexts) { for (const RefPtr& context : m_contextsToNotify) context->sendToAllProcesses(Messages::WebProcess::SetEnabledServices(m_hasImageServices, m_hasSelectionServices)); } m_contextsToNotify.clear(); } } // namespace WebKit #endif // ENABLE(SERVICE_CONTROLS)