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