[email protected] | ed7bbb5 | 2012-05-02 11:07:29 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [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] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_EXTENSIONS_EXTERNAL_PREF_LOADER_H_ |
6 | #define CHROME_BROWSER_EXTENSIONS_EXTERNAL_PREF_LOADER_H_ | ||||
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 7 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 8 | #include <memory> |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 9 | #include <string> |
10 | |||||
[email protected] | 1790275 | 2011-08-31 22:52:54 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 13 | #include "base/values.h" |
dpolukhin | a772ed6 | 2014-10-28 11:54:54 | [diff] [blame] | 14 | #include "chrome/browser/extensions/external_loader.h" |
dpolukhin | a772ed6 | 2014-10-28 11:54:54 | [diff] [blame] | 15 | |
dpolukhin | a772ed6 | 2014-10-28 11:54:54 | [diff] [blame] | 16 | class Profile; |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 17 | |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 18 | namespace extensions { |
19 | |||||
20 | // A specialization of the ExternalLoader that uses a json file to | ||||
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 21 | // look up which external extensions are registered. |
22 | // Instances of this class are expected to be created and destroyed on the UI | ||||
23 | // thread and they are expecting public method calls from the UI thread. | ||||
Istiaque Ahmed | ef99c8ea | 2017-09-06 21:19:57 | [diff] [blame] | 24 | class ExternalPrefLoader : public ExternalLoader { |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 25 | public: |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 26 | enum Options { |
27 | NONE = 0, | ||||
28 | |||||
29 | // Ensure that only root can force an external install by checking | ||||
30 | // that all components of the path to external extensions files are | ||||
31 | // owned by root and not writable by any non-root user. | ||||
dpolukhin | a772ed6 | 2014-10-28 11:54:54 | [diff] [blame] | 32 | ENSURE_PATH_CONTROLLED_BY_ADMIN = 1 << 0, |
33 | |||||
34 | // Delay external preference load. It delays default apps installation | ||||
35 | // to not overload the system on first time user login. | ||||
36 | DELAY_LOAD_UNTIL_PRIORITY_SYNC = 1 << 1, | ||||
[email protected] | 98820ac7 | 2019-01-24 19:47:16 | [diff] [blame] | 37 | |
38 | // Use profile user type filter to load extensions. | ||||
39 | USE_USER_TYPE_PROFILE_FILTER = 1 << 2, | ||||
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 40 | }; |
41 | |||||
[email protected] | ed7bbb5 | 2012-05-02 11:07:29 | [diff] [blame] | 42 | // |base_path_id| is the directory containing the external_extensions.json |
43 | // file or the standalone extension manifest files. Relative file paths to | ||||
dpolukhin | a772ed6 | 2014-10-28 11:54:54 | [diff] [blame] | 44 | // extension files are resolved relative to this path. |profile| is used to |
45 | // wait priority sync if DELAY_LOAD_UNTIL_PRIORITY_SYNC set. | ||||
[email protected] | 98820ac7 | 2019-01-24 19:47:16 | [diff] [blame] | 46 | // |options| is combination of |Options|. |
47 | ExternalPrefLoader(int base_path_id, int options, Profile* profile); | ||||
[email protected] | f0841cd | 2011-01-19 15:07:24 | [diff] [blame] | 48 | |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 49 | const base::FilePath GetBaseCrxFilePath() override; |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 50 | |
51 | protected: | ||||
dpolukhin | a772ed6 | 2014-10-28 11:54:54 | [diff] [blame] | 52 | ~ExternalPrefLoader() override; |
[email protected] | a456773 | 2013-07-25 21:01:20 | [diff] [blame] | 53 | |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 54 | void StartLoading() override; |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 55 | bool IsOptionSet(Options option) { |
56 | return (options_ & option) != 0; | ||||
57 | } | ||||
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 58 | |
[email protected] | ebb489f | 2013-09-19 22:08:52 | [diff] [blame] | 59 | // The resource id of the base path with the information about the json |
60 | // file containing which extensions to load. | ||||
61 | const int base_path_id_; | ||||
62 | |||||
[email protected] | 98820ac7 | 2019-01-24 19:47:16 | [diff] [blame] | 63 | const int options_; |
[email protected] | ebb489f | 2013-09-19 22:08:52 | [diff] [blame] | 64 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 65 | private: |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 66 | friend class base::RefCountedThreadSafe<ExternalLoader>; |
Istiaque Ahmed | 2a59ae0 | 2017-08-24 04:43:26 | [diff] [blame] | 67 | friend class ExternalTestingLoader; |
Istiaque Ahmed | ef99c8ea | 2017-09-06 21:19:57 | [diff] [blame] | 68 | friend class TestExternalPrefLoader; |
69 | |||||
70 | class PrioritySyncReadyWaiter; | ||||
Istiaque Ahmed | 2a59ae0 | 2017-08-24 04:43:26 | [diff] [blame] | 71 | |
72 | // Extracts extension information from a json file serialized by |serializer|. | ||||
73 | // |path| is only used for informational purposes (outputted when an error | ||||
74 | // occurs). An empty dictionary is returned in case of failure (e.g. invalid | ||||
75 | // path or json content). | ||||
76 | static std::unique_ptr<base::DictionaryValue> ExtractExtensionPrefs( | ||||
77 | base::ValueDeserializer* deserializer, | ||||
78 | const base::FilePath& path); | ||||
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 79 | |
dpolukhin | a772ed6 | 2014-10-28 11:54:54 | [diff] [blame] | 80 | // If priority sync ready posts LoadOnFileThread and return true. |
81 | bool PostLoadIfPrioritySyncReady(); | ||||
82 | |||||
ginkage | f66fae52 | 2015-03-03 16:38:16 | [diff] [blame] | 83 | // Post LoadOnFileThread and stop observing for sync service states. |
84 | void PostLoadAndRemoveObservers(); | ||||
85 | |||||
[email protected] | ed7bbb5 | 2012-05-02 11:07:29 | [diff] [blame] | 86 | // Actually searches for and loads candidate standalone extension preference |
87 | // files in the path corresponding to |base_path_id|. | ||||
88 | // Must be called on the file thread. | ||||
Istiaque Ahmed | ef99c8ea | 2017-09-06 21:19:57 | [diff] [blame] | 89 | // Note: Overridden in tests. |
90 | virtual void LoadOnFileThread(); | ||||
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 91 | |
[email protected] | ed7bbb5 | 2012-05-02 11:07:29 | [diff] [blame] | 92 | // Extracts the information contained in an external_extension.json file |
93 | // regarding which extensions to install. |prefs| will be modified to | ||||
94 | // receive the extracted extension information. | ||||
95 | // Must be called from the File thread. | ||||
[email protected] | cb1078de | 2013-12-23 20:04:22 | [diff] [blame] | 96 | void ReadExternalExtensionPrefFile(base::DictionaryValue* prefs); |
[email protected] | ed7bbb5 | 2012-05-02 11:07:29 | [diff] [blame] | 97 | |
98 | // Extracts the information contained in standalone external extension | ||||
99 | // json files (<extension id>.json) regarding what external extensions | ||||
100 | // to install. |prefs| will be modified to receive the extracted extension | ||||
101 | // information. | ||||
102 | // Must be called from the File thread. | ||||
[email protected] | cb1078de | 2013-12-23 20:04:22 | [diff] [blame] | 103 | void ReadStandaloneExtensionPrefFiles(base::DictionaryValue* prefs); |
[email protected] | ed7bbb5 | 2012-05-02 11:07:29 | [diff] [blame] | 104 | |
Istiaque Ahmed | ef99c8ea | 2017-09-06 21:19:57 | [diff] [blame] | 105 | void OnPrioritySyncReady(PrioritySyncReadyWaiter* waiter); |
106 | |||||
[email protected] | ed7bbb5 | 2012-05-02 11:07:29 | [diff] [blame] | 107 | // The path (coresponding to |base_path_id_| containing the json files |
108 | // describing which extensions to load. | ||||
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 109 | base::FilePath base_path_; |
[email protected] | f0841cd | 2011-01-19 15:07:24 | [diff] [blame] | 110 | |
dpolukhin | a772ed6 | 2014-10-28 11:54:54 | [diff] [blame] | 111 | // Profile that loads these external prefs. |
112 | // Needed for waiting for waiting priority sync. | ||||
113 | Profile* profile_; | ||||
114 | |||||
[email protected] | 07072a5d | 2019-02-12 02:39:03 | [diff] [blame] | 115 | // User type determined by |profile_|. Used to filter extensions. In some unit |
116 | // tests may not be set. | ||||
117 | const std::string user_type_; | ||||
118 | |||||
Istiaque Ahmed | ef99c8ea | 2017-09-06 21:19:57 | [diff] [blame] | 119 | std::vector<std::unique_ptr<PrioritySyncReadyWaiter>> pending_waiter_list_; |
dpolukhin | a772ed6 | 2014-10-28 11:54:54 | [diff] [blame] | 120 | |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 121 | DISALLOW_COPY_AND_ASSIGN(ExternalPrefLoader); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 122 | }; |
123 | |||||
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 124 | } // namespace extensions |
125 | |||||
126 | #endif // CHROME_BROWSER_EXTENSIONS_EXTERNAL_PREF_LOADER_H_ |