[email protected] | 4557d22 | 2012-03-04 23:33:36 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 90310d9 | 2011-04-17 07:35:04 | [diff] [blame] | 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 | |
[email protected] | 418e953e | 2011-04-27 21:30:22 | [diff] [blame] | 7 | #include "base/logging.h" |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 8 | #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] | 4557d22 | 2012-03-04 23:33:36 | [diff] [blame^] | 11 | #include "chrome/browser/sync/protocol/sync.pb.h" |
[email protected] | 418e953e | 2011-04-27 21:30:22 | [diff] [blame] | 12 | |
[email protected] | 90310d9 | 2011-04-17 07:35:04 | [diff] [blame] | 13 | ExtensionSyncData::ExtensionSyncData() |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 14 | : uninstalled_(false), |
| 15 | enabled_(false), |
| 16 | incognito_enabled_(false), |
[email protected] | aa7599d | 2011-10-28 07:24:32 | [diff] [blame] | 17 | type_(Extension::SYNC_TYPE_NONE), |
[email protected] | aa7599d | 2011-10-28 07:24:32 | [diff] [blame] | 18 | notifications_disabled_(false) { |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | ExtensionSyncData::ExtensionSyncData(const SyncData& sync_data) |
| 22 | : uninstalled_(false), |
| 23 | enabled_(false), |
| 24 | incognito_enabled_(false), |
[email protected] | aa7599d | 2011-10-28 07:24:32 | [diff] [blame] | 25 | type_(Extension::SYNC_TYPE_NONE), |
[email protected] | aa7599d | 2011-10-28 07:24:32 | [diff] [blame] | 26 | notifications_disabled_(false) { |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 27 | PopulateFromSyncData(sync_data); |
| 28 | } |
| 29 | |
| 30 | ExtensionSyncData::ExtensionSyncData(const SyncChange& sync_change) |
[email protected] | b10a4abe | 2011-12-10 02:31:23 | [diff] [blame] | 31 | : 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] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 36 | PopulateFromSyncData(sync_change.sync_data()); |
| 37 | } |
| 38 | |
| 39 | ExtensionSyncData::ExtensionSyncData(const Extension& extension, |
| 40 | bool enabled, |
[email protected] | aa7599d | 2011-10-28 07:24:32 | [diff] [blame] | 41 | bool incognito_enabled, |
[email protected] | b2689a90 | 2011-12-01 00:41:09 | [diff] [blame] | 42 | const std::string& notifications_client_id, |
[email protected] | 168389f | 2011-12-20 17:12:48 | [diff] [blame] | 43 | bool notifications_disabled, |
| 44 | const StringOrdinal& app_launch_ordinal, |
| 45 | const StringOrdinal& page_ordinal) |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 46 | : 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] | aa7599d | 2011-10-28 07:24:32 | [diff] [blame] | 53 | name_(extension.name()), |
[email protected] | b2689a90 | 2011-12-01 00:41:09 | [diff] [blame] | 54 | notifications_client_id_(notifications_client_id), |
[email protected] | 168389f | 2011-12-20 17:12:48 | [diff] [blame] | 55 | notifications_disabled_(notifications_disabled), |
| 56 | app_launch_ordinal_(app_launch_ordinal), |
| 57 | page_ordinal_(page_ordinal) { |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 58 | } |
[email protected] | 90310d9 | 2011-04-17 07:35:04 | [diff] [blame] | 59 | |
| 60 | ExtensionSyncData::~ExtensionSyncData() {} |
[email protected] | 418e953e | 2011-04-27 21:30:22 | [diff] [blame] | 61 | |
[email protected] | aa7599d | 2011-10-28 07:24:32 | [diff] [blame] | 62 | void ExtensionSyncData::PopulateAppSpecifics( |
| 63 | sync_pb::AppSpecifics* specifics) const { |
| 64 | DCHECK(specifics); |
| 65 | sync_pb::AppNotificationSettings* notif_settings = |
| 66 | specifics->mutable_notification_settings(); |
[email protected] | b2689a90 | 2011-12-01 00:41:09 | [diff] [blame] | 67 | if (!notifications_client_id_.empty()) |
| 68 | notif_settings->set_oauth_client_id(notifications_client_id_); |
[email protected] | aa7599d | 2011-10-28 07:24:32 | [diff] [blame] | 69 | notif_settings->set_disabled(notifications_disabled_); |
[email protected] | 168389f | 2011-12-20 17:12:48 | [diff] [blame] | 70 | |
| 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] | aa7599d | 2011-10-28 07:24:32 | [diff] [blame] | 78 | } |
| 79 | |
[email protected] | 168389f | 2011-12-20 17:12:48 | [diff] [blame] | 80 | void ExtensionSyncData::PopulateExtensionSpecifics( |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 81 | 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] | 0fac519c | 2011-08-19 18:05:57 | [diff] [blame] | 89 | } |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 90 | |
| 91 | SyncData ExtensionSyncData::GetSyncData() const { |
| 92 | sync_pb::EntitySpecifics specifics; |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 93 | |
| 94 | switch (type_) { |
| 95 | case Extension::SYNC_TYPE_EXTENSION: |
[email protected] | 4557d22 | 2012-03-04 23:33:36 | [diff] [blame^] | 96 | PopulateExtensionSpecifics(specifics.mutable_extension()); |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 97 | break; |
| 98 | case Extension::SYNC_TYPE_APP: |
[email protected] | 4557d22 | 2012-03-04 23:33:36 | [diff] [blame^] | 99 | PopulateAppSpecifics(specifics.mutable_app()); |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 100 | break; |
| 101 | default: |
| 102 | LOG(FATAL) << "Attempt to get non-syncable data."; |
| 103 | } |
| 104 | |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 105 | return SyncData::CreateLocalData(id_, name_, specifics); |
| 106 | } |
| 107 | |
| 108 | SyncChange ExtensionSyncData::GetSyncChange( |
| 109 | SyncChange::SyncChangeType change_type) const { |
| 110 | return SyncChange(change_type, GetSyncData()); |
| 111 | } |
| 112 | |
[email protected] | 168389f | 2011-12-20 17:12:48 | [diff] [blame] | 113 | void 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] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 132 | void 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] | e1adb9a | 2011-09-09 17:42:52 | [diff] [blame] | 138 | Version specifics_version(specifics.version()); |
| 139 | if (!specifics_version.IsValid()) |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 140 | LOG(FATAL) << "Attempt to sync bad ExtensionSpecifics."; |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 141 | |
| 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] | e1adb9a | 2011-09-09 17:42:52 | [diff] [blame] | 150 | version_ = specifics_version; |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 151 | enabled_ = specifics.enabled(); |
| 152 | incognito_enabled_ = specifics.incognito_enabled(); |
| 153 | name_ = specifics.name(); |
| 154 | } |
| 155 | |
| 156 | void ExtensionSyncData::PopulateFromSyncData(const SyncData& sync_data) { |
| 157 | const sync_pb::EntitySpecifics& entity_specifics = sync_data.GetSpecifics(); |
[email protected] | 168389f | 2011-12-20 17:12:48 | [diff] [blame] | 158 | |
[email protected] | 4557d22 | 2012-03-04 23:33:36 | [diff] [blame^] | 159 | if (entity_specifics.has_extension()) { |
| 160 | PopulateFromExtensionSpecifics(entity_specifics.extension()); |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 161 | type_ = Extension::SYNC_TYPE_EXTENSION; |
[email protected] | 4557d22 | 2012-03-04 23:33:36 | [diff] [blame^] | 162 | } else if (entity_specifics.has_app()) { |
| 163 | PopulateFromAppSpecifics(entity_specifics.app()); |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 164 | type_ = Extension::SYNC_TYPE_APP; |
| 165 | } else { |
| 166 | LOG(FATAL) << "Attempt to sync bad EntitySpecifics."; |
| 167 | } |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 168 | } |