| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/extension_tab_helper.h" | 5 #include "chrome/browser/extensions/extension_tab_helper.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/extension_service.h" | 7 #include "chrome/browser/extensions/extension_service.h" |
| 8 #include "chrome/browser/extensions/page_action_controller.h" | 8 #include "chrome/browser/extensions/page_action_controller.h" |
| 9 #include "chrome/browser/extensions/script_badge_controller.h" | 9 #include "chrome/browser/extensions/script_badge_controller.h" |
| 10 #include "chrome/browser/extensions/script_executor_impl.h" | 10 #include "chrome/browser/extensions/script_executor_impl.h" |
| 11 #include "chrome/browser/extensions/webstore_inline_installer.h" | 11 #include "chrome/browser/extensions/webstore_inline_installer.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/sessions/restore_tab_helper.h" | 13 #include "chrome/browser/sessions/restore_tab_helper.h" |
| 14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 15 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 16 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
| 16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
| 17 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
| 18 #include "chrome/common/extensions/extension_action.h" | 18 #include "chrome/common/extensions/extension_action.h" |
| 19 #include "chrome/common/extensions/extension_constants.h" | 19 #include "chrome/common/extensions/extension_constants.h" |
| 20 #include "chrome/common/extensions/extension_icon_set.h" | 20 #include "chrome/common/extensions/extension_icon_set.h" |
| 21 #include "chrome/common/extensions/extension_messages.h" | 21 #include "chrome/common/extensions/extension_messages.h" |
| 22 #include "chrome/common/extensions/extension_resource.h" | 22 #include "chrome/common/extensions/extension_resource.h" |
| 23 #include "chrome/common/extensions/extension_switch_utils.h" | 23 #include "chrome/common/extensions/extension_switch_utils.h" |
| 24 #include "content/public/browser/invalidate_type.h" | 24 #include "content/public/browser/invalidate_type.h" |
| 25 #include "content/public/browser/navigation_details.h" | 25 #include "content/public/browser/navigation_details.h" |
| 26 #include "content/public/browser/notification_service.h" | 26 #include "content/public/browser/notification_service.h" |
| 27 #include "content/public/browser/render_process_host.h" | 27 #include "content/public/browser/render_process_host.h" |
| 28 #include "content/public/browser/render_view_host.h" | 28 #include "content/public/browser/render_view_host.h" |
| 29 #include "content/public/browser/render_widget_host_view.h" | 29 #include "content/public/browser/render_widget_host_view.h" |
| 30 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
| 31 #include "ui/gfx/image/image.h" | 31 #include "ui/gfx/image/image.h" |
| 32 | 32 |
| 33 using content::WebContents; | 33 using content::WebContents; |
| 34 using extensions::Extension; | 34 using extensions::Extension; |
| 35 using extensions::ScriptBadgeController; | 35 using extensions::ScriptBadgeController; |
| 36 using extensions::ScriptExecutorImpl; | 36 using extensions::ScriptExecutorImpl; |
| 37 using extensions::PageActionController; | 37 using extensions::PageActionController; |
| 38 | 38 |
| 39 namespace { | 39 namespace { |
| 40 | 40 |
| 41 const char kPermissionError[] = "permission_error"; | 41 const char kPermissionError[] = "permission_error"; |
| 42 | 42 |
| 43 } // namespace | 43 } // namespace |
| 44 | 44 |
| 45 ExtensionTabHelper::ExtensionTabHelper(TabContentsWrapper* wrapper) | 45 ExtensionTabHelper::ExtensionTabHelper(TabContents* tab_contents) |
| 46 : content::WebContentsObserver(wrapper->web_contents()), | 46 : content::WebContentsObserver(tab_contents->web_contents()), |
| 47 delegate_(NULL), | 47 delegate_(NULL), |
| 48 extension_app_(NULL), | 48 extension_app_(NULL), |
| 49 ALLOW_THIS_IN_INITIALIZER_LIST( | 49 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 50 extension_function_dispatcher_(wrapper->profile(), this)), | 50 extension_function_dispatcher_(tab_contents->profile(), this)), |
| 51 wrapper_(wrapper) { | 51 tab_contents_(tab_contents) { |
| 52 if (extensions::switch_utils::IsActionBoxEnabled()) { | 52 if (extensions::switch_utils::IsActionBoxEnabled()) { |
| 53 script_badge_controller_ = new ScriptBadgeController(wrapper); | 53 script_badge_controller_ = new ScriptBadgeController(tab_contents); |
| 54 } else { | 54 } else { |
| 55 script_executor_.reset(new ScriptExecutorImpl(wrapper->web_contents())); | 55 script_executor_.reset( |
| 56 location_bar_controller_.reset(new PageActionController(wrapper)); | 56 new ScriptExecutorImpl(tab_contents->web_contents())); |
| 57 location_bar_controller_.reset(new PageActionController(tab_contents)); |
| 57 } | 58 } |
| 58 } | 59 } |
| 59 | 60 |
| 60 ExtensionTabHelper::~ExtensionTabHelper() { | 61 ExtensionTabHelper::~ExtensionTabHelper() { |
| 61 } | 62 } |
| 62 | 63 |
| 63 void ExtensionTabHelper::CopyStateFrom(const ExtensionTabHelper& source) { | 64 void ExtensionTabHelper::CopyStateFrom(const ExtensionTabHelper& source) { |
| 64 SetExtensionApp(source.extension_app()); | 65 SetExtensionApp(source.extension_app()); |
| 65 extension_app_icon_ = source.extension_app_icon_; | 66 extension_app_icon_ = source.extension_app_icon_; |
| 66 } | 67 } |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 131 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 131 ExtensionService* service = profile->GetExtensionService(); | 132 ExtensionService* service = profile->GetExtensionService(); |
| 132 if (!service) | 133 if (!service) |
| 133 return; | 134 return; |
| 134 | 135 |
| 135 for (ExtensionSet::const_iterator it = service->extensions()->begin(); | 136 for (ExtensionSet::const_iterator it = service->extensions()->begin(); |
| 136 it != service->extensions()->end(); ++it) { | 137 it != service->extensions()->end(); ++it) { |
| 137 ExtensionAction* browser_action = (*it)->browser_action(); | 138 ExtensionAction* browser_action = (*it)->browser_action(); |
| 138 if (browser_action) { | 139 if (browser_action) { |
| 139 browser_action->ClearAllValuesForTab( | 140 browser_action->ClearAllValuesForTab( |
| 140 wrapper_->restore_tab_helper()->session_id().id()); | 141 tab_contents_->restore_tab_helper()->session_id().id()); |
| 141 content::NotificationService::current()->Notify( | 142 content::NotificationService::current()->Notify( |
| 142 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, | 143 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, |
| 143 content::Source<ExtensionAction>(browser_action), | 144 content::Source<ExtensionAction>(browser_action), |
| 144 content::NotificationService::NoDetails()); | 145 content::NotificationService::NoDetails()); |
| 145 } | 146 } |
| 146 | 147 |
| 147 ExtensionAction* page_action = (*it)->page_action(); | 148 ExtensionAction* page_action = (*it)->page_action(); |
| 148 if (page_action) { | 149 if (page_action) { |
| 149 page_action->ClearAllValuesForTab( | 150 page_action->ClearAllValuesForTab( |
| 150 wrapper_->restore_tab_helper()->session_id().id()); | 151 tab_contents_->restore_tab_helper()->session_id().id()); |
| 151 PageActionStateChanged(); | 152 PageActionStateChanged(); |
| 152 } | 153 } |
| 153 } | 154 } |
| 154 } | 155 } |
| 155 | 156 |
| 156 bool ExtensionTabHelper::OnMessageReceived(const IPC::Message& message) { | 157 bool ExtensionTabHelper::OnMessageReceived(const IPC::Message& message) { |
| 157 bool handled = true; | 158 bool handled = true; |
| 158 IPC_BEGIN_MESSAGE_MAP(ExtensionTabHelper, message) | 159 IPC_BEGIN_MESSAGE_MAP(ExtensionTabHelper, message) |
| 159 IPC_MESSAGE_HANDLER(ExtensionHostMsg_DidGetApplicationInfo, | 160 IPC_MESSAGE_HANDLER(ExtensionHostMsg_DidGetApplicationInfo, |
| 160 OnDidGetApplicationInfo) | 161 OnDidGetApplicationInfo) |
| 161 IPC_MESSAGE_HANDLER(ExtensionHostMsg_InstallApplication, | 162 IPC_MESSAGE_HANDLER(ExtensionHostMsg_InstallApplication, |
| 162 OnInstallApplication) | 163 OnInstallApplication) |
| 163 IPC_MESSAGE_HANDLER(ExtensionHostMsg_InlineWebstoreInstall, | 164 IPC_MESSAGE_HANDLER(ExtensionHostMsg_InlineWebstoreInstall, |
| 164 OnInlineWebstoreInstall) | 165 OnInlineWebstoreInstall) |
| 165 IPC_MESSAGE_HANDLER(ExtensionHostMsg_GetAppNotifyChannel, | 166 IPC_MESSAGE_HANDLER(ExtensionHostMsg_GetAppNotifyChannel, |
| 166 OnGetAppNotifyChannel) | 167 OnGetAppNotifyChannel) |
| 167 IPC_MESSAGE_HANDLER(ExtensionHostMsg_GetAppInstallState, | 168 IPC_MESSAGE_HANDLER(ExtensionHostMsg_GetAppInstallState, |
| 168 OnGetAppInstallState); | 169 OnGetAppInstallState); |
| 169 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) | 170 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) |
| 170 IPC_MESSAGE_UNHANDLED(handled = false) | 171 IPC_MESSAGE_UNHANDLED(handled = false) |
| 171 IPC_END_MESSAGE_MAP() | 172 IPC_END_MESSAGE_MAP() |
| 172 return handled; | 173 return handled; |
| 173 } | 174 } |
| 174 | 175 |
| 175 void ExtensionTabHelper::OnDidGetApplicationInfo( | 176 void ExtensionTabHelper::OnDidGetApplicationInfo( |
| 176 int32 page_id, const WebApplicationInfo& info) { | 177 int32 page_id, const WebApplicationInfo& info) { |
| 177 web_app_info_ = info; | 178 web_app_info_ = info; |
| 178 | 179 |
| 179 if (delegate_) | 180 if (delegate_) |
| 180 delegate_->OnDidGetApplicationInfo(wrapper_, page_id); | 181 delegate_->OnDidGetApplicationInfo(tab_contents_, page_id); |
| 181 } | 182 } |
| 182 | 183 |
| 183 void ExtensionTabHelper::OnInstallApplication(const WebApplicationInfo& info) { | 184 void ExtensionTabHelper::OnInstallApplication(const WebApplicationInfo& info) { |
| 184 if (delegate_) | 185 if (delegate_) |
| 185 delegate_->OnInstallApplication(wrapper_, info); | 186 delegate_->OnInstallApplication(tab_contents_, info); |
| 186 } | 187 } |
| 187 | 188 |
| 188 void ExtensionTabHelper::OnInlineWebstoreInstall( | 189 void ExtensionTabHelper::OnInlineWebstoreInstall( |
| 189 int install_id, | 190 int install_id, |
| 190 int return_route_id, | 191 int return_route_id, |
| 191 const std::string& webstore_item_id, | 192 const std::string& webstore_item_id, |
| 192 const GURL& requestor_url) { | 193 const GURL& requestor_url) { |
| 193 scoped_refptr<WebstoreInlineInstaller> installer(new WebstoreInlineInstaller( | 194 scoped_refptr<WebstoreInlineInstaller> installer(new WebstoreInlineInstaller( |
| 194 web_contents(), | 195 web_contents(), |
| 195 install_id, | 196 install_id, |
| 196 return_route_id, | 197 return_route_id, |
| 197 webstore_item_id, | 198 webstore_item_id, |
| 198 requestor_url, | 199 requestor_url, |
| 199 this)); | 200 this)); |
| 200 installer->BeginInstall(); | 201 installer->BeginInstall(); |
| 201 } | 202 } |
| 202 | 203 |
| 203 void ExtensionTabHelper::OnGetAppNotifyChannel( | 204 void ExtensionTabHelper::OnGetAppNotifyChannel( |
| 204 const GURL& requestor_url, | 205 const GURL& requestor_url, |
| 205 const std::string& client_id, | 206 const std::string& client_id, |
| 206 int return_route_id, | 207 int return_route_id, |
| 207 int callback_id) { | 208 int callback_id) { |
| 208 | 209 |
| 209 // Check for permission first. | 210 // Check for permission first. |
| 210 Profile* profile = | 211 Profile* profile = |
| 211 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 212 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 212 ExtensionService* extension_service = profile->GetExtensionService(); | 213 ExtensionService* extension_service = profile->GetExtensionService(); |
| 213 extensions::ProcessMap* process_map = extension_service->process_map(); | 214 extensions::ProcessMap* process_map = extension_service->process_map(); |
| 214 content::RenderProcessHost* process = | 215 content::RenderProcessHost* process = |
| 215 tab_contents_wrapper()->web_contents()->GetRenderProcessHost(); | 216 tab_contents()->web_contents()->GetRenderProcessHost(); |
| 216 const Extension* extension = | 217 const Extension* extension = |
| 217 extension_service->GetInstalledApp(requestor_url); | 218 extension_service->GetInstalledApp(requestor_url); |
| 218 | 219 |
| 219 std::string error; | 220 std::string error; |
| 220 if (!extension || | 221 if (!extension || |
| 221 !extension->HasAPIPermission( | 222 !extension->HasAPIPermission( |
| 222 ExtensionAPIPermission::kAppNotifications) || | 223 ExtensionAPIPermission::kAppNotifications) || |
| 223 !process_map->Contains(extension->id(), process->GetID())) | 224 !process_map->Contains(extension->id(), process->GetID())) |
| 224 error = kPermissionError; | 225 error = kPermissionError; |
| 225 | 226 |
| 226 // Make sure the extension can cross to the main profile, if called from an | 227 // Make sure the extension can cross to the main profile, if called from an |
| 227 // an incognito window. | 228 // an incognito window. |
| 228 if (profile->IsOffTheRecord() && | 229 if (profile->IsOffTheRecord() && |
| 229 !extension_service->CanCrossIncognito(extension)) | 230 !extension_service->CanCrossIncognito(extension)) |
| 230 error = extension_misc::kAppNotificationsIncognitoError; | 231 error = extension_misc::kAppNotificationsIncognitoError; |
| 231 | 232 |
| 232 if (!error.empty()) { | 233 if (!error.empty()) { |
| 233 Send(new ExtensionMsg_GetAppNotifyChannelResponse( | 234 Send(new ExtensionMsg_GetAppNotifyChannelResponse( |
| 234 return_route_id, "", error, callback_id)); | 235 return_route_id, "", error, callback_id)); |
| 235 return; | 236 return; |
| 236 } | 237 } |
| 237 | 238 |
| 238 AppNotifyChannelUI* ui = new AppNotifyChannelUIImpl( | 239 AppNotifyChannelUI* ui = new AppNotifyChannelUIImpl( |
| 239 profile, tab_contents_wrapper(), extension->name(), | 240 profile, tab_contents(), extension->name(), |
| 240 AppNotifyChannelUI::NOTIFICATION_INFOBAR); | 241 AppNotifyChannelUI::NOTIFICATION_INFOBAR); |
| 241 | 242 |
| 242 scoped_refptr<AppNotifyChannelSetup> channel_setup( | 243 scoped_refptr<AppNotifyChannelSetup> channel_setup( |
| 243 new AppNotifyChannelSetup(profile, | 244 new AppNotifyChannelSetup(profile, |
| 244 extension->id(), | 245 extension->id(), |
| 245 client_id, | 246 client_id, |
| 246 requestor_url, | 247 requestor_url, |
| 247 return_route_id, | 248 return_route_id, |
| 248 callback_id, | 249 callback_id, |
| 249 ui, | 250 ui, |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 void ExtensionTabHelper::OnInlineInstallFailure(int install_id, | 369 void ExtensionTabHelper::OnInlineInstallFailure(int install_id, |
| 369 int return_route_id, | 370 int return_route_id, |
| 370 const std::string& error) { | 371 const std::string& error) { |
| 371 Send(new ExtensionMsg_InlineWebstoreInstallResponse( | 372 Send(new ExtensionMsg_InlineWebstoreInstallResponse( |
| 372 return_route_id, install_id, false, error)); | 373 return_route_id, install_id, false, error)); |
| 373 } | 374 } |
| 374 | 375 |
| 375 WebContents* ExtensionTabHelper::GetAssociatedWebContents() const { | 376 WebContents* ExtensionTabHelper::GetAssociatedWebContents() const { |
| 376 return web_contents(); | 377 return web_contents(); |
| 377 } | 378 } |
| OLD | NEW |