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 "extensions/common/extension.h"
8 #include "sync/api/sync_data.h"
9 #include "sync/protocol/app_specifics.pb.h"
10 #include "sync/protocol/sync.pb.h"
12 namespace extensions
{
14 AppSyncData::AppSyncData() {}
16 AppSyncData::AppSyncData(const syncer::SyncData
& sync_data
) {
17 PopulateFromSyncData(sync_data
);
20 AppSyncData::AppSyncData(const syncer::SyncChange
& sync_change
) {
21 PopulateFromSyncData(sync_change
.sync_data());
22 extension_sync_data_
.set_uninstalled(
23 sync_change
.change_type() == syncer::SyncChange::ACTION_DELETE
);
26 AppSyncData::AppSyncData(const Extension
& extension
,
28 bool incognito_enabled
,
29 const syncer::StringOrdinal
& app_launch_ordinal
,
30 const syncer::StringOrdinal
& page_ordinal
,
31 extensions::LaunchType launch_type
)
32 : extension_sync_data_(extension
, enabled
, incognito_enabled
),
33 app_launch_ordinal_(app_launch_ordinal
),
34 page_ordinal_(page_ordinal
),
35 launch_type_(launch_type
) {
38 AppSyncData::~AppSyncData() {}
40 syncer::SyncData
AppSyncData::GetSyncData() const {
41 sync_pb::EntitySpecifics specifics
;
42 PopulateAppSpecifics(specifics
.mutable_app());
44 return syncer::SyncData::CreateLocalData(extension_sync_data_
.id(),
45 extension_sync_data_
.name(),
49 syncer::SyncChange
AppSyncData::GetSyncChange(
50 syncer::SyncChange::SyncChangeType change_type
) const {
51 return syncer::SyncChange(FROM_HERE
, change_type
, GetSyncData());
54 void AppSyncData::PopulateAppSpecifics(sync_pb::AppSpecifics
* specifics
) const {
56 // Only sync the ordinal values and launch type if they are valid.
57 if (app_launch_ordinal_
.IsValid())
58 specifics
->set_app_launch_ordinal(app_launch_ordinal_
.ToInternalValue());
59 if (page_ordinal_
.IsValid())
60 specifics
->set_page_ordinal(page_ordinal_
.ToInternalValue());
62 sync_pb::AppSpecifics::LaunchType sync_launch_type
=
63 static_cast<sync_pb::AppSpecifics::LaunchType
>(launch_type_
);
65 // The corresponding validation of this value during processing of an
66 // AppSyncData is in ExtensionSyncService::ProcessAppSyncData.
67 if (launch_type_
>= LAUNCH_TYPE_FIRST
&& launch_type_
< NUM_LAUNCH_TYPES
&&
68 sync_pb::AppSpecifics_LaunchType_IsValid(sync_launch_type
)) {
69 specifics
->set_launch_type(sync_launch_type
);
72 extension_sync_data_
.PopulateExtensionSpecifics(
73 specifics
->mutable_extension());
76 void AppSyncData::PopulateFromAppSpecifics(
77 const sync_pb::AppSpecifics
& specifics
) {
78 extension_sync_data_
.PopulateFromExtensionSpecifics(specifics
.extension());
80 app_launch_ordinal_
= syncer::StringOrdinal(specifics
.app_launch_ordinal());
81 page_ordinal_
= syncer::StringOrdinal(specifics
.page_ordinal());
83 launch_type_
= specifics
.has_launch_type()
84 ? static_cast<extensions::LaunchType
>(specifics
.launch_type())
85 : LAUNCH_TYPE_INVALID
;
88 void AppSyncData::PopulateFromSyncData(const syncer::SyncData
& sync_data
) {
89 PopulateFromAppSpecifics(sync_data
.GetSpecifics().app());
92 } // namespace extensions