Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / extension_sync_data.cc
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/extension_sync_data.h"
6
7 #include "base/logging.h"
8 #include "chrome/browser/extensions/app_sync_data.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/common/extensions/manifest_url_handler.h"
11 #include "extensions/common/extension.h"
12 #include "sync/api/sync_data.h"
13 #include "sync/protocol/extension_specifics.pb.h"
14 #include "sync/protocol/sync.pb.h"
15
16 namespace extensions {
17
18 ExtensionSyncData::ExtensionSyncData()
19     : uninstalled_(false),
20       enabled_(false),
21       incognito_enabled_(false) {
22 }
23
24 ExtensionSyncData::ExtensionSyncData(const syncer::SyncData& sync_data)
25     : uninstalled_(false),
26       enabled_(false),
27       incognito_enabled_(false) {
28   PopulateFromSyncData(sync_data);
29 }
30
31 ExtensionSyncData::ExtensionSyncData(const syncer::SyncChange& sync_change)
32     : uninstalled_(
33         sync_change.change_type() == syncer::SyncChange::ACTION_DELETE),
34       enabled_(false),
35       incognito_enabled_(false) {
36   PopulateFromSyncData(sync_change.sync_data());
37 }
38
39 ExtensionSyncData::ExtensionSyncData(const Extension& extension,
40                                      bool enabled,
41                                      bool incognito_enabled)
42    :  id_(extension.id()),
43       uninstalled_(false),
44       enabled_(enabled),
45       incognito_enabled_(incognito_enabled),
46       version_(*extension.version()),
47       update_url_(ManifestURL::GetUpdateURL(&extension)),
48       name_(extension.non_localized_name()) {
49 }
50
51 ExtensionSyncData::~ExtensionSyncData() {}
52
53 syncer::SyncData ExtensionSyncData::GetSyncData() const {
54   sync_pb::EntitySpecifics specifics;
55   PopulateExtensionSpecifics(specifics.mutable_extension());
56
57   return syncer::SyncData::CreateLocalData(id_, name_, specifics);
58 }
59
60 syncer::SyncChange ExtensionSyncData::GetSyncChange(
61     syncer::SyncChange::SyncChangeType change_type) const {
62   return syncer::SyncChange(FROM_HERE, change_type, GetSyncData());
63 }
64
65 void ExtensionSyncData::PopulateExtensionSpecifics(
66     sync_pb::ExtensionSpecifics* specifics) const {
67   DCHECK(Extension::IdIsValid(id_));
68   specifics->set_id(id_);
69   specifics->set_update_url(update_url_.spec());
70   specifics->set_version(version_.GetString());
71   specifics->set_enabled(enabled_);
72   specifics->set_incognito_enabled(incognito_enabled_);
73   specifics->set_name(name_);
74 }
75
76 void ExtensionSyncData::PopulateFromExtensionSpecifics(
77     const sync_pb::ExtensionSpecifics& specifics) {
78   if (!Extension::IdIsValid(specifics.id())) {
79     LOG(FATAL) << "Attempt to sync bad ExtensionSpecifics.";
80   }
81
82   Version specifics_version(specifics.version());
83   if (!specifics_version.IsValid())
84     LOG(FATAL) << "Attempt to sync bad ExtensionSpecifics.";
85
86   // The update URL must be either empty or valid.
87   GURL specifics_update_url(specifics.update_url());
88   if (!specifics_update_url.is_empty() && !specifics_update_url.is_valid()) {
89     LOG(FATAL) << "Attempt to sync bad ExtensionSpecifics.";
90   }
91
92   id_ = specifics.id();
93   update_url_ = specifics_update_url;
94   version_ = specifics_version;
95   enabled_ = specifics.enabled();
96   incognito_enabled_ = specifics.incognito_enabled();
97   name_ = specifics.name();
98 }
99
100 void ExtensionSyncData::set_uninstalled(bool uninstalled) {
101   uninstalled_ = uninstalled;
102 }
103
104 void ExtensionSyncData::PopulateFromSyncData(
105     const syncer::SyncData& sync_data) {
106   const sync_pb::EntitySpecifics& entity_specifics = sync_data.GetSpecifics();
107
108   if (entity_specifics.has_extension()) {
109     PopulateFromExtensionSpecifics(entity_specifics.extension());
110   } else {
111     LOG(FATAL) << "Attempt to sync bad EntitySpecifics.";
112   }
113 }
114
115 }  // namespace extensions