1 // Copyright 2014 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_SUPERVISED_USER_SUPERVISED_USER_WHITELIST_SERVICE_H_
6 #define CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_WHITELIST_SERVICE_H_
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/time/time.h"
16 #include "chrome/browser/supervised_user/supervised_users.h"
17 #include "sync/api/syncable_service.h"
20 class SupervisedUserSiteList
;
23 class DictionaryValue
;
27 namespace component_updater
{
28 class SupervisedUserWhitelistInstaller
;
31 namespace user_prefs
{
32 class PrefRegistrySyncable
;
36 class ManagedUserWhitelistSpecifics
;
39 class SupervisedUserWhitelistService
: public syncer::SyncableService
{
41 typedef base::Callback
<void(
42 const std::vector
<scoped_refptr
<SupervisedUserSiteList
> >&)>
43 SiteListsChangedCallback
;
45 SupervisedUserWhitelistService(
47 component_updater::SupervisedUserWhitelistInstaller
* installer
,
48 const std::string
& client_id
);
49 ~SupervisedUserWhitelistService() override
;
51 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
55 // Adds a callback to be called when the list of loaded site lists changes.
56 // The callback will also be called immediately, to get the current
58 void AddSiteListsChangedCallback(const SiteListsChangedCallback
& callback
);
60 // Loads an already existing whitelist on disk (i.e. without downloading it as
62 void LoadWhitelistForTesting(const std::string
& id
,
63 const base::FilePath
& path
);
65 // Unloads a whitelist. Public for testing.
66 void UnloadWhitelist(const std::string
& id
);
68 // Creates Sync data for a whitelist with the given |id| and |name|.
69 // Public for testing.
70 static syncer::SyncData
CreateWhitelistSyncData(const std::string
& id
,
71 const std::string
& name
);
73 // SyncableService implementation:
74 syncer::SyncMergeResult
MergeDataAndStartSyncing(
75 syncer::ModelType type
,
76 const syncer::SyncDataList
& initial_sync_data
,
77 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
,
78 scoped_ptr
<syncer::SyncErrorFactory
> error_handler
) override
;
79 void StopSyncing(syncer::ModelType type
) override
;
80 syncer::SyncDataList
GetAllSyncData(syncer::ModelType type
) const override
;
81 syncer::SyncError
ProcessSyncChanges(
82 const tracked_objects::Location
& from_here
,
83 const syncer::SyncChangeList
& change_list
) override
;
86 // The following methods handle whitelist additions, updates and removals,
87 // usually coming from Sync.
88 void AddNewWhitelist(base::DictionaryValue
* pref_dict
,
89 const sync_pb::ManagedUserWhitelistSpecifics
& whitelist
);
90 void SetWhitelistProperties(
91 base::DictionaryValue
* pref_dict
,
92 const sync_pb::ManagedUserWhitelistSpecifics
& whitelist
);
93 void RemoveWhitelist(base::DictionaryValue
* pref_dict
, const std::string
& id
);
95 // Registers a new or existing whitelist.
96 void RegisterWhitelist(const std::string
& id
,
97 const std::string
& name
,
98 bool new_installation
);
100 void GetLoadedWhitelists(
101 std::vector
<scoped_refptr
<SupervisedUserSiteList
>>* whitelists
);
103 void NotifyWhitelistsChanged();
105 void OnWhitelistReady(const std::string
& id
,
106 const base::FilePath
& whitelist_path
);
107 void OnWhitelistLoaded(
108 const std::string
& id
,
109 base::TimeTicks start_time
,
110 const scoped_refptr
<SupervisedUserSiteList
>& whitelist
);
113 component_updater::SupervisedUserWhitelistInstaller
* installer_
;
115 std::string client_id_
;
116 std::vector
<SiteListsChangedCallback
> site_lists_changed_callbacks_
;
118 // The set of registered whitelists. A whitelist might be registered but not
119 // loaded yet, in which case it will not be in |loaded_whitelists_| yet.
120 // On the other hand, every loaded whitelist has to be registered.
121 std::set
<std::string
> registered_whitelists_
;
122 std::map
<std::string
, scoped_refptr
<SupervisedUserSiteList
> >
125 base::WeakPtrFactory
<SupervisedUserWhitelistService
> weak_ptr_factory_
;
127 DISALLOW_COPY_AND_ASSIGN(SupervisedUserWhitelistService
);
130 #endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_WHITELIST_SERVICE_H_