2 * Copyright 2007, The Android Open Source Project
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #define LOG_TAG "WebCore"
29 #include "FrameLoaderClientAndroid.h"
31 #include "BackForwardList.h"
32 #include "CachedFrame.h"
33 #include "CachedFramePlatformDataAndroid.h"
35 #include "ChromeClientAndroid.h"
36 #include "DOMImplementation.h"
38 #include "DocumentLoader.h"
39 #include "EditorClientAndroid.h"
41 #include "FrameLoader.h"
42 #include "FrameNetworkingContextAndroid.h"
43 #include "FrameTree.h"
44 #include "FrameView.h"
45 #include "GraphicsContext.h"
46 #include "HTMLFrameOwnerElement.h"
47 #include "HTMLPlugInElement.h"
48 #include "HistoryItem.h"
49 #include "IconDatabase.h"
50 #include "MIMETypeRegistry.h"
51 #include "NotImplemented.h"
52 #include "PackageNotifier.h"
54 #include "PlatformBridge.h"
55 #include "PlatformGraphicsContext.h"
56 #include "PlatformString.h"
57 #include "PluginDatabase.h"
58 #include "PluginView.h"
59 #include "PluginViewBase.h"
60 #include "ProgressTracker.h"
61 #include "RenderPart.h"
62 #include "RenderView.h"
63 #include "RenderWidget.h"
64 #include "ResourceError.h"
65 #include "ResourceHandle.h"
66 #include "ResourceHandleInternal.h"
67 #include "SelectionController.h"
71 #include "TextEncoding.h"
72 #include "WebCoreFrameBridge.h"
73 #include "WebCoreResourceLoader.h"
74 #include "WebHistory.h"
75 #include "WebIconDatabase.h"
76 #include "WebFrameView.h"
77 #include "WebViewClientError.h"
78 #include "WebViewCore.h"
79 #include "autofill/WebAutofill.h"
80 #include "android_graphics.h"
82 #include <utils/AssetManager.h>
83 #include <wtf/text/CString.h>
85 #define verifiedOk() // Verified that we don't need to implement this.
87 extern android::AssetManager* globalAssetManager();
91 static const int EXTRA_LAYOUT_DELAY = 1000;
93 FrameLoaderClientAndroid::FrameLoaderClientAndroid(WebFrame* webframe)
95 , m_webFrame(webframe)
96 , m_manualLoader(NULL)
97 , m_hasSentResponseToPlugin(false)
98 , m_onDemandPluginsEnabled(false)
99 , m_didReceiveServerRedirect(false) {
103 FrameLoaderClientAndroid* FrameLoaderClientAndroid::get(const WebCore::Frame* frame)
105 return static_cast<FrameLoaderClientAndroid*> (frame->loader()->client());
108 void FrameLoaderClientAndroid::frameLoaderDestroyed() {
109 registerForIconNotification(false);
115 bool FrameLoaderClientAndroid::hasWebView() const {
117 // there is one web view per page, or top frame.
118 // as android's view is created from Java side, it is always there.
122 void FrameLoaderClientAndroid::makeRepresentation(DocumentLoader*) {
123 m_onDemandPluginsEnabled = false;
124 // don't use representation
128 void FrameLoaderClientAndroid::forceLayout() {
130 m_frame->view()->forceLayout();
131 // FIXME, should we adjust view size here?
132 m_frame->view()->adjustViewSize();
135 void FrameLoaderClientAndroid::forceLayoutForNonHTML() {
139 void FrameLoaderClientAndroid::setCopiesOnScroll() {
140 // this is a hint about whether we need to force redraws, or can
141 // just copy the scrolled content. Since we always force a redraw
142 // anyways, we can ignore this call.
146 void FrameLoaderClientAndroid::detachedFromParent2() {
147 // FIXME, ready to detach frame from view
150 void FrameLoaderClientAndroid::detachedFromParent3() {
151 // FIXME, ready to release view
155 // This function is responsible for associating the "id" with a given
156 // subresource load. The following functions that accept an "id" are
157 // called for each subresource, so they should not be dispatched to the m_frame.
158 void FrameLoaderClientAndroid::assignIdentifierToInitialRequest(unsigned long id,
159 DocumentLoader*, const ResourceRequest&) {
163 void FrameLoaderClientAndroid::dispatchWillSendRequest(DocumentLoader*, unsigned long id,
164 ResourceRequest&, const ResourceResponse&) {
168 bool FrameLoaderClientAndroid::shouldUseCredentialStorage(DocumentLoader*, unsigned long identifier)
174 void FrameLoaderClientAndroid::dispatchDidReceiveAuthenticationChallenge(DocumentLoader*,
175 unsigned long id, const AuthenticationChallenge&) {
179 void FrameLoaderClientAndroid::dispatchDidCancelAuthenticationChallenge(DocumentLoader*,
180 unsigned long id, const AuthenticationChallenge&) {
184 void FrameLoaderClientAndroid::dispatchDidReceiveResponse(DocumentLoader*,
185 unsigned long id, const ResourceResponse&) {
189 void FrameLoaderClientAndroid::dispatchDidReceiveContentLength(DocumentLoader*,
190 unsigned long id, int lengthReceived) {
194 void FrameLoaderClientAndroid::dispatchDidFinishLoading(DocumentLoader*,
199 void FrameLoaderClientAndroid::dispatchDidFailLoading(DocumentLoader* docLoader,
200 unsigned long id, const ResourceError&) {
204 bool FrameLoaderClientAndroid::dispatchDidLoadResourceFromMemoryCache(DocumentLoader*,
205 const ResourceRequest&, const ResourceResponse&, int length) {
210 void FrameLoaderClientAndroid::dispatchDidHandleOnloadEvents() {
213 void FrameLoaderClientAndroid::dispatchDidReceiveServerRedirectForProvisionalLoad() {
214 ASSERT(!m_didReceiveServerRedirect);
215 m_didReceiveServerRedirect = true;
218 void FrameLoaderClientAndroid::dispatchDidCancelClientRedirect() {
222 void FrameLoaderClientAndroid::dispatchWillPerformClientRedirect(const KURL&,
223 double interval, double fireDate) {
227 void FrameLoaderClientAndroid::dispatchDidChangeLocationWithinPage() {
231 void FrameLoaderClientAndroid::dispatchDidPushStateWithinPage()
236 void FrameLoaderClientAndroid::dispatchDidReplaceStateWithinPage()
241 void FrameLoaderClientAndroid::dispatchDidPopStateWithinPage()
246 void FrameLoaderClientAndroid::dispatchWillClose() {
250 void FrameLoaderClientAndroid::dispatchDidReceiveIcon() {
252 if (m_frame->tree() && m_frame->tree()->parent())
254 WTF::String url(m_frame->document()->url().string());
255 // Try to obtain the icon image.
256 // FIXME: This method should not be used from outside WebCore and will be removed.
257 // http://trac.webkit.org/changeset/81484
258 WebCore::Image* icon = WebCore::iconDatabase().synchronousIconForPageURL(url, WebCore::IntSize(16, 16));
259 // If the request fails, try the original request url.
261 DocumentLoader* docLoader = m_frame->loader()->activeDocumentLoader();
262 KURL originalURL = docLoader->originalRequest().url();
263 // FIXME: This method should not be used from outside WebCore and will be removed.
264 // http://trac.webkit.org/changeset/81484
265 icon = WebCore::iconDatabase().synchronousIconForPageURL(originalURL, WebCore::IntSize(16, 16));
267 // There is a bug in webkit where cancelling an icon load is treated as a
268 // failure. When this is fixed, we can ASSERT again that we have an icon.
270 LOGV("Received icon (%p) for %s", icon,
272 m_webFrame->didReceiveIcon(icon);
274 LOGV("Icon data for %s unavailable, registering for notification...",
276 registerForIconNotification();
280 void FrameLoaderClientAndroid::dispatchDidReceiveTouchIconURL(const String& url, bool precomposed) {
282 // Do not report sub frame touch icons
283 if (m_frame->tree() && m_frame->tree()->parent())
285 m_webFrame->didReceiveTouchIconURL(url, precomposed);
288 void FrameLoaderClientAndroid::dispatchDidStartProvisionalLoad() {
292 void FrameLoaderClientAndroid::dispatchDidReceiveTitle(const StringWithDirection& title) {
294 // Used to check for FrameLoadTypeStandard but we only want to send the title for
295 // the top frame and not sub-frames.
296 // FIXME: Use direction of title.
297 if (!m_frame->tree() || !m_frame->tree()->parent()) {
298 m_webFrame->setTitle(title.string());
302 void FrameLoaderClientAndroid::dispatchDidCommitLoad() {
303 #if ENABLE(WEB_AUTOFILL)
304 if (m_frame == m_frame->page()->mainFrame()) {
305 EditorClientAndroid* editorC = static_cast<EditorClientAndroid*>(m_frame->page()->editorClient());
306 WebAutofill* autoFill = editorC->getAutofill();
313 static void loadDataIntoFrame(Frame* frame, KURL baseUrl, const String& url,
314 const String& data) {
315 if (baseUrl.isEmpty()) {
316 baseUrl = blankURL();
318 ResourceRequest request(baseUrl);
319 CString cstr = data.utf8();
320 RefPtr<WebCore::SharedBuffer> buf = WebCore::SharedBuffer::create(cstr.data(), cstr.length());
321 SubstituteData subData(buf, String("text/html"), String("utf-8"),
323 frame->loader()->load(request, subData, false);
326 void FrameLoaderClientAndroid::dispatchDidFailProvisionalLoad(const ResourceError& error) {
328 // Ignore ErrorInterrupted since it is due to a policy interruption. This
329 // is caused by a decision to download the main resource rather than
331 if (error.errorCode() == InternalErrorInterrupted
332 || error.errorCode() == InternalErrorCancelled) {
333 // If we decided to download the main resource or if the user cancelled
334 // it, make sure we report that the load is done.
339 AssetManager* am = globalAssetManager();
341 // Check to see if the error code was not generated internally
342 WebCore::PlatformBridge::rawResId id = WebCore::PlatformBridge::NoDomain;
343 if ((error.errorCode() == ErrorFile ||
344 error.errorCode() == ErrorFileNotFound) &&
345 (!error.localizedDescription().isEmpty())) {
346 id = WebCore::PlatformBridge::LoadError;
348 String filename = m_webFrame->getRawResourceFilename(id);
349 if (filename.isEmpty())
352 // Grab the error page from the asset manager
353 Asset* a = am->openNonAsset(
354 filename.utf8().data(), Asset::ACCESS_BUFFER);
358 // Take the failing url and encode html entities so javascript urls are not
360 CString failingUrl = error.failingURL().utf8();
361 WTF::Vector<char> url;
362 int len = failingUrl.length();
363 const char* data = failingUrl.data();
364 for (int i = 0; i < len; i++) {
366 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
367 || (c >= '0' && c <= '9'))
371 int res = sprintf(buf, "&#%d;", c);
373 url.append(buf, res);
376 // Vector sets up its data buffer lazilly, so if failingUrl is the empty
377 // string, the data buffer will be null. This will result in sanitizedUrl
378 // being null, and the string substitution below will be a no-op.
379 // FIXME: Ideally we'd always have a non-empty URL, or at least improve the
380 // wording of the error page in this case. See http://b/5293782.
381 String sanitizedUrl = url.data() ? String(url.data(), url.size()) : "";
383 // Replace all occurances of %s with the failing url.
384 String s = UTF8Encoding().decode((const char*)a->getBuffer(false), a->getLength());
385 s = s.replace("%s", sanitizedUrl);
387 // Replace all occurances of %e with the error text
388 s = s.replace("%e", error.localizedDescription());
390 // Create the request and the substitute data and tell the FrameLoader to
391 // load with the replacement data.
392 // use KURL(const char*) as KURL(const String& url) can trigger ASSERT for
393 // invalidate URL string.
394 loadDataIntoFrame(m_frame, KURL(ParsedURLString, data), error.failingURL(), s);
399 // Report that the load is finished, since it failed.
403 void FrameLoaderClientAndroid::dispatchDidFailLoad(const ResourceError&) {
404 // called when page is completed with error
408 void FrameLoaderClientAndroid::dispatchDidFinishDocumentLoad() {
409 // called when finishedParsing
413 void FrameLoaderClientAndroid::dispatchDidFinishLoad() {
417 void FrameLoaderClientAndroid::dispatchDidFirstLayout() {
419 // set EXTRA_LAYOUT_DELAY if the loader is not completed yet
420 if (!m_frame->loader()->isComplete())
421 m_frame->document()->setExtraLayoutDelay(EXTRA_LAYOUT_DELAY);
422 // we need to do this here instead of dispatchDidFirstVisuallyNonEmptyLayout
423 // so that about:blank will update the screen.
424 if (!m_frame->tree()->parent()) {
425 // Only need to notify Java side for the top frame
426 WebViewCore::getWebViewCore(m_frame->view())->didFirstLayout();
430 void FrameLoaderClientAndroid::dispatchDidFirstVisuallyNonEmptyLayout()
435 Frame* FrameLoaderClientAndroid::dispatchCreatePage(const NavigationAction&) {
437 #ifdef ANDROID_MULTIPLE_WINDOWS
438 if (m_frame->settings() && m_frame->settings()->supportMultipleWindows())
439 // Always a user gesture since window.open maps to
440 // ChromeClientAndroid::createWindow
441 return m_webFrame->createWindow(false, true);
444 // If the client doesn't support multiple windows, just replace the
445 // current frame's contents.
449 void FrameLoaderClientAndroid::dispatchShow() {
451 m_frame->view()->invalidate();
455 static bool TreatAsAttachment(const String& content_disposition) {
456 // Some broken sites just send
457 // Content-Disposition: ; filename="file"
458 // screen those out here.
459 if (content_disposition.startsWith(";"))
462 if (content_disposition.startsWith("inline", false))
465 // Some broken sites just send
466 // Content-Disposition: filename="file"
467 // without a disposition token... screen those out.
468 if (content_disposition.startsWith("filename", false))
471 // Also in use is Content-Disposition: name="file"
472 if (content_disposition.startsWith("name", false))
475 // We have a content-disposition of "attachment" or unknown.
476 // RFC 2183, section 2.8 says that an unknown disposition
477 // value should be treated as "attachment"
481 void FrameLoaderClientAndroid::dispatchDecidePolicyForResponse(FramePolicyFunction func,
482 const ResourceResponse& response, const ResourceRequest& request) {
488 PolicyChecker* policy = m_frame->loader()->policyChecker();
490 if (request.isNull()) {
491 (policy->*func)(PolicyIgnore);
494 // Default to Use (display internally).
495 PolicyAction action = PolicyUse;
496 // Check if we should Download instead.
497 const String& content_disposition = response.httpHeaderField("Content-Disposition");
498 if (!content_disposition.isEmpty() &&
499 TreatAsAttachment(content_disposition)) {
500 // Server wants to override our normal policy.
501 // Check to see if we are a sub frame (main frame has no owner element)
502 if (m_frame->ownerElement() != 0)
503 action = PolicyIgnore;
505 action = PolicyDownload;
506 (policy->*func)(action);
510 // Ask if it can be handled internally.
511 if (!canShowMIMEType(response.mimeType())) {
512 // Check to see if we are a sub frame (main frame has no owner element)
513 if (m_frame->ownerElement() != 0)
514 action = PolicyIgnore;
516 action = PolicyDownload;
517 (policy->*func)(action);
520 // A status code of 204 indicates no content change. Ignore the result.
521 WebCore::DocumentLoader* docLoader = m_frame->loader()->activeDocumentLoader();
522 if (docLoader->response().httpStatusCode() == 204)
523 action = PolicyIgnore;
524 (policy->*func)(action);
527 void FrameLoaderClientAndroid::dispatchDecidePolicyForNewWindowAction(FramePolicyFunction func,
528 const NavigationAction& action, const ResourceRequest& request,
529 PassRefPtr<FormState> formState, const String& frameName) {
535 if (request.isNull()) {
536 (m_frame->loader()->policyChecker()->*func)(PolicyIgnore);
540 if (action.type() == NavigationTypeFormSubmitted || action.type() == NavigationTypeFormResubmitted)
541 m_frame->loader()->resetMultipleFormSubmissionProtection();
543 // If we get to this point it means that a link has a target that was not
544 // found by the frame tree. Instead of creating a new frame, return the
545 // current frame in dispatchCreatePage.
546 if (canHandleRequest(request))
547 (m_frame->loader()->policyChecker()->*func)(PolicyUse);
549 (m_frame->loader()->policyChecker()->*func)(PolicyIgnore);
552 void FrameLoaderClientAndroid::cancelPolicyCheck() {
556 void FrameLoaderClientAndroid::dispatchUnableToImplementPolicy(const ResourceError&) {
560 void FrameLoaderClientAndroid::dispatchDecidePolicyForNavigationAction(FramePolicyFunction func,
561 const NavigationAction& action, const ResourceRequest& request,
562 PassRefPtr<FormState> formState) {
567 if (request.isNull()) {
568 (m_frame->loader()->policyChecker()->*func)(PolicyIgnore);
572 // Reset multiple form submission protection. If this is a resubmission, we check with the
573 // user and reset the protection if they choose to resubmit the form (see WebCoreFrameBridge.cpp)
574 if (action.type() == NavigationTypeFormSubmitted)
575 m_frame->loader()->resetMultipleFormSubmissionProtection();
577 if (action.type() == NavigationTypeFormResubmitted) {
578 m_webFrame->decidePolicyForFormResubmission(func);
581 (m_frame->loader()->policyChecker()->*func)(PolicyUse);
584 void FrameLoaderClientAndroid::dispatchWillSubmitForm(FramePolicyFunction func, PassRefPtr<FormState>) {
587 (m_frame->loader()->policyChecker()->*func)(PolicyUse);
590 void FrameLoaderClientAndroid::dispatchWillSendSubmitEvent(HTMLFormElement* form)
592 if (m_webFrame->shouldSaveFormData())
593 m_webFrame->saveFormData(form);
596 void FrameLoaderClientAndroid::dispatchDidLoadMainResource(DocumentLoader*) {
600 void FrameLoaderClientAndroid::revertToProvisionalState(DocumentLoader*) {
604 void FrameLoaderClientAndroid::setMainDocumentError(DocumentLoader* docLoader, const ResourceError& error) {
606 if (m_manualLoader) {
607 m_manualLoader->didFail(error);
608 m_manualLoader = NULL;
609 m_hasSentResponseToPlugin = false;
611 if (!error.isNull() && error.errorCode() >= InternalErrorLast && error.errorCode() != ERROR_OK)
612 m_webFrame->reportError(error.errorCode(),
613 error.localizedDescription(), error.failingURL());
617 // This function is called right before the progress is updated.
618 void FrameLoaderClientAndroid::willChangeEstimatedProgress() {
622 // This function is called after the progress has been updated. The bad part
623 // about this is that when a page is completed, this function is called after
624 // the progress has been reset to 0.
625 void FrameLoaderClientAndroid::didChangeEstimatedProgress() {
629 // This will give us the initial estimate when the page first starts to load.
630 void FrameLoaderClientAndroid::postProgressStartedNotification() {
633 m_webFrame->setProgress(m_frame->page()->progress()->estimatedProgress());
636 // This will give us any updated progress including the final progress.
637 void FrameLoaderClientAndroid::postProgressEstimateChangedNotification() {
640 m_webFrame->setProgress(m_frame->page()->progress()->estimatedProgress());
643 // This is just a notification that the progress has finished. Don't call
644 // setProgress(1) because postProgressEstimateChangedNotification will do so.
645 void FrameLoaderClientAndroid::postProgressFinishedNotification() {
646 WebViewCore* core = WebViewCore::getWebViewCore(m_frame->view());
647 if (!m_frame->tree()->parent()) {
648 // only need to notify Java for the top frame
649 core->notifyProgressFinished();
651 // notify plugins that the frame has loaded
652 core->notifyPluginsOnFrameLoad(m_frame);
655 void FrameLoaderClientAndroid::setMainFrameDocumentReady(bool) {
656 // this is only interesting once we provide an external API for the DOM
660 void FrameLoaderClientAndroid::startDownload(const ResourceRequest&) {
664 void FrameLoaderClientAndroid::willChangeTitle(DocumentLoader*) {
668 void FrameLoaderClientAndroid::didChangeTitle(DocumentLoader* loader) {
672 void FrameLoaderClientAndroid::finishedLoading(DocumentLoader* docLoader) {
673 // Telling the frame we received some data and passing 0 as the data is our
674 // way to get work done that is normally done when the first bit of data is
675 // received, even for the case of a document with no data (like about:blank)
676 if (!m_manualLoader) {
677 committedLoad(docLoader, 0, 0);
681 m_manualLoader->didFinishLoading();
682 m_manualLoader = NULL;
683 m_hasSentResponseToPlugin = false;
686 void FrameLoaderClientAndroid::updateGlobalHistory() {
689 DocumentLoader* docLoader = m_frame->loader()->documentLoader();
692 // Code copied from FrameLoader.cpp:createHistoryItem
693 // Only add this URL to the database if it is a valid page
694 if (docLoader->unreachableURL().isEmpty()
695 && docLoader->response().httpStatusCode() < 400) {
696 m_webFrame->updateVisitedHistory(docLoader->urlForHistory(), false);
697 if (!docLoader->serverRedirectSourceForHistory().isNull())
698 m_webFrame->updateVisitedHistory(KURL(ParsedURLString, docLoader->serverRedirectDestinationForHistory()), false);
702 void FrameLoaderClientAndroid::updateGlobalHistoryRedirectLinks() {
703 // Note, do we need to do anything where there is no HistoryItem? If we call
704 // updateGlobalHistory(), we will add bunch of "data:xxx" urls for gmail.com
705 // which is not what we want. Opt to do nothing now.
708 bool FrameLoaderClientAndroid::shouldGoToHistoryItem(HistoryItem* item) const {
709 // hmmm, seems like we might do a more thoughtful check
714 bool FrameLoaderClientAndroid::shouldStopLoadingForHistoryItem(HistoryItem* item) const
719 void FrameLoaderClientAndroid::didDisplayInsecureContent()
724 void FrameLoaderClientAndroid::didRunInsecureContent(SecurityOrigin*, const KURL&)
729 void FrameLoaderClientAndroid::committedLoad(DocumentLoader* loader, const char* data, int length) {
731 loader->commitData(data, length);
733 // commit data may have created a manual plugin loader
734 if (m_manualLoader) {
735 if (!m_hasSentResponseToPlugin) {
736 m_manualLoader->didReceiveResponse(loader->response());
737 // Failure could cause the main document to have an error causing
738 // the manual loader to be reset.
741 m_hasSentResponseToPlugin = true;
743 m_manualLoader->didReceiveData(data, length);
747 ResourceError FrameLoaderClientAndroid::cancelledError(const ResourceRequest& request) {
748 return ResourceError(String(), InternalErrorCancelled, request.url(), String());
751 ResourceError FrameLoaderClientAndroid::cannotShowURLError(const ResourceRequest& request) {
752 return ResourceError(String(), InternalErrorCannotShowUrl, request.url(), String());
755 ResourceError FrameLoaderClientAndroid::interruptForPolicyChangeError(const ResourceRequest& request) {
756 return ResourceError(String(), InternalErrorInterrupted, request.url(), String());
759 ResourceError FrameLoaderClientAndroid::cannotShowMIMETypeError(const ResourceResponse& request) {
760 return ResourceError(String(), InternalErrorCannotShowMimeType, request.url(), String());
763 ResourceError FrameLoaderClientAndroid::fileDoesNotExistError(const ResourceResponse& request) {
764 return ResourceError(String(), InternalErrorFileDoesNotExist, request.url(), String());
767 ResourceError FrameLoaderClientAndroid::pluginWillHandleLoadError(const ResourceResponse& request) {
768 return ResourceError(String(), InternalErrorPluginWillHandleLoadError, request.url(), String());
771 bool FrameLoaderClientAndroid::shouldFallBack(const ResourceError&) {
776 bool FrameLoaderClientAndroid::canHandleRequest(const ResourceRequest& request) const {
777 // This is called by WebCore to determine if this load can be handled by the
778 // WebView. In general, we delegate to the WebFrame, which may ask the
779 // embedding application whether it wishes to hijack the load. However, we
780 // don't allow this if the load is ...
781 // - An intrapage navigation
782 // - An iframe with a HTTP or HTTPS scheme URL
783 bool canHandle = WebCore::equalIgnoringFragmentIdentifier(request.url(), m_frame->document()->url()) ||
784 (request.url().protocol().startsWith("http", false) && m_frame->tree() && m_frame->tree()->parent()) ||
785 m_webFrame->canHandleRequest(request);
787 // If this is a server-side redirect and the WebView will handle loading it,
788 // notify the WebFrame, which may notify the embedding application that
789 // we're loading a new URL.
790 if (m_didReceiveServerRedirect && canHandle)
791 m_webFrame->loadStarted(m_frame);
792 m_didReceiveServerRedirect = false;
797 bool FrameLoaderClientAndroid::canShowMIMEType(const String& mimeType) const {
798 // FIXME: This looks like it has to do with whether or not a type can be
799 // shown "internally" (i.e. inside the browser) regardless of whether
800 // or not the browser is doing the rendering, e.g. a full page plugin.
801 if (MIMETypeRegistry::isSupportedImageResourceMIMEType(mimeType) ||
802 MIMETypeRegistry::isSupportedNonImageMIMEType(mimeType) ||
803 MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType) ||
804 (m_frame && m_frame->settings()
805 && m_frame->settings()->arePluginsEnabled()
806 && PluginDatabase::installedPlugins()->isMIMETypeRegistered(
808 (DOMImplementation::isTextMIMEType(mimeType) &&
809 !mimeType.startsWith("text/vnd")) ||
810 DOMImplementation::isXMLMIMEType(mimeType))
815 bool FrameLoaderClientAndroid::canShowMIMETypeAsHTML(const String& mimeType) const {
819 bool FrameLoaderClientAndroid::representationExistsForURLScheme(const String&) const {
820 // don't use representation
825 String FrameLoaderClientAndroid::generatedMIMETypeForURLScheme(const String& URLScheme) const {
826 // FIXME, copy from Apple's port
827 String mimetype("x-apple-web-kit/");
828 mimetype.append(URLScheme.lower());
832 void FrameLoaderClientAndroid::frameLoadCompleted() {
833 // copied from Apple port, without this back with sub-frame will trigger ASSERT
837 void FrameLoaderClientAndroid::saveViewStateToItem(HistoryItem* item) {
840 // store the current scale (only) for the top frame
841 if (!m_frame->tree()->parent()) {
842 // We should have added a bridge when the child item was added to its
844 AndroidWebHistoryBridge* bridge = item->bridge();
846 WebViewCore* webViewCore = WebViewCore::getWebViewCore(m_frame->view());
847 bridge->setScale(webViewCore->scale());
848 bridge->setTextWrapScale(webViewCore->textWrapScale());
851 WebCore::notifyHistoryItemChanged(item);
854 void FrameLoaderClientAndroid::restoreViewState() {
855 WebViewCore* webViewCore = WebViewCore::getWebViewCore(m_frame->view());
856 HistoryItem* item = m_frame->loader()->history()->currentItem();
857 AndroidWebHistoryBridge* bridge = item->bridge();
858 // restore the scale (only) for the top frame
859 if (!m_frame->tree()->parent()) {
860 webViewCore->restoreScale(bridge->scale(), bridge->textWrapScale());
864 void FrameLoaderClientAndroid::dispatchDidAddBackForwardItem(HistoryItem* item) const {
866 m_webFrame->addHistoryItem(item);
869 void FrameLoaderClientAndroid::dispatchDidRemoveBackForwardItem(HistoryItem* item) const {
871 m_webFrame->removeHistoryItem(0);
874 void FrameLoaderClientAndroid::dispatchDidChangeBackForwardIndex() const {
876 BackForwardList* list = m_frame->page()->backForwardList();
878 m_webFrame->updateHistoryIndex(list->backListCount());
881 void FrameLoaderClientAndroid::provisionalLoadStarted() {
883 m_webFrame->loadStarted(m_frame);
886 void FrameLoaderClientAndroid::didFinishLoad() {
888 m_frame->document()->setExtraLayoutDelay(0);
889 m_webFrame->didFinishLoad(m_frame);
892 void FrameLoaderClientAndroid::prepareForDataSourceReplacement() {
896 PassRefPtr<DocumentLoader> FrameLoaderClientAndroid::createDocumentLoader(
897 const ResourceRequest& request, const SubstituteData& data) {
898 RefPtr<DocumentLoader> loader = DocumentLoader::create(request, data);
899 return loader.release();
902 void FrameLoaderClientAndroid::setTitle(const StringWithDirection& title, const KURL& url) {
903 // Not needed. dispatchDidReceiveTitle is called immediately after this.
904 // url is used to update the Apple port history items.
908 String FrameLoaderClientAndroid::userAgent(const KURL& u) {
909 return m_webFrame->userAgentForURL(&u);
912 void FrameLoaderClientAndroid::savePlatformDataToCachedFrame(WebCore::CachedFrame* cachedFrame) {
913 CachedFramePlatformDataAndroid* platformData = new CachedFramePlatformDataAndroid(m_frame->settings());
914 cachedFrame->setCachedFramePlatformData(platformData);
917 void FrameLoaderClientAndroid::transitionToCommittedFromCachedFrame(WebCore::CachedFrame* cachedFrame) {
918 CachedFramePlatformDataAndroid* platformData = reinterpret_cast<CachedFramePlatformDataAndroid*>(cachedFrame->cachedFramePlatformData());
919 #ifdef ANDROID_META_SUPPORT
920 platformData->restoreMetadata(m_frame->settings());
923 #if ENABLE(ANDROID_OVERFLOW_SCROLL)
925 WebViewCore* webViewCore = WebViewCore::getWebViewCore(m_frame->view());
927 webViewCore->clearContent();
930 m_webFrame->transitionToCommitted(m_frame);
933 void FrameLoaderClientAndroid::transitionToCommittedForNewPage() {
936 #ifdef ANDROID_META_SUPPORT
937 // reset metadata settings for the main frame as they are not preserved cross page
938 if (m_frame == m_frame->page()->mainFrame() && m_frame->settings())
939 m_frame->settings()->resetMetadataSettings();
942 // Save the old WebViewCore before creating a new FrameView. There is one
943 // WebViewCore per page. Each frame, including the main frame and sub frame,
944 // has a 1:1 FrameView and WebFrameView.
945 WebViewCore* webViewCore = WebViewCore::getWebViewCore(m_frame->view());
948 // Save the old WebFrameView's bounds and apply them to the new WebFrameView
949 WebFrameView* oldWebFrameView = static_cast<WebFrameView*> (m_frame->view()->platformWidget());
950 IntRect bounds = oldWebFrameView->getBounds();
951 IntRect visBounds = oldWebFrameView->getVisibleBounds();
952 IntRect windowBounds = oldWebFrameView->getWindowBounds();
953 WebCore::FrameView* oldFrameView = oldWebFrameView->view();
954 const float oldZoomFactor = oldFrameView->frame()->textZoomFactor();
955 m_frame->createView(bounds.size(), oldFrameView->baseBackgroundColor(), oldFrameView->isTransparent(),
956 oldFrameView->fixedLayoutSize(), oldFrameView->useFixedLayout());
957 if (oldZoomFactor != 1.0f && oldZoomFactor != m_frame->textZoomFactor()) {
958 m_frame->setTextZoomFactor(oldZoomFactor);
961 // Create a new WebFrameView for the new FrameView
962 WebFrameView* newFrameView = new WebFrameView(m_frame->view(), webViewCore);
964 #if ENABLE(ANDROID_OVERFLOW_SCROLL)
966 webViewCore->clearContent();
969 newFrameView->setLocation(bounds.x(), bounds.y());
970 newFrameView->setSize(bounds.width(), bounds.height());
971 newFrameView->setVisibleSize(visBounds.width(), visBounds.height());
972 newFrameView->setWindowBounds(windowBounds.x(), windowBounds.y(), windowBounds.width(), windowBounds.height());
973 // newFrameView attaches itself to FrameView which Retains the reference, so
974 // call Release for newFrameView
975 Release(newFrameView);
976 // WebFrameView Retains webViewCore, so call Release for webViewCore
977 Release(webViewCore);
979 m_webFrame->transitionToCommitted(m_frame);
982 void FrameLoaderClientAndroid::dispatchDidBecomeFrameset(bool)
986 bool FrameLoaderClientAndroid::canCachePage() const {
990 void FrameLoaderClientAndroid::download(ResourceHandle* handle, const ResourceRequest&,
991 const ResourceRequest&, const ResourceResponse&) {
992 // Get the C++ side of the load listener and tell it to handle the download
993 handle->getInternal()->m_loader->downloadFile();
996 WTF::PassRefPtr<WebCore::Frame> FrameLoaderClientAndroid::createFrame(const KURL& url, const String& name,
997 HTMLFrameOwnerElement* ownerElement, const String& referrer,
998 bool allowsScrolling, int marginWidth, int marginHeight)
1000 Frame* parent = ownerElement->document()->frame();
1001 FrameLoaderClientAndroid* loaderC = new FrameLoaderClientAndroid(m_webFrame);
1002 RefPtr<Frame> pFrame = Frame::create(parent->page(), ownerElement, loaderC);
1003 Frame* newFrame = pFrame.get();
1004 loaderC->setFrame(newFrame);
1005 // Append the subframe to the parent and set the name of the subframe. The name must be set after
1006 // appending the child so that the name becomes unique.
1007 parent->tree()->appendChild(newFrame);
1008 newFrame->tree()->setName(name);
1009 // Create a new FrameView and WebFrameView for the child frame to draw into.
1010 RefPtr<FrameView> frameView = FrameView::create(newFrame);
1011 WebFrameView* webFrameView = new WebFrameView(frameView.get(),
1012 WebViewCore::getWebViewCore(parent->view()));
1013 // frameView Retains webFrameView, so call Release for webFrameView
1014 Release(webFrameView);
1015 // Attach the frameView to the newFrame.
1016 newFrame->setView(frameView);
1018 newFrame->selection()->setFocused(true);
1019 LOGV("::WebCore:: createSubFrame returning %p", newFrame);
1021 // The creation of the frame may have run arbitrary JavaScript that removed it from the page already.
1022 if (!pFrame->page())
1025 parent->loader()->loadURLIntoChildFrame(url, referrer, pFrame.get());
1027 // onLoad may cuase the frame to be removed from the document. Allow the RefPtr to delete the child frame.
1028 if (!pFrame->tree()->parent())
1031 return pFrame.release();
1034 // YouTube flash url path starts with /v/
1035 static const char slash_v_slash[] = { '/', 'v', '/' };
1036 static const char slash_e_slash[] = { '/', 'e', '/' };
1038 static bool isValidYouTubeVideo(const String& path)
1040 if (!charactersAreAllASCII(path.characters(), path.length()))
1042 unsigned int len = path.length();
1043 if (len <= sizeof(slash_v_slash)) // check for more than just /v/
1045 CString str = path.lower().utf8();
1046 const char* data = str.data();
1047 // Youtube flash url can start with /v/ or /e/
1048 if (memcmp(data, slash_v_slash, sizeof(slash_v_slash)) != 0)
1049 if (memcmp(data, slash_e_slash, sizeof(slash_e_slash)) != 0)
1052 for (unsigned int i = sizeof(slash_v_slash); i < len; i++) {
1054 // Check for alpha-numeric characters only.
1055 if (WTF::isASCIIAlphanumeric(c) || c == '_' || c == '-')
1057 // The url can have more parameters such as &hl=en after the video id.
1058 // Once we start seeing extra parameters we can return true.
1059 return c == '&' && i > sizeof(slash_v_slash);
1064 static bool isYouTubeUrl(const KURL& url, const String& mimeType)
1066 String host = url.host();
1067 bool youtube = host.endsWith("youtube.com")
1068 || host.endsWith("youtube-nocookie.com");
1069 return youtube && isValidYouTubeVideo(url.path())
1070 && equalIgnoringCase(mimeType, "application/x-shockwave-flash");
1073 static bool isYouTubeInstalled() {
1074 return WebCore::packageNotifier().isPackageInstalled("com.google.android.youtube");
1077 // Use PluginViewBase rather than an Android specific sub class as we do not require any
1078 // Android specific functionality; this just renders a placeholder which will later
1079 // activate the real plugin.
1080 class PluginToggleWidget : public PluginViewBase {
1082 PluginToggleWidget(Frame* parent, const IntSize& size,
1083 HTMLPlugInElement* elem, const KURL& url,
1084 const WTF::Vector<String>& paramNames,
1085 const WTF::Vector<String>& paramValues, const String& mimeType,
1092 , m_paramNames(paramNames)
1093 , m_paramValues(paramValues)
1094 , m_mimeType(mimeType)
1095 , m_loadManually(loadManually)
1100 virtual void paint(GraphicsContext* ctx, const IntRect& rect)
1102 // Most of this code is copied from PluginView::paintMissingPluginIcon
1103 // with slight modification.
1105 static RefPtr<Image> image;
1107 image = Image::loadPlatformResource("togglePlugin");
1110 IntRect imageRect(x(), y(), image->width(), image->height());
1112 int xOffset = (width() - imageRect.width()) >> 1;
1113 int yOffset = (height() - imageRect.height()) >> 1;
1115 imageRect.move(xOffset, yOffset);
1117 if (!rect.intersects(imageRect))
1120 // FIXME: We need to clip similarly to paintMissingPluginIcon but it is
1121 // way screwed up right now. It has something to do with how we tell
1122 // webkit the scroll position and it causes the placeholder to get
1123 // clipped very badly. http://b/issue?id=2533303
1126 ctx->clip(frameRect());
1128 ctx->setFillColor(Color::white, ColorSpaceDeviceRGB);
1129 ctx->fillRect(frameRect());
1130 if (frameRect().contains(imageRect)) {
1131 // Leave a 2 pixel padding.
1132 const int pixelWidth = 2;
1133 IntRect innerRect = frameRect();
1134 innerRect.inflate(-pixelWidth);
1135 // Draw a 2 pixel light gray border.
1136 ctx->setStrokeColor(Color::lightGray, ColorSpaceDeviceRGB);
1137 ctx->strokeRect(innerRect, pixelWidth);
1140 // Draw the image in the center
1141 ctx->drawImage(image.get(), ColorSpaceDeviceRGB, imageRect.location());
1145 virtual void handleEvent(Event* event)
1147 if (event->type() != eventNames().clickEvent)
1150 Frame* frame = m_parent->page()->mainFrame();
1152 RenderView* view = frame->contentRenderer();
1153 const HashSet<RenderWidget*> widgets = view->widgets();
1154 HashSet<RenderWidget*>::const_iterator it = widgets.begin();
1155 HashSet<RenderWidget*>::const_iterator end = widgets.end();
1156 for (; it != end; ++it) {
1157 Widget* widget = (*it)->widget();
1158 // PluginWidget is used only with PluginToggleWidget
1159 if (widget && widget->isPluginViewBase()) {
1160 PluginToggleWidget* ptw =
1161 static_cast<PluginToggleWidget*>(widget);
1162 ptw->swapPlugin(*it);
1165 frame = frame->tree()->traverseNext();
1169 void swapPlugin(RenderWidget* renderer) {
1170 typedef FrameLoaderClientAndroid FLCA;
1171 FLCA* client = static_cast<FLCA*>(m_parent->loader()->client());
1172 client->enableOnDemandPlugins();
1173 WTF::PassRefPtr<PluginView> prpWidget =
1174 PluginView::create(m_parent.get(),
1182 RefPtr<Widget> myProtector(this);
1183 prpWidget->focusPluginElement();
1184 renderer->setWidget(prpWidget);
1188 void invalidateRect(const IntRect& rect) { }
1190 RefPtr<Frame> m_parent;
1192 HTMLPlugInElement* m_element;
1194 WTF::Vector<String> m_paramNames;
1195 WTF::Vector<String> m_paramValues;
1197 bool m_loadManually;
1200 WTF::PassRefPtr<Widget> FrameLoaderClientAndroid::createPlugin(
1201 const IntSize& size,
1202 HTMLPlugInElement* element,
1204 const WTF::Vector<String>& names,
1205 const WTF::Vector<String>& values,
1206 const String& mimeType,
1207 bool loadManually) {
1208 WTF::PassRefPtr<PluginView> prpWidget = 0;
1209 #ifdef ANDROID_PLUGINS
1210 // This is copied from PluginView.cpp. We need to determine if a plugin
1211 // will be found before doing some of the work in PluginView.
1212 String mimeTypeCopy = mimeType;
1213 PluginPackage* plugin =
1214 PluginDatabase::installedPlugins()->findPlugin(url, mimeTypeCopy);
1215 if (!plugin && PluginDatabase::installedPlugins()->refresh()) {
1216 mimeTypeCopy = mimeType;
1217 plugin = PluginDatabase::installedPlugins()->findPlugin(url,
1220 Settings* settings = m_frame->settings();
1221 // Do the placeholder if plugins are on-demand and there is a plugin for the
1223 if (settings && settings->arePluginsOnDemand() && plugin &&
1224 !m_onDemandPluginsEnabled) {
1225 return adoptRef(new PluginToggleWidget(m_frame, size, element, url,
1226 names, values, mimeType, loadManually));
1228 prpWidget = PluginView::create(m_frame,
1236 // Return the plugin if it was loaded successfully. Otherwise, fallback to
1237 // the youtube placeholder if possible. No need to check prpWidget as
1238 // PluginView::create will create a PluginView for missing plugins.
1239 // Note: this check really only checks if the plugin was found and not if
1240 // the plugin was loaded.
1241 if (prpWidget->status() == PluginStatusLoadedSuccessfully)
1244 // Create an iframe for youtube urls.
1245 if (isYouTubeUrl(url, mimeType) && isYouTubeInstalled()) {
1246 WTF::RefPtr<Frame> frame = createFrame(blankURL(), String(), element,
1247 String(), false, 0, 0);
1249 // grab everything after /v/
1250 String videoId = url.path().substring(sizeof(slash_v_slash));
1251 // Extract just the video id
1252 unsigned videoIdEnd = 0;
1253 for (; videoIdEnd < videoId.length(); videoIdEnd++) {
1254 if (videoId[videoIdEnd] == '&') {
1255 videoId = videoId.left(videoIdEnd);
1259 AssetManager* am = globalAssetManager();
1260 Asset* a = am->open("webkit/youtube.html",
1261 Asset::ACCESS_BUFFER);
1264 String s = String((const char*)a->getBuffer(false), a->getLength());
1265 s = s.replace("VIDEO_ID", videoId);
1267 loadDataIntoFrame(frame.get(),
1268 KURL(ParsedURLString, "file:///android_asset/webkit/"), String(), s);
1269 // Transfer ownership to a local refptr.
1270 WTF::RefPtr<Widget> widget(frame->view());
1271 return widget.release();
1277 void FrameLoaderClientAndroid::redirectDataToPlugin(Widget* pluginWidget) {
1278 // Do not redirect data if the Widget is our plugin placeholder.
1279 if (pluginWidget->isPluginView()) {
1280 m_manualLoader = static_cast<PluginView*>(pluginWidget);
1284 WTF::PassRefPtr<Widget> FrameLoaderClientAndroid::createJavaAppletWidget(const IntSize&, HTMLAppletElement*,
1285 const KURL& baseURL, const WTF::Vector<String>& paramNames,
1286 const WTF::Vector<String>& paramValues) {
1287 // don't support widget yet
1292 void FrameLoaderClientAndroid::didTransferChildFrameToNewDocument(WebCore::Page*)
1295 // m_webFrame points to the WebFrame for the page that our frame previously
1296 // belonged to. If the frame now belongs to a new page, we need to update
1297 // m_webFrame to point to the WebFrame for the new page.
1298 Page* newPage = m_frame->page();
1299 if (newPage != m_webFrame->page()) {
1300 ChromeClientAndroid* chromeClient = static_cast<ChromeClientAndroid*>(newPage->chrome()->client());
1301 Release(m_webFrame);
1302 m_webFrame = chromeClient->webFrame();
1307 void FrameLoaderClientAndroid::transferLoadingResourceFromPage(unsigned long, DocumentLoader*, const ResourceRequest&, Page*)
1312 // This function is used by the <OBJECT> element to determine the type of
1313 // the contents and work out if it can render it.
1314 ObjectContentType FrameLoaderClientAndroid::objectContentType(const KURL& url, const String& mimeType, bool shouldPreferPlugInsForImages) {
1315 return FrameLoader::defaultObjectContentType(url, mimeType, shouldPreferPlugInsForImages);
1318 // This function allows the application to set the correct CSS media
1319 // style. Android could use it to set the media style 'handheld'. Safari
1320 // may use it to set the media style to 'print' when the user wants to print
1321 // a particular web page.
1322 String FrameLoaderClientAndroid::overrideMediaType() const {
1327 // This function is used to re-attach Javascript<->native code classes.
1328 void FrameLoaderClientAndroid::dispatchDidClearWindowObjectInWorld(DOMWrapperWorld* world)
1330 if (world != mainThreadNormalWorld())
1334 LOGV("::WebCore:: windowObjectCleared called on frame %p for %s\n",
1335 m_frame, m_frame->loader()->url().string().ascii().data());
1336 m_webFrame->windowObjectCleared(m_frame);
1339 void FrameLoaderClientAndroid::documentElementAvailable() {
1342 // functions new to Jun-07 tip of tree merge:
1343 ResourceError FrameLoaderClientAndroid::blockedError(ResourceRequest const& request) {
1344 return ResourceError(String(), InternalErrorFileDoesNotExist, String(), String());
1347 // functions new to Nov-07 tip of tree merge:
1348 void FrameLoaderClientAndroid::didPerformFirstNavigation() const {
1349 // This seems to be just a notification that the UI can listen to, to
1350 // know if the user has performed first navigation action.
1351 // It is called from
1352 // void FrameLoader::addBackForwardItemClippedAtTarget(bool doClip)
1353 // "Navigation" here means a transition from one page to another that
1354 // ends up in the back/forward list.
1357 void FrameLoaderClientAndroid::registerForIconNotification(bool listen) {
1359 WebIconDatabase::RegisterForIconNotification(this);
1361 WebIconDatabase::UnregisterForIconNotification(this);
1364 // This is the WebIconDatabaseClient method for receiving a notification when we
1365 // get the icon for the page.
1366 void FrameLoaderClientAndroid::didAddIconForPageUrl(const String& pageUrl) {
1367 // This call must happen before dispatchDidReceiveIcon since that method
1368 // may register for icon notifications again since the icon data may have
1369 // to be read from disk.
1370 registerForIconNotification(false);
1371 KURL u(ParsedURLString, pageUrl);
1372 if (equalIgnoringFragmentIdentifier(u, m_frame->document()->url())) {
1373 dispatchDidReceiveIcon();
1377 void FrameLoaderClientAndroid::dispatchDidChangeIcons() {
1381 PassRefPtr<FrameNetworkingContext> FrameLoaderClientAndroid::createNetworkingContext()
1383 return FrameNetworkingContextAndroid::create(getFrame());