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.
4
5 #include "chrome/browser/extensions/extension_sync_data.h"
6
7 #include "base/logging.h"
8 #include "chrome/browser/extensions/app_sync_data.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/common/extensions/manifest_url_handler.h"
11 #include "extensions/common/extension.h"
12 #include "sync/api/sync_data.h"
13 #include "sync/protocol/extension_specifics.pb.h"
14 #include "sync/protocol/sync.pb.h"
15
16 namespace extensions {
17
ExtensionSyncData()18 ExtensionSyncData::ExtensionSyncData()
19 : uninstalled_(false),
20 enabled_(false),
21 incognito_enabled_(false),
22 remote_install_(false) {
23 }
24
ExtensionSyncData(const syncer::SyncData & sync_data)25 ExtensionSyncData::ExtensionSyncData(const syncer::SyncData& sync_data)
26 : uninstalled_(false),
27 enabled_(false),
28 incognito_enabled_(false),
29 remote_install_(false) {
30 PopulateFromSyncData(sync_data);
31 }
32
ExtensionSyncData(const syncer::SyncChange & sync_change)33 ExtensionSyncData::ExtensionSyncData(const syncer::SyncChange& sync_change)
34 : uninstalled_(sync_change.change_type() ==
35 syncer::SyncChange::ACTION_DELETE),
36 enabled_(false),
37 incognito_enabled_(false),
38 remote_install_(false) {
39 PopulateFromSyncData(sync_change.sync_data());
40 }
41
ExtensionSyncData(const Extension & extension,bool enabled,bool incognito_enabled,bool remote_install)42 ExtensionSyncData::ExtensionSyncData(const Extension& extension,
43 bool enabled,
44 bool incognito_enabled,
45 bool remote_install)
46 : id_(extension.id()),
47 uninstalled_(false),
48 enabled_(enabled),
49 incognito_enabled_(incognito_enabled),
50 remote_install_(remote_install),
51 version_(extension.from_bookmark() ? base::Version("0")
52 : *extension.version()),
53 update_url_(ManifestURL::GetUpdateURL(&extension)),
54 name_(extension.non_localized_name()) {
55 }
56
~ExtensionSyncData()57 ExtensionSyncData::~ExtensionSyncData() {}
58
GetSyncData() const59 syncer::SyncData ExtensionSyncData::GetSyncData() const {
60 sync_pb::EntitySpecifics specifics;
61 PopulateExtensionSpecifics(specifics.mutable_extension());
62
63 return syncer::SyncData::CreateLocalData(id_, name_, specifics);
64 }
65
GetSyncChange(syncer::SyncChange::SyncChangeType change_type) const66 syncer::SyncChange ExtensionSyncData::GetSyncChange(
67 syncer::SyncChange::SyncChangeType change_type) const {
68 return syncer::SyncChange(FROM_HERE, change_type, GetSyncData());
69 }
70
PopulateExtensionSpecifics(sync_pb::ExtensionSpecifics * specifics) const71 void ExtensionSyncData::PopulateExtensionSpecifics(
72 sync_pb::ExtensionSpecifics* specifics) const {
73 DCHECK(Extension::IdIsValid(id_));
74 specifics->set_id(id_);
75 specifics->set_update_url(update_url_.spec());
76 specifics->set_version(version_.GetString());
77 specifics->set_enabled(enabled_);
78 specifics->set_incognito_enabled(incognito_enabled_);
79 specifics->set_remote_install(remote_install_);
80 specifics->set_name(name_);
81 }
82
PopulateFromExtensionSpecifics(const sync_pb::ExtensionSpecifics & specifics)83 void ExtensionSyncData::PopulateFromExtensionSpecifics(
84 const sync_pb::ExtensionSpecifics& specifics) {
85 if (!Extension::IdIsValid(specifics.id())) {
86 LOG(FATAL) << "Attempt to sync bad ExtensionSpecifics.";
87 }
88
89 Version specifics_version(specifics.version());
90 if (!specifics_version.IsValid())
91 LOG(FATAL) << "Attempt to sync bad ExtensionSpecifics.";
92
93 // The update URL must be either empty or valid.
94 GURL specifics_update_url(specifics.update_url());
95 if (!specifics_update_url.is_empty() && !specifics_update_url.is_valid()) {
96 LOG(FATAL) << "Attempt to sync bad ExtensionSpecifics.";
97 }
98
99 id_ = specifics.id();
100 update_url_ = specifics_update_url;
101 version_ = specifics_version;
102 enabled_ = specifics.enabled();
103 incognito_enabled_ = specifics.incognito_enabled();
104 remote_install_ = specifics.remote_install();
105 name_ = specifics.name();
106 }
107
set_uninstalled(bool uninstalled)108 void ExtensionSyncData::set_uninstalled(bool uninstalled) {
109 uninstalled_ = uninstalled;
110 }
111
PopulateFromSyncData(const syncer::SyncData & sync_data)112 void ExtensionSyncData::PopulateFromSyncData(
113 const syncer::SyncData& sync_data) {
114 const sync_pb::EntitySpecifics& entity_specifics = sync_data.GetSpecifics();
115
116 if (entity_specifics.has_extension()) {
117 PopulateFromExtensionSpecifics(entity_specifics.extension());
118 } else {
119 LOG(FATAL) << "Attempt to sync bad EntitySpecifics.";
120 }
121 }
122
123 } // namespace extensions
124