blob: 2357993f3378eda45fe380c4bb3555130f464361 [file] [log] [blame]
[email protected]ed7bbb52012-05-02 11:07:291// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]8e4560b62011-01-14 10:09:142// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]5df038b2012-07-16 19:03:275#ifndef CHROME_BROWSER_EXTENSIONS_EXTERNAL_PREF_LOADER_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTERNAL_PREF_LOADER_H_
[email protected]8e4560b62011-01-14 10:09:147
[email protected]8e4560b62011-01-14 10:09:148#include <string>
9
[email protected]17902752011-08-31 22:52:5410#include "base/compiler_specific.h"
avia2f4804a2015-12-24 23:11:1311#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/scoped_ptr.h"
dpolukhina772ed62014-10-28 11:54:5413#include "base/scoped_observer.h"
[email protected]8e4560b62011-01-14 10:09:1414#include "base/values.h"
dpolukhina772ed62014-10-28 11:54:5415#include "chrome/browser/extensions/external_loader.h"
blundell7282b512015-11-09 07:21:1116#include "components/browser_sync/browser/profile_sync_service.h"
droger507e7e2f2015-04-03 18:56:2317#include "components/sync_driver/sync_service_observer.h"
sdefresne875d0782015-09-16 12:01:2818#include "components/syncable_prefs/pref_service_syncable_observer.h"
dpolukhina772ed62014-10-28 11:54:5419
dpolukhina772ed62014-10-28 11:54:5420class Profile;
[email protected]8e4560b62011-01-14 10:09:1421
sdefresne50c1e522015-09-18 09:47:5122namespace syncable_prefs {
23class PrefServiceSyncable;
24}
25
[email protected]5df038b2012-07-16 19:03:2726namespace extensions {
27
28// A specialization of the ExternalLoader that uses a json file to
[email protected]8e4560b62011-01-14 10:09:1429// look up which external extensions are registered.
30// Instances of this class are expected to be created and destroyed on the UI
31// thread and they are expecting public method calls from the UI thread.
dpolukhina772ed62014-10-28 11:54:5432class ExternalPrefLoader : public ExternalLoader,
sdefresne50c1e522015-09-18 09:47:5133 public syncable_prefs::PrefServiceSyncableObserver,
droger507e7e2f2015-04-03 18:56:2334 public sync_driver::SyncServiceObserver {
[email protected]8e4560b62011-01-14 10:09:1435 public:
[email protected]73e4c362011-09-22 14:47:1836 enum Options {
37 NONE = 0,
38
39 // Ensure that only root can force an external install by checking
40 // that all components of the path to external extensions files are
41 // owned by root and not writable by any non-root user.
dpolukhina772ed62014-10-28 11:54:5442 ENSURE_PATH_CONTROLLED_BY_ADMIN = 1 << 0,
43
44 // Delay external preference load. It delays default apps installation
45 // to not overload the system on first time user login.
46 DELAY_LOAD_UNTIL_PRIORITY_SYNC = 1 << 1,
[email protected]73e4c362011-09-22 14:47:1847 };
48
[email protected]ed7bbb52012-05-02 11:07:2949 // |base_path_id| is the directory containing the external_extensions.json
50 // file or the standalone extension manifest files. Relative file paths to
dpolukhina772ed62014-10-28 11:54:5451 // extension files are resolved relative to this path. |profile| is used to
52 // wait priority sync if DELAY_LOAD_UNTIL_PRIORITY_SYNC set.
53 ExternalPrefLoader(int base_path_id, Options options, Profile* profile);
[email protected]f0841cd2011-01-19 15:07:2454
dchengae36a4a2014-10-21 12:36:3655 const base::FilePath GetBaseCrxFilePath() override;
[email protected]8e4560b62011-01-14 10:09:1456
57 protected:
dpolukhina772ed62014-10-28 11:54:5458 ~ExternalPrefLoader() override;
[email protected]a4567732013-07-25 21:01:2059
dchengae36a4a2014-10-21 12:36:3660 void StartLoading() override;
[email protected]73e4c362011-09-22 14:47:1861 bool IsOptionSet(Options option) {
62 return (options_ & option) != 0;
63 }
[email protected]8e4560b62011-01-14 10:09:1464
[email protected]ebb489f2013-09-19 22:08:5265 // The resource id of the base path with the information about the json
66 // file containing which extensions to load.
67 const int base_path_id_;
68
69 const Options options_;
70
[email protected]8e4560b62011-01-14 10:09:1471 private:
[email protected]5df038b2012-07-16 19:03:2772 friend class base::RefCountedThreadSafe<ExternalLoader>;
[email protected]8e4560b62011-01-14 10:09:1473
sdefresne50c1e522015-09-18 09:47:5174 // syncable_prefs::PrefServiceSyncableObserver:
dpolukhina772ed62014-10-28 11:54:5475 void OnIsSyncingChanged() override;
76
droger507e7e2f2015-04-03 18:56:2377 // sync_driver::SyncServiceObserver
ginkagef66fae522015-03-03 16:38:1678 void OnStateChanged() override;
79
dpolukhina772ed62014-10-28 11:54:5480 // If priority sync ready posts LoadOnFileThread and return true.
81 bool PostLoadIfPrioritySyncReady();
82
ginkagef66fae522015-03-03 16:38:1683 // Post LoadOnFileThread and stop observing for sync service states.
84 void PostLoadAndRemoveObservers();
85
[email protected]ed7bbb52012-05-02 11:07:2986 // 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.
[email protected]8e4560b62011-01-14 10:09:1489 void LoadOnFileThread();
90
[email protected]ed7bbb52012-05-02 11:07:2991 // Extracts the information contained in an external_extension.json file
92 // regarding which extensions to install. |prefs| will be modified to
93 // receive the extracted extension information.
94 // Must be called from the File thread.
[email protected]cb1078de2013-12-23 20:04:2295 void ReadExternalExtensionPrefFile(base::DictionaryValue* prefs);
[email protected]ed7bbb52012-05-02 11:07:2996
97 // Extracts the information contained in standalone external extension
98 // json files (<extension id>.json) regarding what external extensions
99 // to install. |prefs| will be modified to receive the extracted extension
100 // information.
101 // Must be called from the File thread.
[email protected]cb1078de2013-12-23 20:04:22102 void ReadStandaloneExtensionPrefFiles(base::DictionaryValue* prefs);
[email protected]ed7bbb52012-05-02 11:07:29103
[email protected]ed7bbb52012-05-02 11:07:29104 // The path (coresponding to |base_path_id_| containing the json files
105 // describing which extensions to load.
[email protected]650b2d52013-02-10 03:41:45106 base::FilePath base_path_;
[email protected]f0841cd2011-01-19 15:07:24107
dpolukhina772ed62014-10-28 11:54:54108 // Profile that loads these external prefs.
109 // Needed for waiting for waiting priority sync.
110 Profile* profile_;
111
sdefresne50c1e522015-09-18 09:47:51112 // Used for registering observer for syncable_prefs::PrefServiceSyncable.
113 ScopedObserver<syncable_prefs::PrefServiceSyncable,
114 syncable_prefs::PrefServiceSyncableObserver>
dpolukhina772ed62014-10-28 11:54:54115 syncable_pref_observer_;
116
[email protected]5df038b2012-07-16 19:03:27117 DISALLOW_COPY_AND_ASSIGN(ExternalPrefLoader);
[email protected]8e4560b62011-01-14 10:09:14118};
119
[email protected]5df038b2012-07-16 19:03:27120// A simplified version of ExternalPrefLoader that loads the dictionary
[email protected]8e4560b62011-01-14 10:09:14121// from json data specified in a string.
[email protected]5df038b2012-07-16 19:03:27122class ExternalTestingLoader : public ExternalLoader {
[email protected]8e4560b62011-01-14 10:09:14123 public:
[email protected]5df038b2012-07-16 19:03:27124 ExternalTestingLoader(const std::string& json_data,
[email protected]650b2d52013-02-10 03:41:45125 const base::FilePath& fake_base_path);
[email protected]f0841cd2011-01-19 15:07:24126
dchengae36a4a2014-10-21 12:36:36127 const base::FilePath GetBaseCrxFilePath() override;
[email protected]8e4560b62011-01-14 10:09:14128
129 protected:
dchengae36a4a2014-10-21 12:36:36130 void StartLoading() override;
[email protected]8e4560b62011-01-14 10:09:14131
132 private:
[email protected]5df038b2012-07-16 19:03:27133 friend class base::RefCountedThreadSafe<ExternalLoader>;
[email protected]8e4560b62011-01-14 10:09:14134
dchengae36a4a2014-10-21 12:36:36135 ~ExternalTestingLoader() override;
[email protected]8e4560b62011-01-14 10:09:14136
[email protected]650b2d52013-02-10 03:41:45137 base::FilePath fake_base_path_;
[email protected]cb1078de2013-12-23 20:04:22138 scoped_ptr<base::DictionaryValue> testing_prefs_;
[email protected]8e4560b62011-01-14 10:09:14139
[email protected]5df038b2012-07-16 19:03:27140 DISALLOW_COPY_AND_ASSIGN(ExternalTestingLoader);
[email protected]8e4560b62011-01-14 10:09:14141};
142
[email protected]5df038b2012-07-16 19:03:27143} // namespace extensions
144
145#endif // CHROME_BROWSER_EXTENSIONS_EXTERNAL_PREF_LOADER_H_