File: supervised_user_shared_settings_service.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (347 lines) | stat: -rw-r--r-- 12,283 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_service.h"

#include <map>
#include <set>

#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/values.h"
#include "chrome/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "sync/api/sync_change.h"
#include "sync/api/sync_data.h"
#include "sync/api/sync_error.h"
#include "sync/api/sync_error_factory.h"
#include "sync/api/sync_merge_result.h"
#include "sync/protocol/sync.pb.h"

using base::DictionaryValue;
using base::Value;
using syncer::ModelType;
using syncer::SUPERVISED_USER_SHARED_SETTINGS;
using syncer::SyncChange;
using syncer::SyncChangeList;
using syncer::SyncChangeProcessor;
using syncer::SyncData;
using syncer::SyncDataList;
using syncer::SyncError;
using syncer::SyncErrorFactory;
using syncer::SyncMergeResult;

namespace {

const char kAcknowledged[] = "acknowledged";
const char kValue[] = "value";

DictionaryValue* FindOrCreateDictionary(DictionaryValue* parent,
                                        const std::string& key) {
  DictionaryValue* dict = NULL;
  if (!parent->GetDictionaryWithoutPathExpansion(key, &dict)) {
    dict = new DictionaryValue;
    parent->SetWithoutPathExpansion(key, dict);
  }
  return dict;
}

class ScopedSupervisedUserSharedSettingsUpdate {
 public:
  ScopedSupervisedUserSharedSettingsUpdate(PrefService* prefs,
                                           const std::string& su_id)
      : update_(prefs, prefs::kSupervisedUserSharedSettings), su_id_(su_id) {
    DCHECK(!su_id.empty());

    // A supervised user can only modify their own settings.
    std::string id = prefs->GetString(prefs::kSupervisedUserId);
    DCHECK(id.empty() || id == su_id);
  }

  DictionaryValue* Get() {
    return FindOrCreateDictionary(update_.Get(), su_id_);
  }

 private:
  DictionaryPrefUpdate update_;
  std::string su_id_;
};

SyncData CreateSyncDataForValue(
    const std::string& su_id,
    const std::string& key,
    const Value& dict_value) {
  const DictionaryValue* dict = NULL;
  if (!dict_value.GetAsDictionary(&dict))
    return SyncData();

  const Value* value = NULL;
  if (!dict->Get(kValue, &value))
    return SyncData();

  bool acknowledged = false;
  dict->GetBoolean(kAcknowledged, &acknowledged);

  return SupervisedUserSharedSettingsService::CreateSyncDataForSetting(
      su_id, key, *value, acknowledged);
}

}  // namespace


SupervisedUserSharedSettingsService::SupervisedUserSharedSettingsService(
    PrefService* prefs)
    : prefs_(prefs) {}

SupervisedUserSharedSettingsService::~SupervisedUserSharedSettingsService() {}

void SupervisedUserSharedSettingsService::SetValueInternal(
    const std::string& su_id,
    const std::string& key,
    const Value& value,
    bool acknowledged) {
  ScopedSupervisedUserSharedSettingsUpdate update(prefs_, su_id);
  DictionaryValue* update_dict = update.Get();

  DictionaryValue* dict = NULL;
  bool has_key = update_dict->GetDictionaryWithoutPathExpansion(key, &dict);
  if (!has_key) {
    dict = new DictionaryValue;
    update_dict->SetWithoutPathExpansion(key, dict);
  }
  dict->SetWithoutPathExpansion(kValue, value.DeepCopy());
  dict->SetBooleanWithoutPathExpansion(kAcknowledged, acknowledged);

  if (!sync_processor_)
    return;

  SyncData data = CreateSyncDataForSetting(su_id, key, value, acknowledged);
  SyncChange::SyncChangeType change_type =
      has_key ? SyncChange::ACTION_UPDATE : SyncChange::ACTION_ADD;
  SyncChangeList changes;
  changes.push_back(SyncChange(FROM_HERE, change_type, data));
  SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, changes);
  DCHECK(!error.IsSet()) << error.ToString();
}

const Value* SupervisedUserSharedSettingsService::GetValue(
    const std::string& su_id,
    const std::string& key) {
  const DictionaryValue* data =
      prefs_->GetDictionary(prefs::kSupervisedUserSharedSettings);
  const DictionaryValue* dict = NULL;
  if (!data->GetDictionaryWithoutPathExpansion(su_id, &dict))
    return NULL;

  const DictionaryValue* settings = NULL;
  if (!dict->GetDictionaryWithoutPathExpansion(key, &settings))
    return NULL;

  const Value* value = NULL;
  if (!settings->GetWithoutPathExpansion(kValue, &value))
    return NULL;

  return value;
}

void SupervisedUserSharedSettingsService::SetValue(
    const std::string& su_id,
    const std::string& key,
    const Value& value) {
  SetValueInternal(su_id, key, value, true);
}

scoped_ptr<
    SupervisedUserSharedSettingsService::ChangeCallbackList::Subscription>
SupervisedUserSharedSettingsService::Subscribe(
    const SupervisedUserSharedSettingsService::ChangeCallback& cb) {
  return callbacks_.Add(cb);
}

// static
void SupervisedUserSharedSettingsService::RegisterProfilePrefs(
    user_prefs::PrefRegistrySyncable* registry) {
  registry->RegisterDictionaryPref(
      prefs::kSupervisedUserSharedSettings,
      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}

// static
SyncData SupervisedUserSharedSettingsService::CreateSyncDataForSetting(
    const std::string& su_id,
    const std::string& key,
    const Value& value,
    bool acknowledged) {
  std::string json_value;
  base::JSONWriter::Write(&value, &json_value);
  ::sync_pb::EntitySpecifics specifics;
  specifics.mutable_managed_user_shared_setting()->set_mu_id(su_id);
  specifics.mutable_managed_user_shared_setting()->set_key(key);
  specifics.mutable_managed_user_shared_setting()->set_value(json_value);
  specifics.mutable_managed_user_shared_setting()->set_acknowledged(
      acknowledged);
  std::string title = su_id + ":" + key;
  return SyncData::CreateLocalData(title, title, specifics);
}

void SupervisedUserSharedSettingsService::Shutdown() {}

syncer::SyncMergeResult
SupervisedUserSharedSettingsService::MergeDataAndStartSyncing(
    syncer::ModelType type,
    const syncer::SyncDataList& initial_sync_data,
    scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
    scoped_ptr<syncer::SyncErrorFactory> error_handler) {
  DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, type);
  sync_processor_ = sync_processor.Pass();
  error_handler_ = error_handler.Pass();

  // We keep a map from MU ID to the set of keys that we have seen in the
  // initial sync data.
  std::map<std::string, std::set<std::string> > seen_keys;

  // Iterate over all initial sync data, and update it locally. This means that
  // the value from the server always wins over a local value.
  for (const SyncData& sync_data : initial_sync_data) {
    DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, sync_data.GetDataType());
    const ::sync_pb::ManagedUserSharedSettingSpecifics&
        supervised_user_shared_setting =
            sync_data.GetSpecifics().managed_user_shared_setting();
    scoped_ptr<Value> value(
        base::JSONReader::Read(supervised_user_shared_setting.value()));
    const std::string& su_id = supervised_user_shared_setting.mu_id();
    ScopedSupervisedUserSharedSettingsUpdate update(prefs_, su_id);
    const std::string& key = supervised_user_shared_setting.key();
    DictionaryValue* dict = FindOrCreateDictionary(update.Get(), key);
    dict->SetWithoutPathExpansion(kValue, value.release());

    // Every setting we get from the server should have the acknowledged flag
    // set.
    DCHECK(supervised_user_shared_setting.acknowledged());
    dict->SetBooleanWithoutPathExpansion(
        kAcknowledged, supervised_user_shared_setting.acknowledged());
    callbacks_.Notify(su_id, key);

    seen_keys[su_id].insert(key);
  }

  // Iterate over all settings that we have locally, which includes settings
  // that were just synced down. We filter those out using |seen_keys|.
  SyncChangeList change_list;
  const DictionaryValue* all_settings =
      prefs_->GetDictionary(prefs::kSupervisedUserSharedSettings);
  for (DictionaryValue::Iterator it(*all_settings); !it.IsAtEnd();
       it.Advance()) {
    const DictionaryValue* dict = NULL;
    bool success = it.value().GetAsDictionary(&dict);
    DCHECK(success);

    const std::set<std::string>& seen = seen_keys[it.key()];
    for (DictionaryValue::Iterator jt(*dict); !jt.IsAtEnd(); jt.Advance()) {
      // We only need to upload settings that we haven't seen in the initial
      // sync data (which means they were added locally).
      if (seen.count(jt.key()) > 0)
        continue;

      SyncData data = CreateSyncDataForValue(it.key(), jt.key(), jt.value());
      DCHECK(data.IsValid());
      change_list.push_back(
          SyncChange(FROM_HERE, SyncChange::ACTION_ADD, data));
    }
  }

  SyncMergeResult result(SUPERVISED_USER_SHARED_SETTINGS);
  // Process all the accumulated changes.
  if (change_list.size() > 0) {
    result.set_error(
        sync_processor_->ProcessSyncChanges(FROM_HERE, change_list));
  }

  // TODO(bauerb): Statistics?
  return result;
}

void SupervisedUserSharedSettingsService::StopSyncing(syncer::ModelType type) {
  DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, type);
  sync_processor_.reset();
  error_handler_.reset();
}

syncer::SyncDataList SupervisedUserSharedSettingsService::GetAllSyncData(
    syncer::ModelType type) const {
  DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, type);
  SyncDataList data;
  const DictionaryValue* all_settings =
      prefs_->GetDictionary(prefs::kSupervisedUserSharedSettings);
  for (DictionaryValue::Iterator it(*all_settings); !it.IsAtEnd();
       it.Advance()) {
    const DictionaryValue* dict = NULL;
    bool success = it.value().GetAsDictionary(&dict);
    DCHECK(success);
    for (DictionaryValue::Iterator jt(*dict); !jt.IsAtEnd(); jt.Advance()) {
      data.push_back(CreateSyncDataForValue(it.key(), jt.key(), jt.value()));
    }
  }
  return data;
}

syncer::SyncError SupervisedUserSharedSettingsService::ProcessSyncChanges(
    const tracked_objects::Location& from_here,
    const syncer::SyncChangeList& change_list) {
  for (const SyncChange& sync_change : change_list) {
    SyncData data = sync_change.sync_data();
    DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, data.GetDataType());
    const ::sync_pb::ManagedUserSharedSettingSpecifics&
        supervised_user_shared_setting =
            data.GetSpecifics().managed_user_shared_setting();
    const std::string& key = supervised_user_shared_setting.key();
    const std::string& su_id = supervised_user_shared_setting.mu_id();
    ScopedSupervisedUserSharedSettingsUpdate update(prefs_, su_id);
    DictionaryValue* update_dict = update.Get();
    DictionaryValue* dict = NULL;
    bool has_key = update_dict->GetDictionaryWithoutPathExpansion(key, &dict);
    switch (sync_change.change_type()) {
      case SyncChange::ACTION_ADD:
      case SyncChange::ACTION_UPDATE: {
        // Every setting we get from the server should have the acknowledged
        // flag set.
        DCHECK(supervised_user_shared_setting.acknowledged());

        if (has_key) {
          // If the supervised user already exists, it should be an update
          // action.
          DCHECK_EQ(SyncChange::ACTION_UPDATE, sync_change.change_type());
        } else {
          // Otherwise, it should be an add action.
          DCHECK_EQ(SyncChange::ACTION_ADD, sync_change.change_type());
          dict = new DictionaryValue;
          update_dict->SetWithoutPathExpansion(key, dict);
        }
        scoped_ptr<Value> value(
            base::JSONReader::Read(supervised_user_shared_setting.value()));
        dict->SetWithoutPathExpansion(kValue, value.release());
        dict->SetBooleanWithoutPathExpansion(
            kAcknowledged, supervised_user_shared_setting.acknowledged());
        break;
      }
      case SyncChange::ACTION_DELETE: {
        if (has_key)
          update_dict->RemoveWithoutPathExpansion(key, NULL);
        else
          NOTREACHED() << "Trying to delete nonexistent key " << key;
        break;
      }
      case SyncChange::ACTION_INVALID: {
        NOTREACHED();
        break;
      }
    }
    callbacks_.Notify(su_id, key);
  }

  SyncError error;
  return error;
}