Renaming some parameters passed to DB-related methods to better
[WebKit-https.git] / WebKit / chromium / src / ChromiumBridge.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 "ChromiumBridge.h"
33
34 #include <googleurl/src/url_util.h>
35
36 #include "ChromeClientImpl.h"
37 #include "WebClipboard.h"
38 #include "WebCookie.h"
39 #include "WebCursorInfo.h"
40 #include "WebData.h"
41 #include "WebFrameClient.h"
42 #include "WebFrameImpl.h"
43 #include "WebImage.h"
44 #include "WebKit.h"
45 #include "WebKitClient.h"
46 #include "WebMimeRegistry.h"
47 #include "WebPluginContainerImpl.h"
48 #include "WebPluginListBuilderImpl.h"
49 #include "WebScreenInfo.h"
50 #include "WebString.h"
51 #include "WebURL.h"
52 #include "WebVector.h"
53 #include "WebViewClient.h"
54 #include "WebViewImpl.h"
55 #include "WebWorkerClientImpl.h"
56
57 #if PLATFORM(WIN_OS)
58 #include "WebRect.h"
59 #include "WebSandboxSupport.h"
60 #include "WebThemeEngine.h"
61 #endif
62
63 #if PLATFORM(LINUX)
64 #include "WebSandboxSupport.h"
65 #include "WebFontInfo.h"
66 #endif
67
68 #if WEBKIT_USING_SKIA
69 #include "NativeImageSkia.h"
70 #endif
71
72 #include "BitmapImage.h"
73 #include "Cookie.h"
74 #include "FrameView.h"
75 #include "GraphicsContext.h"
76 #include "KURL.h"
77 #include "NotImplemented.h"
78 #include "PlatformContextSkia.h"
79 #include "PluginData.h"
80 #include "Worker.h"
81 #include "WorkerContextProxy.h"
82 #include <wtf/Assertions.h>
83
84 // We are part of the WebKit implementation.
85 using namespace WebKit;
86
87 namespace WebCore {
88
89 static ChromeClientImpl* toChromeClientImpl(Widget* widget)
90 {
91     FrameView* view;
92     if (widget->isFrameView())
93         view = static_cast<FrameView*>(widget);
94     else if (widget->parent() && widget->parent()->isFrameView())
95         view = static_cast<FrameView*>(widget->parent());
96     else
97         return 0;
98
99     Page* page = view->frame() ? view->frame()->page() : 0;
100     if (!page)
101         return 0;
102
103     return static_cast<ChromeClientImpl*>(page->chrome()->client());
104 }
105
106 static WebWidgetClient* toWebWidgetClient(Widget* widget)
107 {
108     ChromeClientImpl* chromeClientImpl = toChromeClientImpl(widget);
109     if (!chromeClientImpl || !chromeClientImpl->webView())
110         return 0;
111     return chromeClientImpl->webView()->client();
112 }
113
114 // Clipboard ------------------------------------------------------------------
115
116 bool ChromiumBridge::clipboardIsFormatAvailable(
117     PasteboardPrivate::ClipboardFormat format,
118     PasteboardPrivate::ClipboardBuffer buffer)
119 {
120     return webKitClient()->clipboard()->isFormatAvailable(
121         static_cast<WebClipboard::Format>(format),
122         static_cast<WebClipboard::Buffer>(buffer));
123 }
124
125 String ChromiumBridge::clipboardReadPlainText(
126     PasteboardPrivate::ClipboardBuffer buffer)
127 {
128     return webKitClient()->clipboard()->readPlainText(
129         static_cast<WebClipboard::Buffer>(buffer));
130 }
131
132 void ChromiumBridge::clipboardReadHTML(
133     PasteboardPrivate::ClipboardBuffer buffer,
134     String* htmlText, KURL* sourceURL)
135 {
136     WebURL url;
137     *htmlText = webKitClient()->clipboard()->readHTML(
138         static_cast<WebClipboard::Buffer>(buffer), &url);
139     *sourceURL = url;
140 }
141
142 void ChromiumBridge::clipboardWriteSelection(const String& htmlText,
143                                              const KURL& sourceURL,
144                                              const String& plainText,
145                                              bool writeSmartPaste)
146 {
147     webKitClient()->clipboard()->writeHTML(
148         htmlText, sourceURL, plainText, writeSmartPaste);
149 }
150
151 void ChromiumBridge::clipboardWritePlainText(const String& plainText)
152 {
153     webKitClient()->clipboard()->writePlainText(plainText);
154 }
155
156 void ChromiumBridge::clipboardWriteURL(const KURL& url, const String& title)
157 {
158     webKitClient()->clipboard()->writeURL(url, title);
159 }
160
161 void ChromiumBridge::clipboardWriteImage(NativeImagePtr image,
162                                          const KURL& sourceURL,
163                                          const String& title)
164 {
165 #if WEBKIT_USING_SKIA
166     WebImage webImage(*image);
167 #else
168     WebImage webImage(image);
169 #endif
170     webKitClient()->clipboard()->writeImage(webImage, sourceURL, title);
171 }
172
173 // Cookies --------------------------------------------------------------------
174
175 void ChromiumBridge::setCookies(const KURL& url,
176                                 const KURL& firstPartyForCookies,
177                                 const String& cookie)
178 {
179     webKitClient()->setCookies(url, firstPartyForCookies, cookie);
180 }
181
182 String ChromiumBridge::cookies(const KURL& url,
183                                const KURL& firstPartyForCookies)
184 {
185     return webKitClient()->cookies(url, firstPartyForCookies);
186 }
187
188 bool ChromiumBridge::rawCookies(const KURL& url, const KURL& firstPartyForCookies, Vector<Cookie>* rawCookies)
189 {
190     rawCookies->clear();
191     WebVector<WebCookie> webCookies;
192     if (!webKitClient()->rawCookies(url, firstPartyForCookies, &webCookies))
193         return false;
194
195     for (unsigned i = 0; i < webCookies.size(); ++i) {
196         const WebCookie& webCookie = webCookies[i];
197         Cookie cookie(webCookie.name,
198                       webCookie.value,
199                       webCookie.domain,
200                       webCookie.path,
201                       webCookie.expires,
202                       webCookie.httpOnly,
203                       webCookie.secure,
204                       webCookie.session);
205         rawCookies->append(cookie);
206     }
207     return true;
208 }
209
210 void ChromiumBridge::deleteCookie(const KURL& url, const String& cookieName)
211 {
212     webKitClient()->deleteCookie(url, cookieName);
213 }
214
215 // DNS ------------------------------------------------------------------------
216
217 void ChromiumBridge::prefetchDNS(const String& hostname)
218 {
219     webKitClient()->prefetchHostName(hostname);
220 }
221
222 // File ------------------------------------------------------------------------
223
224 bool ChromiumBridge::fileExists(const String& path)
225 {
226     return webKitClient()->fileExists(path);
227 }
228
229 bool ChromiumBridge::deleteFile(const String& path)
230 {
231     return webKitClient()->deleteFile(path);
232 }
233
234 bool ChromiumBridge::deleteEmptyDirectory(const String& path)
235 {
236     return webKitClient()->deleteEmptyDirectory(path);
237 }
238
239 bool ChromiumBridge::getFileSize(const String& path, long long& result)
240 {
241     return webKitClient()->getFileSize(path, result);
242 }
243
244 bool ChromiumBridge::getFileModificationTime(const String& path, time_t& result)
245 {
246     return webKitClient()->getFileModificationTime(path, result);
247 }
248
249 String ChromiumBridge::directoryName(const String& path)
250 {
251     return webKitClient()->directoryName(path);
252 }
253
254 String ChromiumBridge::pathByAppendingComponent(const String& path, const String& component)
255 {
256     return webKitClient()->pathByAppendingComponent(path, component);
257 }
258
259 bool ChromiumBridge::makeAllDirectories(const String& path)
260 {
261     return webKitClient()->makeAllDirectories(path);
262 }
263
264 String ChromiumBridge::getAbsolutePath(const String& path)
265 {
266     return webKitClient()->getAbsolutePath(path);
267 }
268
269 bool ChromiumBridge::isDirectory(const String& path)
270 {
271     return webKitClient()->isDirectory(path);
272 }
273
274 KURL ChromiumBridge::filePathToURL(const String& path)
275 {
276     return webKitClient()->filePathToURL(path);
277 }
278
279 // Font -----------------------------------------------------------------------
280
281 #if PLATFORM(WIN_OS)
282 bool ChromiumBridge::ensureFontLoaded(HFONT font)
283 {
284     WebSandboxSupport* ss = webKitClient()->sandboxSupport();
285
286     // if there is no sandbox, then we can assume the font
287     // was able to be loaded successfully already
288     return ss ? ss->ensureFontLoaded(font) : true;
289 }
290 #endif
291
292 #if PLATFORM(LINUX)
293 String ChromiumBridge::getFontFamilyForCharacters(const UChar* characters, size_t numCharacters)
294 {
295     if (webKitClient()->sandboxSupport())
296         return webKitClient()->sandboxSupport()->getFontFamilyForCharacters(characters, numCharacters);
297
298     WebCString family = WebFontInfo::familyForChars(characters, numCharacters);
299     if (family.data())
300         return WebString::fromUTF8(family.data());
301
302     return WebString();
303 }
304 #endif
305
306 // HTML5 DB -------------------------------------------------------------------
307
308 #if ENABLE(DATABASE)
309 PlatformFileHandle ChromiumBridge::databaseOpenFile(const String& vfsFileName, int desiredFlags, PlatformFileHandle* dirHandle)
310 {
311     return webKitClient()->databaseOpenFile(WebString(vfsFileName), desiredFlags, dirHandle);
312 }
313
314 int ChromiumBridge::databaseDeleteFile(const String& vfsFileName, bool syncDir)
315 {
316     return webKitClient()->databaseDeleteFile(WebString(vfsFileName), syncDir);
317 }
318
319 long ChromiumBridge::databaseGetFileAttributes(const String& vfsFileName)
320 {
321     return webKitClient()->databaseGetFileAttributes(WebString(vfsFileName));
322 }
323
324 long long ChromiumBridge::databaseGetFileSize(const String& vfsFileName)
325 {
326     return webKitClient()->databaseGetFileSize(WebString(vfsFileName));
327 }
328 #endif
329
330 // Keygen ---------------------------------------------------------------------
331
332 String ChromiumBridge::signedPublicKeyAndChallengeString(
333     unsigned keySizeIndex, const String& challenge, const KURL& url)
334 {
335     return webKitClient()->signedPublicKeyAndChallengeString(keySizeIndex,
336                                                              WebString(challenge),
337                                                              WebURL(url));
338 }
339
340 // Language -------------------------------------------------------------------
341
342 String ChromiumBridge::computedDefaultLanguage()
343 {
344     return webKitClient()->defaultLocale();
345 }
346
347 // LayoutTestMode -------------------------------------------------------------
348
349 bool ChromiumBridge::layoutTestMode()
350 {
351     return WebKit::layoutTestMode();
352 }
353
354 // MimeType -------------------------------------------------------------------
355
356 bool ChromiumBridge::isSupportedImageMIMEType(const String& mimeType)
357 {
358     return webKitClient()->mimeRegistry()->supportsImageMIMEType(mimeType)
359         != WebMimeRegistry::IsNotSupported;
360 }
361
362 bool ChromiumBridge::isSupportedJavaScriptMIMEType(const String& mimeType)
363 {
364     return webKitClient()->mimeRegistry()->supportsJavaScriptMIMEType(mimeType)
365         != WebMimeRegistry::IsNotSupported;
366 }
367
368 bool ChromiumBridge::isSupportedNonImageMIMEType(const String& mimeType)
369 {
370     return webKitClient()->mimeRegistry()->supportsNonImageMIMEType(mimeType)
371         != WebMimeRegistry::IsNotSupported;
372 }
373
374 String ChromiumBridge::mimeTypeForExtension(const String& extension)
375 {
376     return webKitClient()->mimeRegistry()->mimeTypeForExtension(extension);
377 }
378
379 String ChromiumBridge::mimeTypeFromFile(const String& path)
380 {
381     return webKitClient()->mimeRegistry()->mimeTypeFromFile(path);
382 }
383
384 String ChromiumBridge::preferredExtensionForMIMEType(const String& mimeType)
385 {
386     return webKitClient()->mimeRegistry()->preferredExtensionForMIMEType(mimeType);
387 }
388
389 // Plugin ---------------------------------------------------------------------
390
391 bool ChromiumBridge::plugins(bool refresh, Vector<PluginInfo*>* results)
392 {
393     WebPluginListBuilderImpl builder(results);
394     webKitClient()->getPluginList(refresh, &builder);
395     return true;  // FIXME: There is no need for this function to return a value.
396 }
397
398 NPObject* ChromiumBridge::pluginScriptableObject(Widget* widget)
399 {
400     if (!widget)
401         return 0;
402
403     ASSERT(!widget->isFrameView());
404
405     // NOTE:  We have to trust that the widget passed to us here is a
406     // WebPluginContainerImpl.  There isn't a way to dynamically verify it,
407     // since the derived class (Widget) has no identifier.
408     return static_cast<WebPluginContainerImpl*>(widget)->scriptableObject();
409 }
410
411 // Resources ------------------------------------------------------------------
412
413 PassRefPtr<Image> ChromiumBridge::loadPlatformImageResource(const char* name)
414 {
415     const WebData& resource = webKitClient()->loadResource(name);
416     if (resource.isEmpty())
417         return Image::nullImage();
418
419     RefPtr<Image> image = BitmapImage::create();
420     image->setData(resource, true);
421     return image;
422 }
423
424 // Sandbox --------------------------------------------------------------------
425
426 bool ChromiumBridge::sandboxEnabled()
427 {
428     return webKitClient()->sandboxEnabled();
429 }
430
431 // SharedTimers ---------------------------------------------------------------
432
433 void ChromiumBridge::setSharedTimerFiredFunction(void (*func)())
434 {
435     webKitClient()->setSharedTimerFiredFunction(func);
436 }
437
438 void ChromiumBridge::setSharedTimerFireTime(double fireTime)
439 {
440     webKitClient()->setSharedTimerFireTime(fireTime);
441 }
442
443 void ChromiumBridge::stopSharedTimer()
444 {
445     webKitClient()->stopSharedTimer();
446 }
447
448 // StatsCounters --------------------------------------------------------------
449
450 void ChromiumBridge::decrementStatsCounter(const char* name)
451 {
452     webKitClient()->decrementStatsCounter(name);
453 }
454
455 void ChromiumBridge::incrementStatsCounter(const char* name)
456 {
457     webKitClient()->incrementStatsCounter(name);
458 }
459
460 // Sudden Termination ---------------------------------------------------------
461
462 void ChromiumBridge::suddenTerminationChanged(bool enabled)
463 {
464     webKitClient()->suddenTerminationChanged(enabled);
465 }
466
467 // SystemTime -----------------------------------------------------------------
468
469 double ChromiumBridge::currentTime()
470 {
471     return webKitClient()->currentTime();
472 }
473
474 // Theming --------------------------------------------------------------------
475
476 #if PLATFORM(WIN_OS)
477
478 void ChromiumBridge::paintButton(
479     GraphicsContext* gc, int part, int state, int classicState,
480     const IntRect& rect)
481 {
482     webKitClient()->themeEngine()->paintButton(
483         gc->platformContext()->canvas(), part, state, classicState, rect);
484 }
485
486 void ChromiumBridge::paintMenuList(
487     GraphicsContext* gc, int part, int state, int classicState,
488     const IntRect& rect)
489 {
490     webKitClient()->themeEngine()->paintMenuList(
491         gc->platformContext()->canvas(), part, state, classicState, rect);
492 }
493
494 void ChromiumBridge::paintScrollbarArrow(
495     GraphicsContext* gc, int state, int classicState,
496     const IntRect& rect)
497 {
498     webKitClient()->themeEngine()->paintScrollbarArrow(
499         gc->platformContext()->canvas(), state, classicState, rect);
500 }
501
502 void ChromiumBridge::paintScrollbarThumb(
503     GraphicsContext* gc, int part, int state, int classicState,
504     const IntRect& rect)
505 {
506     webKitClient()->themeEngine()->paintScrollbarThumb(
507         gc->platformContext()->canvas(), part, state, classicState, rect);
508 }
509
510 void ChromiumBridge::paintScrollbarTrack(
511     GraphicsContext* gc, int part, int state, int classicState,
512     const IntRect& rect, const IntRect& alignRect)
513 {
514     webKitClient()->themeEngine()->paintScrollbarTrack(
515         gc->platformContext()->canvas(), part, state, classicState, rect,
516         alignRect);
517 }
518
519 void ChromiumBridge::paintTextField(
520     GraphicsContext* gc, int part, int state, int classicState,
521     const IntRect& rect, const Color& color, bool fillContentArea,
522     bool drawEdges)
523 {
524     // Fallback to white when |color| is invalid.
525     RGBA32 backgroundColor = color.isValid() ? color.rgb() : Color::white;
526
527     webKitClient()->themeEngine()->paintTextField(
528         gc->platformContext()->canvas(), part, state, classicState, rect,
529         backgroundColor, fillContentArea, drawEdges);
530 }
531
532 void ChromiumBridge::paintTrackbar(
533     GraphicsContext* gc, int part, int state, int classicState,
534     const IntRect& rect)
535 {
536     webKitClient()->themeEngine()->paintTrackbar(
537         gc->platformContext()->canvas(), part, state, classicState, rect);
538 }
539
540 #endif
541
542 // Trace Event ----------------------------------------------------------------
543
544 void ChromiumBridge::traceEventBegin(const char* name, void* id, const char* extra)
545 {
546     webKitClient()->traceEventBegin(name, id, extra);
547 }
548
549 void ChromiumBridge::traceEventEnd(const char* name, void* id, const char* extra)
550 {
551     webKitClient()->traceEventEnd(name, id, extra);
552 }
553
554 // Visited Links --------------------------------------------------------------
555
556 LinkHash ChromiumBridge::visitedLinkHash(const UChar* url, unsigned length)
557 {
558     url_canon::RawCanonOutput<2048> buffer;
559     url_parse::Parsed parsed;
560     if (!url_util::Canonicalize(url, length, 0, &buffer, &parsed))
561         return 0;  // Invalid URLs are unvisited.
562     return webKitClient()->visitedLinkHash(buffer.data(), buffer.length());
563 }
564
565 LinkHash ChromiumBridge::visitedLinkHash(const KURL& base,
566                                          const AtomicString& attributeURL)
567 {
568     // Resolve the relative URL using googleurl and pass the absolute URL up to
569     // the embedder. We could create a GURL object from the base and resolve
570     // the relative URL that way, but calling the lower-level functions
571     // directly saves us the string allocation in most cases.
572     url_canon::RawCanonOutput<2048> buffer;
573     url_parse::Parsed parsed;
574
575 #if USE(GOOGLEURL)
576     const CString& cstr = base.utf8String();
577     const char* data = cstr.data();
578     int length = cstr.length();
579     const url_parse::Parsed& srcParsed = base.parsed();
580 #else
581     // When we're not using GoogleURL, first canonicalize it so we can resolve it
582     // below.
583     url_canon::RawCanonOutput<2048> srcCanon;
584     url_parse::Parsed srcParsed;
585     String str = base.string();
586     if (!url_util::Canonicalize(str.characters(), str.length(), 0, &srcCanon, &srcParsed))
587         return 0;
588     const char* data = srcCanon.data();
589     int length = srcCanon.length();
590 #endif
591
592     if (!url_util::ResolveRelative(data, length, srcParsed, attributeURL.characters(),
593                                    attributeURL.length(), 0, &buffer, &parsed))
594         return 0;  // Invalid resolved URL.
595
596     return webKitClient()->visitedLinkHash(buffer.data(), buffer.length());
597 }
598
599 bool ChromiumBridge::isLinkVisited(LinkHash visitedLinkHash)
600 {
601     return webKitClient()->isLinkVisited(visitedLinkHash);
602 }
603
604 // These are temporary methods that the WebKit layer can use to call to the
605 // Glue layer. Once the Glue layer moves entirely into the WebKit layer, these
606 // methods will be deleted.
607
608 void ChromiumBridge::notifyJSOutOfMemory(Frame* frame)
609 {
610     if (!frame)
611         return;
612
613     WebFrameImpl* webFrame = WebFrameImpl::fromFrame(frame);
614     if (!webFrame->client())
615         return;
616     webFrame->client()->didExhaustMemoryAvailableForScript(webFrame);
617 }
618
619 int ChromiumBridge::memoryUsageMB()
620 {
621     return static_cast<int>(webKitClient()->memoryUsageMB());
622 }
623
624 int ChromiumBridge::screenDepth(Widget* widget)
625 {
626     WebWidgetClient* client = toWebWidgetClient(widget);
627     if (!client)
628         return 0;
629     return client->screenInfo().depth;
630 }
631
632 int ChromiumBridge::screenDepthPerComponent(Widget* widget)
633 {
634     WebWidgetClient* client = toWebWidgetClient(widget);
635     if (!client)
636         return 0;
637     return client->screenInfo().depthPerComponent;
638 }
639
640 bool ChromiumBridge::screenIsMonochrome(Widget* widget)
641 {
642     WebWidgetClient* client = toWebWidgetClient(widget);
643     if (!client)
644         return 0;
645     return client->screenInfo().isMonochrome;
646 }
647
648 IntRect ChromiumBridge::screenRect(Widget* widget)
649 {
650     WebWidgetClient* client = toWebWidgetClient(widget);
651     if (!client)
652         return IntRect();
653     return client->screenInfo().rect;
654 }
655
656 IntRect ChromiumBridge::screenAvailableRect(Widget* widget)
657 {
658     WebWidgetClient* client = toWebWidgetClient(widget);
659     if (!client)
660         return IntRect();
661     return client->screenInfo().availableRect;
662 }
663
664 bool ChromiumBridge::popupsAllowed(NPP npp)
665 {
666     // FIXME: Give the embedder a way to control this.
667     return false;
668 }
669
670 void ChromiumBridge::widgetSetCursor(Widget* widget, const Cursor& cursor)
671 {
672     ChromeClientImpl* client = toChromeClientImpl(widget);
673     if (client)
674         client->setCursor(WebCursorInfo(cursor));
675 }
676
677 void ChromiumBridge::widgetSetFocus(Widget* widget)
678 {
679     ChromeClientImpl* client = toChromeClientImpl(widget);
680     if (client)
681         client->focus();
682 }
683
684 WorkerContextProxy* WorkerContextProxy::create(Worker* worker)
685 {
686     return WebWorkerClientImpl::createWorkerContextProxy(worker);
687 }
688
689 } // namespace WebCore