OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/guest_view/extension_options/extension_options_guest.h" | 5 #include "extensions/browser/guest_view/extension_options/extension_options_gues t.h" |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" | |
9 #include "chrome/browser/extensions/extension_tab_util.h" | |
10 #include "chrome/browser/guest_view/extension_options/extension_options_constant s.h" | |
11 #include "chrome/browser/profiles/profile.h" | |
12 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | |
13 #include "chrome/browser/ui/browser.h" | |
14 #include "chrome/browser/ui/browser_finder.h" | |
15 #include "chrome/browser/ui/browser_window.h" | |
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
17 #include "chrome/common/extensions/api/extension_options_internal.h" | |
18 #include "components/crx_file/id_util.h" | 8 #include "components/crx_file/id_util.h" |
19 #include "components/renderer_context_menu/context_menu_delegate.h" | |
20 #include "content/public/browser/render_process_host.h" | 9 #include "content/public/browser/render_process_host.h" |
21 #include "content/public/browser/site_instance.h" | 10 #include "content/public/browser/site_instance.h" |
22 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
12 #include "extensions/browser/api/extensions_api_client.h" | |
23 #include "extensions/browser/extension_function_dispatcher.h" | 13 #include "extensions/browser/extension_function_dispatcher.h" |
24 #include "extensions/browser/extension_registry.h" | 14 #include "extensions/browser/extension_registry.h" |
15 #include "extensions/browser/extension_web_contents_observer.h" | |
16 #include "extensions/browser/guest_view/extension_options/extension_options_cons tants.h" | |
17 #include "extensions/browser/guest_view/extension_options/extension_options_gues t_delegate.h" | |
25 #include "extensions/browser/guest_view/guest_view_manager.h" | 18 #include "extensions/browser/guest_view/guest_view_manager.h" |
19 #include "extensions/common/api/extension_options_internal.h" | |
26 #include "extensions/common/constants.h" | 20 #include "extensions/common/constants.h" |
27 #include "extensions/common/extension.h" | 21 #include "extensions/common/extension.h" |
28 #include "extensions/common/extension_messages.h" | 22 #include "extensions/common/extension_messages.h" |
29 #include "extensions/common/feature_switch.h" | 23 #include "extensions/common/feature_switch.h" |
30 #include "extensions/common/manifest_handlers/options_page_info.h" | 24 #include "extensions/common/manifest_handlers/options_page_info.h" |
31 #include "extensions/common/permissions/permissions_data.h" | 25 #include "extensions/common/permissions/permissions_data.h" |
32 #include "extensions/strings/grit/extensions_strings.h" | 26 #include "extensions/strings/grit/extensions_strings.h" |
33 #include "ipc/ipc_message_macros.h" | 27 #include "ipc/ipc_message_macros.h" |
34 | 28 |
35 using content::WebContents; | 29 using content::WebContents; |
36 using namespace extensions::api; | 30 using namespace extensions::core_api; |
37 | 31 |
38 // static | 32 // static |
39 const char ExtensionOptionsGuest::Type[] = "extensionoptions"; | 33 const char ExtensionOptionsGuest::Type[] = "extensionoptions"; |
40 | 34 |
41 ExtensionOptionsGuest::ExtensionOptionsGuest( | 35 ExtensionOptionsGuest::ExtensionOptionsGuest( |
42 content::BrowserContext* browser_context, | 36 content::BrowserContext* browser_context, |
43 int guest_instance_id) | 37 int guest_instance_id) |
44 : GuestView<ExtensionOptionsGuest>(browser_context, guest_instance_id) { | 38 : GuestView<ExtensionOptionsGuest>(browser_context, guest_instance_id), |
39 extension_options_guest_delegate_( | |
40 extensions::ExtensionsAPIClient::Get() | |
41 ->CreateExtensionOptionsGuestDelegate()) { | |
45 } | 42 } |
46 | 43 |
47 ExtensionOptionsGuest::~ExtensionOptionsGuest() { | 44 ExtensionOptionsGuest::~ExtensionOptionsGuest() { |
48 } | 45 } |
49 | 46 |
50 // static | 47 // static |
51 extensions::GuestViewBase* ExtensionOptionsGuest::Create( | 48 extensions::GuestViewBase* ExtensionOptionsGuest::Create( |
52 content::BrowserContext* browser_context, | 49 content::BrowserContext* browser_context, |
53 int guest_instance_id) { | 50 int guest_instance_id) { |
54 if (!extensions::FeatureSwitch::embedded_extension_options()->IsEnabled()) { | 51 if (!extensions::FeatureSwitch::embedded_extension_options()->IsEnabled()) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
110 SetUpAutoSize(); | 107 SetUpAutoSize(); |
111 web_contents()->GetController().LoadURL(options_page_, | 108 web_contents()->GetController().LoadURL(options_page_, |
112 content::Referrer(), | 109 content::Referrer(), |
113 content::PAGE_TRANSITION_LINK, | 110 content::PAGE_TRANSITION_LINK, |
114 std::string()); | 111 std::string()); |
115 } | 112 } |
116 | 113 |
117 void ExtensionOptionsGuest::DidInitialize() { | 114 void ExtensionOptionsGuest::DidInitialize() { |
118 extension_function_dispatcher_.reset( | 115 extension_function_dispatcher_.reset( |
119 new extensions::ExtensionFunctionDispatcher(browser_context(), this)); | 116 new extensions::ExtensionFunctionDispatcher(browser_context(), this)); |
120 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents( | 117 if (extension_options_guest_delegate_) |
Fady Samuel
2014/09/09 02:15:39
Put the statement below in a block.
ericzeng
2014/09/09 20:31:32
Done.
| |
121 web_contents()); | 118 extension_options_guest_delegate_->CreateChromeExtensionWebContentsObserver( |
119 web_contents()); | |
122 } | 120 } |
123 | 121 |
124 void ExtensionOptionsGuest::DidStopLoading() { | 122 void ExtensionOptionsGuest::DidStopLoading() { |
125 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 123 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); |
126 DispatchEventToEmbedder(new extensions::GuestViewBase::Event( | 124 DispatchEventToEmbedder(new extensions::GuestViewBase::Event( |
127 extensions::api::extension_options_internal::OnLoad::kEventName, | 125 extension_options_internal::OnLoad::kEventName, args.Pass())); |
128 args.Pass())); | |
129 } | 126 } |
130 | 127 |
131 const char* ExtensionOptionsGuest::GetAPINamespace() const { | 128 const char* ExtensionOptionsGuest::GetAPINamespace() const { |
132 return extensionoptions::kAPINamespace; | 129 return extensionoptions::kAPINamespace; |
133 } | 130 } |
134 | 131 |
135 int ExtensionOptionsGuest::GetTaskPrefix() const { | 132 int ExtensionOptionsGuest::GetTaskPrefix() const { |
136 return IDS_EXTENSION_TASK_MANAGER_EXTENSIONOPTIONS_TAG_PREFIX; | 133 return IDS_EXTENSION_TASK_MANAGER_EXTENSIONOPTIONS_TAG_PREFIX; |
137 } | 134 } |
138 | 135 |
(...skipping 13 matching lines...) Expand all Loading... | |
152 return true; | 149 return true; |
153 } | 150 } |
154 | 151 |
155 content::WebContents* ExtensionOptionsGuest::GetAssociatedWebContents() const { | 152 content::WebContents* ExtensionOptionsGuest::GetAssociatedWebContents() const { |
156 return web_contents(); | 153 return web_contents(); |
157 } | 154 } |
158 | 155 |
159 content::WebContents* ExtensionOptionsGuest::OpenURLFromTab( | 156 content::WebContents* ExtensionOptionsGuest::OpenURLFromTab( |
160 content::WebContents* source, | 157 content::WebContents* source, |
161 const content::OpenURLParams& params) { | 158 const content::OpenURLParams& params) { |
162 Browser* browser = | 159 if (!extension_options_guest_delegate_) |
163 chrome::FindBrowserWithWebContents(embedder_web_contents()); | 160 return NULL; |
164 | 161 |
165 // Don't allow external URLs with the CURRENT_TAB disposition be opened in | 162 // Don't allow external URLs with the CURRENT_TAB disposition be opened in |
166 // this guest view, change the disposition to NEW_FOREGROUND_TAB. | 163 // this guest view, change the disposition to NEW_FOREGROUND_TAB. |
167 if ((!params.url.SchemeIs(extensions::kExtensionScheme) || | 164 if ((!params.url.SchemeIs(extensions::kExtensionScheme) || |
168 params.url.host() != options_page_.host()) && | 165 params.url.host() != options_page_.host()) && |
169 params.disposition == CURRENT_TAB) { | 166 params.disposition == CURRENT_TAB) { |
170 return browser->OpenURL( | 167 return extension_options_guest_delegate_->OpenURLInNewTab( |
168 embedder_web_contents(), | |
171 content::OpenURLParams(params.url, | 169 content::OpenURLParams(params.url, |
172 params.referrer, | 170 params.referrer, |
173 params.frame_tree_node_id, | 171 params.frame_tree_node_id, |
174 NEW_FOREGROUND_TAB, | 172 NEW_FOREGROUND_TAB, |
175 params.transition, | 173 params.transition, |
176 params.is_renderer_initiated)); | 174 params.is_renderer_initiated)); |
177 } | 175 } |
178 return browser->OpenURL(params); | 176 return extension_options_guest_delegate_->OpenURLInNewTab( |
177 embedder_web_contents(), params); | |
179 } | 178 } |
180 | 179 |
181 void ExtensionOptionsGuest::CloseContents(content::WebContents* source) { | 180 void ExtensionOptionsGuest::CloseContents(content::WebContents* source) { |
182 DispatchEventToEmbedder(new extensions::GuestViewBase::Event( | 181 DispatchEventToEmbedder(new extensions::GuestViewBase::Event( |
183 extension_options_internal::OnClose::kEventName, | 182 extension_options_internal::OnClose::kEventName, |
184 make_scoped_ptr(new base::DictionaryValue()))); | 183 make_scoped_ptr(new base::DictionaryValue()))); |
185 } | 184 } |
186 | 185 |
187 bool ExtensionOptionsGuest::HandleContextMenu( | 186 bool ExtensionOptionsGuest::HandleContextMenu( |
188 const content::ContextMenuParams& params) { | 187 const content::ContextMenuParams& params) { |
189 ContextMenuDelegate* menu_delegate = | 188 return extension_options_guest_delegate_ |
Fady Samuel
2014/09/09 02:15:39
Could you please use an if statement instead with
ericzeng
2014/09/09 20:31:32
Done.
| |
190 ContextMenuDelegate::FromWebContents(web_contents()); | 189 ? extension_options_guest_delegate_->HandleContextMenu( |
191 DCHECK(menu_delegate); | 190 web_contents(), params) |
192 | 191 : false; |
193 scoped_ptr<RenderViewContextMenu> menu = | |
194 menu_delegate->BuildMenu(web_contents(), params); | |
195 menu_delegate->ShowMenu(menu.Pass()); | |
196 return true; | |
197 } | 192 } |
198 | 193 |
199 bool ExtensionOptionsGuest::ShouldCreateWebContents( | 194 bool ExtensionOptionsGuest::ShouldCreateWebContents( |
200 content::WebContents* web_contents, | 195 content::WebContents* web_contents, |
201 int route_id, | 196 int route_id, |
202 WindowContainerType window_container_type, | 197 WindowContainerType window_container_type, |
203 const base::string16& frame_name, | 198 const base::string16& frame_name, |
204 const GURL& target_url, | 199 const GURL& target_url, |
205 const std::string& partition_id, | 200 const std::string& partition_id, |
206 content::SessionStorageNamespace* session_storage_namespace) { | 201 content::SessionStorageNamespace* session_storage_namespace) { |
207 // This method handles opening links from within the guest. Since this guest | 202 // This method handles opening links from within the guest. Since this guest |
208 // view is used for displaying embedded extension options, we want any | 203 // view is used for displaying embedded extension options, we want any |
209 // external links to be opened in a new tab, not in a new guest view. | 204 // external links to be opened in a new tab, not in a new guest view. |
210 // Therefore we just open the URL in a new tab, and since we aren't handling | 205 // Therefore we just open the URL in a new tab, and since we aren't handling |
211 // the new web contents, we return false. | 206 // the new web contents, we return false. |
212 Browser* browser = | |
213 chrome::FindBrowserWithWebContents(embedder_web_contents()); | |
214 content::OpenURLParams params(target_url, | |
215 content::Referrer(), | |
216 NEW_FOREGROUND_TAB, | |
217 content::PAGE_TRANSITION_LINK, | |
218 false); | |
219 browser->OpenURL(params); | |
220 // TODO(ericzeng): Open the tab in the background if the click was a | 207 // TODO(ericzeng): Open the tab in the background if the click was a |
221 // ctrl-click or middle mouse button click | 208 // ctrl-click or middle mouse button click |
209 if (extension_options_guest_delegate_) { | |
Fady Samuel
2014/09/09 02:15:39
Early exit preferred for readability:
if (!extens
ericzeng
2014/09/09 20:31:32
False is returned whether or not the extension_opt
| |
210 extension_options_guest_delegate_->OpenURLInNewTab( | |
211 embedder_web_contents(), | |
212 content::OpenURLParams(target_url, | |
213 content::Referrer(), | |
214 NEW_FOREGROUND_TAB, | |
215 content::PAGE_TRANSITION_LINK, | |
216 false)); | |
217 } | |
222 return false; | 218 return false; |
223 } | 219 } |
224 | 220 |
225 bool ExtensionOptionsGuest::OnMessageReceived(const IPC::Message& message) { | 221 bool ExtensionOptionsGuest::OnMessageReceived(const IPC::Message& message) { |
226 bool handled = true; | 222 bool handled = true; |
227 IPC_BEGIN_MESSAGE_MAP(ExtensionOptionsGuest, message) | 223 IPC_BEGIN_MESSAGE_MAP(ExtensionOptionsGuest, message) |
228 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) | 224 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) |
229 IPC_MESSAGE_UNHANDLED(handled = false) | 225 IPC_MESSAGE_UNHANDLED(handled = false) |
230 IPC_END_MESSAGE_MAP() | 226 IPC_END_MESSAGE_MAP() |
231 return handled; | 227 return handled; |
232 } | 228 } |
233 | 229 |
234 void ExtensionOptionsGuest::OnRequest( | 230 void ExtensionOptionsGuest::OnRequest( |
235 const ExtensionHostMsg_Request_Params& params) { | 231 const ExtensionHostMsg_Request_Params& params) { |
236 extension_function_dispatcher_->Dispatch( | 232 extension_function_dispatcher_->Dispatch(params, |
237 params, web_contents()->GetRenderViewHost()); | 233 web_contents()->GetRenderViewHost()); |
238 } | 234 } |
239 | 235 |
240 void ExtensionOptionsGuest::SetUpAutoSize() { | 236 void ExtensionOptionsGuest::SetUpAutoSize() { |
241 // Read the autosize parameters passed in from the embedder. | 237 // Read the autosize parameters passed in from the embedder. |
242 bool auto_size_enabled = false; | 238 bool auto_size_enabled = false; |
243 attach_params()->GetBoolean(extensionoptions::kAttributeAutoSize, | 239 attach_params()->GetBoolean(extensionoptions::kAttributeAutoSize, |
244 &auto_size_enabled); | 240 &auto_size_enabled); |
245 | 241 |
246 int max_height = 0; | 242 int max_height = 0; |
247 int max_width = 0; | 243 int max_width = 0; |
248 attach_params()->GetInteger(extensionoptions::kAttributeMaxHeight, | 244 attach_params()->GetInteger(extensionoptions::kAttributeMaxHeight, |
249 &max_height); | 245 &max_height); |
250 attach_params()->GetInteger(extensionoptions::kAttributeMaxWidth, &max_width); | 246 attach_params()->GetInteger(extensionoptions::kAttributeMaxWidth, &max_width); |
251 | 247 |
252 int min_height = 0; | 248 int min_height = 0; |
253 int min_width = 0; | 249 int min_width = 0; |
254 attach_params()->GetInteger(extensionoptions::kAttributeMinHeight, | 250 attach_params()->GetInteger(extensionoptions::kAttributeMinHeight, |
255 &min_height); | 251 &min_height); |
256 attach_params()->GetInteger(extensionoptions::kAttributeMinWidth, &min_width); | 252 attach_params()->GetInteger(extensionoptions::kAttributeMinWidth, &min_width); |
257 | 253 |
258 // Call SetAutoSize to apply all the appropriate validation and clipping of | 254 // Call SetAutoSize to apply all the appropriate validation and clipping of |
259 // values. | 255 // values. |
260 SetAutoSize(auto_size_enabled, | 256 SetAutoSize(auto_size_enabled, |
261 gfx::Size(min_width, min_height), | 257 gfx::Size(min_width, min_height), |
262 gfx::Size(max_width, max_height)); | 258 gfx::Size(max_width, max_height)); |
263 } | 259 } |
OLD | NEW |