| 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_) { |
| 121 web_contents()); | 118 extension_options_guest_delegate_->CreateChromeExtensionWebContentsObserver( |
| 119 web_contents()); |
| 120 } |
| 122 } | 121 } |
| 123 | 122 |
| 124 void ExtensionOptionsGuest::DidStopLoading() { | 123 void ExtensionOptionsGuest::DidStopLoading() { |
| 125 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 124 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); |
| 126 DispatchEventToEmbedder(new extensions::GuestViewBase::Event( | 125 DispatchEventToEmbedder(new extensions::GuestViewBase::Event( |
| 127 extensions::api::extension_options_internal::OnLoad::kEventName, | 126 extension_options_internal::OnLoad::kEventName, args.Pass())); |
| 128 args.Pass())); | |
| 129 } | 127 } |
| 130 | 128 |
| 131 const char* ExtensionOptionsGuest::GetAPINamespace() const { | 129 const char* ExtensionOptionsGuest::GetAPINamespace() const { |
| 132 return extensionoptions::kAPINamespace; | 130 return extensionoptions::kAPINamespace; |
| 133 } | 131 } |
| 134 | 132 |
| 135 int ExtensionOptionsGuest::GetTaskPrefix() const { | 133 int ExtensionOptionsGuest::GetTaskPrefix() const { |
| 136 return IDS_EXTENSION_TASK_MANAGER_EXTENSIONOPTIONS_TAG_PREFIX; | 134 return IDS_EXTENSION_TASK_MANAGER_EXTENSIONOPTIONS_TAG_PREFIX; |
| 137 } | 135 } |
| 138 | 136 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 152 return true; | 150 return true; |
| 153 } | 151 } |
| 154 | 152 |
| 155 content::WebContents* ExtensionOptionsGuest::GetAssociatedWebContents() const { | 153 content::WebContents* ExtensionOptionsGuest::GetAssociatedWebContents() const { |
| 156 return web_contents(); | 154 return web_contents(); |
| 157 } | 155 } |
| 158 | 156 |
| 159 content::WebContents* ExtensionOptionsGuest::OpenURLFromTab( | 157 content::WebContents* ExtensionOptionsGuest::OpenURLFromTab( |
| 160 content::WebContents* source, | 158 content::WebContents* source, |
| 161 const content::OpenURLParams& params) { | 159 const content::OpenURLParams& params) { |
| 162 Browser* browser = | 160 if (!extension_options_guest_delegate_) |
| 163 chrome::FindBrowserWithWebContents(embedder_web_contents()); | 161 return NULL; |
| 164 | 162 |
| 165 // Don't allow external URLs with the CURRENT_TAB disposition be opened in | 163 // Don't allow external URLs with the CURRENT_TAB disposition be opened in |
| 166 // this guest view, change the disposition to NEW_FOREGROUND_TAB. | 164 // this guest view, change the disposition to NEW_FOREGROUND_TAB. |
| 167 if ((!params.url.SchemeIs(extensions::kExtensionScheme) || | 165 if ((!params.url.SchemeIs(extensions::kExtensionScheme) || |
| 168 params.url.host() != options_page_.host()) && | 166 params.url.host() != options_page_.host()) && |
| 169 params.disposition == CURRENT_TAB) { | 167 params.disposition == CURRENT_TAB) { |
| 170 return browser->OpenURL( | 168 return extension_options_guest_delegate_->OpenURLInNewTab( |
| 169 embedder_web_contents(), |
| 171 content::OpenURLParams(params.url, | 170 content::OpenURLParams(params.url, |
| 172 params.referrer, | 171 params.referrer, |
| 173 params.frame_tree_node_id, | 172 params.frame_tree_node_id, |
| 174 NEW_FOREGROUND_TAB, | 173 NEW_FOREGROUND_TAB, |
| 175 params.transition, | 174 params.transition, |
| 176 params.is_renderer_initiated)); | 175 params.is_renderer_initiated)); |
| 177 } | 176 } |
| 178 return browser->OpenURL(params); | 177 return extension_options_guest_delegate_->OpenURLInNewTab( |
| 178 embedder_web_contents(), params); |
| 179 } | 179 } |
| 180 | 180 |
| 181 void ExtensionOptionsGuest::CloseContents(content::WebContents* source) { | 181 void ExtensionOptionsGuest::CloseContents(content::WebContents* source) { |
| 182 DispatchEventToEmbedder(new extensions::GuestViewBase::Event( | 182 DispatchEventToEmbedder(new extensions::GuestViewBase::Event( |
| 183 extension_options_internal::OnClose::kEventName, | 183 extension_options_internal::OnClose::kEventName, |
| 184 make_scoped_ptr(new base::DictionaryValue()))); | 184 make_scoped_ptr(new base::DictionaryValue()))); |
| 185 } | 185 } |
| 186 | 186 |
| 187 bool ExtensionOptionsGuest::HandleContextMenu( | 187 bool ExtensionOptionsGuest::HandleContextMenu( |
| 188 const content::ContextMenuParams& params) { | 188 const content::ContextMenuParams& params) { |
| 189 ContextMenuDelegate* menu_delegate = | 189 if (!extension_options_guest_delegate_) |
| 190 ContextMenuDelegate::FromWebContents(web_contents()); | 190 return false; |
| 191 DCHECK(menu_delegate); | |
| 192 | 191 |
| 193 scoped_ptr<RenderViewContextMenu> menu = | 192 return extension_options_guest_delegate_->HandleContextMenu(web_contents(), |
| 194 menu_delegate->BuildMenu(web_contents(), params); | 193 params); |
| 195 menu_delegate->ShowMenu(menu.Pass()); | |
| 196 return true; | |
| 197 } | 194 } |
| 198 | 195 |
| 199 bool ExtensionOptionsGuest::ShouldCreateWebContents( | 196 bool ExtensionOptionsGuest::ShouldCreateWebContents( |
| 200 content::WebContents* web_contents, | 197 content::WebContents* web_contents, |
| 201 int route_id, | 198 int route_id, |
| 202 WindowContainerType window_container_type, | 199 WindowContainerType window_container_type, |
| 203 const base::string16& frame_name, | 200 const base::string16& frame_name, |
| 204 const GURL& target_url, | 201 const GURL& target_url, |
| 205 const std::string& partition_id, | 202 const std::string& partition_id, |
| 206 content::SessionStorageNamespace* session_storage_namespace) { | 203 content::SessionStorageNamespace* session_storage_namespace) { |
| 207 // This method handles opening links from within the guest. Since this guest | 204 // This method handles opening links from within the guest. Since this guest |
| 208 // view is used for displaying embedded extension options, we want any | 205 // 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. | 206 // 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 | 207 // Therefore we just open the URL in a new tab, and since we aren't handling |
| 211 // the new web contents, we return false. | 208 // 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 | 209 // TODO(ericzeng): Open the tab in the background if the click was a |
| 221 // ctrl-click or middle mouse button click | 210 // ctrl-click or middle mouse button click |
| 211 if (extension_options_guest_delegate_) { |
| 212 extension_options_guest_delegate_->OpenURLInNewTab( |
| 213 embedder_web_contents(), |
| 214 content::OpenURLParams(target_url, |
| 215 content::Referrer(), |
| 216 NEW_FOREGROUND_TAB, |
| 217 content::PAGE_TRANSITION_LINK, |
| 218 false)); |
| 219 } |
| 222 return false; | 220 return false; |
| 223 } | 221 } |
| 224 | 222 |
| 225 bool ExtensionOptionsGuest::OnMessageReceived(const IPC::Message& message) { | 223 bool ExtensionOptionsGuest::OnMessageReceived(const IPC::Message& message) { |
| 226 bool handled = true; | 224 bool handled = true; |
| 227 IPC_BEGIN_MESSAGE_MAP(ExtensionOptionsGuest, message) | 225 IPC_BEGIN_MESSAGE_MAP(ExtensionOptionsGuest, message) |
| 228 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) | 226 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) |
| 229 IPC_MESSAGE_UNHANDLED(handled = false) | 227 IPC_MESSAGE_UNHANDLED(handled = false) |
| 230 IPC_END_MESSAGE_MAP() | 228 IPC_END_MESSAGE_MAP() |
| 231 return handled; | 229 return handled; |
| 232 } | 230 } |
| 233 | 231 |
| 234 void ExtensionOptionsGuest::OnRequest( | 232 void ExtensionOptionsGuest::OnRequest( |
| 235 const ExtensionHostMsg_Request_Params& params) { | 233 const ExtensionHostMsg_Request_Params& params) { |
| 236 extension_function_dispatcher_->Dispatch( | 234 extension_function_dispatcher_->Dispatch(params, |
| 237 params, web_contents()->GetRenderViewHost()); | 235 web_contents()->GetRenderViewHost()); |
| 238 } | 236 } |
| 239 | 237 |
| 240 void ExtensionOptionsGuest::SetUpAutoSize() { | 238 void ExtensionOptionsGuest::SetUpAutoSize() { |
| 241 // Read the autosize parameters passed in from the embedder. | 239 // Read the autosize parameters passed in from the embedder. |
| 242 bool auto_size_enabled = false; | 240 bool auto_size_enabled = false; |
| 243 attach_params()->GetBoolean(extensionoptions::kAttributeAutoSize, | 241 attach_params()->GetBoolean(extensionoptions::kAttributeAutoSize, |
| 244 &auto_size_enabled); | 242 &auto_size_enabled); |
| 245 | 243 |
| 246 int max_height = 0; | 244 int max_height = 0; |
| 247 int max_width = 0; | 245 int max_width = 0; |
| 248 attach_params()->GetInteger(extensionoptions::kAttributeMaxHeight, | 246 attach_params()->GetInteger(extensionoptions::kAttributeMaxHeight, |
| 249 &max_height); | 247 &max_height); |
| 250 attach_params()->GetInteger(extensionoptions::kAttributeMaxWidth, &max_width); | 248 attach_params()->GetInteger(extensionoptions::kAttributeMaxWidth, &max_width); |
| 251 | 249 |
| 252 int min_height = 0; | 250 int min_height = 0; |
| 253 int min_width = 0; | 251 int min_width = 0; |
| 254 attach_params()->GetInteger(extensionoptions::kAttributeMinHeight, | 252 attach_params()->GetInteger(extensionoptions::kAttributeMinHeight, |
| 255 &min_height); | 253 &min_height); |
| 256 attach_params()->GetInteger(extensionoptions::kAttributeMinWidth, &min_width); | 254 attach_params()->GetInteger(extensionoptions::kAttributeMinWidth, &min_width); |
| 257 | 255 |
| 258 // Call SetAutoSize to apply all the appropriate validation and clipping of | 256 // Call SetAutoSize to apply all the appropriate validation and clipping of |
| 259 // values. | 257 // values. |
| 260 SetAutoSize(auto_size_enabled, | 258 SetAutoSize(auto_size_enabled, |
| 261 gfx::Size(min_width, min_height), | 259 gfx::Size(min_width, min_height), |
| 262 gfx::Size(max_width, max_height)); | 260 gfx::Size(max_width, max_height)); |
| 263 } | 261 } |
| OLD | NEW |