Chromium Code Reviews| 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/permissions/permissions_data.h" | |
| 19 #include "ipc/ipc_message_macros.h" | |
| 20 | |
| 21 using content::WebContents; | |
| 22 | |
| 23 // static | |
| 24 const char ExtensionOptionsGuest::Type[] = "extensionoptions"; | |
| 25 | |
| 26 ExtensionOptionsGuest::ExtensionOptionsGuest( | |
| 27 content::BrowserContext* browser_context, | |
| 28 int guest_instance_id) | |
| 29 : GuestView<ExtensionOptionsGuest>(browser_context, guest_instance_id) { | |
| 30 } | |
| 31 | |
| 32 ExtensionOptionsGuest::~ExtensionOptionsGuest() { | |
| 33 } | |
| 34 | |
| 35 bool ExtensionOptionsGuest::CanEmbedderUseGuestView( | |
| 36 const std::string& embedder_extension_id) { | |
| 37 const extensions::Extension* embedder_extension = | |
| 38 extensions::ExtensionRegistry::Get(browser_context()) | |
| 39 ->enabled_extensions() | |
| 40 .GetByID(embedder_extension_id); | |
| 41 return embedder_extension->permissions_data()->HasAPIPermission( | |
|
lazyboy
2014/07/18 19:55:40
if (embedder_extension) { .... }
ericzeng
2014/07/18 20:23:08
Done.
| |
| 42 extensions::APIPermission::kEmbeddedExtensionOptions); | |
| 43 } | |
| 44 | |
| 45 void ExtensionOptionsGuest::CreateWebContents( | |
| 46 const std::string& embedder_extension_id, | |
| 47 int embedder_render_process_id, | |
| 48 const base::DictionaryValue& create_params, | |
| 49 const WebContentsCreatedCallback& callback) { | |
| 50 content::BrowserContext* browser_context = | |
| 51 content::RenderProcessHost::FromID(embedder_render_process_id) | |
| 52 ->GetBrowserContext(); | |
| 53 | |
| 54 // Get the extension's base URL. | |
| 55 std::string extension_id; | |
| 56 create_params.GetString(extensionoptions::kExtensionId, &extension_id); | |
| 57 if (extension_id.empty()) { | |
| 58 callback.Run(NULL); | |
| 59 return; | |
| 60 } | |
| 61 DCHECK(extensions::Extension::IdIsValid(extension_id)); | |
| 62 | |
| 63 GURL extension_url = | |
| 64 extensions::Extension::GetBaseURLFromExtensionId(extension_id); | |
| 65 if (!extension_url.is_valid()) { | |
| 66 callback.Run(NULL); | |
| 67 return; | |
| 68 } | |
| 69 | |
| 70 // Get the options page URL for later use. | |
| 71 extensions::ExtensionRegistry* registry = | |
| 72 extensions::ExtensionRegistry::Get(browser_context); | |
| 73 const extensions::Extension* extension = | |
| 74 registry->enabled_extensions().GetByID(extension_id); | |
| 75 options_page_ = extensions::ManifestURL::GetOptionsPage(extension); | |
| 76 if (!options_page_.is_valid()) { | |
| 77 callback.Run(NULL); | |
| 78 return; | |
| 79 } | |
| 80 | |
| 81 // Create a Webcontents using the extension URL. | |
| 82 content::SiteInstance* options_site_instance = | |
| 83 content::SiteInstance::CreateForURL(browser_context, extension_url); | |
| 84 WebContents::CreateParams params(browser_context, options_site_instance); | |
| 85 params.guest_delegate = this; | |
| 86 callback.Run(WebContents::Create(params)); | |
| 87 } | |
| 88 | |
| 89 void ExtensionOptionsGuest::DidAttachToEmbedder() { | |
| 90 guest_web_contents()->GetController().LoadURL(options_page_, | |
| 91 content::Referrer(), | |
| 92 content::PAGE_TRANSITION_LINK, | |
| 93 std::string()); | |
| 94 } | |
| 95 | |
| 96 void ExtensionOptionsGuest::DidInitialize() { | |
| 97 extension_function_dispatcher_.reset( | |
| 98 new extensions::ExtensionFunctionDispatcher(browser_context(), this)); | |
| 99 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents( | |
| 100 guest_web_contents()); | |
| 101 } | |
| 102 | |
| 103 content::WebContents* ExtensionOptionsGuest::GetAssociatedWebContents() const { | |
| 104 return guest_web_contents(); | |
| 105 } | |
| 106 | |
| 107 bool ExtensionOptionsGuest::OnMessageReceived(const IPC::Message& message) { | |
| 108 bool handled = true; | |
| 109 IPC_BEGIN_MESSAGE_MAP(ExtensionOptionsGuest, message) | |
| 110 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) | |
| 111 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 112 IPC_END_MESSAGE_MAP() | |
| 113 return handled; | |
| 114 } | |
| 115 | |
| 116 void ExtensionOptionsGuest::OnRequest( | |
| 117 const ExtensionHostMsg_Request_Params& params) { | |
| 118 extension_function_dispatcher_->Dispatch( | |
| 119 params, guest_web_contents()->GetRenderViewHost()); | |
| 120 } | |
| OLD | NEW |