OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/guest_view/extension_options/extension_options_guest.h" |
| 6 |
| 7 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" |
| 8 #include "chrome/browser/guest_view/extension_options/extension_options_constant
s.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/common/extensions/manifest_url_handler.h" |
| 11 #include "content/public/browser/render_process_host.h" |
| 12 #include "content/public/browser/site_instance.h" |
| 13 #include "content/public/browser/web_contents.h" |
| 14 #include "extensions/browser/extension_function_dispatcher.h" |
| 15 #include "extensions/browser/extension_registry.h" |
| 16 #include "extensions/common/extension.h" |
| 17 #include "extensions/common/extension_messages.h" |
| 18 #include "extensions/common/feature_switch.h" |
| 19 #include "extensions/common/permissions/permissions_data.h" |
| 20 #include "ipc/ipc_message_macros.h" |
| 21 |
| 22 using content::WebContents; |
| 23 |
| 24 // static |
| 25 const char ExtensionOptionsGuest::Type[] = "extensionoptions"; |
| 26 |
| 27 ExtensionOptionsGuest::ExtensionOptionsGuest( |
| 28 content::BrowserContext* browser_context, |
| 29 int guest_instance_id) |
| 30 : GuestView<ExtensionOptionsGuest>(browser_context, guest_instance_id) { |
| 31 } |
| 32 |
| 33 ExtensionOptionsGuest::~ExtensionOptionsGuest() { |
| 34 } |
| 35 |
| 36 bool ExtensionOptionsGuest::CanEmbedderUseGuestView( |
| 37 const std::string& embedder_extension_id) { |
| 38 const extensions::Extension* embedder_extension = |
| 39 extensions::ExtensionRegistry::Get(browser_context()) |
| 40 ->enabled_extensions() |
| 41 .GetByID(embedder_extension_id); |
| 42 if (!embedder_extension) |
| 43 return false; |
| 44 return embedder_extension->permissions_data()->HasAPIPermission( |
| 45 extensions::APIPermission::kEmbeddedExtensionOptions); |
| 46 } |
| 47 |
| 48 // static |
| 49 GuestViewBase* ExtensionOptionsGuest::Create( |
| 50 content::BrowserContext* browser_context, |
| 51 int guest_instance_id) { |
| 52 if (!extensions::FeatureSwitch::embedded_extension_options()->IsEnabled()) { |
| 53 return NULL; |
| 54 } |
| 55 return new ExtensionOptionsGuest(browser_context, guest_instance_id); |
| 56 } |
| 57 |
| 58 void ExtensionOptionsGuest::CreateWebContents( |
| 59 const std::string& embedder_extension_id, |
| 60 int embedder_render_process_id, |
| 61 const base::DictionaryValue& create_params, |
| 62 const WebContentsCreatedCallback& callback) { |
| 63 // Get the extension's base URL. |
| 64 std::string extension_id; |
| 65 create_params.GetString(extensionoptions::kExtensionId, &extension_id); |
| 66 if (extension_id.empty()) { |
| 67 callback.Run(NULL); |
| 68 return; |
| 69 } |
| 70 DCHECK(extensions::Extension::IdIsValid(extension_id)); |
| 71 |
| 72 GURL extension_url = |
| 73 extensions::Extension::GetBaseURLFromExtensionId(extension_id); |
| 74 if (!extension_url.is_valid()) { |
| 75 callback.Run(NULL); |
| 76 return; |
| 77 } |
| 78 |
| 79 // Get the options page URL for later use. |
| 80 extensions::ExtensionRegistry* registry = |
| 81 extensions::ExtensionRegistry::Get(browser_context()); |
| 82 const extensions::Extension* extension = |
| 83 registry->enabled_extensions().GetByID(extension_id); |
| 84 options_page_ = extensions::ManifestURL::GetOptionsPage(extension); |
| 85 if (!options_page_.is_valid()) { |
| 86 callback.Run(NULL); |
| 87 return; |
| 88 } |
| 89 |
| 90 // Create a WebContents using the extension URL. The options page's |
| 91 // WebContents should live in the same process as its parent extension's |
| 92 // WebContents, so we can use |extension_url| for creating the SiteInstance. |
| 93 content::SiteInstance* options_site_instance = |
| 94 content::SiteInstance::CreateForURL(browser_context(), extension_url); |
| 95 WebContents::CreateParams params(browser_context(), options_site_instance); |
| 96 params.guest_delegate = this; |
| 97 callback.Run(WebContents::Create(params)); |
| 98 } |
| 99 |
| 100 void ExtensionOptionsGuest::DidAttachToEmbedder() { |
| 101 guest_web_contents()->GetController().LoadURL(options_page_, |
| 102 content::Referrer(), |
| 103 content::PAGE_TRANSITION_LINK, |
| 104 std::string()); |
| 105 } |
| 106 |
| 107 void ExtensionOptionsGuest::DidInitialize() { |
| 108 extension_function_dispatcher_.reset( |
| 109 new extensions::ExtensionFunctionDispatcher(browser_context(), this)); |
| 110 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents( |
| 111 guest_web_contents()); |
| 112 } |
| 113 |
| 114 content::WebContents* ExtensionOptionsGuest::GetAssociatedWebContents() const { |
| 115 return guest_web_contents(); |
| 116 } |
| 117 |
| 118 bool ExtensionOptionsGuest::OnMessageReceived(const IPC::Message& message) { |
| 119 bool handled = true; |
| 120 IPC_BEGIN_MESSAGE_MAP(ExtensionOptionsGuest, message) |
| 121 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) |
| 122 IPC_MESSAGE_UNHANDLED(handled = false) |
| 123 IPC_END_MESSAGE_MAP() |
| 124 return handled; |
| 125 } |
| 126 |
| 127 void ExtensionOptionsGuest::OnRequest( |
| 128 const ExtensionHostMsg_Request_Params& params) { |
| 129 extension_function_dispatcher_->Dispatch( |
| 130 params, guest_web_contents()->GetRenderViewHost()); |
| 131 } |
OLD | NEW |