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/extension_service.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/browser/extension_system.h" |
| 17 #include "extensions/common/extension.h" |
| 18 #include "extensions/common/extension_messages.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 ExtensionOptionsGuest::~ExtensionOptionsGuest() {} |
| 33 |
| 34 bool ExtensionOptionsGuest::CanEmbedderUseGuestView( |
| 35 const std::string& embedder_extension_id) { |
| 36 Profile* profile = Profile::FromBrowserContext(browser_context()); |
| 37 ExtensionService* service = |
| 38 extensions::ExtensionSystem::Get(profile)->extension_service(); |
| 39 const extensions::Extension* embedder_extension = |
| 40 service->GetExtensionById(embedder_extension_id, false); |
| 41 const extensions::PermissionsData* permissions_data = |
| 42 embedder_extension->permissions_data(); |
| 43 return permissions_data->HasAPIPermission( |
| 44 extensions::APIPermission::kEmbeddedExtensionOptions); |
| 45 } |
| 46 |
| 47 void ExtensionOptionsGuest::CreateWebContents( |
| 48 const std::string& embedder_extension_id, |
| 49 int embedder_render_process_id, |
| 50 const base::DictionaryValue& create_params, |
| 51 const WebContentsCreatedCallback& callback) { |
| 52 content::RenderProcessHost* embedder_render_process_host = |
| 53 content::RenderProcessHost::FromID(embedder_render_process_id); |
| 54 content::BrowserContext* browser_context = |
| 55 embedder_render_process_host->GetBrowserContext(); |
| 56 |
| 57 // Get the extension's base URL |
| 58 std::string extension_id; |
| 59 create_params.GetString(extensionoptions::kExtensionId, &extension_id); |
| 60 if (extension_id.empty()) { |
| 61 callback.Run(NULL); |
| 62 return; |
| 63 } |
| 64 GURL extension_url = |
| 65 extensions::Extension::GetBaseURLFromExtensionId(extension_id); |
| 66 if (!extension_url.is_valid()) { |
| 67 callback.Run(NULL); |
| 68 return; |
| 69 } |
| 70 |
| 71 // Get the options page URL for later use |
| 72 extensions::ExtensionRegistry* registry = |
| 73 extensions::ExtensionRegistry::Get(browser_context); |
| 74 const extensions::Extension* extension = |
| 75 registry->GetExtensionById(extension_id, |
| 76 extensions::ExtensionRegistry::ENABLED); |
| 77 options_page_ = extensions::ManifestURL::GetOptionsPage(extension); |
| 78 if (!options_page_.is_valid()) { |
| 79 callback.Run(NULL); |
| 80 return; |
| 81 } |
| 82 |
| 83 // Create a Webcontents using the extension URL |
| 84 content::SiteInstance* options_site_instance = |
| 85 content::SiteInstance::CreateForURL(browser_context, extension_url); |
| 86 WebContents::CreateParams params(browser_context, |
| 87 options_site_instance); |
| 88 params.guest_delegate = this; |
| 89 callback.Run(WebContents::Create(params)); |
| 90 } |
| 91 |
| 92 void ExtensionOptionsGuest::DidAttachToEmbedder() { |
| 93 guest_web_contents()->GetController().LoadURL(options_page_, |
| 94 content::Referrer(), |
| 95 content::PAGE_TRANSITION_LINK, |
| 96 std::string()); |
| 97 } |
| 98 |
| 99 void ExtensionOptionsGuest::DidInitialize() { |
| 100 extension_function_dispatcher_.reset( |
| 101 new extensions::ExtensionFunctionDispatcher(browser_context(), this)); |
| 102 } |
| 103 |
| 104 content::WebContents* ExtensionOptionsGuest::GetAssociatedWebContents() const { |
| 105 return guest_web_contents(); |
| 106 } |
| 107 |
| 108 bool ExtensionOptionsGuest::OnMessageReceived(const IPC::Message& message) { |
| 109 bool handled = true; |
| 110 IPC_BEGIN_MESSAGE_MAP(ExtensionOptionsGuest, message) |
| 111 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) |
| 112 IPC_MESSAGE_UNHANDLED(handled = false) |
| 113 IPC_END_MESSAGE_MAP() |
| 114 return handled; |
| 115 } |
| 116 |
| 117 void ExtensionOptionsGuest::OnRequest( |
| 118 const ExtensionHostMsg_Request_Params¶ms) { |
| 119 extension_function_dispatcher_->Dispatch( |
| 120 params, guest_web_contents()->GetRenderViewHost()); |
| 121 } |
OLD | NEW |