Chromium Code Reviews| 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/pending_extension_manager.h" | |
| 6 | |
| 5 #include "base/logging.h" | 7 #include "base/logging.h" |
| 6 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 7 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 8 #include "chrome/browser/extensions/pending_extension_manager.h" | |
| 9 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
| 10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 11 | 12 |
| 12 using content::BrowserThread; | 13 using content::BrowserThread; |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // Install predicate used by AddFromExternalUpdateUrl(). | 17 // Install predicate used by AddFromExternalUpdateUrl(). |
| 17 bool AlwaysInstall(const Extension& extension) { | 18 bool AlwaysInstall(const Extension& extension) { |
| 18 return true; | 19 return true; |
| 19 } | 20 } |
| 20 | 21 |
| 21 } // namespace | 22 } // namespace |
| 22 | 23 |
| 23 PendingExtensionManager::PendingExtensionManager( | 24 PendingExtensionManager::PendingExtensionManager( |
| 24 const ExtensionServiceInterface& service) | 25 const ExtensionServiceInterface& service) |
| 25 : service_(service) { | 26 : service_(service) { |
| 26 } | 27 } |
| 27 | 28 |
| 28 PendingExtensionManager::~PendingExtensionManager() {} | 29 PendingExtensionManager::~PendingExtensionManager() {} |
| 29 | 30 |
| 30 bool PendingExtensionManager::GetById( | 31 bool PendingExtensionManager::GetById( |
| 31 const std::string& id, | 32 const std::string& id, |
| 32 PendingExtensionInfo* out_pending_extension_info) const { | 33 PendingExtensionInfo* out_pending_extension_info) const { |
| 33 | 34 PendingExtensionList::const_iterator iter = GetExtensionListConstIterById(id); |
|
Aaron Boodman
2012/04/05 22:06:43
It would be cleaner to make the pending list store
| |
| 34 PendingExtensionMap::const_iterator it = pending_extension_map_.find(id); | 35 if (iter != pending_extension_list_.end()) { |
| 35 if (it != pending_extension_map_.end()) { | 36 *out_pending_extension_info = iter->second; |
| 36 *out_pending_extension_info = it->second; | |
| 37 return true; | 37 return true; |
| 38 } | 38 } |
| 39 | 39 |
| 40 return false; | 40 return false; |
| 41 } | 41 } |
| 42 | 42 |
| 43 void PendingExtensionManager::Remove(const std::string& id) { | 43 bool PendingExtensionManager::Remove(const std::string& id) { |
| 44 pending_extension_map_.erase(id); | 44 PendingExtensionList::iterator iter = GetExtensionListIterById(id); |
| 45 if (iter != pending_extension_list_.end()) { | |
| 46 pending_extension_list_.erase(iter); | |
| 47 return true; | |
| 48 } | |
| 49 | |
| 50 return false; | |
| 45 } | 51 } |
| 46 | 52 |
| 47 bool PendingExtensionManager::IsIdPending(const std::string& id) const { | 53 bool PendingExtensionManager::IsIdPending(const std::string& id) const { |
| 48 return ContainsKey(pending_extension_map_, id); | 54 PendingExtensionList::const_iterator iter = GetExtensionListConstIterById(id); |
| 55 return iter != pending_extension_list_.end(); | |
| 49 } | 56 } |
| 50 | 57 |
| 51 bool PendingExtensionManager::AddFromSync( | 58 bool PendingExtensionManager::AddFromSync( |
| 52 const std::string& id, | 59 const std::string& id, |
| 53 const GURL& update_url, | 60 const GURL& update_url, |
| 54 PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install, | 61 PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install, |
| 55 bool install_silently) { | 62 bool install_silently) { |
| 56 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 63 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 57 | 64 |
| 58 if (service_.GetInstalledExtension(id)) { | 65 if (service_.GetInstalledExtension(id)) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 kUpdateUrl, | 131 kUpdateUrl, |
| 125 &AlwaysInstall, | 132 &AlwaysInstall, |
| 126 kIsFromSync, | 133 kIsFromSync, |
| 127 kInstallSilently, | 134 kInstallSilently, |
| 128 install_source); | 135 install_source); |
| 129 | 136 |
| 130 return true; | 137 return true; |
| 131 } | 138 } |
| 132 | 139 |
| 133 void PendingExtensionManager::GetPendingIdsForUpdateCheck( | 140 void PendingExtensionManager::GetPendingIdsForUpdateCheck( |
| 134 std::set<std::string>* out_ids_for_update_check) const { | 141 std::list<std::string>* out_ids_for_update_check) const { |
| 135 PendingExtensionMap::const_iterator iter; | 142 PendingExtensionList::const_iterator iter; |
| 136 for (iter = pending_extension_map_.begin(); | 143 for (iter = pending_extension_list_.begin(); |
| 137 iter != pending_extension_map_.end(); | 144 iter != pending_extension_list_.end(); |
| 138 ++iter) { | 145 ++iter) { |
| 139 Extension::Location install_source = iter->second.install_source(); | 146 Extension::Location install_source = iter->second.install_source(); |
| 140 | 147 |
| 141 // Some install sources read a CRX from the filesystem. They can | 148 // Some install sources read a CRX from the filesystem. They can |
| 142 // not be fetched from an update URL, so don't include them in the | 149 // not be fetched from an update URL, so don't include them in the |
| 143 // set of ids. | 150 // set of ids. |
| 144 if (install_source == Extension::EXTERNAL_PREF || | 151 if (install_source == Extension::EXTERNAL_PREF || |
| 145 install_source == Extension::EXTERNAL_REGISTRY) | 152 install_source == Extension::EXTERNAL_REGISTRY) |
| 146 continue; | 153 continue; |
| 147 | 154 |
| 148 out_ids_for_update_check->insert(iter->first); | 155 out_ids_for_update_check->push_back(iter->first); |
| 149 } | 156 } |
| 150 } | 157 } |
| 151 | 158 |
| 152 bool PendingExtensionManager::AddExtensionImpl( | 159 bool PendingExtensionManager::AddExtensionImpl( |
| 153 const std::string& id, | 160 const std::string& id, |
| 154 const GURL& update_url, | 161 const GURL& update_url, |
| 155 PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install, | 162 PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install, |
| 156 bool is_from_sync, | 163 bool is_from_sync, |
| 157 bool install_silently, | 164 bool install_silently, |
| 158 Extension::Location install_source) { | 165 Extension::Location install_source) { |
| 159 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 166 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 160 | 167 |
| 161 // Will add a pending extension record unless this variable is set to false. | 168 // Will add a pending extension record unless this variable is set to false. |
| 162 bool should_add_pending_record = true; | 169 bool should_add_pending_record = true; |
| 163 | 170 |
| 164 if (ContainsKey(pending_extension_map_, id)) { | 171 PendingExtensionList::iterator iter = GetExtensionListIterById(id); |
| 172 if (iter != pending_extension_list_.end()) { | |
| 165 // Bugs in this code will manifest as sporadic incorrect extension | 173 // Bugs in this code will manifest as sporadic incorrect extension |
| 166 // locations in situations where multiple install sources run at the | 174 // locations in situations where multiple install sources run at the |
| 167 // same time. For example, on first login to a chrome os machine, an | 175 // same time. For example, on first login to a chrome os machine, an |
| 168 // extension may be requested by sync and the default extension set. | 176 // extension may be requested by sync and the default extension set. |
| 169 // The following logging will help diagnose such issues. | 177 // The following logging will help diagnose such issues. |
| 170 VLOG(1) << "Extension id " << id | 178 VLOG(1) << "Extension id " << id |
| 171 << " was entered for update more than once." | 179 << " was entered for update more than once." |
| 172 << " old location: " << pending_extension_map_[id].install_source() | 180 << " old location: " << iter->second.install_source() |
| 173 << " new location: " << install_source; | 181 << " new location: " << install_source; |
| 174 | 182 |
| 175 Extension::Location higher_priority_location = | 183 Extension::Location higher_priority_location = |
| 176 Extension::GetHigherPriorityLocation( | 184 Extension::GetHigherPriorityLocation( |
| 177 install_source, pending_extension_map_[id].install_source()); | 185 install_source, iter->second.install_source()); |
| 178 | 186 |
| 179 if (higher_priority_location == install_source) { | 187 if (higher_priority_location == install_source) { |
| 180 VLOG(1) << "Overwrite existing record."; | 188 VLOG(1) << "Overwrite existing record."; |
| 181 | 189 |
| 182 } else { | 190 } else { |
| 183 VLOG(1) << "Keep existing record."; | 191 VLOG(1) << "Keep existing record."; |
| 184 should_add_pending_record = false; | 192 should_add_pending_record = false; |
| 185 } | 193 } |
| 186 } | 194 } |
| 187 | 195 |
| 188 if (should_add_pending_record) { | 196 if (should_add_pending_record) { |
| 189 pending_extension_map_[id] = PendingExtensionInfo( | 197 PendingExtensionInfo new_pending_record(update_url, |
| 190 update_url, | 198 should_allow_install, |
| 191 should_allow_install, | 199 is_from_sync, |
| 192 is_from_sync, | 200 install_silently, |
| 193 install_silently, | 201 install_source); |
| 194 install_source); | 202 |
| 203 pending_extension_list_.push_back(std::make_pair(id, new_pending_record)); | |
| 195 return true; | 204 return true; |
| 196 } | 205 } |
| 197 return false; | 206 return false; |
| 198 } | 207 } |
| 199 | 208 |
| 200 void PendingExtensionManager::AddForTesting( | 209 void PendingExtensionManager::AddForTesting( |
| 201 const std::string& id, | 210 const std::string& id, |
| 202 const PendingExtensionInfo& pending_extension_info) { | 211 const PendingExtensionInfo& pending_extension_info) { |
| 203 pending_extension_map_[id] = pending_extension_info; | 212 pending_extension_list_.push_back(std::make_pair(id, pending_extension_info)); |
| 204 } | 213 } |
| 214 | |
| 215 PendingExtensionManager::PendingExtensionList::iterator | |
| 216 PendingExtensionManager::GetExtensionListIterById(const std::string& id) { | |
| 217 PendingExtensionList::iterator iter; | |
| 218 for (iter = pending_extension_list_.begin(); | |
|
Dmitry Polukhin
2012/04/06 08:10:05
It is basically std::find algorithm and IMHO it wo
| |
| 219 iter != pending_extension_list_.end(); | |
| 220 ++iter) { | |
| 221 if (id == iter->first) { | |
| 222 return iter; | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 return iter; | |
| 227 } | |
| 228 | |
| 229 PendingExtensionManager::PendingExtensionList::const_iterator | |
| 230 PendingExtensionManager::GetExtensionListConstIterById( | |
| 231 const std::string& id) const { | |
| 232 PendingExtensionList::const_iterator iter; | |
| 233 for (iter = pending_extension_list_.begin(); | |
| 234 iter != pending_extension_list_.end(); | |
| 235 ++iter) { | |
| 236 if (id == iter->first) { | |
| 237 return iter; | |
| 238 } | |
| 239 } | |
| 240 | |
| 241 return iter; | |
| 242 } | |
| OLD | NEW |