File: syncable_settings_storage.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 (425 lines) | stat: -rw-r--r-- 13,890 bytes parent folder | download | duplicates (2)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright (c) 2012 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/extensions/api/storage/syncable_settings_storage.h"

#include "base/strings/stringprintf.h"
#include "chrome/browser/extensions/api/storage/settings_sync_processor.h"
#include "chrome/browser/extensions/api/storage/settings_sync_util.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/api/storage/settings_namespace.h"
#include "sync/api/sync_data.h"
#include "sync/protocol/extension_setting_specifics.pb.h"

namespace extensions {

using content::BrowserThread;

SyncableSettingsStorage::SyncableSettingsStorage(
    const scoped_refptr<ObserverListThreadSafe<SettingsObserver> >&
        observers,
    const std::string& extension_id,
    ValueStore* delegate,
    syncer::ModelType sync_type,
    const syncer::SyncableService::StartSyncFlare& flare)
    : observers_(observers),
      extension_id_(extension_id),
      delegate_(delegate),
      sync_type_(sync_type),
      flare_(flare) {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
}

SyncableSettingsStorage::~SyncableSettingsStorage() {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
}

size_t SyncableSettingsStorage::GetBytesInUse(const std::string& key) {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  return delegate_->GetBytesInUse(key);
}

size_t SyncableSettingsStorage::GetBytesInUse(
    const std::vector<std::string>& keys) {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  return delegate_->GetBytesInUse(keys);
}

size_t SyncableSettingsStorage::GetBytesInUse() {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  return delegate_->GetBytesInUse();
}

ValueStore::ReadResult SyncableSettingsStorage::Get(
    const std::string& key) {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  return delegate_->Get(key);
}

ValueStore::ReadResult SyncableSettingsStorage::Get(
    const std::vector<std::string>& keys) {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  return delegate_->Get(keys);
}

ValueStore::ReadResult SyncableSettingsStorage::Get() {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  return delegate_->Get();
}

ValueStore::WriteResult SyncableSettingsStorage::Set(
    WriteOptions options, const std::string& key, const base::Value& value) {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  WriteResult result = delegate_->Set(options, key, value);
  if (result->HasError()) {
    return result.Pass();
  }
  SyncResultIfEnabled(result);
  return result.Pass();
}

ValueStore::WriteResult SyncableSettingsStorage::Set(
    WriteOptions options, const base::DictionaryValue& values) {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  WriteResult result = delegate_->Set(options, values);
  if (result->HasError()) {
    return result.Pass();
  }
  SyncResultIfEnabled(result);
  return result.Pass();
}

ValueStore::WriteResult SyncableSettingsStorage::Remove(
    const std::string& key) {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  WriteResult result = delegate_->Remove(key);
  if (result->HasError()) {
    return result.Pass();
  }
  SyncResultIfEnabled(result);
  return result.Pass();
}

ValueStore::WriteResult SyncableSettingsStorage::Remove(
    const std::vector<std::string>& keys) {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  WriteResult result = delegate_->Remove(keys);
  if (result->HasError()) {
    return result.Pass();
  }
  SyncResultIfEnabled(result);
  return result.Pass();
}

ValueStore::WriteResult SyncableSettingsStorage::Clear() {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  WriteResult result = delegate_->Clear();
  if (result->HasError()) {
    return result.Pass();
  }
  SyncResultIfEnabled(result);
  return result.Pass();
}

bool SyncableSettingsStorage::Restore() {
  // If we're syncing, stop - we don't want to push the deletion of any data.
  // At next startup, when we start up the sync service, we'll get back any
  // data which was stored intact on Sync.
  // TODO (rdevlin.cronin): Investigate if there's a way we can trigger
  // MergeDataAndStartSyncing() to immediately get back any data we can,
  // and continue syncing.
  StopSyncing();
  return delegate_->Restore();
}

bool SyncableSettingsStorage::RestoreKey(const std::string& key) {
  // If we're syncing, stop - we don't want to push the deletion of any data.
  // At next startup, when we start up the sync service, we'll get back any
  // data which was stored intact on Sync.
  // TODO (rdevlin.cronin): Investigate if there's a way we can trigger
  // MergeDataAndStartSyncing() to immediately get back any data we can,
  // and continue syncing.
  StopSyncing();
  return delegate_->RestoreKey(key);
}

void SyncableSettingsStorage::SyncResultIfEnabled(
    const ValueStore::WriteResult& result) {
  if (result->changes().empty())
    return;

  if (sync_processor_.get()) {
    syncer::SyncError error = sync_processor_->SendChanges(result->changes());
    if (error.IsSet())
      StopSyncing();
  } else {
    // Tell sync to try and start soon, because syncable changes to sync_type_
    // have started happening. This will cause sync to call us back
    // asynchronously via StartSyncing(...) as soon as possible.
    flare_.Run(sync_type_);
  }
}

// Sync-related methods.

syncer::SyncError SyncableSettingsStorage::StartSyncing(
    const base::DictionaryValue& sync_state,
    scoped_ptr<SettingsSyncProcessor> sync_processor) {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  DCHECK(!sync_processor_.get());

  sync_processor_ = sync_processor.Pass();
  sync_processor_->Init(sync_state);

  ReadResult maybe_settings = delegate_->Get();
  if (maybe_settings->HasError()) {
    return syncer::SyncError(
        FROM_HERE,
        syncer::SyncError::DATATYPE_ERROR,
        base::StringPrintf("Failed to get settings: %s",
            maybe_settings->error().message.c_str()),
        sync_processor_->type());
  }

  const base::DictionaryValue& settings = maybe_settings->settings();
  return sync_state.empty() ?
      SendLocalSettingsToSync(settings) :
      OverwriteLocalSettingsWithSync(sync_state, settings);
}

syncer::SyncError SyncableSettingsStorage::SendLocalSettingsToSync(
    const base::DictionaryValue& settings) {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);

  ValueStoreChangeList changes;
  for (base::DictionaryValue::Iterator i(settings); !i.IsAtEnd(); i.Advance()) {
    changes.push_back(ValueStoreChange(i.key(), NULL, i.value().DeepCopy()));
  }

  if (changes.empty())
    return syncer::SyncError();

  syncer::SyncError error = sync_processor_->SendChanges(changes);
  if (error.IsSet())
    StopSyncing();

  return error;
}

syncer::SyncError SyncableSettingsStorage::OverwriteLocalSettingsWithSync(
    const base::DictionaryValue& sync_state,
    const base::DictionaryValue& settings) {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  // Treat this as a list of changes to sync and use ProcessSyncChanges.
  // This gives notifications etc for free.
  scoped_ptr<base::DictionaryValue> new_sync_state(sync_state.DeepCopy());

  SettingSyncDataList changes;
  for (base::DictionaryValue::Iterator it(settings);
       !it.IsAtEnd(); it.Advance()) {
    scoped_ptr<base::Value> sync_value;
    if (new_sync_state->RemoveWithoutPathExpansion(it.key(), &sync_value)) {
      if (sync_value->Equals(&it.value())) {
        // Sync and local values are the same, no changes to send.
      } else {
        // Sync value is different, update local setting with new value.
        changes.push_back(
            SettingSyncData(
                syncer::SyncChange::ACTION_UPDATE,
                extension_id_,
                it.key(),
                sync_value.Pass()));
      }
    } else {
      // Not synced, delete local setting.
      changes.push_back(
          SettingSyncData(
              syncer::SyncChange::ACTION_DELETE,
              extension_id_,
              it.key(),
              scoped_ptr<base::Value>(new base::DictionaryValue())));
    }
  }

  // Add all new settings to local settings.
  while (!new_sync_state->empty()) {
    base::DictionaryValue::Iterator first_entry(*new_sync_state);
    std::string key = first_entry.key();
    scoped_ptr<base::Value> value;
    CHECK(new_sync_state->RemoveWithoutPathExpansion(key, &value));
    changes.push_back(
        SettingSyncData(
            syncer::SyncChange::ACTION_ADD,
            extension_id_,
            key,
            value.Pass()));
  }

  if (changes.empty())
    return syncer::SyncError();

  return ProcessSyncChanges(changes);
}

void SyncableSettingsStorage::StopSyncing() {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  sync_processor_.reset();
}

syncer::SyncError SyncableSettingsStorage::ProcessSyncChanges(
    const SettingSyncDataList& sync_changes) {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  DCHECK(!sync_changes.empty()) << "No sync changes for " << extension_id_;

  if (!sync_processor_.get()) {
    return syncer::SyncError(
        FROM_HERE,
        syncer::SyncError::DATATYPE_ERROR,
        std::string("Sync is inactive for ") + extension_id_,
        syncer::UNSPECIFIED);
  }

  std::vector<syncer::SyncError> errors;
  ValueStoreChangeList changes;

  for (SettingSyncDataList::const_iterator it = sync_changes.begin();
      it != sync_changes.end(); ++it) {
    DCHECK_EQ(extension_id_, it->extension_id());

    const std::string& key = it->key();
    const base::Value& value = it->value();

    scoped_ptr<base::Value> current_value;
    {
      ReadResult maybe_settings = Get(it->key());
      if (maybe_settings->HasError()) {
        errors.push_back(syncer::SyncError(
            FROM_HERE,
            syncer::SyncError::DATATYPE_ERROR,
            base::StringPrintf("Error getting current sync state for %s/%s: %s",
                extension_id_.c_str(), key.c_str(),
                maybe_settings->error().message.c_str()),
            sync_processor_->type()));
        continue;
      }
      maybe_settings->settings().RemoveWithoutPathExpansion(key,
                                                            &current_value);
    }

    syncer::SyncError error;

    switch (it->change_type()) {
      case syncer::SyncChange::ACTION_ADD:
        if (!current_value.get()) {
          error = OnSyncAdd(key, value.DeepCopy(), &changes);
        } else {
          // Already a value; hopefully a local change has beaten sync in a
          // race and it's not a bug, so pretend it's an update.
          LOG(WARNING) << "Got add from sync for existing setting " <<
              extension_id_ << "/" << key;
          error = OnSyncUpdate(
              key, current_value.release(), value.DeepCopy(), &changes);
        }
        break;

      case syncer::SyncChange::ACTION_UPDATE:
        if (current_value.get()) {
          error = OnSyncUpdate(
              key, current_value.release(), value.DeepCopy(), &changes);
        } else {
          // Similarly, pretend it's an add.
          LOG(WARNING) << "Got update from sync for nonexistent setting" <<
              extension_id_ << "/" << key;
          error = OnSyncAdd(key, value.DeepCopy(), &changes);
        }
        break;

      case syncer::SyncChange::ACTION_DELETE:
        if (current_value.get()) {
          error = OnSyncDelete(key, current_value.release(), &changes);
        } else {
          // Similarly, ignore it.
          LOG(WARNING) << "Got delete from sync for nonexistent setting " <<
              extension_id_ << "/" << key;
        }
        break;

      default:
        NOTREACHED();
    }

    if (error.IsSet()) {
      errors.push_back(error);
    }
  }

  sync_processor_->NotifyChanges(changes);

  observers_->Notify(
      &SettingsObserver::OnSettingsChanged,
      extension_id_,
      settings_namespace::SYNC,
      ValueStoreChange::ToJson(changes));

  // TODO(kalman): Something sensible with multiple errors.
  return errors.empty() ? syncer::SyncError() : errors[0];
}

syncer::SyncError SyncableSettingsStorage::OnSyncAdd(
    const std::string& key,
    base::Value* new_value,
    ValueStoreChangeList* changes) {
  DCHECK(new_value);
  WriteResult result = delegate_->Set(IGNORE_QUOTA, key, *new_value);
  if (result->HasError()) {
    return syncer::SyncError(
        FROM_HERE,
        syncer::SyncError::DATATYPE_ERROR,
        base::StringPrintf("Error pushing sync add to local settings: %s",
            result->error().message.c_str()),
        sync_processor_->type());
  }
  changes->push_back(ValueStoreChange(key, NULL, new_value));
  return syncer::SyncError();
}

syncer::SyncError SyncableSettingsStorage::OnSyncUpdate(
    const std::string& key,
    base::Value* old_value,
    base::Value* new_value,
    ValueStoreChangeList* changes) {
  DCHECK(old_value);
  DCHECK(new_value);
  WriteResult result = delegate_->Set(IGNORE_QUOTA, key, *new_value);
  if (result->HasError()) {
    return syncer::SyncError(
        FROM_HERE,
        syncer::SyncError::DATATYPE_ERROR,
        base::StringPrintf("Error pushing sync update to local settings: %s",
            result->error().message.c_str()),
        sync_processor_->type());
  }
  changes->push_back(ValueStoreChange(key, old_value, new_value));
  return syncer::SyncError();
}

syncer::SyncError SyncableSettingsStorage::OnSyncDelete(
    const std::string& key,
    base::Value* old_value,
    ValueStoreChangeList* changes) {
  DCHECK(old_value);
  WriteResult result = delegate_->Remove(key);
  if (result->HasError()) {
    return syncer::SyncError(
        FROM_HERE,
        syncer::SyncError::DATATYPE_ERROR,
        base::StringPrintf("Error pushing sync remove to local settings: %s",
            result->error().message.c_str()),
        sync_processor_->type());
  }
  changes->push_back(ValueStoreChange(key, old_value, NULL));
  return syncer::SyncError();
}

}  // namespace extensions