[email protected] | f3d3b38 | 2014-03-14 21:19:28 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f3d3b38 | 2014-03-14 21:19:28 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_EXTENSIONS_PENDING_EXTENSION_MANAGER_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_PENDING_EXTENSION_MANAGER_H_ |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 7 | |
[email protected] | 51a3bf8b | 2012-06-08 22:53:06 | [diff] [blame] | 8 | #include <list> |
asargent | 56282ab7 | 2016-09-09 16:58:03 | [diff] [blame] | 9 | #include <map> |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 10 | #include <string> |
| 11 | |
thestig | a0e18cd | 2015-09-25 04:58:36 | [diff] [blame] | 12 | #include "base/gtest_prod_util.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 13 | #include "base/macros.h" |
asargent | 56282ab7 | 2016-09-09 16:58:03 | [diff] [blame] | 14 | #include "base/time/time.h" |
[email protected] | f3d3b38 | 2014-03-14 21:19:28 | [diff] [blame] | 15 | #include "chrome/browser/extensions/pending_extension_info.h" |
asargent | 56282ab7 | 2016-09-09 16:58:03 | [diff] [blame] | 16 | #include "extensions/common/extension_id.h" |
[email protected] | d42c1115 | 2013-08-22 19:36:32 | [diff] [blame] | 17 | #include "extensions/common/manifest.h" |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 18 | |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 19 | class GURL; |
[email protected] | 1f04ef4 | 2013-04-22 07:35:50 | [diff] [blame] | 20 | |
| 21 | namespace base { |
[email protected] | e398785 | 2012-05-04 10:06:30 | [diff] [blame] | 22 | class Version; |
[email protected] | 1f04ef4 | 2013-04-22 07:35:50 | [diff] [blame] | 23 | } |
[email protected] | 42a0816 | 2012-03-16 18:09:11 | [diff] [blame] | 24 | |
[email protected] | 46f3e10 | 2014-03-25 01:22:45 | [diff] [blame] | 25 | namespace content { |
| 26 | class BrowserContext; |
| 27 | } |
| 28 | |
Devlin Cronin | eea1b7a | 2018-05-26 02:46:21 | [diff] [blame] | 29 | namespace extensions { |
[email protected] | 3f213ad | 2012-07-26 23:39:41 | [diff] [blame] | 30 | FORWARD_DECLARE_TEST(ExtensionServiceTest, |
| 31 | UpdatePendingExtensionAlreadyInstalled); |
| 32 | |
[email protected] | b52f8ca | 2013-11-28 08:25:22 | [diff] [blame] | 33 | class PendingExtensionManager; |
| 34 | |
[email protected] | 42a0816 | 2012-03-16 18:09:11 | [diff] [blame] | 35 | class ExtensionUpdaterTest; |
| 36 | void SetupPendingExtensionManagerForTest( |
| 37 | int count, const GURL& update_url, |
| 38 | PendingExtensionManager* pending_extension_manager); |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 39 | |
| 40 | // Class PendingExtensionManager manages the set of extensions which are |
[email protected] | 7576451 | 2011-12-19 19:54:28 | [diff] [blame] | 41 | // being installed or updated. In general, installation and updates take |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 42 | // time, because they involve downloading, unpacking, and installing. |
| 43 | // This class allows us to avoid race cases where multiple sources install |
| 44 | // the same extension. |
[email protected] | 6c9bedf | 2014-05-21 03:55:51 | [diff] [blame] | 45 | // The ExtensionService creates an instance of this class, and manages its |
| 46 | // lifetime. This class should only be used from the UI thread. |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 47 | class PendingExtensionManager { |
| 48 | public: |
[email protected] | 6c9bedf | 2014-05-21 03:55:51 | [diff] [blame] | 49 | explicit PendingExtensionManager(content::BrowserContext* context); |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 50 | ~PendingExtensionManager(); |
| 51 | |
| 52 | // TODO(skerner): Many of these methods can be private once code in |
| 53 | // ExtensionService is moved into methods of this class. |
| 54 | |
[email protected] | 51a3bf8b | 2012-06-08 22:53:06 | [diff] [blame] | 55 | // Remove extension with id |id| from the set of pending extensions. Returns |
| 56 | // true if such an extension was found and removed, false otherwise. |
| 57 | bool Remove(const std::string& id); |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 58 | |
[email protected] | 51a3bf8b | 2012-06-08 22:53:06 | [diff] [blame] | 59 | // Get the information for a pending extension. Returns a pointer to the |
| 60 | // pending extension with id |id|, or NULL if there is no such extension. |
| 61 | const PendingExtensionInfo* GetById(const std::string& id) const; |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 62 | |
| 63 | // Is |id| in the set of pending extensions? |
| 64 | bool IsIdPending(const std::string& id) const; |
| 65 | |
[email protected] | 9bd9a686 | 2012-11-29 09:24:22 | [diff] [blame] | 66 | // Returns true if there are any extensions pending. |
| 67 | bool HasPendingExtensions() const; |
| 68 | |
[email protected] | d31e19e | 2012-08-22 00:19:04 | [diff] [blame] | 69 | // Whether there is pending extension install from sync. |
| 70 | bool HasPendingExtensionFromSync() const; |
| 71 | |
Devlin Cronin | 2b3acda | 2019-05-30 19:11:37 | [diff] [blame] | 72 | // Whether there is a high-priority pending extension (one from either policy |
| 73 | // or an external component extension). |
| 74 | bool HasHighPriorityPendingExtension() const; |
Oleg Davydov | 6541a64f | 2019-04-17 13:17:33 | [diff] [blame] | 75 | |
asargent | 56282ab7 | 2016-09-09 16:58:03 | [diff] [blame] | 76 | // Notifies the manager that we are reinstalling the policy force-installed |
| 77 | // extension with |id| because we detected corruption in the current copy. |
| 78 | void ExpectPolicyReinstallForCorruption(const ExtensionId& id); |
| 79 | |
| 80 | // Are we expecting a reinstall of the extension with |id| due to corruption? |
| 81 | bool IsPolicyReinstallForCorruptionExpected(const ExtensionId& id) const; |
| 82 | |
lazyboy | 77214d3c | 2017-04-04 16:46:12 | [diff] [blame] | 83 | // Whether or not there are any corrupted policy extensions. |
| 84 | bool HasAnyPolicyReinstallForCorruption() const; |
| 85 | |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 86 | // Adds an extension in a pending state; the extension with the |
| 87 | // given info will be installed on the next auto-update cycle. |
[email protected] | 145a317b | 2011-04-12 16:03:46 | [diff] [blame] | 88 | // Return true if the extension was added. Will return false |
| 89 | // if the extension is pending from another source which overrides |
| 90 | // sync installs (such as a policy extension) or if the extension |
| 91 | // is already installed. |
treib | e960e28 | 2015-09-11 10:38:08 | [diff] [blame] | 92 | // After installation, the extension will be granted permissions iff |
| 93 | // |version| is valid and matches the actual installed version. |
[email protected] | 145a317b | 2011-04-12 16:03:46 | [diff] [blame] | 94 | bool AddFromSync( |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 95 | const std::string& id, |
| 96 | const GURL& update_url, |
treib | e960e28 | 2015-09-11 10:38:08 | [diff] [blame] | 97 | const base::Version& version, |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 98 | PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install, |
mamir | 192d788 | 2016-06-22 17:10:16 | [diff] [blame] | 99 | bool remote_install); |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 100 | |
[email protected] | 9f4e4f08 | 2013-06-21 07:11:19 | [diff] [blame] | 101 | // Adds an extension that was depended on by another extension. |
| 102 | bool AddFromExtensionImport( |
| 103 | const std::string& id, |
| 104 | const GURL& update_url, |
| 105 | PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install); |
| 106 | |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 107 | // Given an extension id and an update URL, schedule the extension |
| 108 | // to be fetched, installed, and activated. |
[email protected] | 9060d8b0 | 2012-01-13 02:14:30 | [diff] [blame] | 109 | bool AddFromExternalUpdateUrl(const std::string& id, |
[email protected] | d8fd0fd | 2014-03-24 13:16:06 | [diff] [blame] | 110 | const std::string& install_parameter, |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 111 | const GURL& update_url, |
[email protected] | 464213a | 2013-10-15 01:06:48 | [diff] [blame] | 112 | Manifest::Location location, |
| 113 | int creation_flags, |
| 114 | bool mark_acknowledged); |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 115 | |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 116 | // Add a pending extension record for an external CRX file. |
[email protected] | 9060d8b0 | 2012-01-13 02:14:30 | [diff] [blame] | 117 | // Return true if the CRX should be installed, false if an existing |
| 118 | // pending record overrides it. |
| 119 | bool AddFromExternalFile( |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 120 | const std::string& id, |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 121 | Manifest::Location location, |
[email protected] | 464213a | 2013-10-15 01:06:48 | [diff] [blame] | 122 | const base::Version& version, |
| 123 | int creation_flags, |
| 124 | bool mark_acknowledged); |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 125 | |
[email protected] | 51a3bf8b | 2012-06-08 22:53:06 | [diff] [blame] | 126 | // Get the list of pending IDs that should be installed from an update URL. |
[email protected] | 9060d8b0 | 2012-01-13 02:14:30 | [diff] [blame] | 127 | // Pending extensions that will be installed from local files will not be |
| 128 | // included in the set. |
| 129 | void GetPendingIdsForUpdateCheck( |
[email protected] | 51a3bf8b | 2012-06-08 22:53:06 | [diff] [blame] | 130 | std::list<std::string>* out_ids_for_update_check) const; |
[email protected] | 9060d8b0 | 2012-01-13 02:14:30 | [diff] [blame] | 131 | |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 132 | private: |
[email protected] | 51a3bf8b | 2012-06-08 22:53:06 | [diff] [blame] | 133 | typedef std::list<PendingExtensionInfo> PendingExtensionList; |
[email protected] | 9060d8b0 | 2012-01-13 02:14:30 | [diff] [blame] | 134 | |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 135 | // Assumes an extension with id |id| is not already installed. |
[email protected] | 145a317b | 2011-04-12 16:03:46 | [diff] [blame] | 136 | // Return true if the extension was added. |
| 137 | bool AddExtensionImpl( |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 138 | const std::string& id, |
[email protected] | d8fd0fd | 2014-03-24 13:16:06 | [diff] [blame] | 139 | const std::string& install_parameter, |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 140 | const GURL& update_url, |
[email protected] | 1f04ef4 | 2013-04-22 07:35:50 | [diff] [blame] | 141 | const base::Version& version, |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 142 | PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install, |
| 143 | bool is_from_sync, |
[email protected] | 464213a | 2013-10-15 01:06:48 | [diff] [blame] | 144 | Manifest::Location install_source, |
mamir | 0128d5a | 2016-07-15 20:55:48 | [diff] [blame] | 145 | int creation_flags, |
[email protected] | 21db9ef | 2014-05-16 02:06:27 | [diff] [blame] | 146 | bool mark_acknowledged, |
| 147 | bool remote_install); |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 148 | |
| 149 | // Add a pending extension record directly. Used for unit tests that need |
| 150 | // to set an inital state. Use friendship to allow the tests to call this |
| 151 | // method. |
[email protected] | 51a3bf8b | 2012-06-08 22:53:06 | [diff] [blame] | 152 | void AddForTesting(const PendingExtensionInfo& pending_extension_info); |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 153 | |
[email protected] | 46f3e10 | 2014-03-25 01:22:45 | [diff] [blame] | 154 | // The BrowserContext with which the manager is associated. |
| 155 | content::BrowserContext* context_; |
| 156 | |
[email protected] | 51a3bf8b | 2012-06-08 22:53:06 | [diff] [blame] | 157 | PendingExtensionList pending_extension_list_; |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 158 | |
asargent | 56282ab7 | 2016-09-09 16:58:03 | [diff] [blame] | 159 | // A set of policy force-installed extension ids that are being reinstalled |
| 160 | // due to corruption, mapped to the time we detected the corruption. |
| 161 | std::map<ExtensionId, base::TimeTicks> expected_policy_reinstalls_; |
| 162 | |
Devlin Cronin | eea1b7a | 2018-05-26 02:46:21 | [diff] [blame] | 163 | FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest, |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 164 | UpdatePendingExtensionAlreadyInstalled); |
[email protected] | 3f213ad | 2012-07-26 23:39:41 | [diff] [blame] | 165 | friend class ExtensionUpdaterTest; |
| 166 | friend void SetupPendingExtensionManagerForTest( |
[email protected] | b2907fd | 2011-03-25 16:43:37 | [diff] [blame] | 167 | int count, const GURL& update_url, |
| 168 | PendingExtensionManager* pending_extension_manager); |
| 169 | |
| 170 | DISALLOW_COPY_AND_ASSIGN(PendingExtensionManager); |
| 171 | }; |
| 172 | |
[email protected] | 3f213ad | 2012-07-26 23:39:41 | [diff] [blame] | 173 | } // namespace extensions |
| 174 | |
[email protected] | f3d3b38 | 2014-03-14 21:19:28 | [diff] [blame] | 175 | #endif // CHROME_BROWSER_EXTENSIONS_PENDING_EXTENSION_MANAGER_H_ |