Cancel composition when focused node was changed
[framework/web/webkit-efl.git] / Source / WebKit2 / WebProcess / WebCoreSupport / WebPlatformStrategies.cpp
1 /*
2  * Copyright (C) 2010, 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #include "config.h"
27 #include "WebPlatformStrategies.h"
28
29 #if USE(PLATFORM_STRATEGIES)
30
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>
42
43 #if USE(CF)
44 #include <wtf/RetainPtr.h>
45 #endif
46
47 using namespace WebCore;
48
49 namespace WebKit {
50
51 void WebPlatformStrategies::initialize()
52 {
53     DEFINE_STATIC_LOCAL(WebPlatformStrategies, platformStrategies, ());
54     setPlatformStrategies(&platformStrategies);
55 }
56
57 WebPlatformStrategies::WebPlatformStrategies()
58     : m_pluginCacheIsPopulated(false)
59     , m_shouldRefreshPlugins(false)
60 {
61 }
62
63 CookiesStrategy* WebPlatformStrategies::createCookiesStrategy()
64 {
65     return this;
66 }
67
68 PluginStrategy* WebPlatformStrategies::createPluginStrategy()
69 {
70     return this;
71 }
72
73 VisitedLinkStrategy* WebPlatformStrategies::createVisitedLinkStrategy()
74 {
75     return this;
76 }
77
78 PasteboardStrategy* WebPlatformStrategies::createPasteboardStrategy()
79 {
80     return this;
81 }
82
83 // CookiesStrategy
84
85 void WebPlatformStrategies::notifyCookiesChanged()
86 {
87     WebCookieManager::shared().dispatchCookiesDidChange();
88 }
89
90 // PluginStrategy
91
92 void WebPlatformStrategies::refreshPlugins()
93 {
94     m_cachedPlugins.clear();
95     m_pluginCacheIsPopulated = false;
96     m_shouldRefreshPlugins = true;
97
98     populatePluginCache();
99 }
100
101 void WebPlatformStrategies::getPluginInfo(const WebCore::Page*, Vector<WebCore::PluginInfo>& plugins)
102 {
103     populatePluginCache();
104     plugins = m_cachedPlugins;
105 }
106
107 static BlockingResponseMap<Vector<WebCore::PluginInfo> >& responseMap()
108 {
109     AtomicallyInitializedStatic(BlockingResponseMap<Vector<WebCore::PluginInfo> >&, responseMap = *new BlockingResponseMap<Vector<WebCore::PluginInfo> >);
110     return responseMap;
111 }
112
113 void handleDidGetPlugins(uint64_t requestID, const Vector<WebCore::PluginInfo>& plugins)
114 {
115     responseMap().didReceiveResponse(requestID, adoptPtr(new Vector<WebCore::PluginInfo>(plugins)));
116 }
117
118 static uint64_t generateRequestID()
119 {
120     static int uniqueID;
121     return atomicIncrement(&uniqueID);
122 }
123
124 void WebPlatformStrategies::populatePluginCache()
125 {
126     if (m_pluginCacheIsPopulated)
127         return;
128
129     ASSERT(m_cachedPlugins.isEmpty());
130     
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);
134
135     m_cachedPlugins = *responseMap().waitForResponse(requestID);
136     
137     m_shouldRefreshPlugins = false;
138     m_pluginCacheIsPopulated = true;
139 }
140
141 // VisitedLinkStrategy
142
143 bool WebPlatformStrategies::isLinkVisited(Page*, LinkHash linkHash, const KURL&, const AtomicString&)
144 {
145     return WebProcess::shared().isLinkVisited(linkHash);
146 }
147
148 void WebPlatformStrategies::addVisitedLink(Page*, LinkHash linkHash)
149 {
150     WebProcess::shared().addVisitedLink(linkHash);
151 }
152
153 #if PLATFORM(MAC)
154 // PasteboardStrategy
155
156 void WebPlatformStrategies::getTypes(Vector<String>& types, const String& pasteboardName)
157 {
158     WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardTypes(pasteboardName),
159                                                 Messages::WebContext::GetPasteboardTypes::Reply(types), 0);
160 }
161
162 PassRefPtr<WebCore::SharedBuffer> WebPlatformStrategies::bufferForType(const String& pasteboardType, const String& pasteboardName)
163 {
164     SharedMemory::Handle handle;
165     uint64_t size = 0;
166     WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardBufferForType(pasteboardName, pasteboardType),
167                                                 Messages::WebContext::GetPasteboardBufferForType::Reply(handle, size), 0);
168     if (handle.isNull())
169         return 0;
170     RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::create(handle, SharedMemory::ReadOnly);
171     return SharedBuffer::create(static_cast<unsigned char *>(sharedMemoryBuffer->data()), size);
172 }
173
174 void WebPlatformStrategies::getPathnamesForType(Vector<String>& pathnames, const String& pasteboardType, const String& pasteboardName)
175 {
176     WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardPathnamesForType(pasteboardName, pasteboardType),
177                                                 Messages::WebContext::GetPasteboardPathnamesForType::Reply(pathnames), 0);
178 }
179
180 String WebPlatformStrategies::stringForType(const String& pasteboardType, const String& pasteboardName)
181 {
182     String value;
183     WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardStringForType(pasteboardName, pasteboardType),
184                                                 Messages::WebContext::GetPasteboardStringForType::Reply(value), 0);
185     return value;
186 }
187
188 void WebPlatformStrategies::copy(const String& fromPasteboard, const String& toPasteboard)
189 {
190     WebProcess::shared().connection()->send(Messages::WebContext::PasteboardCopy(fromPasteboard, toPasteboard), 0);
191 }
192
193 int WebPlatformStrategies::changeCount(const WTF::String &pasteboardName)
194 {
195     uint64_t changeCount;
196     WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardChangeCount(pasteboardName),
197                                                 Messages::WebContext::GetPasteboardChangeCount::Reply(changeCount), 0);
198     return changeCount;
199 }
200
201 String WebPlatformStrategies::uniqueName()
202 {
203     String pasteboardName;
204     WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardUniqueName(),
205                                                 Messages::WebContext::GetPasteboardUniqueName::Reply(pasteboardName), 0);
206     return pasteboardName;
207 }
208
209 Color WebPlatformStrategies::color(const String& pasteboardName)
210 {
211     Color color;
212     WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardColor(pasteboardName),
213                                                 Messages::WebContext::GetPasteboardColor::Reply(color), 0);
214     return color;
215 }
216
217 KURL WebPlatformStrategies::url(const String& pasteboardName)
218 {
219     String urlString;
220     WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardURL(pasteboardName),
221                                                 Messages::WebContext::GetPasteboardURL::Reply(urlString), 0);
222     return KURL(ParsedURLString, urlString);
223 }
224
225 void WebPlatformStrategies::addTypes(const Vector<String>& pasteboardTypes, const String& pasteboardName)
226 {
227     WebProcess::shared().connection()->send(Messages::WebContext::AddPasteboardTypes(pasteboardName, pasteboardTypes), 0);
228 }
229
230 void WebPlatformStrategies::setTypes(const Vector<String>& pasteboardTypes, const String& pasteboardName)
231 {
232     WebProcess::shared().connection()->send(Messages::WebContext::SetPasteboardTypes(pasteboardName, pasteboardTypes), 0);
233 }
234
235 void WebPlatformStrategies::setBufferForType(PassRefPtr<SharedBuffer> buffer, const String& pasteboardType, const String& pasteboardName)
236 {
237     SharedMemory::Handle handle;
238     if (buffer) {
239         RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::create(buffer->size());
240         memcpy(sharedMemoryBuffer->data(), buffer->data(), buffer->size());
241         sharedMemoryBuffer->createHandle(handle, SharedMemory::ReadOnly);
242     }
243     WebProcess::shared().connection()->send(Messages::WebContext::SetPasteboardBufferForType(pasteboardName, pasteboardType, handle, buffer ? buffer->size() : 0), 0);
244 }
245
246 void WebPlatformStrategies::setPathnamesForType(const Vector<String>& pathnames, const String& pasteboardType, const String& pasteboardName)
247 {
248     WebProcess::shared().connection()->send(Messages::WebContext::SetPasteboardPathnamesForType(pasteboardName, pasteboardType, pathnames), 0);
249 }
250
251 void WebPlatformStrategies::setStringForType(const String& string, const String& pasteboardType, const String& pasteboardName)
252 {
253     WebProcess::shared().connection()->send(Messages::WebContext::SetPasteboardStringForType(pasteboardName, pasteboardType, string), 0);
254 }
255 #endif
256
257 } // namespace WebKit
258
259 #endif // USE(PLATFORM_STRATEGIES)