1 // Copyright (c) 2012 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 #include "chrome/browser/extensions/api/storage/setting_sync_data.h"
7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h"
9 #include "sync/api/sync_data.h"
10 #include "sync/protocol/app_setting_specifics.pb.h"
11 #include "sync/protocol/extension_setting_specifics.pb.h"
12 #include "sync/protocol/sync.pb.h"
14 namespace extensions
{
16 SettingSyncData::SettingSyncData(const syncer::SyncChange
& sync_change
)
17 : change_type_(sync_change
.change_type()) {
18 ExtractSyncData(sync_change
.sync_data());
21 SettingSyncData::SettingSyncData(const syncer::SyncData
& sync_data
)
22 : change_type_(syncer::SyncChange::ACTION_INVALID
) {
23 ExtractSyncData(sync_data
);
26 SettingSyncData::SettingSyncData(syncer::SyncChange::SyncChangeType change_type
,
27 const std::string
& extension_id
,
28 const std::string
& key
,
29 scoped_ptr
<base::Value
> value
)
30 : change_type_(change_type
),
31 extension_id_(extension_id
),
33 value_(value
.Pass()) {
36 SettingSyncData::~SettingSyncData() {}
38 scoped_ptr
<base::Value
> SettingSyncData::PassValue() {
39 DCHECK(value_
) << "value has already been Pass()ed";
43 void SettingSyncData::ExtractSyncData(const syncer::SyncData
& sync_data
) {
44 sync_pb::EntitySpecifics specifics
= sync_data
.GetSpecifics();
45 // The specifics are exclusively either extension or app settings.
46 DCHECK_NE(specifics
.has_extension_setting(), specifics
.has_app_setting());
47 const sync_pb::ExtensionSettingSpecifics
& extension_specifics
=
48 specifics
.has_extension_setting()
49 ? specifics
.extension_setting()
50 : specifics
.app_setting().extension_setting();
52 extension_id_
= extension_specifics
.extension_id();
53 key_
= extension_specifics
.key();
54 value_
= base::JSONReader::Read(extension_specifics
.value());
57 LOG(WARNING
) << "Specifics for " << extension_id_
<< "/" << key_
58 << " had bad JSON for value: " << extension_specifics
.value();
59 value_
.reset(new base::DictionaryValue());
63 } // namespace extensions