• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "config.h"
32 #include "PlatformBridge.h"
33 
34 #include <googleurl/src/url_util.h>
35 
36 #include "Chrome.h"
37 #include "ChromeClientImpl.h"
38 #include "WebAudioBus.h"
39 #include "WebClipboard.h"
40 #include "WebCookie.h"
41 #include "WebCookieJar.h"
42 #include "WebData.h"
43 #include "WebDragData.h"
44 #include "WebFileUtilities.h"
45 #include "WebFrameClient.h"
46 #include "WebFrameImpl.h"
47 #include "WebIDBKey.h"
48 #include "WebImage.h"
49 #include "WebKit.h"
50 #include "WebKitClient.h"
51 #include "WebMimeRegistry.h"
52 #include "WebPluginContainerImpl.h"
53 #include "WebPluginListBuilderImpl.h"
54 #include "WebSandboxSupport.h"
55 #include "WebSerializedScriptValue.h"
56 #include "WebScreenInfo.h"
57 #include "WebString.h"
58 #include "WebURL.h"
59 #include "WebVector.h"
60 #include "WebViewClient.h"
61 #include "WebViewImpl.h"
62 #include "WebWorkerClientImpl.h"
63 
64 #if USE(CG)
65 #include <CoreGraphics/CGContext.h>
66 #endif
67 
68 #if OS(WINDOWS)
69 #include "WebRect.h"
70 #include "win/WebThemeEngine.h"
71 #endif
72 
73 #if OS(LINUX) || OS(FREEBSD)
74 #include "linux/WebThemeEngine.h"
75 #include "WebFontInfo.h"
76 #include "WebFontRenderStyle.h"
77 #endif
78 
79 #if OS(DARWIN)
80 #include "mac/WebThemeEngine.h"
81 #endif
82 
83 #if WEBKIT_USING_SKIA
84 #include "NativeImageSkia.h"
85 #endif
86 
87 #include "BitmapImage.h"
88 #include "Cookie.h"
89 #include "FrameView.h"
90 #include "GraphicsContext.h"
91 #include "IDBFactoryBackendProxy.h"
92 #include "KURL.h"
93 #include "NotImplemented.h"
94 #include "PlatformContextSkia.h"
95 #include "PluginData.h"
96 #include "SharedBuffer.h"
97 
98 #include "Worker.h"
99 #include "WorkerContextProxy.h"
100 #include <wtf/Assertions.h>
101 
102 // We are part of the WebKit implementation.
103 using namespace WebKit;
104 
105 namespace WebCore {
106 
toWebWidgetClient(Widget * widget)107 static WebWidgetClient* toWebWidgetClient(Widget* widget)
108 {
109     if (!widget)
110         return 0;
111 
112     FrameView* view;
113     if (widget->isFrameView())
114         view = static_cast<FrameView*>(widget);
115     else if (widget->parent() && widget->parent()->isFrameView())
116         view = static_cast<FrameView*>(widget->parent());
117     else
118         return 0;
119 
120     Page* page = view->frame() ? view->frame()->page() : 0;
121     if (!page)
122         return 0;
123 
124     void* webView = page->chrome()->client()->webView();
125     if (!webView)
126         return 0;
127 
128     return static_cast<WebViewImpl*>(webView)->client();
129 }
130 
getCookieJar(const Document * document)131 static WebCookieJar* getCookieJar(const Document* document)
132 {
133     WebFrameImpl* frameImpl = WebFrameImpl::fromFrame(document->frame());
134     if (!frameImpl || !frameImpl->client())
135         return 0;
136     WebCookieJar* cookieJar = frameImpl->client()->cookieJar(frameImpl);
137     if (!cookieJar)
138         cookieJar = webKitClient()->cookieJar();
139     return cookieJar;
140 }
141 
142 // Cache ----------------------------------------------------------------------
143 
cacheMetadata(const KURL & url,double responseTime,const Vector<char> & data)144 void PlatformBridge::cacheMetadata(const KURL& url, double responseTime, const Vector<char>& data)
145 {
146     webKitClient()->cacheMetadata(url, responseTime, data.data(), data.size());
147 }
148 
149 // Clipboard ------------------------------------------------------------------
150 
clipboardIsFormatAvailable(PasteboardPrivate::ClipboardFormat format,PasteboardPrivate::ClipboardBuffer buffer)151 bool PlatformBridge::clipboardIsFormatAvailable(
152     PasteboardPrivate::ClipboardFormat format,
153     PasteboardPrivate::ClipboardBuffer buffer)
154 {
155     return webKitClient()->clipboard()->isFormatAvailable(
156         static_cast<WebClipboard::Format>(format),
157         static_cast<WebClipboard::Buffer>(buffer));
158 }
159 
clipboardReadPlainText(PasteboardPrivate::ClipboardBuffer buffer)160 String PlatformBridge::clipboardReadPlainText(
161     PasteboardPrivate::ClipboardBuffer buffer)
162 {
163     return webKitClient()->clipboard()->readPlainText(
164         static_cast<WebClipboard::Buffer>(buffer));
165 }
166 
clipboardReadHTML(PasteboardPrivate::ClipboardBuffer buffer,String * htmlText,KURL * sourceURL)167 void PlatformBridge::clipboardReadHTML(
168     PasteboardPrivate::ClipboardBuffer buffer,
169     String* htmlText, KURL* sourceURL)
170 {
171     WebURL url;
172     *htmlText = webKitClient()->clipboard()->readHTML(
173         static_cast<WebClipboard::Buffer>(buffer), &url);
174     *sourceURL = url;
175 }
176 
clipboardReadImage(PasteboardPrivate::ClipboardBuffer buffer)177 PassRefPtr<SharedBuffer> PlatformBridge::clipboardReadImage(
178     PasteboardPrivate::ClipboardBuffer buffer)
179 {
180     return webKitClient()->clipboard()->readImage(static_cast<WebClipboard::Buffer>(buffer));
181 }
182 
clipboardWriteSelection(const String & htmlText,const KURL & sourceURL,const String & plainText,bool writeSmartPaste)183 void PlatformBridge::clipboardWriteSelection(const String& htmlText,
184                                              const KURL& sourceURL,
185                                              const String& plainText,
186                                              bool writeSmartPaste)
187 {
188     webKitClient()->clipboard()->writeHTML(
189         htmlText, sourceURL, plainText, writeSmartPaste);
190 }
191 
clipboardWritePlainText(const String & plainText)192 void PlatformBridge::clipboardWritePlainText(const String& plainText)
193 {
194     webKitClient()->clipboard()->writePlainText(plainText);
195 }
196 
clipboardWriteURL(const KURL & url,const String & title)197 void PlatformBridge::clipboardWriteURL(const KURL& url, const String& title)
198 {
199     webKitClient()->clipboard()->writeURL(url, title);
200 }
201 
clipboardWriteImage(NativeImagePtr image,const KURL & sourceURL,const String & title)202 void PlatformBridge::clipboardWriteImage(NativeImagePtr image,
203                                          const KURL& sourceURL,
204                                          const String& title)
205 {
206 #if WEBKIT_USING_SKIA
207     WebImage webImage(*image);
208 #else
209     WebImage webImage(image);
210 #endif
211     webKitClient()->clipboard()->writeImage(webImage, sourceURL, title);
212 }
213 
clipboardWriteData(const String & type,const String & data,const String & metadata)214 void PlatformBridge::clipboardWriteData(const String& type,
215                                         const String& data,
216                                         const String& metadata)
217 {
218     webKitClient()->clipboard()->writeData(type, data, metadata);
219 }
220 
clipboardReadAvailableTypes(PasteboardPrivate::ClipboardBuffer buffer,bool * containsFilenames)221 HashSet<String> PlatformBridge::clipboardReadAvailableTypes(
222     PasteboardPrivate::ClipboardBuffer buffer, bool* containsFilenames)
223 {
224     WebVector<WebString> result = webKitClient()->clipboard()->readAvailableTypes(
225         static_cast<WebClipboard::Buffer>(buffer), containsFilenames);
226     HashSet<String> types;
227     for (size_t i = 0; i < result.size(); ++i)
228         types.add(result[i]);
229     return types;
230 }
231 
clipboardReadData(PasteboardPrivate::ClipboardBuffer buffer,const String & type,String & data,String & metadata)232 bool PlatformBridge::clipboardReadData(PasteboardPrivate::ClipboardBuffer buffer,
233                                        const String& type, String& data, String& metadata)
234 {
235     WebString resultData;
236     WebString resultMetadata;
237     bool succeeded = webKitClient()->clipboard()->readData(
238         static_cast<WebClipboard::Buffer>(buffer), type, &resultData, &resultMetadata);
239     if (succeeded) {
240         data = resultData;
241         metadata = resultMetadata;
242     }
243     return succeeded;
244 }
245 
clipboardReadFilenames(PasteboardPrivate::ClipboardBuffer buffer)246 Vector<String> PlatformBridge::clipboardReadFilenames(PasteboardPrivate::ClipboardBuffer buffer)
247 {
248     WebVector<WebString> result = webKitClient()->clipboard()->readFilenames(
249         static_cast<WebClipboard::Buffer>(buffer));
250     Vector<String> convertedResult;
251     for (size_t i = 0; i < result.size(); ++i)
252         convertedResult.append(result[i]);
253     return convertedResult;
254 }
255 
256 // Cookies --------------------------------------------------------------------
257 
setCookies(const Document * document,const KURL & url,const String & value)258 void PlatformBridge::setCookies(const Document* document, const KURL& url,
259                                 const String& value)
260 {
261     WebCookieJar* cookieJar = getCookieJar(document);
262     if (cookieJar)
263         cookieJar->setCookie(url, document->firstPartyForCookies(), value);
264 }
265 
cookies(const Document * document,const KURL & url)266 String PlatformBridge::cookies(const Document* document, const KURL& url)
267 {
268     String result;
269     WebCookieJar* cookieJar = getCookieJar(document);
270     if (cookieJar)
271         result = cookieJar->cookies(url, document->firstPartyForCookies());
272     return result;
273 }
274 
cookieRequestHeaderFieldValue(const Document * document,const KURL & url)275 String PlatformBridge::cookieRequestHeaderFieldValue(const Document* document,
276                                                      const KURL& url)
277 {
278     String result;
279     WebCookieJar* cookieJar = getCookieJar(document);
280     if (cookieJar)
281         result = cookieJar->cookieRequestHeaderFieldValue(url, document->firstPartyForCookies());
282     return result;
283 }
284 
rawCookies(const Document * document,const KURL & url,Vector<Cookie> & rawCookies)285 bool PlatformBridge::rawCookies(const Document* document, const KURL& url, Vector<Cookie>& rawCookies)
286 {
287     rawCookies.clear();
288     WebVector<WebCookie> webCookies;
289 
290     WebCookieJar* cookieJar = getCookieJar(document);
291     if (cookieJar)
292         cookieJar->rawCookies(url, document->firstPartyForCookies(), webCookies);
293 
294     for (unsigned i = 0; i < webCookies.size(); ++i) {
295         const WebCookie& webCookie = webCookies[i];
296         Cookie cookie(webCookie.name,
297                       webCookie.value,
298                       webCookie.domain,
299                       webCookie.path,
300                       webCookie.expires,
301                       webCookie.httpOnly,
302                       webCookie.secure,
303                       webCookie.session);
304         rawCookies.append(cookie);
305     }
306     return true;
307 }
308 
deleteCookie(const Document * document,const KURL & url,const String & cookieName)309 void PlatformBridge::deleteCookie(const Document* document, const KURL& url, const String& cookieName)
310 {
311     WebCookieJar* cookieJar = getCookieJar(document);
312     if (cookieJar)
313         cookieJar->deleteCookie(url, cookieName);
314 }
315 
cookiesEnabled(const Document * document)316 bool PlatformBridge::cookiesEnabled(const Document* document)
317 {
318     bool result = false;
319     WebCookieJar* cookieJar = getCookieJar(document);
320     if (cookieJar)
321         result = cookieJar->cookiesEnabled(document->cookieURL(), document->firstPartyForCookies());
322     return result;
323 }
324 
325 // DNS ------------------------------------------------------------------------
326 
prefetchDNS(const String & hostname)327 void PlatformBridge::prefetchDNS(const String& hostname)
328 {
329     webKitClient()->prefetchHostName(hostname);
330 }
331 
332 // File ------------------------------------------------------------------------
333 
fileExists(const String & path)334 bool PlatformBridge::fileExists(const String& path)
335 {
336     return webKitClient()->fileUtilities()->fileExists(path);
337 }
338 
deleteFile(const String & path)339 bool PlatformBridge::deleteFile(const String& path)
340 {
341     return webKitClient()->fileUtilities()->deleteFile(path);
342 }
343 
deleteEmptyDirectory(const String & path)344 bool PlatformBridge::deleteEmptyDirectory(const String& path)
345 {
346     return webKitClient()->fileUtilities()->deleteEmptyDirectory(path);
347 }
348 
getFileSize(const String & path,long long & result)349 bool PlatformBridge::getFileSize(const String& path, long long& result)
350 {
351     return webKitClient()->fileUtilities()->getFileSize(path, result);
352 }
353 
revealFolderInOS(const String & path)354 void PlatformBridge::revealFolderInOS(const String& path)
355 {
356     webKitClient()->fileUtilities()->revealFolderInOS(path);
357 }
358 
getFileModificationTime(const String & path,time_t & result)359 bool PlatformBridge::getFileModificationTime(const String& path, time_t& result)
360 {
361     double modificationTime;
362     if (!webKitClient()->fileUtilities()->getFileModificationTime(path, modificationTime))
363         return false;
364     result = static_cast<time_t>(modificationTime);
365     return true;
366 }
367 
directoryName(const String & path)368 String PlatformBridge::directoryName(const String& path)
369 {
370     return webKitClient()->fileUtilities()->directoryName(path);
371 }
372 
pathByAppendingComponent(const String & path,const String & component)373 String PlatformBridge::pathByAppendingComponent(const String& path, const String& component)
374 {
375     return webKitClient()->fileUtilities()->pathByAppendingComponent(path, component);
376 }
377 
makeAllDirectories(const String & path)378 bool PlatformBridge::makeAllDirectories(const String& path)
379 {
380     return webKitClient()->fileUtilities()->makeAllDirectories(path);
381 }
382 
getAbsolutePath(const String & path)383 String PlatformBridge::getAbsolutePath(const String& path)
384 {
385     return webKitClient()->fileUtilities()->getAbsolutePath(path);
386 }
387 
isDirectory(const String & path)388 bool PlatformBridge::isDirectory(const String& path)
389 {
390     return webKitClient()->fileUtilities()->isDirectory(path);
391 }
392 
filePathToURL(const String & path)393 KURL PlatformBridge::filePathToURL(const String& path)
394 {
395     return webKitClient()->fileUtilities()->filePathToURL(path);
396 }
397 
openFile(const String & path,FileOpenMode mode)398 PlatformFileHandle PlatformBridge::openFile(const String& path, FileOpenMode mode)
399 {
400     return webKitClient()->fileUtilities()->openFile(path, mode);
401 }
402 
closeFile(PlatformFileHandle & handle)403 void PlatformBridge::closeFile(PlatformFileHandle& handle)
404 {
405     webKitClient()->fileUtilities()->closeFile(handle);
406 }
407 
seekFile(PlatformFileHandle handle,long long offset,FileSeekOrigin origin)408 long long PlatformBridge::seekFile(PlatformFileHandle handle, long long offset, FileSeekOrigin origin)
409 {
410     return webKitClient()->fileUtilities()->seekFile(handle, offset, origin);
411 }
412 
truncateFile(PlatformFileHandle handle,long long offset)413 bool PlatformBridge::truncateFile(PlatformFileHandle handle, long long offset)
414 {
415     return webKitClient()->fileUtilities()->truncateFile(handle, offset);
416 }
417 
readFromFile(PlatformFileHandle handle,char * data,int length)418 int PlatformBridge::readFromFile(PlatformFileHandle handle, char* data, int length)
419 {
420     return webKitClient()->fileUtilities()->readFromFile(handle, data, length);
421 }
422 
writeToFile(PlatformFileHandle handle,const char * data,int length)423 int PlatformBridge::writeToFile(PlatformFileHandle handle, const char* data, int length)
424 {
425     return webKitClient()->fileUtilities()->writeToFile(handle, data, length);
426 }
427 
428 // Font -----------------------------------------------------------------------
429 
430 #if OS(WINDOWS)
ensureFontLoaded(HFONT font)431 bool PlatformBridge::ensureFontLoaded(HFONT font)
432 {
433     WebSandboxSupport* ss = webKitClient()->sandboxSupport();
434 
435     // if there is no sandbox, then we can assume the font
436     // was able to be loaded successfully already
437     return ss ? ss->ensureFontLoaded(font) : true;
438 }
439 #endif
440 
441 #if OS(LINUX) || OS(FREEBSD)
getFontFamilyForCharacters(const UChar * characters,size_t numCharacters,const char * preferredLocale)442 String PlatformBridge::getFontFamilyForCharacters(const UChar* characters, size_t numCharacters, const char* preferredLocale)
443 {
444     if (webKitClient()->sandboxSupport())
445         return webKitClient()->sandboxSupport()->getFontFamilyForCharacters(characters, numCharacters, preferredLocale);
446 
447     WebCString family = WebFontInfo::familyForChars(characters, numCharacters, preferredLocale);
448     if (family.data())
449         return WebString::fromUTF8(family.data());
450 
451     return WebString();
452 }
453 
getRenderStyleForStrike(const char * font,int sizeAndStyle,FontRenderStyle * result)454 void PlatformBridge::getRenderStyleForStrike(const char* font, int sizeAndStyle, FontRenderStyle* result)
455 {
456     WebFontRenderStyle style;
457 
458     if (webKitClient()->sandboxSupport())
459         webKitClient()->sandboxSupport()->getRenderStyleForStrike(font, sizeAndStyle, &style);
460     else
461         WebFontInfo::renderStyleForStrike(font, sizeAndStyle, &style);
462 
463     style.toFontRenderStyle(result);
464 }
465 #endif
466 
467 #if OS(DARWIN)
loadFont(NSFont * srcFont,ATSFontContainerRef * out)468 bool PlatformBridge::loadFont(NSFont* srcFont, ATSFontContainerRef* out)
469 {
470     WebSandboxSupport* ss = webKitClient()->sandboxSupport();
471     if (ss)
472         return ss->loadFont(srcFont, out);
473 
474     // This function should only be called in response to an error loading a
475     // font due to being blocked by the sandbox.
476     // This by definition shouldn't happen if there is no sandbox support.
477     ASSERT_NOT_REACHED();
478     *out = 0;
479     return false;
480 }
481 #endif
482 
483 // Databases ------------------------------------------------------------------
484 
databaseOpenFile(const String & vfsFileName,int desiredFlags)485 PlatformFileHandle PlatformBridge::databaseOpenFile(const String& vfsFileName, int desiredFlags)
486 {
487     return webKitClient()->databaseOpenFile(WebString(vfsFileName), desiredFlags);
488 }
489 
databaseDeleteFile(const String & vfsFileName,bool syncDir)490 int PlatformBridge::databaseDeleteFile(const String& vfsFileName, bool syncDir)
491 {
492     return webKitClient()->databaseDeleteFile(WebString(vfsFileName), syncDir);
493 }
494 
databaseGetFileAttributes(const String & vfsFileName)495 long PlatformBridge::databaseGetFileAttributes(const String& vfsFileName)
496 {
497     return webKitClient()->databaseGetFileAttributes(WebString(vfsFileName));
498 }
499 
databaseGetFileSize(const String & vfsFileName)500 long long PlatformBridge::databaseGetFileSize(const String& vfsFileName)
501 {
502     return webKitClient()->databaseGetFileSize(WebString(vfsFileName));
503 }
504 
505 // Indexed Database -----------------------------------------------------------
506 
idbFactory()507 PassRefPtr<IDBFactoryBackendInterface> PlatformBridge::idbFactory()
508 {
509     // There's no reason why we need to allocate a new proxy each time, but
510     // there's also no strong reason not to.
511     return IDBFactoryBackendProxy::create();
512 }
513 
createIDBKeysFromSerializedValuesAndKeyPath(const Vector<RefPtr<SerializedScriptValue>> & values,const String & keyPath,Vector<RefPtr<IDBKey>> & keys)514 void PlatformBridge::createIDBKeysFromSerializedValuesAndKeyPath(const Vector<RefPtr<SerializedScriptValue> >& values, const String& keyPath, Vector<RefPtr<IDBKey> >& keys)
515 {
516     WebVector<WebSerializedScriptValue> webValues = values;
517     WebVector<WebIDBKey> webKeys;
518     webKitClient()->createIDBKeysFromSerializedValuesAndKeyPath(webValues, keyPath, webKeys);
519 
520     size_t webKeysSize = webKeys.size();
521     keys.reserveCapacity(webKeysSize);
522     for (size_t i = 0; i < webKeysSize; ++i)
523         keys.append(PassRefPtr<IDBKey>(webKeys[i]));
524 }
525 
injectIDBKeyIntoSerializedValue(PassRefPtr<IDBKey> key,PassRefPtr<SerializedScriptValue> value,const String & keyPath)526 PassRefPtr<SerializedScriptValue> PlatformBridge::injectIDBKeyIntoSerializedValue(PassRefPtr<IDBKey> key, PassRefPtr<SerializedScriptValue> value, const String& keyPath)
527 {
528     return webKitClient()->injectIDBKeyIntoSerializedValue(key, value, keyPath);
529 }
530 
531 // Keygen ---------------------------------------------------------------------
532 
signedPublicKeyAndChallengeString(unsigned keySizeIndex,const String & challenge,const KURL & url)533 String PlatformBridge::signedPublicKeyAndChallengeString(
534     unsigned keySizeIndex, const String& challenge, const KURL& url)
535 {
536     return webKitClient()->signedPublicKeyAndChallengeString(keySizeIndex,
537                                                              WebString(challenge),
538                                                              WebURL(url));
539 }
540 
541 // Language -------------------------------------------------------------------
542 
computedDefaultLanguage()543 String PlatformBridge::computedDefaultLanguage()
544 {
545     return webKitClient()->defaultLocale();
546 }
547 
548 // LayoutTestMode -------------------------------------------------------------
549 
layoutTestMode()550 bool PlatformBridge::layoutTestMode()
551 {
552     return WebKit::layoutTestMode();
553 }
554 
555 // MimeType -------------------------------------------------------------------
556 
isSupportedImageMIMEType(const String & mimeType)557 bool PlatformBridge::isSupportedImageMIMEType(const String& mimeType)
558 {
559     return webKitClient()->mimeRegistry()->supportsImageMIMEType(mimeType)
560         != WebMimeRegistry::IsNotSupported;
561 }
562 
isSupportedJavaScriptMIMEType(const String & mimeType)563 bool PlatformBridge::isSupportedJavaScriptMIMEType(const String& mimeType)
564 {
565     return webKitClient()->mimeRegistry()->supportsJavaScriptMIMEType(mimeType)
566         != WebMimeRegistry::IsNotSupported;
567 }
568 
isSupportedNonImageMIMEType(const String & mimeType)569 bool PlatformBridge::isSupportedNonImageMIMEType(const String& mimeType)
570 {
571     return webKitClient()->mimeRegistry()->supportsNonImageMIMEType(mimeType)
572         != WebMimeRegistry::IsNotSupported;
573 }
574 
mimeTypeForExtension(const String & extension)575 String PlatformBridge::mimeTypeForExtension(const String& extension)
576 {
577     return webKitClient()->mimeRegistry()->mimeTypeForExtension(extension);
578 }
579 
mimeTypeFromFile(const String & path)580 String PlatformBridge::mimeTypeFromFile(const String& path)
581 {
582     return webKitClient()->mimeRegistry()->mimeTypeFromFile(path);
583 }
584 
preferredExtensionForMIMEType(const String & mimeType)585 String PlatformBridge::preferredExtensionForMIMEType(const String& mimeType)
586 {
587     return webKitClient()->mimeRegistry()->preferredExtensionForMIMEType(mimeType);
588 }
589 
590 // Plugin ---------------------------------------------------------------------
591 
plugins(bool refresh,Vector<PluginInfo> * results)592 bool PlatformBridge::plugins(bool refresh, Vector<PluginInfo>* results)
593 {
594     WebPluginListBuilderImpl builder(results);
595     webKitClient()->getPluginList(refresh, &builder);
596     return true;  // FIXME: There is no need for this function to return a value.
597 }
598 
pluginScriptableObject(Widget * widget)599 NPObject* PlatformBridge::pluginScriptableObject(Widget* widget)
600 {
601     if (!widget || !widget->isPluginContainer())
602         return 0;
603 
604     return static_cast<WebPluginContainerImpl*>(widget)->scriptableObject();
605 }
606 
607 // Resources ------------------------------------------------------------------
608 
loadPlatformImageResource(const char * name)609 PassRefPtr<Image> PlatformBridge::loadPlatformImageResource(const char* name)
610 {
611     const WebData& resource = webKitClient()->loadResource(name);
612     if (resource.isEmpty())
613         return Image::nullImage();
614 
615     RefPtr<Image> image = BitmapImage::create();
616     image->setData(resource, true);
617     return image;
618 }
619 
620 #if ENABLE(WEB_AUDIO)
621 
loadPlatformAudioResource(const char * name,double sampleRate)622 PassOwnPtr<AudioBus> PlatformBridge::loadPlatformAudioResource(const char* name, double sampleRate)
623 {
624     const WebData& resource = webKitClient()->loadResource(name);
625     if (resource.isEmpty())
626         return 0;
627 
628     return decodeAudioFileData(resource.data(), resource.size(), sampleRate);
629 }
630 
decodeAudioFileData(const char * data,size_t size,double sampleRate)631 PassOwnPtr<AudioBus> PlatformBridge::decodeAudioFileData(const char* data, size_t size, double sampleRate)
632 {
633     WebAudioBus webAudioBus;
634     if (webKitClient()->loadAudioResource(&webAudioBus, data, size, sampleRate))
635         return webAudioBus.release();
636     return 0;
637 }
638 
639 #endif // ENABLE(WEB_AUDIO)
640 
641 // Sandbox --------------------------------------------------------------------
642 
sandboxEnabled()643 bool PlatformBridge::sandboxEnabled()
644 {
645     return webKitClient()->sandboxEnabled();
646 }
647 
648 // SharedTimers ---------------------------------------------------------------
649 
setSharedTimerFiredFunction(void (* func)())650 void PlatformBridge::setSharedTimerFiredFunction(void (*func)())
651 {
652     webKitClient()->setSharedTimerFiredFunction(func);
653 }
654 
setSharedTimerFireTime(double fireTime)655 void PlatformBridge::setSharedTimerFireTime(double fireTime)
656 {
657     webKitClient()->setSharedTimerFireTime(fireTime);
658 }
659 
stopSharedTimer()660 void PlatformBridge::stopSharedTimer()
661 {
662     webKitClient()->stopSharedTimer();
663 }
664 
665 // StatsCounters --------------------------------------------------------------
666 
decrementStatsCounter(const char * name)667 void PlatformBridge::decrementStatsCounter(const char* name)
668 {
669     webKitClient()->decrementStatsCounter(name);
670 }
671 
incrementStatsCounter(const char * name)672 void PlatformBridge::incrementStatsCounter(const char* name)
673 {
674     webKitClient()->incrementStatsCounter(name);
675 }
676 
histogramCustomCounts(const char * name,int sample,int min,int max,int bucketCount)677 void PlatformBridge::histogramCustomCounts(const char* name, int sample, int min, int max, int bucketCount)
678 {
679     webKitClient()->histogramCustomCounts(name, sample, min, max, bucketCount);
680 }
681 
histogramEnumeration(const char * name,int sample,int boundaryValue)682 void PlatformBridge::histogramEnumeration(const char* name, int sample, int boundaryValue)
683 {
684     webKitClient()->histogramEnumeration(name, sample, boundaryValue);
685 }
686 
687 // Sudden Termination ---------------------------------------------------------
688 
suddenTerminationChanged(bool enabled)689 void PlatformBridge::suddenTerminationChanged(bool enabled)
690 {
691     webKitClient()->suddenTerminationChanged(enabled);
692 }
693 
694 // SystemTime -----------------------------------------------------------------
695 
currentTime()696 double PlatformBridge::currentTime()
697 {
698     return webKitClient()->currentTime();
699 }
700 
701 // Theming --------------------------------------------------------------------
702 
703 #if OS(WINDOWS)
704 
paintButton(GraphicsContext * gc,int part,int state,int classicState,const IntRect & rect)705 void PlatformBridge::paintButton(
706     GraphicsContext* gc, int part, int state, int classicState,
707     const IntRect& rect)
708 {
709     webKitClient()->themeEngine()->paintButton(
710         gc->platformContext()->canvas(), part, state, classicState, rect);
711 }
712 
paintMenuList(GraphicsContext * gc,int part,int state,int classicState,const IntRect & rect)713 void PlatformBridge::paintMenuList(
714     GraphicsContext* gc, int part, int state, int classicState,
715     const IntRect& rect)
716 {
717     webKitClient()->themeEngine()->paintMenuList(
718         gc->platformContext()->canvas(), part, state, classicState, rect);
719 }
720 
paintScrollbarArrow(GraphicsContext * gc,int state,int classicState,const IntRect & rect)721 void PlatformBridge::paintScrollbarArrow(
722     GraphicsContext* gc, int state, int classicState,
723     const IntRect& rect)
724 {
725     webKitClient()->themeEngine()->paintScrollbarArrow(
726         gc->platformContext()->canvas(), state, classicState, rect);
727 }
728 
paintScrollbarThumb(GraphicsContext * gc,int part,int state,int classicState,const IntRect & rect)729 void PlatformBridge::paintScrollbarThumb(
730     GraphicsContext* gc, int part, int state, int classicState,
731     const IntRect& rect)
732 {
733     webKitClient()->themeEngine()->paintScrollbarThumb(
734         gc->platformContext()->canvas(), part, state, classicState, rect);
735 }
736 
paintScrollbarTrack(GraphicsContext * gc,int part,int state,int classicState,const IntRect & rect,const IntRect & alignRect)737 void PlatformBridge::paintScrollbarTrack(
738     GraphicsContext* gc, int part, int state, int classicState,
739     const IntRect& rect, const IntRect& alignRect)
740 {
741     webKitClient()->themeEngine()->paintScrollbarTrack(
742         gc->platformContext()->canvas(), part, state, classicState, rect,
743         alignRect);
744 }
745 
paintSpinButton(GraphicsContext * gc,int part,int state,int classicState,const IntRect & rect)746 void PlatformBridge::paintSpinButton(
747     GraphicsContext* gc, int part, int state, int classicState,
748     const IntRect& rect)
749 {
750     webKitClient()->themeEngine()->paintSpinButton(
751         gc->platformContext()->canvas(), part, state, classicState, rect);
752 }
753 
paintTextField(GraphicsContext * gc,int part,int state,int classicState,const IntRect & rect,const Color & color,bool fillContentArea,bool drawEdges)754 void PlatformBridge::paintTextField(
755     GraphicsContext* gc, int part, int state, int classicState,
756     const IntRect& rect, const Color& color, bool fillContentArea,
757     bool drawEdges)
758 {
759     // Fallback to white when |color| is invalid.
760     RGBA32 backgroundColor = color.isValid() ? color.rgb() : Color::white;
761 
762     webKitClient()->themeEngine()->paintTextField(
763         gc->platformContext()->canvas(), part, state, classicState, rect,
764         backgroundColor, fillContentArea, drawEdges);
765 }
766 
paintTrackbar(GraphicsContext * gc,int part,int state,int classicState,const IntRect & rect)767 void PlatformBridge::paintTrackbar(
768     GraphicsContext* gc, int part, int state, int classicState,
769     const IntRect& rect)
770 {
771     webKitClient()->themeEngine()->paintTrackbar(
772         gc->platformContext()->canvas(), part, state, classicState, rect);
773 }
774 
paintProgressBar(GraphicsContext * gc,const IntRect & barRect,const IntRect & valueRect,bool determinate,double animatedSeconds)775 void PlatformBridge::paintProgressBar(
776     GraphicsContext* gc, const IntRect& barRect, const IntRect& valueRect, bool determinate, double animatedSeconds)
777 {
778     webKitClient()->themeEngine()->paintProgressBar(
779         gc->platformContext()->canvas(), barRect, valueRect, determinate, animatedSeconds);
780 }
781 
782 #elif OS(LINUX)
783 
WebThemePart(PlatformBridge::ThemePart part)784 static WebThemeEngine::Part WebThemePart(PlatformBridge::ThemePart part)
785 {
786     switch (part) {
787     case PlatformBridge::PartScrollbarDownArrow: return WebThemeEngine::PartScrollbarDownArrow;
788     case PlatformBridge::PartScrollbarLeftArrow: return WebThemeEngine::PartScrollbarLeftArrow;
789     case PlatformBridge::PartScrollbarRightArrow: return WebThemeEngine::PartScrollbarRightArrow;
790     case PlatformBridge::PartScrollbarUpArrow: return WebThemeEngine::PartScrollbarUpArrow;
791     case PlatformBridge::PartScrollbarHorizontalThumb: return WebThemeEngine::PartScrollbarHorizontalThumb;
792     case PlatformBridge::PartScrollbarVerticalThumb: return WebThemeEngine::PartScrollbarVerticalThumb;
793     case PlatformBridge::PartScrollbarHorizontalTrack: return WebThemeEngine::PartScrollbarHorizontalTrack;
794     case PlatformBridge::PartScrollbarVerticalTrack: return WebThemeEngine::PartScrollbarVerticalTrack;
795     case PlatformBridge::PartCheckbox: return WebThemeEngine::PartCheckbox;
796     case PlatformBridge::PartRadio: return WebThemeEngine::PartRadio;
797     case PlatformBridge::PartButton: return WebThemeEngine::PartButton;
798     case PlatformBridge::PartTextField: return WebThemeEngine::PartTextField;
799     case PlatformBridge::PartMenuList: return WebThemeEngine::PartMenuList;
800     case PlatformBridge::PartSliderTrack: return WebThemeEngine::PartSliderTrack;
801     case PlatformBridge::PartSliderThumb: return WebThemeEngine::PartSliderThumb;
802     case PlatformBridge::PartInnerSpinButton: return WebThemeEngine::PartInnerSpinButton;
803     case PlatformBridge::PartProgressBar: return WebThemeEngine::PartProgressBar;
804     }
805     ASSERT_NOT_REACHED();
806     return WebThemeEngine::PartScrollbarDownArrow;
807 }
808 
WebThemeState(PlatformBridge::ThemePaintState state)809 static WebThemeEngine::State WebThemeState(PlatformBridge::ThemePaintState state)
810 {
811     switch (state) {
812     case PlatformBridge::StateDisabled: return WebThemeEngine::StateDisabled;
813     case PlatformBridge::StateHover: return WebThemeEngine::StateHover;
814     case PlatformBridge::StateNormal: return WebThemeEngine::StateNormal;
815     case PlatformBridge::StatePressed: return WebThemeEngine::StatePressed;
816     }
817     ASSERT_NOT_REACHED();
818     return WebThemeEngine::StateDisabled;
819 }
820 
GetWebThemeExtraParams(PlatformBridge::ThemePart part,PlatformBridge::ThemePaintState state,const PlatformBridge::ThemePaintExtraParams * extraParams,WebThemeEngine::ExtraParams * webThemeExtraParams)821 static void GetWebThemeExtraParams(PlatformBridge::ThemePart part, PlatformBridge::ThemePaintState state, const PlatformBridge::ThemePaintExtraParams* extraParams, WebThemeEngine::ExtraParams* webThemeExtraParams)
822 {
823     switch (part) {
824     case PlatformBridge::PartScrollbarHorizontalTrack:
825     case PlatformBridge::PartScrollbarVerticalTrack:
826         webThemeExtraParams->scrollbarTrack.trackX = extraParams->scrollbarTrack.trackX;
827         webThemeExtraParams->scrollbarTrack.trackY = extraParams->scrollbarTrack.trackY;
828         webThemeExtraParams->scrollbarTrack.trackWidth = extraParams->scrollbarTrack.trackWidth;
829         webThemeExtraParams->scrollbarTrack.trackHeight = extraParams->scrollbarTrack.trackHeight;
830         break;
831     case PlatformBridge::PartCheckbox:
832         webThemeExtraParams->button.checked = extraParams->button.checked;
833         webThemeExtraParams->button.indeterminate = extraParams->button.indeterminate;
834         break;
835     case PlatformBridge::PartRadio:
836         webThemeExtraParams->button.checked = extraParams->button.checked;
837         break;
838     case PlatformBridge::PartButton:
839         webThemeExtraParams->button.isDefault = extraParams->button.isDefault;
840         webThemeExtraParams->button.hasBorder = extraParams->button.hasBorder;
841         webThemeExtraParams->button.backgroundColor = extraParams->button.backgroundColor;
842         break;
843     case PlatformBridge::PartTextField:
844         webThemeExtraParams->textField.isTextArea = extraParams->textField.isTextArea;
845         webThemeExtraParams->textField.isListbox = extraParams->textField.isListbox;
846         webThemeExtraParams->textField.backgroundColor = extraParams->textField.backgroundColor;
847         break;
848     case PlatformBridge::PartMenuList:
849         webThemeExtraParams->menuList.hasBorder = extraParams->menuList.hasBorder;
850         webThemeExtraParams->menuList.hasBorderRadius = extraParams->menuList.hasBorderRadius;
851         webThemeExtraParams->menuList.arrowX = extraParams->menuList.arrowX;
852         webThemeExtraParams->menuList.arrowY = extraParams->menuList.arrowY;
853         webThemeExtraParams->menuList.backgroundColor = extraParams->menuList.backgroundColor;
854         break;
855     case PlatformBridge::PartSliderTrack:
856     case PlatformBridge::PartSliderThumb:
857         webThemeExtraParams->slider.vertical = extraParams->slider.vertical;
858         webThemeExtraParams->slider.inDrag = extraParams->slider.inDrag;
859         break;
860     case PlatformBridge::PartInnerSpinButton:
861         webThemeExtraParams->innerSpin.spinUp = extraParams->innerSpin.spinUp;
862         webThemeExtraParams->innerSpin.readOnly = extraParams->innerSpin.readOnly;
863         break;
864     case PlatformBridge::PartProgressBar:
865         webThemeExtraParams->progressBar.determinate = extraParams->progressBar.determinate;
866         webThemeExtraParams->progressBar.valueRectX = extraParams->progressBar.valueRectX;
867         webThemeExtraParams->progressBar.valueRectY = extraParams->progressBar.valueRectY;
868         webThemeExtraParams->progressBar.valueRectWidth = extraParams->progressBar.valueRectWidth;
869         webThemeExtraParams->progressBar.valueRectHeight = extraParams->progressBar.valueRectHeight;
870         break;
871     default:
872         break; // Parts that have no extra params get here.
873     }
874 }
875 
getThemePartSize(ThemePart part)876 IntSize PlatformBridge::getThemePartSize(ThemePart part)
877 {
878      return webKitClient()->themeEngine()->getSize(WebThemePart(part));
879 }
880 
paintThemePart(GraphicsContext * gc,ThemePart part,ThemePaintState state,const IntRect & rect,const ThemePaintExtraParams * extraParams)881 void PlatformBridge::paintThemePart(
882     GraphicsContext* gc, ThemePart part, ThemePaintState state, const IntRect& rect, const ThemePaintExtraParams* extraParams)
883 {
884     WebThemeEngine::ExtraParams webThemeExtraParams;
885     GetWebThemeExtraParams(part, state, extraParams, &webThemeExtraParams);
886     webKitClient()->themeEngine()->paint(
887         gc->platformContext()->canvas(), WebThemePart(part), WebThemeState(state), rect, &webThemeExtraParams);
888 }
889 
890 #elif OS(DARWIN)
891 
paintScrollbarThumb(GraphicsContext * gc,ThemePaintState state,ThemePaintSize size,const IntRect & rect,const ThemePaintScrollbarInfo & scrollbarInfo)892 void PlatformBridge::paintScrollbarThumb(
893     GraphicsContext* gc, ThemePaintState state, ThemePaintSize size, const IntRect& rect, const ThemePaintScrollbarInfo& scrollbarInfo)
894 {
895     WebThemeEngine::ScrollbarInfo webThemeScrollbarInfo;
896 
897     webThemeScrollbarInfo.orientation = static_cast<WebThemeEngine::ScrollbarOrientation>(scrollbarInfo.orientation);
898     webThemeScrollbarInfo.parent = static_cast<WebThemeEngine::ScrollbarParent>(scrollbarInfo.parent);
899     webThemeScrollbarInfo.maxValue = scrollbarInfo.maxValue;
900     webThemeScrollbarInfo.currentValue = scrollbarInfo.currentValue;
901     webThemeScrollbarInfo.visibleSize = scrollbarInfo.visibleSize;
902     webThemeScrollbarInfo.totalSize = scrollbarInfo.totalSize;
903 
904     webKitClient()->themeEngine()->paintScrollbarThumb(
905         gc->platformContext(),
906         static_cast<WebThemeEngine::State>(state),
907         static_cast<WebThemeEngine::Size>(size),
908         rect,
909         webThemeScrollbarInfo);
910 }
911 
912 #endif
913 
914 // Trace Event ----------------------------------------------------------------
915 
traceEventBegin(const char * name,void * id,const char * extra)916 void PlatformBridge::traceEventBegin(const char* name, void* id, const char* extra)
917 {
918     webKitClient()->traceEventBegin(name, id, extra);
919 }
920 
traceEventEnd(const char * name,void * id,const char * extra)921 void PlatformBridge::traceEventEnd(const char* name, void* id, const char* extra)
922 {
923     webKitClient()->traceEventEnd(name, id, extra);
924 }
925 
926 // Visited Links --------------------------------------------------------------
927 
visitedLinkHash(const UChar * url,unsigned length)928 LinkHash PlatformBridge::visitedLinkHash(const UChar* url, unsigned length)
929 {
930     url_canon::RawCanonOutput<2048> buffer;
931     url_parse::Parsed parsed;
932     if (!url_util::Canonicalize(url, length, 0, &buffer, &parsed))
933         return 0;  // Invalid URLs are unvisited.
934     return webKitClient()->visitedLinkHash(buffer.data(), buffer.length());
935 }
936 
visitedLinkHash(const KURL & base,const AtomicString & attributeURL)937 LinkHash PlatformBridge::visitedLinkHash(const KURL& base,
938                                          const AtomicString& attributeURL)
939 {
940     // Resolve the relative URL using googleurl and pass the absolute URL up to
941     // the embedder. We could create a GURL object from the base and resolve
942     // the relative URL that way, but calling the lower-level functions
943     // directly saves us the string allocation in most cases.
944     url_canon::RawCanonOutput<2048> buffer;
945     url_parse::Parsed parsed;
946 
947 #if USE(GOOGLEURL)
948     const CString& cstr = base.utf8String();
949     const char* data = cstr.data();
950     int length = cstr.length();
951     const url_parse::Parsed& srcParsed = base.parsed();
952 #else
953     // When we're not using GoogleURL, first canonicalize it so we can resolve it
954     // below.
955     url_canon::RawCanonOutput<2048> srcCanon;
956     url_parse::Parsed srcParsed;
957     String str = base.string();
958     if (!url_util::Canonicalize(str.characters(), str.length(), 0, &srcCanon, &srcParsed))
959         return 0;
960     const char* data = srcCanon.data();
961     int length = srcCanon.length();
962 #endif
963 
964     if (!url_util::ResolveRelative(data, length, srcParsed, attributeURL.characters(),
965                                    attributeURL.length(), 0, &buffer, &parsed))
966         return 0;  // Invalid resolved URL.
967 
968     return webKitClient()->visitedLinkHash(buffer.data(), buffer.length());
969 }
970 
isLinkVisited(LinkHash visitedLinkHash)971 bool PlatformBridge::isLinkVisited(LinkHash visitedLinkHash)
972 {
973     return webKitClient()->isLinkVisited(visitedLinkHash);
974 }
975 
976 // These are temporary methods that the WebKit layer can use to call to the
977 // Glue layer. Once the Glue layer moves entirely into the WebKit layer, these
978 // methods will be deleted.
979 
notifyJSOutOfMemory(Frame * frame)980 void PlatformBridge::notifyJSOutOfMemory(Frame* frame)
981 {
982     if (!frame)
983         return;
984 
985     WebFrameImpl* webFrame = WebFrameImpl::fromFrame(frame);
986     if (!webFrame->client())
987         return;
988     webFrame->client()->didExhaustMemoryAvailableForScript(webFrame);
989 }
990 
memoryUsageMB()991 int PlatformBridge::memoryUsageMB()
992 {
993     return static_cast<int>(webKitClient()->memoryUsageMB());
994 }
995 
actualMemoryUsageMB()996 int PlatformBridge::actualMemoryUsageMB()
997 {
998     return static_cast<int>(webKitClient()->actualMemoryUsageMB());
999 }
1000 
screenDepth(Widget * widget)1001 int PlatformBridge::screenDepth(Widget* widget)
1002 {
1003     WebWidgetClient* client = toWebWidgetClient(widget);
1004     if (!client)
1005         return 0;
1006     return client->screenInfo().depth;
1007 }
1008 
screenDepthPerComponent(Widget * widget)1009 int PlatformBridge::screenDepthPerComponent(Widget* widget)
1010 {
1011     WebWidgetClient* client = toWebWidgetClient(widget);
1012     if (!client)
1013         return 0;
1014     return client->screenInfo().depthPerComponent;
1015 }
1016 
screenIsMonochrome(Widget * widget)1017 bool PlatformBridge::screenIsMonochrome(Widget* widget)
1018 {
1019     WebWidgetClient* client = toWebWidgetClient(widget);
1020     if (!client)
1021         return 0;
1022     return client->screenInfo().isMonochrome;
1023 }
1024 
screenRect(Widget * widget)1025 IntRect PlatformBridge::screenRect(Widget* widget)
1026 {
1027     WebWidgetClient* client = toWebWidgetClient(widget);
1028     if (!client)
1029         return IntRect();
1030     return client->screenInfo().rect;
1031 }
1032 
screenAvailableRect(Widget * widget)1033 IntRect PlatformBridge::screenAvailableRect(Widget* widget)
1034 {
1035     WebWidgetClient* client = toWebWidgetClient(widget);
1036     if (!client)
1037         return IntRect();
1038     return client->screenInfo().availableRect;
1039 }
1040 
popupsAllowed(NPP npp)1041 bool PlatformBridge::popupsAllowed(NPP npp)
1042 {
1043     // FIXME: Give the embedder a way to control this.
1044     return false;
1045 }
1046 
create(Worker * worker)1047 WorkerContextProxy* WorkerContextProxy::create(Worker* worker)
1048 {
1049     return WebWorkerClientImpl::createWorkerContextProxy(worker);
1050 }
1051 
1052 } // namespace WebCore
1053