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 if (embedder_extension) { | |
| 42 return embedder_extension->permissions_data()->HasAPIPermission( | |
| 43 extensions::APIPermission::kEmbeddedExtensionOptions); | |
| 44 } | |
| 45 return false; | |
| 46 } | |
| 47 | |
| 48 void ExtensionOptionsGuest::CreateWebContents( | |
| 49 const std::string& embedder_extension_id, | |
| 50 int embedder_render_process_id, | |
| 51 const base::DictionaryValue& create_params, | |
| 52 const WebContentsCreatedCallback& callback) { | |
| 53 content::BrowserContext* browser_context = | |
| 54 content::RenderProcessHost::FromID(embedder_render_process_id) | |
| 55 ->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 DCHECK(extensions::Extension::IdIsValid(extension_id)); | |
| 65 | |
| 66 GURL extension_url = | |
| 67 extensions::Extension::GetBaseURLFromExtensionId(extension_id); | |
| 68 if (!extension_url.is_valid()) { | |
| 69 callback.Run(NULL); | |
| 70 return; | |
| 71 } | |
| 72 | |
| 73 // Get the options page URL for later use. | |
| 74 extensions::ExtensionRegistry* registry = | |
| 75 extensions::ExtensionRegistry::Get(browser_context); | |
| 76 const extensions::Extension* extension = | |
| 77 registry->enabled_extensions().GetByID(extension_id); | |
| 78 options_page_ = extensions::ManifestURL::GetOptionsPage(extension); | |
| 79 if (!options_page_.is_valid()) { | |
| 80 callback.Run(NULL); | |
|
lazyboy
2014/07/18 20:37:38
For extensions that don't have options page, we sh
ericzeng
2014/07/18 21:43:45
In this patchset, I don't think anything explicitl
| |
| 81 return; | |
| 82 } | |
| 83 | |
| 84 // Create a Webcontents using the extension URL. | |
|
lazyboy
2014/07/18 20:37:38
You also want to add a bit more doc here: "Options
ericzeng
2014/07/18 21:43:45
Done.
| |
| 85 content::SiteInstance* options_site_instance = | |
| 86 content::SiteInstance::CreateForURL(browser_context, extension_url); | |
| 87 WebContents::CreateParams params(browser_context, 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 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents( | |
| 103 guest_web_contents()); | |
| 104 } | |
| 105 | |
| 106 content::WebContents* ExtensionOptionsGuest::GetAssociatedWebContents() const { | |
| 107 return guest_web_contents(); | |
| 108 } | |
| 109 | |
| 110 bool ExtensionOptionsGuest::OnMessageReceived(const IPC::Message& message) { | |
| 111 bool handled = true; | |
| 112 IPC_BEGIN_MESSAGE_MAP(ExtensionOptionsGuest, message) | |
| 113 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) | |
| 114 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 115 IPC_END_MESSAGE_MAP() | |
| 116 return handled; | |
| 117 } | |
| 118 | |
| 119 void ExtensionOptionsGuest::OnRequest( | |
| 120 const ExtensionHostMsg_Request_Params& params) { | |
| 121 extension_function_dispatcher_->Dispatch( | |
| 122 params, guest_web_contents()->GetRenderViewHost()); | |
| 123 } | |
| OLD | NEW |