blob: e656eff4fca2e9d4bf537ee90c2af9fb7a9afb38 [file] [log] [blame]
[email protected]4557d222012-03-04 23:33:361// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]90310d92011-04-17 07:35:042// 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
[email protected]418e953e2011-04-27 21:30:227#include "base/logging.h"
[email protected]3bdba0d2011-08-23 07:17:308#include "chrome/browser/extensions/extension_service.h"
9#include "chrome/browser/sync/protocol/app_specifics.pb.h"
10#include "chrome/browser/sync/protocol/extension_specifics.pb.h"
[email protected]4557d222012-03-04 23:33:3611#include "chrome/browser/sync/protocol/sync.pb.h"
[email protected]418e953e2011-04-27 21:30:2212
[email protected]90310d92011-04-17 07:35:0413ExtensionSyncData::ExtensionSyncData()
[email protected]3bdba0d2011-08-23 07:17:3014 : uninstalled_(false),
15 enabled_(false),
16 incognito_enabled_(false),
[email protected]aa7599d2011-10-28 07:24:3217 type_(Extension::SYNC_TYPE_NONE),
[email protected]aa7599d2011-10-28 07:24:3218 notifications_disabled_(false) {
[email protected]3bdba0d2011-08-23 07:17:3019}
20
21ExtensionSyncData::ExtensionSyncData(const SyncData& sync_data)
22 : uninstalled_(false),
23 enabled_(false),
24 incognito_enabled_(false),
[email protected]aa7599d2011-10-28 07:24:3225 type_(Extension::SYNC_TYPE_NONE),
[email protected]aa7599d2011-10-28 07:24:3226 notifications_disabled_(false) {
[email protected]3bdba0d2011-08-23 07:17:3027 PopulateFromSyncData(sync_data);
28}
29
30ExtensionSyncData::ExtensionSyncData(const SyncChange& sync_change)
[email protected]b10a4abe2011-12-10 02:31:2331 : uninstalled_(sync_change.change_type() == SyncChange::ACTION_DELETE),
32 enabled_(false),
33 incognito_enabled_(false),
34 type_(Extension::SYNC_TYPE_NONE),
35 notifications_disabled_(false) {
[email protected]3bdba0d2011-08-23 07:17:3036 PopulateFromSyncData(sync_change.sync_data());
37}
38
39ExtensionSyncData::ExtensionSyncData(const Extension& extension,
40 bool enabled,
[email protected]aa7599d2011-10-28 07:24:3241 bool incognito_enabled,
[email protected]b2689a902011-12-01 00:41:0942 const std::string& notifications_client_id,
[email protected]168389f2011-12-20 17:12:4843 bool notifications_disabled,
44 const StringOrdinal& app_launch_ordinal,
45 const StringOrdinal& page_ordinal)
[email protected]3bdba0d2011-08-23 07:17:3046 : id_(extension.id()),
47 uninstalled_(false),
48 enabled_(enabled),
49 incognito_enabled_(incognito_enabled),
50 type_(extension.GetSyncType()),
51 version_(*extension.version()),
52 update_url_(extension.update_url()),
[email protected]aa7599d2011-10-28 07:24:3253 name_(extension.name()),
[email protected]b2689a902011-12-01 00:41:0954 notifications_client_id_(notifications_client_id),
[email protected]168389f2011-12-20 17:12:4855 notifications_disabled_(notifications_disabled),
56 app_launch_ordinal_(app_launch_ordinal),
57 page_ordinal_(page_ordinal) {
[email protected]3bdba0d2011-08-23 07:17:3058}
[email protected]90310d92011-04-17 07:35:0459
60ExtensionSyncData::~ExtensionSyncData() {}
[email protected]418e953e2011-04-27 21:30:2261
[email protected]aa7599d2011-10-28 07:24:3262void ExtensionSyncData::PopulateAppSpecifics(
63 sync_pb::AppSpecifics* specifics) const {
64 DCHECK(specifics);
65 sync_pb::AppNotificationSettings* notif_settings =
66 specifics->mutable_notification_settings();
[email protected]b2689a902011-12-01 00:41:0967 if (!notifications_client_id_.empty())
68 notif_settings->set_oauth_client_id(notifications_client_id_);
[email protected]aa7599d2011-10-28 07:24:3269 notif_settings->set_disabled(notifications_disabled_);
[email protected]168389f2011-12-20 17:12:4870
71 // Only sync the ordinal values if they are valid.
72 if (app_launch_ordinal_.IsValid())
73 specifics->set_app_launch_ordinal(app_launch_ordinal_.ToString());
74 if (page_ordinal_.IsValid())
75 specifics->set_page_ordinal(page_ordinal_.ToString());
76
77 PopulateExtensionSpecifics(specifics->mutable_extension());
[email protected]aa7599d2011-10-28 07:24:3278}
79
[email protected]168389f2011-12-20 17:12:4880void ExtensionSyncData::PopulateExtensionSpecifics(
[email protected]3bdba0d2011-08-23 07:17:3081 sync_pb::ExtensionSpecifics* specifics) const {
82 DCHECK(Extension::IdIsValid(id_));
83 specifics->set_id(id_);
84 specifics->set_update_url(update_url_.spec());
85 specifics->set_version(version_.GetString());
86 specifics->set_enabled(enabled_);
87 specifics->set_incognito_enabled(incognito_enabled_);
88 specifics->set_name(name_);
[email protected]0fac519c2011-08-19 18:05:5789}
[email protected]3bdba0d2011-08-23 07:17:3090
91SyncData ExtensionSyncData::GetSyncData() const {
92 sync_pb::EntitySpecifics specifics;
[email protected]3bdba0d2011-08-23 07:17:3093
94 switch (type_) {
95 case Extension::SYNC_TYPE_EXTENSION:
[email protected]4557d222012-03-04 23:33:3696 PopulateExtensionSpecifics(specifics.mutable_extension());
[email protected]3bdba0d2011-08-23 07:17:3097 break;
98 case Extension::SYNC_TYPE_APP:
[email protected]4557d222012-03-04 23:33:3699 PopulateAppSpecifics(specifics.mutable_app());
[email protected]3bdba0d2011-08-23 07:17:30100 break;
101 default:
102 LOG(FATAL) << "Attempt to get non-syncable data.";
103 }
104
[email protected]3bdba0d2011-08-23 07:17:30105 return SyncData::CreateLocalData(id_, name_, specifics);
106}
107
108SyncChange ExtensionSyncData::GetSyncChange(
109 SyncChange::SyncChangeType change_type) const {
110 return SyncChange(change_type, GetSyncData());
111}
112
[email protected]168389f2011-12-20 17:12:48113void ExtensionSyncData::PopulateFromAppSpecifics(
114 const sync_pb::AppSpecifics& specifics) {
115 PopulateFromExtensionSpecifics(specifics.extension());
116
117 if (specifics.has_notification_settings() &&
118 specifics.notification_settings().has_oauth_client_id()) {
119 notifications_client_id_ =
120 specifics.notification_settings().oauth_client_id();
121 }
122
123 notifications_disabled_ =
124 specifics.has_notification_settings() &&
125 specifics.notification_settings().has_disabled() &&
126 specifics.notification_settings().disabled();
127
128 app_launch_ordinal_ = StringOrdinal(specifics.app_launch_ordinal());
129 page_ordinal_ = StringOrdinal(specifics.page_ordinal());
130}
131
[email protected]3bdba0d2011-08-23 07:17:30132void ExtensionSyncData::PopulateFromExtensionSpecifics(
133 const sync_pb::ExtensionSpecifics& specifics) {
134 if (!Extension::IdIsValid(specifics.id())) {
135 LOG(FATAL) << "Attempt to sync bad ExtensionSpecifics.";
136 }
137
[email protected]e1adb9a2011-09-09 17:42:52138 Version specifics_version(specifics.version());
139 if (!specifics_version.IsValid())
[email protected]3bdba0d2011-08-23 07:17:30140 LOG(FATAL) << "Attempt to sync bad ExtensionSpecifics.";
[email protected]3bdba0d2011-08-23 07:17:30141
142 // The update URL must be either empty or valid.
143 GURL specifics_update_url(specifics.update_url());
144 if (!specifics_update_url.is_empty() && !specifics_update_url.is_valid()) {
145 LOG(FATAL) << "Attempt to sync bad ExtensionSpecifics.";
146 }
147
148 id_ = specifics.id();
149 update_url_ = specifics_update_url;
[email protected]e1adb9a2011-09-09 17:42:52150 version_ = specifics_version;
[email protected]3bdba0d2011-08-23 07:17:30151 enabled_ = specifics.enabled();
152 incognito_enabled_ = specifics.incognito_enabled();
153 name_ = specifics.name();
154}
155
156void ExtensionSyncData::PopulateFromSyncData(const SyncData& sync_data) {
157 const sync_pb::EntitySpecifics& entity_specifics = sync_data.GetSpecifics();
[email protected]168389f2011-12-20 17:12:48158
[email protected]4557d222012-03-04 23:33:36159 if (entity_specifics.has_extension()) {
160 PopulateFromExtensionSpecifics(entity_specifics.extension());
[email protected]3bdba0d2011-08-23 07:17:30161 type_ = Extension::SYNC_TYPE_EXTENSION;
[email protected]4557d222012-03-04 23:33:36162 } else if (entity_specifics.has_app()) {
163 PopulateFromAppSpecifics(entity_specifics.app());
[email protected]3bdba0d2011-08-23 07:17:30164 type_ = Extension::SYNC_TYPE_APP;
165 } else {
166 LOG(FATAL) << "Attempt to sync bad EntitySpecifics.";
167 }
[email protected]3bdba0d2011-08-23 07:17:30168}