2 * Copyright (C) 2010, 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "WebPlatformStrategies.h"
29 #if USE(PLATFORM_STRATEGIES)
31 #include "BlockingResponseMap.h"
32 #include "PluginInfoStore.h"
33 #include "WebContextMessages.h"
34 #include "WebCookieManager.h"
35 #include "WebCoreArgumentCoders.h"
36 #include "WebProcess.h"
37 #include <WebCore/Color.h>
38 #include <WebCore/KURL.h>
39 #include <WebCore/Page.h>
40 #include <WebCore/PlatformPasteboard.h>
41 #include <wtf/Atomics.h>
44 #include <wtf/RetainPtr.h>
47 using namespace WebCore;
51 void WebPlatformStrategies::initialize()
53 DEFINE_STATIC_LOCAL(WebPlatformStrategies, platformStrategies, ());
54 setPlatformStrategies(&platformStrategies);
57 WebPlatformStrategies::WebPlatformStrategies()
58 : m_pluginCacheIsPopulated(false)
59 , m_shouldRefreshPlugins(false)
63 CookiesStrategy* WebPlatformStrategies::createCookiesStrategy()
68 PluginStrategy* WebPlatformStrategies::createPluginStrategy()
73 VisitedLinkStrategy* WebPlatformStrategies::createVisitedLinkStrategy()
78 PasteboardStrategy* WebPlatformStrategies::createPasteboardStrategy()
85 void WebPlatformStrategies::notifyCookiesChanged()
87 WebCookieManager::shared().dispatchCookiesDidChange();
92 void WebPlatformStrategies::refreshPlugins()
94 m_cachedPlugins.clear();
95 m_pluginCacheIsPopulated = false;
96 m_shouldRefreshPlugins = true;
98 populatePluginCache();
101 void WebPlatformStrategies::getPluginInfo(const WebCore::Page*, Vector<WebCore::PluginInfo>& plugins)
103 populatePluginCache();
104 plugins = m_cachedPlugins;
107 static BlockingResponseMap<Vector<WebCore::PluginInfo> >& responseMap()
109 AtomicallyInitializedStatic(BlockingResponseMap<Vector<WebCore::PluginInfo> >&, responseMap = *new BlockingResponseMap<Vector<WebCore::PluginInfo> >);
113 void handleDidGetPlugins(uint64_t requestID, const Vector<WebCore::PluginInfo>& plugins)
115 responseMap().didReceiveResponse(requestID, adoptPtr(new Vector<WebCore::PluginInfo>(plugins)));
118 static uint64_t generateRequestID()
121 return atomicIncrement(&uniqueID);
124 void WebPlatformStrategies::populatePluginCache()
126 if (m_pluginCacheIsPopulated)
129 ASSERT(m_cachedPlugins.isEmpty());
131 // FIXME: Should we do something in case of error here?
132 uint64_t requestID = generateRequestID();
133 WebProcess::shared().connection()->send(Messages::WebContext::GetPlugins(requestID, m_shouldRefreshPlugins), 0);
135 m_cachedPlugins = *responseMap().waitForResponse(requestID);
137 m_shouldRefreshPlugins = false;
138 m_pluginCacheIsPopulated = true;
141 // VisitedLinkStrategy
143 bool WebPlatformStrategies::isLinkVisited(Page*, LinkHash linkHash, const KURL&, const AtomicString&)
145 return WebProcess::shared().isLinkVisited(linkHash);
148 void WebPlatformStrategies::addVisitedLink(Page*, LinkHash linkHash)
150 WebProcess::shared().addVisitedLink(linkHash);
154 // PasteboardStrategy
156 void WebPlatformStrategies::getTypes(Vector<String>& types, const String& pasteboardName)
158 WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardTypes(pasteboardName),
159 Messages::WebContext::GetPasteboardTypes::Reply(types), 0);
162 PassRefPtr<WebCore::SharedBuffer> WebPlatformStrategies::bufferForType(const String& pasteboardType, const String& pasteboardName)
164 SharedMemory::Handle handle;
166 WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardBufferForType(pasteboardName, pasteboardType),
167 Messages::WebContext::GetPasteboardBufferForType::Reply(handle, size), 0);
170 RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::create(handle, SharedMemory::ReadOnly);
171 return SharedBuffer::create(static_cast<unsigned char *>(sharedMemoryBuffer->data()), size);
174 void WebPlatformStrategies::getPathnamesForType(Vector<String>& pathnames, const String& pasteboardType, const String& pasteboardName)
176 WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardPathnamesForType(pasteboardName, pasteboardType),
177 Messages::WebContext::GetPasteboardPathnamesForType::Reply(pathnames), 0);
180 String WebPlatformStrategies::stringForType(const String& pasteboardType, const String& pasteboardName)
183 WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardStringForType(pasteboardName, pasteboardType),
184 Messages::WebContext::GetPasteboardStringForType::Reply(value), 0);
188 void WebPlatformStrategies::copy(const String& fromPasteboard, const String& toPasteboard)
190 WebProcess::shared().connection()->send(Messages::WebContext::PasteboardCopy(fromPasteboard, toPasteboard), 0);
193 int WebPlatformStrategies::changeCount(const WTF::String &pasteboardName)
195 uint64_t changeCount;
196 WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardChangeCount(pasteboardName),
197 Messages::WebContext::GetPasteboardChangeCount::Reply(changeCount), 0);
201 String WebPlatformStrategies::uniqueName()
203 String pasteboardName;
204 WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardUniqueName(),
205 Messages::WebContext::GetPasteboardUniqueName::Reply(pasteboardName), 0);
206 return pasteboardName;
209 Color WebPlatformStrategies::color(const String& pasteboardName)
212 WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardColor(pasteboardName),
213 Messages::WebContext::GetPasteboardColor::Reply(color), 0);
217 KURL WebPlatformStrategies::url(const String& pasteboardName)
220 WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardURL(pasteboardName),
221 Messages::WebContext::GetPasteboardURL::Reply(urlString), 0);
222 return KURL(ParsedURLString, urlString);
225 void WebPlatformStrategies::addTypes(const Vector<String>& pasteboardTypes, const String& pasteboardName)
227 WebProcess::shared().connection()->send(Messages::WebContext::AddPasteboardTypes(pasteboardName, pasteboardTypes), 0);
230 void WebPlatformStrategies::setTypes(const Vector<String>& pasteboardTypes, const String& pasteboardName)
232 WebProcess::shared().connection()->send(Messages::WebContext::SetPasteboardTypes(pasteboardName, pasteboardTypes), 0);
235 void WebPlatformStrategies::setBufferForType(PassRefPtr<SharedBuffer> buffer, const String& pasteboardType, const String& pasteboardName)
237 SharedMemory::Handle handle;
239 RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::create(buffer->size());
240 memcpy(sharedMemoryBuffer->data(), buffer->data(), buffer->size());
241 sharedMemoryBuffer->createHandle(handle, SharedMemory::ReadOnly);
243 WebProcess::shared().connection()->send(Messages::WebContext::SetPasteboardBufferForType(pasteboardName, pasteboardType, handle, buffer ? buffer->size() : 0), 0);
246 void WebPlatformStrategies::setPathnamesForType(const Vector<String>& pathnames, const String& pasteboardType, const String& pasteboardName)
248 WebProcess::shared().connection()->send(Messages::WebContext::SetPasteboardPathnamesForType(pasteboardName, pasteboardType, pathnames), 0);
251 void WebPlatformStrategies::setStringForType(const String& string, const String& pasteboardType, const String& pasteboardName)
253 WebProcess::shared().connection()->send(Messages::WebContext::SetPasteboardStringForType(pasteboardName, pasteboardType, string), 0);
257 } // namespace WebKit
259 #endif // USE(PLATFORM_STRATEGIES)