Chromium Code Reviews| Index: chrome/browser/guest_view/mime_handler_view/mime_handler_view_guest.cc |
| diff --git a/chrome/browser/guest_view/mime_handler_view/mime_handler_view_guest.cc b/chrome/browser/guest_view/mime_handler_view/mime_handler_view_guest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e2bde2225d41e7e13f7db0b7d3b87f93022a4599 |
| --- /dev/null |
| +++ b/chrome/browser/guest_view/mime_handler_view/mime_handler_view_guest.cc |
| @@ -0,0 +1,212 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/guest_view/mime_handler_view/mime_handler_view_guest.h" |
| + |
| +#include "base/strings/stringprintf.h" |
| +#include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" |
| +#include "chrome/browser/extensions/extension_renderer_state.h" |
| +#include "chrome/browser/guest_view/guest_view_manager.h" |
| +#include "chrome/browser/guest_view/web_view/web_view_renderer_state.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "content/public/common/url_constants.h" |
| + |
| +#include "ipc/ipc_message_macros.h" |
| +#include "extensions/common/extension_messages.h" |
| + |
| +using content::WebContents; |
| + |
| +// static |
| +const char MimeHandlerViewGuest::Type[] = "mimehandler"; |
| + |
| +MimeHandlerViewGuest::MimeHandlerViewGuest( |
| + content::BrowserContext* browser_context, |
| + int guest_instance_id) |
| + : GuestView<MimeHandlerViewGuest>( |
| + browser_context, guest_instance_id) { |
| +} |
| + |
| +MimeHandlerViewGuest::~MimeHandlerViewGuest() { |
| +} |
| + |
| +void MimeHandlerViewGuest::CreateWebContents( |
| + const std::string& embedder_extension_id, |
| + int embedder_render_process_id, |
| + const base::DictionaryValue& create_params, |
| + const WebContentsCreatedCallback& callback) { |
| + content::RenderProcessHost* embedder_render_process_host = |
| + content::RenderProcessHost::FromID(embedder_render_process_id); |
| + |
| + std::string emburl; |
| + if (!create_params.GetString("emburl", &emburl)) { |
| + emburl = embedder_extension_id; |
| + } else { |
| + printf("Found emburl: %s\n", emburl.c_str()); |
| + } |
| + GURL guest_site(base::StringPrintf("%s://%s/", |
| + content::kGuestScheme, |
| + //embedder_extension_id.c_str(), |
| + emburl.c_str())); |
| + |
| + // If we already have a webview tag in the same app using the same storage |
| + // partition, we should use the same SiteInstance so the existing tag and |
| + // the new tag can script each other. |
| + GuestViewManager* guest_view_manager = |
| + GuestViewManager::FromBrowserContext( |
| + embedder_render_process_host->GetBrowserContext()); |
| + content::SiteInstance* guest_site_instance = |
| + guest_view_manager->GetGuestSiteInstance(guest_site); |
| + if (!guest_site_instance) { |
| + // Create the SiteInstance in a new BrowsingInstance, which will ensure |
| + // that webview tags are also not allowed to send messages across |
| + // different partitions. |
| + guest_site_instance = content::SiteInstance::CreateForURL( |
| + embedder_render_process_host->GetBrowserContext(), guest_site); |
| + } |
| + WebContents::CreateParams params( |
| + embedder_render_process_host->GetBrowserContext(), |
| + guest_site_instance); |
| + params.guest_delegate = this; |
| + callback.Run(WebContents::Create(params)); |
| +} |
| + |
| +void MimeHandlerViewGuest::DidAttachToEmbedder() { |
| + //CHECK(0); |
| + std::string src; |
| + if (extra_params()->GetString("src", &src) && !src.empty()) { |
| + NavigateGuest(src); |
|
Fady Samuel
2014/07/09 14:54:17
We don't expect MIME type handlers to navigate awa
lazyboy
2014/07/10 04:11:00
We still need to navigate once.
So we need one Nav
Fady Samuel
2014/07/10 15:13:42
Can we grab the URL from the browser process so th
|
| + printf("NONEMPTY!!!!"); |
| + } |
| + printf("src...... %s\n", src.c_str()); |
| + DCHECK(!GetOpener()); |
| +} |
| + |
| +void MimeHandlerViewGuest::NavigateGuest(const std::string& src) { |
| + GURL url = ResolveURL(src); |
| + |
| +// // Do not allow navigating a guest to schemes other than known safe schemes. |
| +// // This will block the embedder trying to load unwanted schemes, e.g. |
| +// // chrome://settings. |
| +// bool scheme_is_blocked = |
| +// (!content::ChildProcessSecurityPolicy::GetInstance()->IsWebSafeScheme( |
| +// url.scheme()) && |
| +// !url.SchemeIs(url::kAboutScheme)) || |
| +// url.SchemeIs(url::kJavaScriptScheme); |
| +// if (scheme_is_blocked || !url.is_valid()) { |
| +// std::string error_type(net::ErrorToString(net::ERR_ABORTED)); |
| +// DCHECK(StartsWithASCII(error_type, "net::", true)); |
| +// error_type.erase(0, 5); |
| +// LoadAbort(true /* is_top_level */, url, error_type); |
| +// return; |
| +// } |
| + |
| + GURL validated_url(url); |
| + guest_web_contents()->GetRenderProcessHost()-> |
| + FilterURL(false, &validated_url); |
| + // As guests do not swap processes on navigation, only navigations to |
| + // normal web URLs are supported. No protocol handlers are installed for |
| + // other schemes (e.g., WebUI or extensions), and no permissions or bindings |
| + // can be granted to the guest process. |
| + LoadURLWithParams(validated_url, |
| + content::Referrer(), |
| + content::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| + guest_web_contents()); |
| +} |
| + |
| +GURL MimeHandlerViewGuest::ResolveURL(const std::string& src) { |
| + return GURL(src); |
| +} |
| + |
| +void MimeHandlerViewGuest::LoadURLWithParams( |
| + const GURL& url, |
| + const content::Referrer& referrer, |
| + content::PageTransition transition_type, |
| + content::WebContents* web_contents) { |
| + content::NavigationController::LoadURLParams load_url_params(url); |
| + load_url_params.referrer = referrer; |
| + load_url_params.transition_type = transition_type; |
| + load_url_params.extra_headers = std::string(); |
| + web_contents->GetController().LoadURLWithParams(load_url_params); |
| +} |
| + |
| +void MimeHandlerViewGuest::WillAttachToEmbedder() { |
| + // We must install the mapping from guests to WebViews prior to resuming |
| + // suspended resource loads so that the WebRequest API will catch resource |
| + // requests. |
| + //AddMimeHandlerViewToExtensionRendererState(); |
| + PushMimeHandlerViewStateToIOThread(); |
| +} |
| + |
| +void MimeHandlerViewGuest::PushMimeHandlerViewStateToIOThread() { |
| + printf("+++++ %s\n", __PRETTY_FUNCTION__); |
| + ////const GURL& site_url = guest_web_contents()->GetSiteInstance()->GetSiteURL(); |
| + ////std::string partition_domain; |
| + ////std::string partition_id; |
| + ////bool in_memory; |
| + ////if (!GetGuestPartitionConfigForSite( |
| + //// site_url, &partition_domain, &partition_id, &in_memory)) { |
| + //// NOTREACHED(); |
| + //// return; |
| + ////} |
| + //DCHECK(embedder_extension_id() == partition_domain); |
| + |
| + WebViewRendererState::WebViewInfo web_view_info; |
| + web_view_info.embedder_process_id = embedder_render_process_id(); |
| + web_view_info.instance_id = view_instance_id(); |
| + web_view_info.embedder_extension_id = embedder_extension_id(); |
| + |
| + printf("Post tasking AddGuest\n"); |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(&WebViewRendererState::AddGuest, |
| + base::Unretained(WebViewRendererState::GetInstance()), |
| + guest_web_contents()->GetRenderProcessHost()->GetID(), |
| + guest_web_contents()->GetRoutingID(), |
| + web_view_info)); |
| +} |
| + |
| +void MimeHandlerViewGuest::AttachWebContentsHelpers( |
| + content::WebContents* contents) { |
| + // Required for ExtensionHostMsg_PostMessage. |
| + extensions::ChromeExtensionWebContentsObserver::CreateForWebContents( |
| + contents); |
| +} |
| + |
| +void MimeHandlerViewGuest::DidInitialize() { |
| + printf("++++ %s\n", __PRETTY_FUNCTION__); |
| + extension_function_dispatcher_.reset( |
| + new extensions::ExtensionFunctionDispatcher( |
| + browser_context(), this)); |
| + AttachWebContentsHelpers(guest_web_contents()); |
| +} |
| + |
| +extensions::WindowController* MimeHandlerViewGuest::GetExtensionWindowController() const { |
| + return NULL; |
| +} |
| + |
| +content::WebContents* MimeHandlerViewGuest::GetAssociatedWebContents() const { |
| + return guest_web_contents(); |
| +} |
| + |
| +bool MimeHandlerViewGuest::OnMessageReceived(const IPC::Message& message) { |
| + printf("+++++ %s\n", __PRETTY_FUNCTION__); |
| + bool handled = true; |
| + printf("type: %d\n", message.type()); |
| + printf("ExtensionHostMsg_Request::ID: %d\n", |
| + ExtensionHostMsg_Request::ID); |
| + IPC_BEGIN_MESSAGE_MAP(MimeHandlerViewGuest, message) |
| + IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +void MimeHandlerViewGuest::OnRequest( |
| + const ExtensionHostMsg_Request_Params& params) { |
| + CHECK(0); |
|
Fady Samuel
2014/07/09 14:54:17
We don't expect this to get called?
lazyboy
2014/07/10 04:11:00
No, we only need postMessage (chrome.runtime.sendM
|
| + extension_function_dispatcher_->Dispatch( |
| + params, guest_web_contents()->GetRenderViewHost()); |
| +} |