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