Upstream version 7.36.149.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       remote_install_(false) {
23 }
24
25 ExtensionSyncData::ExtensionSyncData(const syncer::SyncData& sync_data)
26     : uninstalled_(false),
27       enabled_(false),
28       incognito_enabled_(false),
29       remote_install_(false) {
30   PopulateFromSyncData(sync_data);
31 }
32
33 ExtensionSyncData::ExtensionSyncData(const syncer::SyncChange& sync_change)
34     : uninstalled_(
35         sync_change.change_type() == syncer::SyncChange::ACTION_DELETE),
36       enabled_(false),
37       incognito_enabled_(false),
38       remote_install_(false) {
39   PopulateFromSyncData(sync_change.sync_data());
40 }
41
42 ExtensionSyncData::ExtensionSyncData(const Extension& extension,
43                                      bool enabled,
44                                      bool incognito_enabled)
45    :  id_(extension.id()),
46       uninstalled_(false),
47       enabled_(enabled),
48       incognito_enabled_(incognito_enabled),
49       remote_install_(false),
50       version_(extension.from_bookmark() ? base::Version("0")
51                                          : *extension.version()),
52       update_url_(ManifestURL::GetUpdateURL(&extension)),
53       name_(extension.non_localized_name()) {
54 }
55
56 ExtensionSyncData::~ExtensionSyncData() {}
57
58 syncer::SyncData ExtensionSyncData::GetSyncData() const {
59   sync_pb::EntitySpecifics specifics;
60   PopulateExtensionSpecifics(specifics.mutable_extension());
61
62   return syncer::SyncData::CreateLocalData(id_, name_, specifics);
63 }
64
65 syncer::SyncChange ExtensionSyncData::GetSyncChange(
66     syncer::SyncChange::SyncChangeType change_type) const {
67   return syncer::SyncChange(FROM_HERE, change_type, GetSyncData());
68 }
69
70 void ExtensionSyncData::PopulateExtensionSpecifics(
71     sync_pb::ExtensionSpecifics* specifics) const {
72   DCHECK(Extension::IdIsValid(id_));
73   specifics->set_id(id_);
74   specifics->set_update_url(update_url_.spec());
75   specifics->set_version(version_.GetString());
76   specifics->set_enabled(enabled_);
77   specifics->set_incognito_enabled(incognito_enabled_);
78   specifics->set_remote_install(remote_install_);
79   specifics->set_name(name_);
80 }
81
82 void ExtensionSyncData::PopulateFromExtensionSpecifics(
83     const sync_pb::ExtensionSpecifics& specifics) {
84   if (!Extension::IdIsValid(specifics.id())) {
85     LOG(FATAL) << "Attempt to sync bad ExtensionSpecifics.";
86   }
87
88   Version specifics_version(specifics.version());
89   if (!specifics_version.IsValid())
90     LOG(FATAL) << "Attempt to sync bad ExtensionSpecifics.";
91
92   // The update URL must be either empty or valid.
93   GURL specifics_update_url(specifics.update_url());
94   if (!specifics_update_url.is_empty() && !specifics_update_url.is_valid()) {
95     LOG(FATAL) << "Attempt to sync bad ExtensionSpecifics.";
96   }
97
98   id_ = specifics.id();
99   update_url_ = specifics_update_url;
100   version_ = specifics_version;
101   enabled_ = specifics.enabled();
102   incognito_enabled_ = specifics.incognito_enabled();
103   remote_install_ = specifics.remote_install();
104   name_ = specifics.name();
105 }
106
107 void ExtensionSyncData::set_uninstalled(bool uninstalled) {
108   uninstalled_ = uninstalled;
109 }
110
111 void ExtensionSyncData::PopulateFromSyncData(
112     const syncer::SyncData& sync_data) {
113   const sync_pb::EntitySpecifics& entity_specifics = sync_data.GetSpecifics();
114
115   if (entity_specifics.has_extension()) {
116     PopulateFromExtensionSpecifics(entity_specifics.extension());
117   } else {
118     LOG(FATAL) << "Attempt to sync bad EntitySpecifics.";
119   }
120 }
121
122 }  // namespace extensions