diff options
author | skerner@chromium.org <skerner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-25 16:43:37 +0000 |
---|---|---|
committer | skerner@chromium.org <skerner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-25 16:43:37 +0000 |
commit | b2907fdad77e3bef53604f1b7a8370d92d79be3e (patch) | |
tree | 4377126f7da6d7197d877e06cab7c7eb86f35156 /chrome/browser/extensions/pending_extension_manager.cc | |
parent | 68bf41ae4642cc0b1ad6f9d5c4050acaf3d7d1d5 (diff) | |
download | chromium_src-b2907fdad77e3bef53604f1b7a8370d92d79be3e.zip chromium_src-b2907fdad77e3bef53604f1b7a8370d92d79be3e.tar.gz chromium_src-b2907fdad77e3bef53604f1b7a8370d92d79be3e.tar.bz2 |
Move ExtensionService code that manages pending extensions into its own class.
This change should have no impact on chrome's behavior.
Once all code paths that install an extension inform PendingExtensionManger that they are starting an install, we can correctly merge requests to install the same extension from multiple sources. This will remove two hacky checks to avoid install races we have seen on Chrome Os. It will also ensure that more hacks are not needed.
BUG=61000
TEST=ExtensionUpdaterTest.*:ExtensionServiceTest.*'
Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=79407
Review URL: http://codereview.chromium.org/6670055
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@79414 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/pending_extension_manager.cc')
-rw-r--r-- | chrome/browser/extensions/pending_extension_manager.cc | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/chrome/browser/extensions/pending_extension_manager.cc b/chrome/browser/extensions/pending_extension_manager.cc new file mode 100644 index 0000000..17290bf --- /dev/null +++ b/chrome/browser/extensions/pending_extension_manager.cc @@ -0,0 +1,184 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/logging.h" +#include "base/stl_util-inl.h" +#include "chrome/browser/extensions/extension_service.h" +#include "chrome/browser/extensions/pending_extension_manager.h" +#include "content/browser/browser_thread.h" + +namespace { + +// Install predicate used by AddFromDefaultAppList(). +bool IsApp(const Extension& extension) { + return extension.is_app(); +} + +// Install predicate used by AddFromExternalUpdateUrl(). +bool AlwaysInstall(const Extension& extension) { + return true; +} + +} // namespace + +PendingExtensionManager::PendingExtensionManager( + const ExtensionUpdateService& service) + : service_(service) { +} + +PendingExtensionManager::~PendingExtensionManager() {} + +bool PendingExtensionManager::GetById( + const std::string& id, + PendingExtensionInfo* out_pending_extension_info) const { + + PendingExtensionMap::const_iterator it = pending_extension_map_.find(id); + if (it != pending_extension_map_.end()) { + *out_pending_extension_info = it->second; + return true; + } + + return false; +} + +void PendingExtensionManager::Remove(const std::string& id) { + pending_extension_map_.erase(id); +} + +bool PendingExtensionManager::IsIdPending(const std::string& id) const { + return ContainsKey(pending_extension_map_, id); +} + +void PendingExtensionManager::AddFromSync( + const std::string& id, + const GURL& update_url, + PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install, + bool install_silently, + bool enable_on_install, + bool enable_incognito_on_install) { + CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + + if (service_.GetExtensionById(id, true)) { + LOG(DFATAL) << "Trying to add pending extension " << id + << " which already exists"; + return; + } + + AddExtensionImpl(id, update_url, should_allow_install, true, + install_silently, enable_on_install, + enable_incognito_on_install, + Extension::INTERNAL); +} + +void PendingExtensionManager::AddFromExternalUpdateUrl( + const std::string& id, const GURL& update_url, + Extension::Location location) { + CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + + const bool kIsFromSync = false; + const bool kInstallSilently = true; + const bool kEnableOnInstall = true; + const bool kEnableIncognitoOnInstall = false; + + if (service_.const_extension_prefs().IsExtensionKilled(id)) + return; + + if (service_.GetExtensionById(id, true)) { + LOG(DFATAL) << "Trying to add extension " << id + << " by external update, but it is already installed."; + return; + } + + AddExtensionImpl(id, update_url, &AlwaysInstall, + kIsFromSync, kInstallSilently, + kEnableOnInstall, kEnableIncognitoOnInstall, + location); +} + + +// TODO(akalin): Change DefaultAppList to DefaultExtensionList and +// remove the IsApp() check. +void PendingExtensionManager::AddFromDefaultAppList( + const std::string& id) { + CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + + const bool kIsFromSync = false; + const bool kInstallSilently = true; + const bool kEnableOnInstall = true; + const bool kEnableIncognitoOnInstall = true; + + // This can legitimately happen if the user manually installed one of the + // default apps before this code ran. + if (service_.GetExtensionById(id, true)) + return; + + AddExtensionImpl(id, GURL(), &IsApp, + kIsFromSync, kInstallSilently, + kEnableOnInstall, kEnableIncognitoOnInstall, + Extension::INTERNAL); +} + +void PendingExtensionManager::AddFromExternalFile( + const std::string& id, + Extension::Location location) { + + GURL kUpdateUrl = GURL(); + bool kIsFromSync = false; + bool kInstallSilently = true; + bool kEnableOnInstall = true; + bool kEnableIncognitoOnInstall = false; + + pending_extension_map_[id] = + PendingExtensionInfo(kUpdateUrl, + &AlwaysInstall, + kIsFromSync, + kInstallSilently, + kEnableOnInstall, + kEnableIncognitoOnInstall, + location); +} + +void PendingExtensionManager::AddExtensionImpl( + const std::string& id, const GURL& update_url, + PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install, + bool is_from_sync, bool install_silently, + bool enable_on_install, bool enable_incognito_on_install, + Extension::Location install_source) { + CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + + // If a non-sync update is pending, a sync request should not + // overwrite it. This is important for external extensions. + // If an external extension download is pending, and the user has + // the extension in their sync profile, the install should set the + // type to be external. An external extension should not be + // rejected if it fails the safty checks for a syncable extension. + // TODO(skerner): Work out other potential overlapping conditions. + // (crbug.com/61000) + + PendingExtensionInfo pending_extension_info; + bool has_pending_ext = GetById(id, &pending_extension_info); + if (has_pending_ext) { + VLOG(1) << "Extension id " << id + << " was entered for update more than once." + << " old is_from_sync = " << pending_extension_info.is_from_sync() + << " new is_from_sync = " << is_from_sync; + if (!pending_extension_info.is_from_sync() && is_from_sync) + return; + } + + pending_extension_map_[id] = + PendingExtensionInfo(update_url, + should_allow_install, + is_from_sync, + install_silently, + enable_on_install, + enable_incognito_on_install, + install_source); +} + +void PendingExtensionManager::AddForTesting( + const std::string& id, + const PendingExtensionInfo& pending_extension_info) { + pending_extension_map_[id] = pending_extension_info; +} |