1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTERNAL_PREF_LOADER_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTERNAL_PREF_LOADER_H_
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/scoped_observer.h"
13 #include "base/values.h"
14 #include "chrome/browser/extensions/external_loader.h"
15 #include "chrome/browser/sync/profile_sync_service.h"
16 #include "components/sync_driver/sync_service_observer.h"
17 #include "components/syncable_prefs/pref_service_syncable_observer.h"
21 namespace syncable_prefs
{
22 class PrefServiceSyncable
;
25 namespace extensions
{
27 // A specialization of the ExternalLoader that uses a json file to
28 // look up which external extensions are registered.
29 // Instances of this class are expected to be created and destroyed on the UI
30 // thread and they are expecting public method calls from the UI thread.
31 class ExternalPrefLoader
: public ExternalLoader
,
32 public syncable_prefs::PrefServiceSyncableObserver
,
33 public sync_driver::SyncServiceObserver
{
38 // Ensure that only root can force an external install by checking
39 // that all components of the path to external extensions files are
40 // owned by root and not writable by any non-root user.
41 ENSURE_PATH_CONTROLLED_BY_ADMIN
= 1 << 0,
43 // Delay external preference load. It delays default apps installation
44 // to not overload the system on first time user login.
45 DELAY_LOAD_UNTIL_PRIORITY_SYNC
= 1 << 1,
48 // |base_path_id| is the directory containing the external_extensions.json
49 // file or the standalone extension manifest files. Relative file paths to
50 // extension files are resolved relative to this path. |profile| is used to
51 // wait priority sync if DELAY_LOAD_UNTIL_PRIORITY_SYNC set.
52 ExternalPrefLoader(int base_path_id
, Options options
, Profile
* profile
);
54 const base::FilePath
GetBaseCrxFilePath() override
;
57 ~ExternalPrefLoader() override
;
59 void StartLoading() override
;
60 bool IsOptionSet(Options option
) {
61 return (options_
& option
) != 0;
64 // The resource id of the base path with the information about the json
65 // file containing which extensions to load.
66 const int base_path_id_
;
68 const Options options_
;
71 friend class base::RefCountedThreadSafe
<ExternalLoader
>;
73 // syncable_prefs::PrefServiceSyncableObserver:
74 void OnIsSyncingChanged() override
;
76 // sync_driver::SyncServiceObserver
77 void OnStateChanged() override
;
79 // If priority sync ready posts LoadOnFileThread and return true.
80 bool PostLoadIfPrioritySyncReady();
82 // Post LoadOnFileThread and stop observing for sync service states.
83 void PostLoadAndRemoveObservers();
85 // Actually searches for and loads candidate standalone extension preference
86 // files in the path corresponding to |base_path_id|.
87 // Must be called on the file thread.
88 void LoadOnFileThread();
90 // Extracts the information contained in an external_extension.json file
91 // regarding which extensions to install. |prefs| will be modified to
92 // receive the extracted extension information.
93 // Must be called from the File thread.
94 void ReadExternalExtensionPrefFile(base::DictionaryValue
* prefs
);
96 // Extracts the information contained in standalone external extension
97 // json files (<extension id>.json) regarding what external extensions
98 // to install. |prefs| will be modified to receive the extracted extension
100 // Must be called from the File thread.
101 void ReadStandaloneExtensionPrefFiles(base::DictionaryValue
* prefs
);
103 // The path (coresponding to |base_path_id_| containing the json files
104 // describing which extensions to load.
105 base::FilePath base_path_
;
107 // Profile that loads these external prefs.
108 // Needed for waiting for waiting priority sync.
111 // Used for registering observer for syncable_prefs::PrefServiceSyncable.
112 ScopedObserver
<syncable_prefs::PrefServiceSyncable
,
113 syncable_prefs::PrefServiceSyncableObserver
>
114 syncable_pref_observer_
;
116 DISALLOW_COPY_AND_ASSIGN(ExternalPrefLoader
);
119 // A simplified version of ExternalPrefLoader that loads the dictionary
120 // from json data specified in a string.
121 class ExternalTestingLoader
: public ExternalLoader
{
123 ExternalTestingLoader(const std::string
& json_data
,
124 const base::FilePath
& fake_base_path
);
126 const base::FilePath
GetBaseCrxFilePath() override
;
129 void StartLoading() override
;
132 friend class base::RefCountedThreadSafe
<ExternalLoader
>;
134 ~ExternalTestingLoader() override
;
136 base::FilePath fake_base_path_
;
137 scoped_ptr
<base::DictionaryValue
> testing_prefs_
;
139 DISALLOW_COPY_AND_ASSIGN(ExternalTestingLoader
);
142 } // namespace extensions
144 #endif // CHROME_BROWSER_EXTENSIONS_EXTERNAL_PREF_LOADER_H_