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/app_sync_data.h"
7 #include "chrome/common/extensions/manifest_handlers/app_icon_color_info.h"
8 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
9 #include "extensions/common/extension.h"
10 #include "sync/api/sync_data.h"
11 #include "sync/protocol/app_specifics.pb.h"
12 #include "sync/protocol/sync.pb.h"
14 namespace extensions
{
16 AppSyncData::AppSyncData() {}
18 AppSyncData::AppSyncData(const Extension
& extension
,
20 bool incognito_enabled
,
22 ExtensionSyncData::OptionalBoolean all_urls_enabled
,
23 const syncer::StringOrdinal
& app_launch_ordinal
,
24 const syncer::StringOrdinal
& page_ordinal
,
25 extensions::LaunchType launch_type
)
26 : extension_sync_data_(extension
,
31 app_launch_ordinal_(app_launch_ordinal
),
32 page_ordinal_(page_ordinal
),
33 launch_type_(launch_type
) {
34 if (extension
.from_bookmark()) {
35 bookmark_app_description_
= extension
.description();
36 bookmark_app_url_
= AppLaunchInfo::GetLaunchWebURL(&extension
).spec();
37 bookmark_app_icon_color_
= AppIconColorInfo::GetIconColorString(&extension
);
41 AppSyncData::~AppSyncData() {}
44 scoped_ptr
<AppSyncData
> AppSyncData::CreateFromSyncData(
45 const syncer::SyncData
& sync_data
) {
46 scoped_ptr
<AppSyncData
> data(new AppSyncData
);
47 if (data
->PopulateFromSyncData(sync_data
))
49 return scoped_ptr
<AppSyncData
>();
53 scoped_ptr
<AppSyncData
> AppSyncData::CreateFromSyncChange(
54 const syncer::SyncChange
& sync_change
) {
55 scoped_ptr
<AppSyncData
> data(CreateFromSyncData(sync_change
.sync_data()));
57 return scoped_ptr
<AppSyncData
>();
59 data
->extension_sync_data_
.set_uninstalled(sync_change
.change_type() ==
60 syncer::SyncChange::ACTION_DELETE
);
64 syncer::SyncData
AppSyncData::GetSyncData() const {
65 sync_pb::EntitySpecifics specifics
;
66 PopulateAppSpecifics(specifics
.mutable_app());
68 return syncer::SyncData::CreateLocalData(extension_sync_data_
.id(),
69 extension_sync_data_
.name(),
73 syncer::SyncChange
AppSyncData::GetSyncChange(
74 syncer::SyncChange::SyncChangeType change_type
) const {
75 return syncer::SyncChange(FROM_HERE
, change_type
, GetSyncData());
78 void AppSyncData::PopulateAppSpecifics(sync_pb::AppSpecifics
* specifics
) const {
80 // Only sync the ordinal values and launch type if they are valid.
81 if (app_launch_ordinal_
.IsValid())
82 specifics
->set_app_launch_ordinal(app_launch_ordinal_
.ToInternalValue());
83 if (page_ordinal_
.IsValid())
84 specifics
->set_page_ordinal(page_ordinal_
.ToInternalValue());
86 sync_pb::AppSpecifics::LaunchType sync_launch_type
=
87 static_cast<sync_pb::AppSpecifics::LaunchType
>(launch_type_
);
89 // The corresponding validation of this value during processing of an
90 // AppSyncData is in ExtensionSyncService::ProcessAppSyncData.
91 if (launch_type_
>= LAUNCH_TYPE_FIRST
&& launch_type_
< NUM_LAUNCH_TYPES
&&
92 sync_pb::AppSpecifics_LaunchType_IsValid(sync_launch_type
)) {
93 specifics
->set_launch_type(sync_launch_type
);
96 if (!bookmark_app_url_
.empty())
97 specifics
->set_bookmark_app_url(bookmark_app_url_
);
99 if (!bookmark_app_description_
.empty())
100 specifics
->set_bookmark_app_description(bookmark_app_description_
);
102 if (!bookmark_app_icon_color_
.empty())
103 specifics
->set_bookmark_app_icon_color(bookmark_app_icon_color_
);
105 extension_sync_data_
.PopulateExtensionSpecifics(
106 specifics
->mutable_extension());
109 bool AppSyncData::PopulateFromAppSpecifics(
110 const sync_pb::AppSpecifics
& specifics
) {
111 if (!extension_sync_data_
.PopulateFromExtensionSpecifics(
112 specifics
.extension()))
115 app_launch_ordinal_
= syncer::StringOrdinal(specifics
.app_launch_ordinal());
116 page_ordinal_
= syncer::StringOrdinal(specifics
.page_ordinal());
118 launch_type_
= specifics
.has_launch_type()
119 ? static_cast<extensions::LaunchType
>(specifics
.launch_type())
120 : LAUNCH_TYPE_INVALID
;
122 bookmark_app_url_
= specifics
.bookmark_app_url();
123 bookmark_app_description_
= specifics
.bookmark_app_description();
124 bookmark_app_icon_color_
= specifics
.bookmark_app_icon_color();
128 bool AppSyncData::PopulateFromSyncData(const syncer::SyncData
& sync_data
) {
129 return PopulateFromAppSpecifics(sync_data
.GetSpecifics().app());
132 } // namespace extensions