Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / extensions / pending_extension_manager.cc
blob632ea4da55f7e5687f2fb8b398e68e3b648b8cdf
1 // Copyright 2014 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.
5 #include "chrome/browser/extensions/pending_extension_manager.h"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "base/version.h"
11 #include "chrome/common/extensions/extension_constants.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "extensions/browser/extension_prefs.h"
14 #include "extensions/browser/extension_registry.h"
15 #include "extensions/common/constants.h"
16 #include "extensions/common/extension.h"
17 #include "url/gurl.h"
19 using content::BrowserThread;
21 namespace {
23 // Install predicate used by AddFromExternalUpdateUrl().
24 bool AlwaysInstall(const extensions::Extension* extension) {
25 return true;
28 std::string GetVersionString(const Version& version) {
29 return version.IsValid() ? version.GetString() : "invalid";
32 } // namespace
34 namespace extensions {
36 PendingExtensionManager::PendingExtensionManager(
37 content::BrowserContext* context)
38 : context_(context) {}
40 PendingExtensionManager::~PendingExtensionManager() {}
42 const PendingExtensionInfo* PendingExtensionManager::GetById(
43 const std::string& id) const {
44 PendingExtensionList::const_iterator iter;
45 for (iter = pending_extension_list_.begin();
46 iter != pending_extension_list_.end();
47 ++iter) {
48 if (id == iter->id())
49 return &(*iter);
52 return NULL;
55 bool PendingExtensionManager::Remove(const std::string& id) {
56 PendingExtensionList::iterator iter;
57 for (iter = pending_extension_list_.begin();
58 iter != pending_extension_list_.end();
59 ++iter) {
60 if (id == iter->id()) {
61 pending_extension_list_.erase(iter);
62 return true;
66 return false;
69 bool PendingExtensionManager::IsIdPending(const std::string& id) const {
70 return GetById(id) != NULL;
73 bool PendingExtensionManager::HasPendingExtensions() const {
74 return !pending_extension_list_.empty();
77 bool PendingExtensionManager::HasPendingExtensionFromSync() const {
78 PendingExtensionList::const_iterator iter;
79 for (iter = pending_extension_list_.begin();
80 iter != pending_extension_list_.end();
81 ++iter) {
82 if (iter->is_from_sync())
83 return true;
86 return false;
89 bool PendingExtensionManager::AddFromSync(
90 const std::string& id,
91 const GURL& update_url,
92 const base::Version& version,
93 PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install,
94 bool remote_install,
95 bool installed_by_custodian) {
96 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
98 if (ExtensionRegistry::Get(context_)->GetExtensionById(
99 id, ExtensionRegistry::EVERYTHING)) {
100 LOG(ERROR) << "Trying to add pending extension " << id
101 << " which already exists";
102 return false;
105 // Make sure we don't ever try to install the CWS app, because even though
106 // it is listed as a syncable app (because its values need to be synced) it
107 // should already be installed on every instance.
108 if (id == extensions::kWebStoreAppId) {
109 NOTREACHED();
110 return false;
113 int creation_flags = Extension::NO_FLAGS;
114 if (installed_by_custodian) {
115 creation_flags |= Extension::WAS_INSTALLED_BY_CUSTODIAN;
118 static const bool kIsFromSync = true;
119 static const Manifest::Location kSyncLocation = Manifest::INTERNAL;
120 static const bool kMarkAcknowledged = false;
122 return AddExtensionImpl(id,
123 std::string(),
124 update_url,
125 version,
126 should_allow_install,
127 kIsFromSync,
128 kSyncLocation,
129 creation_flags,
130 kMarkAcknowledged,
131 remote_install);
134 bool PendingExtensionManager::AddFromExtensionImport(
135 const std::string& id,
136 const GURL& update_url,
137 PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install) {
138 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
140 if (ExtensionRegistry::Get(context_)->GetExtensionById(
141 id, ExtensionRegistry::EVERYTHING)) {
142 LOG(ERROR) << "Trying to add pending extension " << id
143 << " which already exists";
144 return false;
147 static const bool kIsFromSync = false;
148 static const Manifest::Location kManifestLocation = Manifest::INTERNAL;
149 static const bool kMarkAcknowledged = false;
150 static const bool kRemoteInstall = false;
152 return AddExtensionImpl(id,
153 std::string(),
154 update_url,
155 Version(),
156 should_allow_install,
157 kIsFromSync,
158 kManifestLocation,
159 Extension::NO_FLAGS,
160 kMarkAcknowledged,
161 kRemoteInstall);
164 bool PendingExtensionManager::AddFromExternalUpdateUrl(
165 const std::string& id,
166 const std::string& install_parameter,
167 const GURL& update_url,
168 Manifest::Location location,
169 int creation_flags,
170 bool mark_acknowledged) {
171 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
173 static const bool kIsFromSync = false;
174 static const bool kRemoteInstall = false;
176 const Extension* extension = ExtensionRegistry::Get(context_)
177 ->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
178 if (extension && location == Manifest::GetHigherPriorityLocation(
179 location, extension->location())) {
180 // If the new location has higher priority than the location of an existing
181 // extension, let the update process overwrite the existing extension.
182 } else {
183 if (ExtensionPrefs::Get(context_)->IsExternalExtensionUninstalled(id))
184 return false;
186 if (extension) {
187 LOG(DFATAL) << "Trying to add extension " << id
188 << " by external update, but it is already installed.";
189 return false;
193 return AddExtensionImpl(id,
194 install_parameter,
195 update_url,
196 Version(),
197 &AlwaysInstall,
198 kIsFromSync,
199 location,
200 creation_flags,
201 mark_acknowledged,
202 kRemoteInstall);
206 bool PendingExtensionManager::AddFromExternalFile(
207 const std::string& id,
208 Manifest::Location install_source,
209 const Version& version,
210 int creation_flags,
211 bool mark_acknowledged) {
212 // TODO(skerner): AddFromSync() checks to see if the extension is
213 // installed, but this method assumes that the caller already
214 // made sure it is not installed. Make all AddFrom*() methods
215 // consistent.
216 const GURL& kUpdateUrl = GURL::EmptyGURL();
217 static const bool kIsFromSync = false;
218 static const bool kRemoteInstall = false;
220 return AddExtensionImpl(id,
221 std::string(),
222 kUpdateUrl,
223 version,
224 &AlwaysInstall,
225 kIsFromSync,
226 install_source,
227 creation_flags,
228 mark_acknowledged,
229 kRemoteInstall);
232 void PendingExtensionManager::GetPendingIdsForUpdateCheck(
233 std::list<std::string>* out_ids_for_update_check) const {
234 PendingExtensionList::const_iterator iter;
235 for (iter = pending_extension_list_.begin();
236 iter != pending_extension_list_.end();
237 ++iter) {
238 Manifest::Location install_source = iter->install_source();
240 // Some install sources read a CRX from the filesystem. They can
241 // not be fetched from an update URL, so don't include them in the
242 // set of ids.
243 if (install_source == Manifest::EXTERNAL_PREF ||
244 install_source == Manifest::EXTERNAL_REGISTRY ||
245 install_source == Manifest::EXTERNAL_POLICY) {
246 continue;
249 out_ids_for_update_check->push_back(iter->id());
253 bool PendingExtensionManager::AddExtensionImpl(
254 const std::string& id,
255 const std::string& install_parameter,
256 const GURL& update_url,
257 const Version& version,
258 PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install,
259 bool is_from_sync,
260 Manifest::Location install_source,
261 int creation_flags,
262 bool mark_acknowledged,
263 bool remote_install) {
264 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
266 PendingExtensionInfo info(id,
267 install_parameter,
268 update_url,
269 version,
270 should_allow_install,
271 is_from_sync,
272 install_source,
273 creation_flags,
274 mark_acknowledged,
275 remote_install);
277 if (const PendingExtensionInfo* pending = GetById(id)) {
278 // Bugs in this code will manifest as sporadic incorrect extension
279 // locations in situations where multiple install sources run at the
280 // same time. For example, on first login to a chrome os machine, an
281 // extension may be requested by sync and the default extension set.
282 // The following logging will help diagnose such issues.
283 VLOG(1) << "Extension id " << id
284 << " was entered for update more than once."
285 << " old location: " << pending->install_source()
286 << " new location: " << install_source
287 << " old version: " << GetVersionString(pending->version())
288 << " new version: " << GetVersionString(version);
290 // Never override an existing extension with an older version. Only
291 // extensions from local CRX files have a known version; extensions from an
292 // update URL will get the latest version.
294 // If |pending| has the same or higher precedence than |info| then don't
295 // install |info| over |pending|.
296 if (pending->CompareTo(info) >= 0)
297 return false;
299 VLOG(1) << "Overwrite existing record.";
301 std::replace(pending_extension_list_.begin(),
302 pending_extension_list_.end(),
303 *pending,
304 info);
305 } else {
306 pending_extension_list_.push_back(info);
309 return true;
312 void PendingExtensionManager::AddForTesting(
313 const PendingExtensionInfo& pending_extension_info) {
314 pending_extension_list_.push_back(pending_extension_info);
317 } // namespace extensions