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/extension_sync_data.h"
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"
16 namespace extensions
{
18 ExtensionSyncData::ExtensionSyncData()
19 : uninstalled_(false),
21 incognito_enabled_(false) {
24 ExtensionSyncData::ExtensionSyncData(const syncer::SyncData
& sync_data
)
25 : uninstalled_(false),
27 incognito_enabled_(false) {
28 PopulateFromSyncData(sync_data
);
31 ExtensionSyncData::ExtensionSyncData(const syncer::SyncChange
& sync_change
)
33 sync_change
.change_type() == syncer::SyncChange::ACTION_DELETE
),
35 incognito_enabled_(false) {
36 PopulateFromSyncData(sync_change
.sync_data());
39 ExtensionSyncData::ExtensionSyncData(const Extension
& extension
,
41 bool incognito_enabled
)
42 : id_(extension
.id()),
45 incognito_enabled_(incognito_enabled
),
46 version_(*extension
.version()),
47 update_url_(ManifestURL::GetUpdateURL(&extension
)),
48 name_(extension
.non_localized_name()) {
51 ExtensionSyncData::~ExtensionSyncData() {}
53 syncer::SyncData
ExtensionSyncData::GetSyncData() const {
54 sync_pb::EntitySpecifics specifics
;
55 PopulateExtensionSpecifics(specifics
.mutable_extension());
57 return syncer::SyncData::CreateLocalData(id_
, name_
, specifics
);
60 syncer::SyncChange
ExtensionSyncData::GetSyncChange(
61 syncer::SyncChange::SyncChangeType change_type
) const {
62 return syncer::SyncChange(FROM_HERE
, change_type
, GetSyncData());
65 void ExtensionSyncData::PopulateExtensionSpecifics(
66 sync_pb::ExtensionSpecifics
* specifics
) const {
67 DCHECK(Extension::IdIsValid(id_
));
68 specifics
->set_id(id_
);
69 specifics
->set_update_url(update_url_
.spec());
70 specifics
->set_version(version_
.GetString());
71 specifics
->set_enabled(enabled_
);
72 specifics
->set_incognito_enabled(incognito_enabled_
);
73 specifics
->set_name(name_
);
76 void ExtensionSyncData::PopulateFromExtensionSpecifics(
77 const sync_pb::ExtensionSpecifics
& specifics
) {
78 if (!Extension::IdIsValid(specifics
.id())) {
79 LOG(FATAL
) << "Attempt to sync bad ExtensionSpecifics.";
82 Version
specifics_version(specifics
.version());
83 if (!specifics_version
.IsValid())
84 LOG(FATAL
) << "Attempt to sync bad ExtensionSpecifics.";
86 // The update URL must be either empty or valid.
87 GURL
specifics_update_url(specifics
.update_url());
88 if (!specifics_update_url
.is_empty() && !specifics_update_url
.is_valid()) {
89 LOG(FATAL
) << "Attempt to sync bad ExtensionSpecifics.";
93 update_url_
= specifics_update_url
;
94 version_
= specifics_version
;
95 enabled_
= specifics
.enabled();
96 incognito_enabled_
= specifics
.incognito_enabled();
97 name_
= specifics
.name();
100 void ExtensionSyncData::set_uninstalled(bool uninstalled
) {
101 uninstalled_
= uninstalled
;
104 void ExtensionSyncData::PopulateFromSyncData(
105 const syncer::SyncData
& sync_data
) {
106 const sync_pb::EntitySpecifics
& entity_specifics
= sync_data
.GetSpecifics();
108 if (entity_specifics
.has_extension()) {
109 PopulateFromExtensionSpecifics(entity_specifics
.extension());
111 LOG(FATAL
) << "Attempt to sync bad EntitySpecifics.";
115 } // namespace extensions