| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 | 48 |
| 49 void ScriptBadgeController::GetAttentionFor( | 49 void ScriptBadgeController::GetAttentionFor( |
| 50 const std::string& extension_id) { | 50 const std::string& extension_id) { |
| 51 ExtensionAction* script_badge = AddExtensionToCurrentActions(extension_id); | 51 ExtensionAction* script_badge = AddExtensionToCurrentActions(extension_id); |
| 52 if (!script_badge) | 52 if (!script_badge) |
| 53 return; | 53 return; |
| 54 | 54 |
| 55 // TODO(jyasskin): Modify the icon's appearance to indicate that the | 55 // TODO(jyasskin): Modify the icon's appearance to indicate that the |
| 56 // extension is merely asking for permission to run: | 56 // extension is merely asking for permission to run: |
| 57 // http://crbug.com/133142 | 57 // http://crbug.com/133142 |
| 58 TabContents* tab_contents = TabContents::FromWebContents(web_contents()); | 58 script_badge->SetAppearance(SessionID::IdForTab(web_contents()), |
| 59 script_badge->SetAppearance(SessionID::IdForTab(tab_contents), | |
| 60 ExtensionAction::WANTS_ATTENTION); | 59 ExtensionAction::WANTS_ATTENTION); |
| 61 | 60 |
| 62 NotifyChange(); | 61 NotifyChange(); |
| 63 } | 62 } |
| 64 | 63 |
| 65 LocationBarController::Action ScriptBadgeController::OnClicked( | 64 LocationBarController::Action ScriptBadgeController::OnClicked( |
| 66 const std::string& extension_id, int mouse_button) { | 65 const std::string& extension_id, int mouse_button) { |
| 67 ExtensionService* service = GetExtensionService(); | 66 ExtensionService* service = GetExtensionService(); |
| 68 if (!service) | 67 if (!service) |
| 69 return ACTION_NONE; | 68 return ACTION_NONE; |
| 70 | 69 |
| 71 const Extension* extension = service->extensions()->GetByID(extension_id); | 70 const Extension* extension = service->extensions()->GetByID(extension_id); |
| 72 CHECK(extension); | 71 CHECK(extension); |
| 73 ExtensionAction* script_badge = extension->script_badge(); | 72 ExtensionAction* script_badge = extension->script_badge(); |
| 74 CHECK(script_badge); | 73 CHECK(script_badge); |
| 75 | 74 |
| 76 switch (mouse_button) { | 75 switch (mouse_button) { |
| 77 case 1: // left | 76 case 1: // left |
| 78 case 2: { // middle | 77 case 2: { // middle |
| 79 extensions::TabHelper::FromWebContents(web_contents())-> | 78 extensions::TabHelper::FromWebContents(web_contents())-> |
| 80 active_tab_permission_manager()->GrantIfRequested(extension); | 79 active_tab_permission_manager()->GrantIfRequested(extension); |
| 81 | 80 |
| 82 TabContents* tab_contents = TabContents::FromWebContents(web_contents()); | 81 TabContents* tab_contents = TabContents::FromWebContents(web_contents()); |
| 83 // Even if clicking the badge doesn't immediately cause the extension to | 82 // Even if clicking the badge doesn't immediately cause the extension to |
| 84 // run script on the page, we want to help users associate clicking with | 83 // run script on the page, we want to help users associate clicking with |
| 85 // the extension having permission to modify the page, so we make the icon | 84 // the extension having permission to modify the page, so we make the icon |
| 86 // full-colored immediately. | 85 // full-colored immediately. |
| 87 if (script_badge->SetAppearance(SessionID::IdForTab(tab_contents), | 86 if (script_badge->SetAppearance(SessionID::IdForTab(web_contents()), |
| 88 ExtensionAction::ACTIVE)) | 87 ExtensionAction::ACTIVE)) |
| 89 NotifyChange(); | 88 NotifyChange(); |
| 90 | 89 |
| 91 // Fire the scriptBadge.onClicked event. | 90 // Fire the scriptBadge.onClicked event. |
| 92 GetExtensionService()->browser_event_router()->ScriptBadgeExecuted( | 91 GetExtensionService()->browser_event_router()->ScriptBadgeExecuted( |
| 93 tab_contents->profile(), | 92 tab_contents->profile(), |
| 94 *script_badge, | 93 *script_badge, |
| 95 SessionID::IdForTab(tab_contents)); | 94 SessionID::IdForTab(web_contents())); |
| 96 | 95 |
| 97 // TODO(jyasskin): The fallback order should be user-defined popup -> | 96 // TODO(jyasskin): The fallback order should be user-defined popup -> |
| 98 // onClicked handler -> default popup. | 97 // onClicked handler -> default popup. |
| 99 return ACTION_SHOW_SCRIPT_POPUP; | 98 return ACTION_SHOW_SCRIPT_POPUP; |
| 100 } | 99 } |
| 101 case 3: // right | 100 case 3: // right |
| 102 // Don't grant access on right clicks, so users can investigate | 101 // Don't grant access on right clicks, so users can investigate |
| 103 // the extension without danger. | 102 // the extension without danger. |
| 104 | 103 |
| 105 return extension->ShowConfigureContextMenus() ? | 104 return extension->ShowConfigureContextMenus() ? |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 current_actions_.push_back(script_badge); | 240 current_actions_.push_back(script_badge); |
| 242 return script_badge; | 241 return script_badge; |
| 243 } | 242 } |
| 244 | 243 |
| 245 bool ScriptBadgeController::MarkExtensionExecuting( | 244 bool ScriptBadgeController::MarkExtensionExecuting( |
| 246 const std::string& extension_id) { | 245 const std::string& extension_id) { |
| 247 ExtensionAction* script_badge = AddExtensionToCurrentActions(extension_id); | 246 ExtensionAction* script_badge = AddExtensionToCurrentActions(extension_id); |
| 248 if (!script_badge) | 247 if (!script_badge) |
| 249 return false; | 248 return false; |
| 250 | 249 |
| 251 TabContents* tab_contents = TabContents::FromWebContents(web_contents()); | 250 script_badge->SetAppearance(SessionID::IdForTab(web_contents()), |
| 252 script_badge->SetAppearance(SessionID::IdForTab(tab_contents), | |
| 253 ExtensionAction::ACTIVE); | 251 ExtensionAction::ACTIVE); |
| 254 return true; | 252 return true; |
| 255 } | 253 } |
| 256 | 254 |
| 257 bool ScriptBadgeController::EraseExtension(const Extension* extension) { | 255 bool ScriptBadgeController::EraseExtension(const Extension* extension) { |
| 258 if (extensions_in_current_actions_.erase(extension->id()) == 0) | 256 if (extensions_in_current_actions_.erase(extension->id()) == 0) |
| 259 return false; | 257 return false; |
| 260 | 258 |
| 261 size_t size_before = current_actions_.size(); | 259 size_t size_before = current_actions_.size(); |
| 262 | 260 |
| 263 for (std::vector<ExtensionAction*>::iterator it = current_actions_.begin(); | 261 for (std::vector<ExtensionAction*>::iterator it = current_actions_.begin(); |
| 264 it != current_actions_.end(); ++it) { | 262 it != current_actions_.end(); ++it) { |
| 265 // Safe to -> the extension action because we still have a handle to the | 263 // Safe to -> the extension action because we still have a handle to the |
| 266 // owner Extension. | 264 // owner Extension. |
| 267 // | 265 // |
| 268 // Also note that this means that when extensions are uninstalled their | 266 // Also note that this means that when extensions are uninstalled their |
| 269 // script badges will disappear, even though they're still acting on the | 267 // script badges will disappear, even though they're still acting on the |
| 270 // page (since they would have already acted). | 268 // page (since they would have already acted). |
| 271 if ((*it)->extension_id() == extension->id()) { | 269 if ((*it)->extension_id() == extension->id()) { |
| 272 current_actions_.erase(it); | 270 current_actions_.erase(it); |
| 273 break; | 271 break; |
| 274 } | 272 } |
| 275 } | 273 } |
| 276 | 274 |
| 277 CHECK_EQ(size_before, current_actions_.size() + 1); | 275 CHECK_EQ(size_before, current_actions_.size() + 1); |
| 278 return true; | 276 return true; |
| 279 } | 277 } |
| 280 | 278 |
| 281 } // namespace extensions | 279 } // namespace extensions |
| OLD | NEW |