| 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/script_badge_controller.h" | 5 #include "chrome/browser/extensions/script_badge_controller.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "chrome/browser/extensions/browser_event_router.h" | 10 #include "chrome/browser/extensions/browser_event_router.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "content/public/browser/navigation_details.h" | 22 #include "content/public/browser/navigation_details.h" |
| 23 #include "content/public/browser/navigation_entry.h" | 23 #include "content/public/browser/navigation_entry.h" |
| 24 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
| 25 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 26 #include "googleurl/src/gurl.h" | 26 #include "googleurl/src/gurl.h" |
| 27 #include "ipc/ipc_message.h" | 27 #include "ipc/ipc_message.h" |
| 28 #include "ipc/ipc_message_macros.h" | 28 #include "ipc/ipc_message_macros.h" |
| 29 | 29 |
| 30 namespace extensions { | 30 namespace extensions { |
| 31 | 31 |
| 32 ScriptBadgeController::ScriptBadgeController(TabContents* tab_contents, | 32 ScriptBadgeController::ScriptBadgeController(content::WebContents* web_contents, |
| 33 ScriptExecutor* script_executor) | 33 ScriptExecutor* script_executor) |
| 34 : ScriptExecutor::Observer(script_executor), | 34 : ScriptExecutor::Observer(script_executor), |
| 35 content::WebContentsObserver(tab_contents->web_contents()), | 35 content::WebContentsObserver(web_contents) { |
| 36 tab_contents_(tab_contents) { | 36 Profile* profile = |
| 37 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 37 registrar_.Add(this, | 38 registrar_.Add(this, |
| 38 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 39 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 39 content::Source<Profile>(tab_contents->profile())); | 40 content::Source<Profile>(profile)); |
| 40 } | 41 } |
| 41 | 42 |
| 42 ScriptBadgeController::~ScriptBadgeController() {} | 43 ScriptBadgeController::~ScriptBadgeController() {} |
| 43 | 44 |
| 44 std::vector<ExtensionAction*> ScriptBadgeController::GetCurrentActions() const { | 45 std::vector<ExtensionAction*> ScriptBadgeController::GetCurrentActions() const { |
| 45 return current_actions_; | 46 return current_actions_; |
| 46 } | 47 } |
| 47 | 48 |
| 48 void ScriptBadgeController::GetAttentionFor( | 49 void ScriptBadgeController::GetAttentionFor( |
| 49 const std::string& extension_id) { | 50 const std::string& extension_id) { |
| 50 ExtensionAction* script_badge = AddExtensionToCurrentActions(extension_id); | 51 ExtensionAction* script_badge = AddExtensionToCurrentActions(extension_id); |
| 51 if (!script_badge) | 52 if (!script_badge) |
| 52 return; | 53 return; |
| 53 | 54 |
| 54 // TODO(jyasskin): Modify the icon's appearance to indicate that the | 55 // TODO(jyasskin): Modify the icon's appearance to indicate that the |
| 55 // extension is merely asking for permission to run: | 56 // extension is merely asking for permission to run: |
| 56 // http://crbug.com/133142 | 57 // http://crbug.com/133142 |
| 57 script_badge->SetAppearance(SessionID::IdForTab(tab_contents_), | 58 TabContents* tab_contents = TabContents::FromWebContents(web_contents()); |
| 59 script_badge->SetAppearance(SessionID::IdForTab(tab_contents), |
| 58 ExtensionAction::WANTS_ATTENTION); | 60 ExtensionAction::WANTS_ATTENTION); |
| 59 | 61 |
| 60 NotifyChange(); | 62 NotifyChange(); |
| 61 } | 63 } |
| 62 | 64 |
| 63 LocationBarController::Action ScriptBadgeController::OnClicked( | 65 LocationBarController::Action ScriptBadgeController::OnClicked( |
| 64 const std::string& extension_id, int mouse_button) { | 66 const std::string& extension_id, int mouse_button) { |
| 65 ExtensionService* service = GetExtensionService(); | 67 ExtensionService* service = GetExtensionService(); |
| 66 if (!service) | 68 if (!service) |
| 67 return ACTION_NONE; | 69 return ACTION_NONE; |
| 68 | 70 |
| 69 const Extension* extension = service->extensions()->GetByID(extension_id); | 71 const Extension* extension = service->extensions()->GetByID(extension_id); |
| 70 CHECK(extension); | 72 CHECK(extension); |
| 71 ExtensionAction* script_badge = extension->script_badge(); | 73 ExtensionAction* script_badge = extension->script_badge(); |
| 72 CHECK(script_badge); | 74 CHECK(script_badge); |
| 73 | 75 |
| 74 switch (mouse_button) { | 76 switch (mouse_button) { |
| 75 case 1: // left | 77 case 1: // left |
| 76 case 2: // middle | 78 case 2: { // middle |
| 77 tab_contents_->extension_tab_helper()->active_tab_permission_manager()-> | 79 TabContents* tab_contents = TabContents::FromWebContents(web_contents()); |
| 80 tab_contents->extension_tab_helper()->active_tab_permission_manager()-> |
| 78 GrantIfRequested(extension); | 81 GrantIfRequested(extension); |
| 79 | 82 |
| 80 // Even if clicking the badge doesn't immediately cause the extension to | 83 // Even if clicking the badge doesn't immediately cause the extension to |
| 81 // run script on the page, we want to help users associate clicking with | 84 // run script on the page, we want to help users associate clicking with |
| 82 // the extension having permission to modify the page, so we make the icon | 85 // the extension having permission to modify the page, so we make the icon |
| 83 // full-colored immediately. | 86 // full-colored immediately. |
| 84 if (script_badge->SetAppearance(SessionID::IdForTab(tab_contents_), | 87 if (script_badge->SetAppearance(SessionID::IdForTab(tab_contents), |
| 85 ExtensionAction::ACTIVE)) | 88 ExtensionAction::ACTIVE)) |
| 86 NotifyChange(); | 89 NotifyChange(); |
| 87 | 90 |
| 88 // Fire the scriptBadge.onClicked event. | 91 // Fire the scriptBadge.onClicked event. |
| 89 GetExtensionService()->browser_event_router()->ScriptBadgeExecuted( | 92 GetExtensionService()->browser_event_router()->ScriptBadgeExecuted( |
| 90 tab_contents_->profile(), | 93 tab_contents->profile(), |
| 91 *script_badge, | 94 *script_badge, |
| 92 SessionID::IdForTab(tab_contents_)); | 95 SessionID::IdForTab(tab_contents)); |
| 93 | 96 |
| 94 // TODO(jyasskin): The fallback order should be user-defined popup -> | 97 // TODO(jyasskin): The fallback order should be user-defined popup -> |
| 95 // onClicked handler -> default popup. | 98 // onClicked handler -> default popup. |
| 96 return ACTION_SHOW_SCRIPT_POPUP; | 99 return ACTION_SHOW_SCRIPT_POPUP; |
| 100 } |
| 97 case 3: // right | 101 case 3: // right |
| 98 // Don't grant access on right clicks, so users can investigate | 102 // Don't grant access on right clicks, so users can investigate |
| 99 // the extension without danger. | 103 // the extension without danger. |
| 100 | 104 |
| 101 return extension->ShowConfigureContextMenus() ? | 105 return extension->ShowConfigureContextMenus() ? |
| 102 ACTION_SHOW_CONTEXT_MENU : ACTION_NONE; | 106 ACTION_SHOW_CONTEXT_MENU : ACTION_NONE; |
| 103 } | 107 } |
| 104 | 108 |
| 105 return ACTION_NONE; | 109 return ACTION_NONE; |
| 106 } | 110 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 127 on_page_id, | 131 on_page_id, |
| 128 extension_id.c_str()); | 132 extension_id.c_str()); |
| 129 char buf[1024]; | 133 char buf[1024]; |
| 130 base::snprintf(buf, arraysize(buf), "%s", message.c_str()); | 134 base::snprintf(buf, arraysize(buf), "%s", message.c_str()); |
| 131 LOG(ERROR) << message; | 135 LOG(ERROR) << message; |
| 132 return; | 136 return; |
| 133 } | 137 } |
| 134 } | 138 } |
| 135 | 139 |
| 136 ExtensionService* ScriptBadgeController::GetExtensionService() { | 140 ExtensionService* ScriptBadgeController::GetExtensionService() { |
| 141 TabContents* tab_contents = TabContents::FromWebContents(web_contents()); |
| 137 return extensions::ExtensionSystem::Get( | 142 return extensions::ExtensionSystem::Get( |
| 138 tab_contents_->profile())->extension_service(); | 143 tab_contents->profile())->extension_service(); |
| 139 } | 144 } |
| 140 | 145 |
| 141 int32 ScriptBadgeController::GetPageID() { | 146 int32 ScriptBadgeController::GetPageID() { |
| 142 content::NavigationEntry* nav_entry = | 147 content::NavigationEntry* nav_entry = |
| 143 tab_contents_->web_contents()->GetController().GetActiveEntry(); | 148 web_contents()->GetController().GetActiveEntry(); |
| 144 return nav_entry ? nav_entry->GetPageID() : -1; | 149 return nav_entry ? nav_entry->GetPageID() : -1; |
| 145 } | 150 } |
| 146 | 151 |
| 147 void ScriptBadgeController::NotifyChange() { | 152 void ScriptBadgeController::NotifyChange() { |
| 153 TabContents* tab_contents = TabContents::FromWebContents(web_contents()); |
| 148 content::NotificationService::current()->Notify( | 154 content::NotificationService::current()->Notify( |
| 149 chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED, | 155 chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED, |
| 150 content::Source<Profile>(tab_contents_->profile()), | 156 content::Source<Profile>(tab_contents->profile()), |
| 151 content::Details<TabContents>(tab_contents_)); | 157 content::Details<content::WebContents>(web_contents())); |
| 152 } | 158 } |
| 153 | 159 |
| 154 void ScriptBadgeController::DidNavigateMainFrame( | 160 void ScriptBadgeController::DidNavigateMainFrame( |
| 155 const content::LoadCommittedDetails& details, | 161 const content::LoadCommittedDetails& details, |
| 156 const content::FrameNavigateParams& params) { | 162 const content::FrameNavigateParams& params) { |
| 157 if (details.is_in_page) | 163 if (details.is_in_page) |
| 158 return; | 164 return; |
| 159 extensions_in_current_actions_.clear(); | 165 extensions_in_current_actions_.clear(); |
| 160 current_actions_.clear(); | 166 current_actions_.clear(); |
| 161 } | 167 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 current_actions_.push_back(script_badge); | 241 current_actions_.push_back(script_badge); |
| 236 return script_badge; | 242 return script_badge; |
| 237 } | 243 } |
| 238 | 244 |
| 239 bool ScriptBadgeController::MarkExtensionExecuting( | 245 bool ScriptBadgeController::MarkExtensionExecuting( |
| 240 const std::string& extension_id) { | 246 const std::string& extension_id) { |
| 241 ExtensionAction* script_badge = AddExtensionToCurrentActions(extension_id); | 247 ExtensionAction* script_badge = AddExtensionToCurrentActions(extension_id); |
| 242 if (!script_badge) | 248 if (!script_badge) |
| 243 return false; | 249 return false; |
| 244 | 250 |
| 245 script_badge->SetAppearance(SessionID::IdForTab(tab_contents_), | 251 TabContents* tab_contents = TabContents::FromWebContents(web_contents()); |
| 252 script_badge->SetAppearance(SessionID::IdForTab(tab_contents), |
| 246 ExtensionAction::ACTIVE); | 253 ExtensionAction::ACTIVE); |
| 247 return true; | 254 return true; |
| 248 } | 255 } |
| 249 | 256 |
| 250 bool ScriptBadgeController::EraseExtension(const Extension* extension) { | 257 bool ScriptBadgeController::EraseExtension(const Extension* extension) { |
| 251 if (extensions_in_current_actions_.erase(extension->id()) == 0) | 258 if (extensions_in_current_actions_.erase(extension->id()) == 0) |
| 252 return false; | 259 return false; |
| 253 | 260 |
| 254 size_t size_before = current_actions_.size(); | 261 size_t size_before = current_actions_.size(); |
| 255 | 262 |
| 256 for (std::vector<ExtensionAction*>::iterator it = current_actions_.begin(); | 263 for (std::vector<ExtensionAction*>::iterator it = current_actions_.begin(); |
| 257 it != current_actions_.end(); ++it) { | 264 it != current_actions_.end(); ++it) { |
| 258 // Safe to -> the extension action because we still have a handle to the | 265 // Safe to -> the extension action because we still have a handle to the |
| 259 // owner Extension. | 266 // owner Extension. |
| 260 // | 267 // |
| 261 // Also note that this means that when extensions are uninstalled their | 268 // Also note that this means that when extensions are uninstalled their |
| 262 // script badges will disappear, even though they're still acting on the | 269 // script badges will disappear, even though they're still acting on the |
| 263 // page (since they would have already acted). | 270 // page (since they would have already acted). |
| 264 if ((*it)->extension_id() == extension->id()) { | 271 if ((*it)->extension_id() == extension->id()) { |
| 265 current_actions_.erase(it); | 272 current_actions_.erase(it); |
| 266 break; | 273 break; |
| 267 } | 274 } |
| 268 } | 275 } |
| 269 | 276 |
| 270 CHECK_EQ(size_before, current_actions_.size() + 1); | 277 CHECK_EQ(size_before, current_actions_.size() + 1); |
| 271 return true; | 278 return true; |
| 272 } | 279 } |
| 273 | 280 |
| 274 } // namespace extensions | 281 } // namespace extensions |
| OLD | NEW |