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/app_sync_data.h"
6
7 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
8 #include "extensions/common/extension.h"
9 #include "sync/api/sync_data.h"
10 #include "sync/protocol/app_specifics.pb.h"
11 #include "sync/protocol/sync.pb.h"
12
13 namespace extensions {
14
AppSyncData()15 AppSyncData::AppSyncData() {}
16
AppSyncData(const syncer::SyncData & sync_data)17 AppSyncData::AppSyncData(const syncer::SyncData& sync_data) {
18 PopulateFromSyncData(sync_data);
19 }
20
AppSyncData(const syncer::SyncChange & sync_change)21 AppSyncData::AppSyncData(const syncer::SyncChange& sync_change) {
22 PopulateFromSyncData(sync_change.sync_data());
23 extension_sync_data_.set_uninstalled(
24 sync_change.change_type() == syncer::SyncChange::ACTION_DELETE);
25 }
26
AppSyncData(const Extension & extension,bool enabled,bool incognito_enabled,bool remote_install,const syncer::StringOrdinal & app_launch_ordinal,const syncer::StringOrdinal & page_ordinal,extensions::LaunchType launch_type)27 AppSyncData::AppSyncData(const Extension& extension,
28 bool enabled,
29 bool incognito_enabled,
30 bool remote_install,
31 const syncer::StringOrdinal& app_launch_ordinal,
32 const syncer::StringOrdinal& page_ordinal,
33 extensions::LaunchType launch_type)
34 : extension_sync_data_(extension,
35 enabled,
36 incognito_enabled,
37 remote_install),
38 app_launch_ordinal_(app_launch_ordinal),
39 page_ordinal_(page_ordinal),
40 launch_type_(launch_type) {
41 if (extension.from_bookmark()) {
42 bookmark_app_description_ = extension.description();
43 bookmark_app_url_ = AppLaunchInfo::GetLaunchWebURL(&extension).spec();
44 }
45 }
46
~AppSyncData()47 AppSyncData::~AppSyncData() {}
48
GetSyncData() const49 syncer::SyncData AppSyncData::GetSyncData() const {
50 sync_pb::EntitySpecifics specifics;
51 PopulateAppSpecifics(specifics.mutable_app());
52
53 return syncer::SyncData::CreateLocalData(extension_sync_data_.id(),
54 extension_sync_data_.name(),
55 specifics);
56 }
57
GetSyncChange(syncer::SyncChange::SyncChangeType change_type) const58 syncer::SyncChange AppSyncData::GetSyncChange(
59 syncer::SyncChange::SyncChangeType change_type) const {
60 return syncer::SyncChange(FROM_HERE, change_type, GetSyncData());
61 }
62
PopulateAppSpecifics(sync_pb::AppSpecifics * specifics) const63 void AppSyncData::PopulateAppSpecifics(sync_pb::AppSpecifics* specifics) const {
64 DCHECK(specifics);
65 // Only sync the ordinal values and launch type if they are valid.
66 if (app_launch_ordinal_.IsValid())
67 specifics->set_app_launch_ordinal(app_launch_ordinal_.ToInternalValue());
68 if (page_ordinal_.IsValid())
69 specifics->set_page_ordinal(page_ordinal_.ToInternalValue());
70
71 sync_pb::AppSpecifics::LaunchType sync_launch_type =
72 static_cast<sync_pb::AppSpecifics::LaunchType>(launch_type_);
73
74 // The corresponding validation of this value during processing of an
75 // AppSyncData is in ExtensionSyncService::ProcessAppSyncData.
76 if (launch_type_ >= LAUNCH_TYPE_FIRST && launch_type_ < NUM_LAUNCH_TYPES &&
77 sync_pb::AppSpecifics_LaunchType_IsValid(sync_launch_type)) {
78 specifics->set_launch_type(sync_launch_type);
79 }
80
81 if (!bookmark_app_url_.empty())
82 specifics->set_bookmark_app_url(bookmark_app_url_);
83
84 if (!bookmark_app_description_.empty())
85 specifics->set_bookmark_app_description(bookmark_app_description_);
86
87 extension_sync_data_.PopulateExtensionSpecifics(
88 specifics->mutable_extension());
89 }
90
PopulateFromAppSpecifics(const sync_pb::AppSpecifics & specifics)91 void AppSyncData::PopulateFromAppSpecifics(
92 const sync_pb::AppSpecifics& specifics) {
93 extension_sync_data_.PopulateFromExtensionSpecifics(specifics.extension());
94
95 app_launch_ordinal_ = syncer::StringOrdinal(specifics.app_launch_ordinal());
96 page_ordinal_ = syncer::StringOrdinal(specifics.page_ordinal());
97
98 launch_type_ = specifics.has_launch_type()
99 ? static_cast<extensions::LaunchType>(specifics.launch_type())
100 : LAUNCH_TYPE_INVALID;
101
102 bookmark_app_url_ = specifics.bookmark_app_url();
103 bookmark_app_description_ = specifics.bookmark_app_description();
104 }
105
PopulateFromSyncData(const syncer::SyncData & sync_data)106 void AppSyncData::PopulateFromSyncData(const syncer::SyncData& sync_data) {
107 PopulateFromAppSpecifics(sync_data.GetSpecifics().app());
108 }
109
110 } // namespace extensions
111