[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/logging.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 6 | #include "base/stl_util.h" |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 7 | #include "chrome/browser/extensions/extension_service.h" |
| 8 | #include "chrome/browser/extensions/pending_extension_manager.h" |
[email protected] | 145a317b | 2011-04-12 16:03:46 | [diff] [blame] | 9 | #include "chrome/common/extensions/extension.h" |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 10 | #include "content/browser/browser_thread.h" |
| 11 | |
| 12 | namespace { |
| 13 | |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 14 | // Install predicate used by AddFromExternalUpdateUrl(). |
| 15 | bool AlwaysInstall(const Extension& extension) { |
| 16 | return true; |
| 17 | } |
| 18 | |
| 19 | } // namespace |
| 20 | |
| 21 | PendingExtensionManager::PendingExtensionManager( |
[email protected] | 2859946f | 2011-04-04 18:18:06 | [diff] [blame] | 22 | const ExtensionServiceInterface& service) |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 23 | : service_(service) { |
| 24 | } |
| 25 | |
| 26 | PendingExtensionManager::~PendingExtensionManager() {} |
| 27 | |
| 28 | bool PendingExtensionManager::GetById( |
| 29 | const std::string& id, |
| 30 | PendingExtensionInfo* out_pending_extension_info) const { |
| 31 | |
| 32 | PendingExtensionMap::const_iterator it = pending_extension_map_.find(id); |
| 33 | if (it != pending_extension_map_.end()) { |
| 34 | *out_pending_extension_info = it->second; |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | void PendingExtensionManager::Remove(const std::string& id) { |
| 42 | pending_extension_map_.erase(id); |
| 43 | } |
| 44 | |
| 45 | bool PendingExtensionManager::IsIdPending(const std::string& id) const { |
| 46 | return ContainsKey(pending_extension_map_, id); |
| 47 | } |
| 48 | |
[email protected] | 145a317b | 2011-04-12 16:03:46 | [diff] [blame] | 49 | bool PendingExtensionManager::AddFromSync( |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 50 | const std::string& id, |
| 51 | const GURL& update_url, |
| 52 | PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install, |
[email protected] | 6cc7dbae | 2011-04-29 21:18:33 | [diff] [blame] | 53 | bool install_silently) { |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 54 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 55 | |
[email protected] | 8001df2 | 2011-04-28 19:59:47 | [diff] [blame] | 56 | if (service_.GetInstalledExtension(id)) { |
[email protected] | 145a317b | 2011-04-12 16:03:46 | [diff] [blame] | 57 | LOG(ERROR) << "Trying to add pending extension " << id |
| 58 | << " which already exists"; |
| 59 | return false; |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 60 | } |
| 61 | |
[email protected] | 145a317b | 2011-04-12 16:03:46 | [diff] [blame] | 62 | const bool kIsFromSync = true; |
| 63 | const Extension::Location kSyncLocation = Extension::INTERNAL; |
| 64 | |
| 65 | return AddExtensionImpl(id, update_url, should_allow_install, |
[email protected] | 6cc7dbae | 2011-04-29 21:18:33 | [diff] [blame] | 66 | kIsFromSync, install_silently, kSyncLocation); |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void PendingExtensionManager::AddFromExternalUpdateUrl( |
| 70 | const std::string& id, const GURL& update_url, |
| 71 | Extension::Location location) { |
| 72 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 73 | |
| 74 | const bool kIsFromSync = false; |
| 75 | const bool kInstallSilently = true; |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 76 | |
[email protected] | 8a87a533 | 2011-08-11 17:54:59 | [diff] [blame^] | 77 | const Extension* extension = service_.GetInstalledExtension(id); |
| 78 | if (extension && |
| 79 | location == Extension::GetHigherPriorityLocation(location, |
| 80 | extension->location())) { |
| 81 | // If the new location has higher priority than the location of an existing |
| 82 | // extension, let the update process overwrite the existing extension. |
| 83 | } else { |
| 84 | if (service_.IsExternalExtensionUninstalled(id)) { |
| 85 | return; |
| 86 | } |
| 87 | if (extension) { |
| 88 | LOG(DFATAL) << "Trying to add extension " << id |
| 89 | << " by external update, but it is already installed."; |
| 90 | return; |
| 91 | } |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | AddExtensionImpl(id, update_url, &AlwaysInstall, |
| 95 | kIsFromSync, kInstallSilently, |
[email protected] | 6cc7dbae | 2011-04-29 21:18:33 | [diff] [blame] | 96 | location); |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 100 | void PendingExtensionManager::AddFromExternalFile( |
| 101 | const std::string& id, |
| 102 | Extension::Location location) { |
| 103 | |
| 104 | GURL kUpdateUrl = GURL(); |
| 105 | bool kIsFromSync = false; |
| 106 | bool kInstallSilently = true; |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 107 | |
| 108 | pending_extension_map_[id] = |
| 109 | PendingExtensionInfo(kUpdateUrl, |
| 110 | &AlwaysInstall, |
| 111 | kIsFromSync, |
| 112 | kInstallSilently, |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 113 | location); |
| 114 | } |
| 115 | |
[email protected] | 145a317b | 2011-04-12 16:03:46 | [diff] [blame] | 116 | bool PendingExtensionManager::AddExtensionImpl( |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 117 | const std::string& id, const GURL& update_url, |
| 118 | PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install, |
| 119 | bool is_from_sync, bool install_silently, |
[email protected] | 6cc7dbae | 2011-04-29 21:18:33 | [diff] [blame] | 120 | Extension::Location install_source) { |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 121 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 122 | |
[email protected] | 145a317b | 2011-04-12 16:03:46 | [diff] [blame] | 123 | // Will add a pending extension record unless this variable is set to false. |
| 124 | bool should_add_pending_record = true; |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 125 | |
[email protected] | 145a317b | 2011-04-12 16:03:46 | [diff] [blame] | 126 | if (ContainsKey(pending_extension_map_, id)) { |
| 127 | // Bugs in this code will manifest as sporadic incorrect extension |
| 128 | // locations in situations where multiple install sources run at the |
| 129 | // same time. For example, on first login to a chrome os machine, an |
| 130 | // extension may be requested by sync sync and the default extension set. |
| 131 | // The following logging will help diagnose such issues. |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 132 | VLOG(1) << "Extension id " << id |
| 133 | << " was entered for update more than once." |
[email protected] | 145a317b | 2011-04-12 16:03:46 | [diff] [blame] | 134 | << " old location: " << pending_extension_map_[id].install_source() |
| 135 | << " new location: " << install_source; |
| 136 | |
| 137 | Extension::Location higher_priority_location = |
| 138 | Extension::GetHigherPriorityLocation( |
| 139 | install_source, pending_extension_map_[id].install_source()); |
| 140 | |
| 141 | if (higher_priority_location == install_source) { |
| 142 | VLOG(1) << "Overwrite existing record."; |
| 143 | |
| 144 | } else { |
| 145 | VLOG(1) << "Keep existing record."; |
| 146 | should_add_pending_record = false; |
| 147 | } |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | 145a317b | 2011-04-12 16:03:46 | [diff] [blame] | 150 | if (should_add_pending_record) { |
| 151 | pending_extension_map_[id] = PendingExtensionInfo( |
| 152 | update_url, |
| 153 | should_allow_install, |
| 154 | is_from_sync, |
| 155 | install_silently, |
[email protected] | 145a317b | 2011-04-12 16:03:46 | [diff] [blame] | 156 | install_source); |
| 157 | return true; |
| 158 | } |
| 159 | return false; |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void PendingExtensionManager::AddForTesting( |
| 163 | const std::string& id, |
| 164 | const PendingExtensionInfo& pending_extension_info) { |
| 165 | pending_extension_map_[id] = pending_extension_info; |
| 166 | } |