Chromium Code Reviews| Index: chrome/browser/guest_view/extension_options/extension_options_guest.cc |
| diff --git a/chrome/browser/guest_view/extension_options/extension_options_guest.cc b/chrome/browser/guest_view/extension_options/extension_options_guest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..36b57e55e8b529413f26193e2e80fbdef3294e23 |
| --- /dev/null |
| +++ b/chrome/browser/guest_view/extension_options/extension_options_guest.cc |
| @@ -0,0 +1,121 @@ |
| +// 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/extension_options/extension_options_guest.h" |
| + |
| +#include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" |
| +#include "chrome/browser/guest_view/extension_options/extension_options_constants.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/extensions/manifest_url_handler.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "content/public/browser/site_instance.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "extensions/browser/extension_function_dispatcher.h" |
| +#include "extensions/browser/extension_registry.h" |
| +#include "extensions/common/extension.h" |
| +#include "extensions/common/extension_messages.h" |
| +#include "extensions/common/permissions/permissions_data.h" |
| +#include "ipc/ipc_message_macros.h" |
| + |
| +using content::WebContents; |
| + |
| +// static |
| +const char ExtensionOptionsGuest::Type[] = "extensionoptions"; |
| + |
| +ExtensionOptionsGuest::ExtensionOptionsGuest( |
| + content::BrowserContext* browser_context, |
| + int guest_instance_id) |
| + : GuestView<ExtensionOptionsGuest>(browser_context, guest_instance_id) { |
| +} |
| + |
| +ExtensionOptionsGuest::~ExtensionOptionsGuest() { |
| +} |
| + |
| +bool ExtensionOptionsGuest::CanEmbedderUseGuestView( |
| + const std::string& embedder_extension_id) { |
| + extensions::ExtensionRegistry* registry = |
|
Devlin
2014/07/17 20:05:09
nit: to me, this looks a bit excessive with the va
ericzeng
2014/07/17 23:14:47
I'll create a variable just for the extension, and
|
| + extensions::ExtensionRegistry::Get(browser_context()); |
| + const extensions::Extension* embedder_extension = |
| + registry->enabled_extensions().GetByID(embedder_extension_id); |
| + const extensions::PermissionsData* permissions_data = |
| + embedder_extension->permissions_data(); |
| + return permissions_data->HasAPIPermission( |
| + extensions::APIPermission::kEmbeddedExtensionOptions); |
| +} |
| + |
| +void ExtensionOptionsGuest::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 = |
|
Devlin
2014/07/17 20:05:09
nit: Can we inline these?
content::BrowserContext
ericzeng
2014/07/17 23:14:47
Done.
|
| + content::RenderProcessHost::FromID(embedder_render_process_id); |
| + content::BrowserContext* browser_context = |
| + embedder_render_process_host->GetBrowserContext(); |
| + |
| + // Get the extension's base URL. |
| + std::string extension_id; |
| + create_params.GetString(extensionoptions::kExtensionId, &extension_id); |
| + if (extension_id.empty()) { |
| + callback.Run(NULL); |
| + return; |
| + } |
|
Devlin
2014/07/17 20:05:09
Maybe a DCHECK(Extension::IdIsValid(extension_id))
ericzeng
2014/07/17 23:14:47
Done.
|
| + GURL extension_url = |
| + extensions::Extension::GetBaseURLFromExtensionId(extension_id); |
| + if (!extension_url.is_valid()) { |
| + callback.Run(NULL); |
| + return; |
| + } |
| + |
| + // Get the options page URL for later use. |
| + extensions::ExtensionRegistry* registry = |
| + extensions::ExtensionRegistry::Get(browser_context); |
| + const extensions::Extension* extension = |
| + registry->enabled_extensions().GetByID(extension_id); |
| + options_page_ = extensions::ManifestURL::GetOptionsPage(extension); |
| + if (!options_page_.is_valid()) { |
| + callback.Run(NULL); |
| + return; |
| + } |
| + |
| + // Create a Webcontents using the extension URL. |
| + content::SiteInstance* options_site_instance = |
| + content::SiteInstance::CreateForURL(browser_context, extension_url); |
| + WebContents::CreateParams params(browser_context, options_site_instance); |
| + params.guest_delegate = this; |
| + callback.Run(WebContents::Create(params)); |
| +} |
| + |
| +void ExtensionOptionsGuest::DidAttachToEmbedder() { |
| + guest_web_contents()->GetController().LoadURL(options_page_, |
| + content::Referrer(), |
| + content::PAGE_TRANSITION_LINK, |
| + std::string()); |
| +} |
| + |
| +void ExtensionOptionsGuest::DidInitialize() { |
| + extension_function_dispatcher_.reset( |
| + new extensions::ExtensionFunctionDispatcher(browser_context(), this)); |
| + extensions::ChromeExtensionWebContentsObserver::CreateForWebContents( |
| + guest_web_contents()); |
| +} |
| + |
| +content::WebContents* ExtensionOptionsGuest::GetAssociatedWebContents() const { |
| + return guest_web_contents(); |
| +} |
| + |
| +bool ExtensionOptionsGuest::OnMessageReceived(const IPC::Message& message) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(ExtensionOptionsGuest, message) |
| + IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) |
|
Devlin
2014/07/17 20:05:09
nit: git cl format will undo this, but I think tha
ericzeng
2014/07/17 23:14:47
Yeah, it looks like I undid the indentation with g
|
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +void ExtensionOptionsGuest::OnRequest( |
| + const ExtensionHostMsg_Request_Params& params) { |
| + extension_function_dispatcher_->Dispatch( |
| + params, guest_web_contents()->GetRenderViewHost()); |
| +} |