15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#ifndef CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SHARED_SETTINGS_SERVICE_H_
6f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#define CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SHARED_SETTINGS_SERVICE_H_
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/callback.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/callback_list.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
11f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/supervised_user/supervised_users.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/core/keyed_service.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "sync/api/syncable_service.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class PrefService;
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace base {
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class DictionaryValue;
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class Value;
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace user_prefs {
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class PrefRegistrySyncable;
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
26f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// SupervisedUserSharedSettingsService syncs settings (as key-value pairs) that
27f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// can be modified both by a supervised user and their manager.
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// A supervised user can only modify their own settings, whereas a manager can
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// modify settings for all their supervised users.
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The shared settings are stored in the user preferences in a multi-level
32f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// dictionary. The first level is the SU ID (called "managed user ID" on the
33f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// server for historic reasons), the second level is the key for the setting,
34f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// and the third level is a dictionary with a "value" key for the value and an
35f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// "acknowledged" flag, which is used to wait for the Sync server to acknowledge
36f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// that it has seen a setting change (see SupervisedUserSharedSettingsUpdate for
37f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// how to use this).
38f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class SupervisedUserSharedSettingsService : public KeyedService,
39f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                            public syncer::SyncableService {
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Called whenever a setting changes (see Subscribe() below).
42f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef base::Callback<void(const std::string& /* su_id */,
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              const std::string& /* key */)> ChangeCallback;
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  typedef base::CallbackList<
45f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      void(const std::string& /* su_id */, const std::string& /* key */)>
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ChangeCallbackList;
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This constructor is public only for testing. Use
49f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // |SupervisedUserSharedSettingsServiceFactory::GetForProfile(...)| instead to
50f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // get an instance of this service in production code.
51f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  explicit SupervisedUserSharedSettingsService(PrefService* prefs);
52f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  virtual ~SupervisedUserSharedSettingsService();
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns the value for the given |key| and the supervised user identified by
55f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // |su_id|. If either the supervised user or the key does not exist, NULL is
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // returned. Note that if the profile that owns this service belongs to a
57f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // supervised user, callers will only see settings for their own |su_id|, i.e.
58f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // a non-matching |su_id| is treated as non-existent.
59f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  const base::Value* GetValue(const std::string& su_id, const std::string& key);
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Sets the value for the given |key| and the supervised user identified by
62f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // |su_id|. If the profile that owns this service belongs to a supervised
63f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // user, |su_id| must be their own.
64f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void SetValue(const std::string& su_id,
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                const std::string& key,
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                const base::Value& value);
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Subscribes to changes in the synced settings. The callback will be notified
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // whenever any setting for any supervised user is changed via Sync (but not
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // for local changes). Subscribers should filter the settings and users they
71f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // are interested in with the |su_id| and |key| parameters to the callback.
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<ChangeCallbackList::Subscription> Subscribe(
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const ChangeCallback& cb);
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Public for testing.
78f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void SetValueInternal(const std::string& su_id,
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        const std::string& key,
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        const base::Value& value,
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        bool acknowledged);
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Public for testing.
84f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  static syncer::SyncData CreateSyncDataForSetting(const std::string& su_id,
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                   const std::string& key,
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                   const base::Value& value,
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                   bool acknowledged);
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // KeyedService implementation:
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void Shutdown() OVERRIDE;
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // SyncableService implementation:
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual syncer::SyncMergeResult MergeDataAndStartSyncing(
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      syncer::ModelType type,
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const syncer::SyncDataList& initial_sync_data,
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<syncer::SyncErrorFactory> error_handler) OVERRIDE;
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void StopSyncing(syncer::ModelType type) OVERRIDE;
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      OVERRIDE;
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual syncer::SyncError ProcessSyncChanges(
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const tracked_objects::Location& from_here,
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const syncer::SyncChangeList& change_list) OVERRIDE;
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<syncer::SyncErrorFactory> error_handler_;
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ChangeCallbackList callbacks_;
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PrefService* prefs_;
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
114f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif  // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SHARED_SETTINGS_SERVICE_H_
115