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/extensions/extension_service.h" | |
Devlin
2014/07/16 20:55:06
Won't need this or extension_system anymore.
ericzeng
2014/07/16 22:27:38
Done.
| |
9 #include "chrome/browser/guest_view/extension_options/extension_options_constant s.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "chrome/common/extensions/manifest_url_handler.h" | |
12 #include "content/public/browser/render_process_host.h" | |
13 #include "content/public/browser/site_instance.h" | |
14 #include "content/public/browser/web_contents.h" | |
15 #include "extensions/browser/extension_function_dispatcher.h" | |
16 #include "extensions/browser/extension_registry.h" | |
17 #include "extensions/browser/extension_system.h" | |
18 #include "extensions/common/extension.h" | |
19 #include "extensions/common/extension_messages.h" | |
20 #include "extensions/common/permissions/permissions_data.h" | |
21 #include "ipc/ipc_message_macros.h" | |
22 | |
23 using content::WebContents; | |
24 | |
25 // static | |
26 const char ExtensionOptionsGuest::Type[] = "extensionoptions"; | |
27 | |
28 ExtensionOptionsGuest::ExtensionOptionsGuest( | |
29 content::BrowserContext* browser_context, | |
30 int guest_instance_id) | |
31 : GuestView<ExtensionOptionsGuest>(browser_context, guest_instance_id) {} | |
32 | |
33 ExtensionOptionsGuest::~ExtensionOptionsGuest() {} | |
34 | |
35 bool ExtensionOptionsGuest::CanEmbedderUseGuestView( | |
36 const std::string& embedder_extension_id) { | |
37 Profile* profile = Profile::FromBrowserContext(browser_context()); | |
38 ExtensionService* service = | |
39 extensions::ExtensionSystem::Get(profile)->extension_service(); | |
40 const extensions::Extension* embedder_extension = | |
41 service->GetExtensionById(embedder_extension_id, false); | |
Devlin
2014/07/16 20:55:06
instead, use ExtensionRegistry::enabled_extensions
ericzeng
2014/07/16 22:27:37
Done.
| |
42 const extensions::PermissionsData* permissions_data = | |
43 embedder_extension->permissions_data(); | |
44 return permissions_data->HasAPIPermission( | |
45 extensions::APIPermission::kEmbeddedExtensionOptions); | |
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::RenderProcessHost* embedder_render_process_host = | |
54 content::RenderProcessHost::FromID(embedder_render_process_id); | |
55 content::BrowserContext* browser_context = | |
56 embedder_render_process_host->GetBrowserContext(); | |
57 | |
58 // Get the extension's base URL | |
Devlin
2014/07/16 20:55:06
.
ericzeng
2014/07/16 22:27:37
Done.
| |
59 std::string extension_id; | |
60 create_params.GetString(extensionoptions::kExtensionId, &extension_id); | |
61 if (extension_id.empty()) { | |
62 callback.Run(NULL); | |
63 return; | |
64 } | |
65 GURL extension_url = | |
66 extensions::Extension::GetBaseURLFromExtensionId(extension_id); | |
67 if (!extension_url.is_valid()) { | |
68 callback.Run(NULL); | |
69 return; | |
70 } | |
71 | |
72 // Get the options page URL for later use | |
Devlin
2014/07/16 20:55:06
.
ericzeng
2014/07/16 22:27:38
Done.
| |
73 extensions::ExtensionRegistry* registry = | |
74 extensions::ExtensionRegistry::Get(browser_context); | |
75 const extensions::Extension* extension = | |
76 registry->GetExtensionById(extension_id, | |
Devlin
2014/07/16 20:55:06
nit: ever-so-slightly-faster: registry->enabled_ex
ericzeng
2014/07/16 22:27:38
Done.
| |
77 extensions::ExtensionRegistry::ENABLED); | |
78 options_page_ = extensions::ManifestURL::GetOptionsPage(extension); | |
79 if (!options_page_.is_valid()) { | |
80 callback.Run(NULL); | |
81 return; | |
82 } | |
83 | |
84 // Create a Webcontents using the extension URL | |
Devlin
2014/07/16 20:55:06
.
ericzeng
2014/07/16 22:27:38
Done.
| |
85 content::SiteInstance* options_site_instance = | |
86 content::SiteInstance::CreateForURL(browser_context, extension_url); | |
87 WebContents::CreateParams params(browser_context, | |
88 options_site_instance); | |
89 params.guest_delegate = this; | |
90 callback.Run(WebContents::Create(params)); | |
91 } | |
92 | |
93 void ExtensionOptionsGuest::DidAttachToEmbedder() { | |
94 guest_web_contents()->GetController().LoadURL(options_page_, | |
95 content::Referrer(), | |
96 content::PAGE_TRANSITION_LINK, | |
97 std::string()); | |
98 } | |
99 | |
100 void ExtensionOptionsGuest::DidInitialize() { | |
101 extension_function_dispatcher_.reset( | |
102 new extensions::ExtensionFunctionDispatcher(browser_context(), this)); | |
103 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents( | |
104 guest_web_contents()); | |
105 } | |
106 | |
107 content::WebContents* ExtensionOptionsGuest::GetAssociatedWebContents() const { | |
108 return guest_web_contents(); | |
109 } | |
110 | |
111 bool ExtensionOptionsGuest::OnMessageReceived(const IPC::Message& message) { | |
112 bool handled = true; | |
113 IPC_BEGIN_MESSAGE_MAP(ExtensionOptionsGuest, message) | |
114 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) | |
115 IPC_MESSAGE_UNHANDLED(handled = false) | |
116 IPC_END_MESSAGE_MAP() | |
117 return handled; | |
118 } | |
119 | |
120 void ExtensionOptionsGuest::OnRequest( | |
121 const ExtensionHostMsg_Request_Params¶ms) { | |
122 extension_function_dispatcher_->Dispatch( | |
123 params, guest_web_contents()->GetRenderViewHost()); | |
124 } | |
OLD | NEW |