Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / external_pref_loader.h
blob7e0375806792e23bf85ef65b2d490e55817e7548
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_
8 #include <string>
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/prefs/pref_service_syncable_observer.h"
16 #include "chrome/browser/sync/profile_sync_service.h"
17 #include "components/sync_driver/sync_service_observer.h"
19 class PrefServiceSyncable;
20 class Profile;
22 namespace extensions {
24 // A specialization of the ExternalLoader that uses a json file to
25 // look up which external extensions are registered.
26 // Instances of this class are expected to be created and destroyed on the UI
27 // thread and they are expecting public method calls from the UI thread.
28 class ExternalPrefLoader : public ExternalLoader,
29 public PrefServiceSyncableObserver,
30 public sync_driver::SyncServiceObserver {
31 public:
32 enum Options {
33 NONE = 0,
35 // Ensure that only root can force an external install by checking
36 // that all components of the path to external extensions files are
37 // owned by root and not writable by any non-root user.
38 ENSURE_PATH_CONTROLLED_BY_ADMIN = 1 << 0,
40 // Delay external preference load. It delays default apps installation
41 // to not overload the system on first time user login.
42 DELAY_LOAD_UNTIL_PRIORITY_SYNC = 1 << 1,
45 // |base_path_id| is the directory containing the external_extensions.json
46 // file or the standalone extension manifest files. Relative file paths to
47 // extension files are resolved relative to this path. |profile| is used to
48 // wait priority sync if DELAY_LOAD_UNTIL_PRIORITY_SYNC set.
49 ExternalPrefLoader(int base_path_id, Options options, Profile* profile);
51 const base::FilePath GetBaseCrxFilePath() override;
53 protected:
54 ~ExternalPrefLoader() override;
56 void StartLoading() override;
57 bool IsOptionSet(Options option) {
58 return (options_ & option) != 0;
61 // The resource id of the base path with the information about the json
62 // file containing which extensions to load.
63 const int base_path_id_;
65 const Options options_;
67 private:
68 friend class base::RefCountedThreadSafe<ExternalLoader>;
70 // PrefServiceSyncableObserver:
71 void OnIsSyncingChanged() override;
73 // sync_driver::SyncServiceObserver
74 void OnStateChanged() override;
76 // If priority sync ready posts LoadOnFileThread and return true.
77 bool PostLoadIfPrioritySyncReady();
79 // Post LoadOnFileThread and stop observing for sync service states.
80 void PostLoadAndRemoveObservers();
82 // Actually searches for and loads candidate standalone extension preference
83 // files in the path corresponding to |base_path_id|.
84 // Must be called on the file thread.
85 void LoadOnFileThread();
87 // Extracts the information contained in an external_extension.json file
88 // regarding which extensions to install. |prefs| will be modified to
89 // receive the extracted extension information.
90 // Must be called from the File thread.
91 void ReadExternalExtensionPrefFile(base::DictionaryValue* prefs);
93 // Extracts the information contained in standalone external extension
94 // json files (<extension id>.json) regarding what external extensions
95 // to install. |prefs| will be modified to receive the extracted extension
96 // information.
97 // Must be called from the File thread.
98 void ReadStandaloneExtensionPrefFiles(base::DictionaryValue* prefs);
100 // The path (coresponding to |base_path_id_| containing the json files
101 // describing which extensions to load.
102 base::FilePath base_path_;
104 // Profile that loads these external prefs.
105 // Needed for waiting for waiting priority sync.
106 Profile* profile_;
108 // Used for registering observer for PrefServiceSyncable.
109 ScopedObserver<PrefServiceSyncable, PrefServiceSyncableObserver>
110 syncable_pref_observer_;
112 DISALLOW_COPY_AND_ASSIGN(ExternalPrefLoader);
115 // A simplified version of ExternalPrefLoader that loads the dictionary
116 // from json data specified in a string.
117 class ExternalTestingLoader : public ExternalLoader {
118 public:
119 ExternalTestingLoader(const std::string& json_data,
120 const base::FilePath& fake_base_path);
122 const base::FilePath GetBaseCrxFilePath() override;
124 protected:
125 void StartLoading() override;
127 private:
128 friend class base::RefCountedThreadSafe<ExternalLoader>;
130 ~ExternalTestingLoader() override;
132 base::FilePath fake_base_path_;
133 scoped_ptr<base::DictionaryValue> testing_prefs_;
135 DISALLOW_COPY_AND_ASSIGN(ExternalTestingLoader);
138 } // namespace extensions
140 #endif // CHROME_BROWSER_EXTENSIONS_EXTERNAL_PREF_LOADER_H_