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