| 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 "chrome/browser/extensions/extension_browser_event_router.h" | 7 #include "chrome/browser/extensions/extension_browser_event_router.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/extension_system.h" | 9 #include "chrome/browser/extensions/extension_system.h" |
| 10 #include "chrome/browser/extensions/extension_tab_helper.h" | 10 #include "chrome/browser/extensions/extension_tab_helper.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 33 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 34 content::Source<Profile>(tab_contents->profile())); | 34 content::Source<Profile>(tab_contents->profile())); |
| 35 } | 35 } |
| 36 | 36 |
| 37 ScriptBadgeController::~ScriptBadgeController() {} | 37 ScriptBadgeController::~ScriptBadgeController() {} |
| 38 | 38 |
| 39 std::vector<ExtensionAction*> ScriptBadgeController::GetCurrentActions() const { | 39 std::vector<ExtensionAction*> ScriptBadgeController::GetCurrentActions() const { |
| 40 return current_actions_; | 40 return current_actions_; |
| 41 } | 41 } |
| 42 | 42 |
| 43 void ScriptBadgeController::GetAttentionFor( |
| 44 const std::string& extension_id) { |
| 45 ExtensionAction* script_badge = AddExtensionToCurrentActions(extension_id); |
| 46 if (!script_badge) |
| 47 return; |
| 48 |
| 49 // TODO(jyasskin): Modify the icon's appearance to indicate that the |
| 50 // extension is merely asking for permission to run: |
| 51 // http://crbug.com/133142 |
| 52 script_badge->SetIsVisible( |
| 53 tab_contents_->extension_tab_helper()->tab_id(), true); |
| 54 |
| 55 NotifyChange(); |
| 56 } |
| 57 |
| 43 LocationBarController::Action ScriptBadgeController::OnClicked( | 58 LocationBarController::Action ScriptBadgeController::OnClicked( |
| 44 const std::string& extension_id, int mouse_button) { | 59 const std::string& extension_id, int mouse_button) { |
| 45 ExtensionService* service = GetExtensionService(); | 60 ExtensionService* service = GetExtensionService(); |
| 46 if (!service) | 61 if (!service) |
| 47 return ACTION_NONE; | 62 return ACTION_NONE; |
| 48 | 63 |
| 49 const Extension* extension = service->extensions()->GetByID(extension_id); | 64 const Extension* extension = service->extensions()->GetByID(extension_id); |
| 50 CHECK(extension); | 65 CHECK(extension); |
| 51 ExtensionAction* script_badge = extension->script_badge(); | 66 ExtensionAction* script_badge = extension->script_badge(); |
| 52 CHECK(script_badge); | 67 CHECK(script_badge); |
| 53 | 68 |
| 69 tab_contents_->extension_tab_helper()->active_tab_permission_manager()-> |
| 70 GrantIfRequested(extension); |
| 71 |
| 54 switch (mouse_button) { | 72 switch (mouse_button) { |
| 55 case 1: // left | 73 case 1: // left |
| 56 return ACTION_SHOW_SCRIPT_POPUP; | 74 return ACTION_SHOW_SCRIPT_POPUP; |
| 57 case 2: // middle | 75 case 2: // middle |
| 58 // TODO(yoz): Show the popup if it's available or a default if not. | 76 // TODO(yoz): Show the popup if it's available or a default if not. |
| 59 | 77 |
| 60 // Fire the scriptBadge.onClicked event. | 78 // Fire the scriptBadge.onClicked event. |
| 61 GetExtensionService()->browser_event_router()->ScriptBadgeExecuted( | 79 GetExtensionService()->browser_event_router()->ScriptBadgeExecuted( |
| 62 tab_contents_->profile(), | 80 tab_contents_->profile(), |
| 63 *script_badge, | 81 *script_badge, |
| 64 tab_contents_->extension_tab_helper()->tab_id()); | 82 tab_contents_->extension_tab_helper()->tab_id()); |
| 65 return ACTION_NONE; | 83 return ACTION_NONE; |
| 66 case 3: // right | 84 case 3: // right |
| 67 return extension->ShowConfigureContextMenus() ? | 85 return extension->ShowConfigureContextMenus() ? |
| 68 ACTION_SHOW_CONTEXT_MENU : ACTION_NONE; | 86 ACTION_SHOW_CONTEXT_MENU : ACTION_NONE; |
| 69 } | 87 } |
| 70 | 88 |
| 71 return ACTION_NONE; | 89 return ACTION_NONE; |
| 72 } | 90 } |
| 73 | 91 |
| 74 void ScriptBadgeController::OnExecuteScriptFinished( | 92 void ScriptBadgeController::OnExecuteScriptFinished( |
| 75 const std::string& extension_id, | 93 const std::string& extension_id, |
| 76 bool success, | 94 bool success, |
| 77 int32 page_id, | 95 int32 page_id, |
| 78 const std::string& error) { | 96 const std::string& error) { |
| 79 if (success && page_id == GetPageID()) { | 97 if (success && page_id == GetPageID()) { |
| 80 if (InsertExtension(extension_id)) | 98 if (MarkExtensionExecuting(extension_id)) |
| 81 NotifyChange(); | 99 NotifyChange(); |
| 82 } | 100 } |
| 83 } | 101 } |
| 84 | 102 |
| 85 ExtensionService* ScriptBadgeController::GetExtensionService() { | 103 ExtensionService* ScriptBadgeController::GetExtensionService() { |
| 86 return extensions::ExtensionSystem::Get( | 104 return extensions::ExtensionSystem::Get( |
| 87 tab_contents_->profile())->extension_service(); | 105 tab_contents_->profile())->extension_service(); |
| 88 } | 106 } |
| 89 | 107 |
| 90 int32 ScriptBadgeController::GetPageID() { | 108 int32 ScriptBadgeController::GetPageID() { |
| 91 return tab_contents_->web_contents()->GetController().GetActiveEntry()-> | 109 return tab_contents_->web_contents()->GetController().GetActiveEntry()-> |
| 92 GetPageID(); | 110 GetPageID(); |
| 93 } | 111 } |
| 94 | 112 |
| 95 void ScriptBadgeController::NotifyChange() { | 113 void ScriptBadgeController::NotifyChange() { |
| 96 content::NotificationService::current()->Notify( | 114 content::NotificationService::current()->Notify( |
| 97 chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED, | 115 chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED, |
| 98 content::Source<Profile>(tab_contents_->profile()), | 116 content::Source<Profile>(tab_contents_->profile()), |
| 99 content::Details<TabContents>(tab_contents_)); | 117 content::Details<TabContents>(tab_contents_)); |
| 100 } | 118 } |
| 101 | 119 |
| 102 void ScriptBadgeController::DidNavigateMainFrame( | 120 void ScriptBadgeController::DidNavigateMainFrame( |
| 103 const content::LoadCommittedDetails& details, | 121 const content::LoadCommittedDetails& details, |
| 104 const content::FrameNavigateParams& params) { | 122 const content::FrameNavigateParams& params) { |
| 105 if (details.is_in_page) | 123 if (details.is_in_page) |
| 106 return; | 124 return; |
| 107 extensions_executing_scripts_.clear(); | 125 extensions_in_current_actions_.clear(); |
| 108 current_actions_.clear(); | 126 current_actions_.clear(); |
| 109 } | 127 } |
| 110 | 128 |
| 111 void ScriptBadgeController::Observe( | 129 void ScriptBadgeController::Observe( |
| 112 int type, | 130 int type, |
| 113 const content::NotificationSource& source, | 131 const content::NotificationSource& source, |
| 114 const content::NotificationDetails& details) { | 132 const content::NotificationDetails& details) { |
| 115 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); | 133 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| 116 const Extension* extension = | 134 const Extension* extension = |
| 117 content::Details<UnloadedExtensionInfo>(details)->extension; | 135 content::Details<UnloadedExtensionInfo>(details)->extension; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 130 } | 148 } |
| 131 | 149 |
| 132 void ScriptBadgeController::OnContentScriptsExecuting( | 150 void ScriptBadgeController::OnContentScriptsExecuting( |
| 133 const std::set<std::string>& extension_ids, int32 page_id) { | 151 const std::set<std::string>& extension_ids, int32 page_id) { |
| 134 if (page_id != GetPageID()) | 152 if (page_id != GetPageID()) |
| 135 return; | 153 return; |
| 136 | 154 |
| 137 bool changed = false; | 155 bool changed = false; |
| 138 for (std::set<std::string>::const_iterator it = extension_ids.begin(); | 156 for (std::set<std::string>::const_iterator it = extension_ids.begin(); |
| 139 it != extension_ids.end(); ++it) { | 157 it != extension_ids.end(); ++it) { |
| 140 changed |= InsertExtension(*it); | 158 changed |= MarkExtensionExecuting(*it); |
| 141 } | 159 } |
| 142 if (changed) | 160 if (changed) |
| 143 NotifyChange(); | 161 NotifyChange(); |
| 144 } | 162 } |
| 145 | 163 |
| 146 bool ScriptBadgeController::InsertExtension(const std::string& extension_id) { | 164 ExtensionAction* ScriptBadgeController::AddExtensionToCurrentActions( |
| 147 if (!extensions_executing_scripts_.insert(extension_id).second) | 165 const std::string& extension_id) { |
| 148 return false; | 166 if (!extensions_in_current_actions_.insert(extension_id).second) |
| 167 return NULL; |
| 149 | 168 |
| 150 ExtensionService* service = GetExtensionService(); | 169 ExtensionService* service = GetExtensionService(); |
| 151 if (!service) | 170 if (!service) |
| 152 return false; | 171 return NULL; |
| 153 | 172 |
| 154 const Extension* extension = service->extensions()->GetByID(extension_id); | 173 const Extension* extension = service->extensions()->GetByID(extension_id); |
| 155 if (!extension) | 174 if (!extension) |
| 156 return false; | 175 return NULL; |
| 157 | 176 |
| 158 ExtensionAction* script_badge = extension->script_badge(); | 177 ExtensionAction* script_badge = extension->script_badge(); |
| 159 current_actions_.push_back(script_badge); | 178 current_actions_.push_back(script_badge); |
| 179 return script_badge; |
| 180 } |
| 181 |
| 182 bool ScriptBadgeController::MarkExtensionExecuting( |
| 183 const std::string& extension_id) { |
| 184 ExtensionAction* script_badge = AddExtensionToCurrentActions(extension_id); |
| 185 if (!script_badge) |
| 186 return false; |
| 187 |
| 160 script_badge->RunIconAnimation( | 188 script_badge->RunIconAnimation( |
| 161 tab_contents_->extension_tab_helper()->tab_id()); | 189 tab_contents_->extension_tab_helper()->tab_id()); |
| 162 | |
| 163 return true; | 190 return true; |
| 164 } | 191 } |
| 165 | 192 |
| 166 bool ScriptBadgeController::EraseExtension(const Extension* extension) { | 193 bool ScriptBadgeController::EraseExtension(const Extension* extension) { |
| 167 if (extensions_executing_scripts_.erase(extension->id()) == 0) | 194 if (extensions_in_current_actions_.erase(extension->id()) == 0) |
| 168 return false; | 195 return false; |
| 169 | 196 |
| 170 size_t size_before = current_actions_.size(); | 197 size_t size_before = current_actions_.size(); |
| 171 | 198 |
| 172 for (std::vector<ExtensionAction*>::iterator it = current_actions_.begin(); | 199 for (std::vector<ExtensionAction*>::iterator it = current_actions_.begin(); |
| 173 it != current_actions_.end(); ++it) { | 200 it != current_actions_.end(); ++it) { |
| 174 // Safe to -> the extension action because we still have a handle to the | 201 // Safe to -> the extension action because we still have a handle to the |
| 175 // owner Extension. | 202 // owner Extension. |
| 176 // | 203 // |
| 177 // Also note that this means that when extensions are uninstalled their | 204 // Also note that this means that when extensions are uninstalled their |
| 178 // script badges will disappear, even though they're still acting on the | 205 // script badges will disappear, even though they're still acting on the |
| 179 // page (since they would have already acted). | 206 // page (since they would have already acted). |
| 180 if ((*it)->extension_id() == extension->id()) { | 207 if ((*it)->extension_id() == extension->id()) { |
| 181 current_actions_.erase(it); | 208 current_actions_.erase(it); |
| 182 break; | 209 break; |
| 183 } | 210 } |
| 184 } | 211 } |
| 185 | 212 |
| 186 CHECK_EQ(size_before, current_actions_.size() + 1); | 213 CHECK_EQ(size_before, current_actions_.size() + 1); |
| 187 return true; | 214 return true; |
| 188 } | 215 } |
| 189 | 216 |
| 190 } // namespace extensions | 217 } // namespace extensions |
| OLD | NEW |