• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 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/sync/glue/extension_sync_traits.h"
6 
7 #include "base/string_piece.h"
8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/sync/engine/syncapi.h"
10 #include "chrome/browser/sync/glue/extension_util.h"
11 #include "chrome/browser/sync/protocol/app_specifics.pb.h"
12 #include "chrome/browser/sync/protocol/extension_specifics.pb.h"
13 #include "chrome/common/extensions/extension.h"
14 
15 namespace browser_sync {
16 
ExtensionSyncTraits(syncable::ModelType model_type,IsValidAndSyncablePredicate is_valid_and_syncable,ShouldHandleExtensionUninstallPredicate should_handle_extension_uninstall,const char * root_node_tag,ExtensionSpecificsGetter extension_specifics_getter,ExtensionSpecificsSetter extension_specifics_setter,ExtensionSpecificsEntityGetter extension_specifics_entity_getter)17 ExtensionSyncTraits::ExtensionSyncTraits(
18     syncable::ModelType model_type,
19     IsValidAndSyncablePredicate is_valid_and_syncable,
20     ShouldHandleExtensionUninstallPredicate
21         should_handle_extension_uninstall,
22     const char* root_node_tag,
23     ExtensionSpecificsGetter extension_specifics_getter,
24     ExtensionSpecificsSetter extension_specifics_setter,
25     ExtensionSpecificsEntityGetter extension_specifics_entity_getter)
26     : model_type(model_type),
27       is_valid_and_syncable(is_valid_and_syncable),
28       should_handle_extension_uninstall(should_handle_extension_uninstall),
29       root_node_tag(root_node_tag),
30       extension_specifics_getter(extension_specifics_getter),
31       extension_specifics_setter(extension_specifics_setter),
32       extension_specifics_entity_getter(extension_specifics_entity_getter) {}
33 
~ExtensionSyncTraits()34 ExtensionSyncTraits::~ExtensionSyncTraits() {}
35 
36 namespace {
37 
GetExtensionSpecifics(const sync_api::BaseNode & node)38 const sync_pb::ExtensionSpecifics& GetExtensionSpecifics(
39         const sync_api::BaseNode& node) {
40   return node.GetExtensionSpecifics();
41 }
42 
SetExtensionSpecifics(const sync_pb::ExtensionSpecifics & extension_specifics,sync_api::WriteNode * node)43 void SetExtensionSpecifics(
44     const sync_pb::ExtensionSpecifics& extension_specifics,
45     sync_api::WriteNode* node) {
46   node->SetTitle(UTF8ToWide(extension_specifics.name()));
47   node->SetExtensionSpecifics(extension_specifics);
48 }
49 
GetExtensionSpecificsFromEntity(const sync_pb::EntitySpecifics & entity_specifics,sync_pb::ExtensionSpecifics * extension_specifics)50 bool GetExtensionSpecificsFromEntity(
51     const sync_pb::EntitySpecifics& entity_specifics,
52     sync_pb::ExtensionSpecifics* extension_specifics) {
53   if (!entity_specifics.HasExtension(sync_pb::extension)) {
54     return false;
55   }
56   *extension_specifics = entity_specifics.GetExtension(sync_pb::extension);
57   return true;
58 }
59 
IsSyncableExtension(Extension::Type type,const GURL & update_url)60 bool IsSyncableExtension(Extension::Type type, const GURL& update_url) {
61   switch (type) {
62     case Extension::TYPE_EXTENSION:
63       return true;
64     case Extension::TYPE_USER_SCRIPT:
65       // We only want to sync user scripts with update URLs.
66       return !update_url.is_empty();
67     default:
68       return false;
69   }
70 }
71 
IsValidAndSyncableExtension(const Extension & extension)72 bool IsValidAndSyncableExtension(const Extension& extension) {
73   return
74       IsExtensionValid(extension) &&
75       IsSyncableExtension(extension.GetType(), extension.update_url());
76 }
77 
IsExtensionUninstall(const UninstalledExtensionInfo & uninstalled_extension_info)78 bool IsExtensionUninstall(
79     const UninstalledExtensionInfo& uninstalled_extension_info) {
80   return IsSyncableExtension(uninstalled_extension_info.extension_type,
81                              uninstalled_extension_info.update_url);
82 }
83 
84 }  // namespace
85 
GetExtensionSyncTraits()86 ExtensionSyncTraits GetExtensionSyncTraits() {
87   return ExtensionSyncTraits(syncable::EXTENSIONS,
88                              &IsValidAndSyncableExtension,
89                              &IsExtensionUninstall,
90                              "google_chrome_extensions",
91                              &GetExtensionSpecifics,
92                              &SetExtensionSpecifics,
93                              &GetExtensionSpecificsFromEntity);
94 }
95 
96 namespace {
97 
GetExtensionSpecificsOfApp(const sync_api::BaseNode & node)98 const sync_pb::ExtensionSpecifics& GetExtensionSpecificsOfApp(
99         const sync_api::BaseNode& node) {
100   return node.GetAppSpecifics().extension();
101 }
102 
SetExtensionSpecificsOfApp(const sync_pb::ExtensionSpecifics & extension_specifics,sync_api::WriteNode * node)103 void SetExtensionSpecificsOfApp(
104     const sync_pb::ExtensionSpecifics& extension_specifics,
105     sync_api::WriteNode* node) {
106   node->SetTitle(UTF8ToWide(extension_specifics.name()));
107   sync_pb::AppSpecifics app_specifics;
108   *app_specifics.mutable_extension() = extension_specifics;
109   node->SetAppSpecifics(app_specifics);
110 }
111 
GetExtensionSpecificsFromEntityOfApp(const sync_pb::EntitySpecifics & entity_specifics,sync_pb::ExtensionSpecifics * extension_specifics)112 bool GetExtensionSpecificsFromEntityOfApp(
113     const sync_pb::EntitySpecifics& entity_specifics,
114     sync_pb::ExtensionSpecifics* extension_specifics) {
115   if (!entity_specifics.HasExtension(sync_pb::app)) {
116     return false;
117   }
118   *extension_specifics =
119       entity_specifics.GetExtension(sync_pb::app).extension();
120   return true;
121 }
122 
IsSyncableApp(Extension::Type type)123 bool IsSyncableApp(Extension::Type type) {
124   return
125       (type == Extension::TYPE_HOSTED_APP) ||
126       (type == Extension::TYPE_PACKAGED_APP);
127 }
128 
IsValidAndSyncableApp(const Extension & extension)129 bool IsValidAndSyncableApp(
130     const Extension& extension) {
131   return IsExtensionValid(extension) && IsSyncableApp(extension.GetType());
132 }
133 
IsAppUninstall(const UninstalledExtensionInfo & uninstalled_extension_info)134 bool IsAppUninstall(
135     const UninstalledExtensionInfo& uninstalled_extension_info) {
136   return IsSyncableApp(uninstalled_extension_info.extension_type);
137 }
138 
139 }  // namespace
140 
GetAppSyncTraits()141 ExtensionSyncTraits GetAppSyncTraits() {
142   return ExtensionSyncTraits(syncable::APPS,
143                              &IsValidAndSyncableApp,
144                              &IsAppUninstall,
145                              "google_chrome_apps",
146                              &GetExtensionSpecificsOfApp,
147                              &SetExtensionSpecificsOfApp,
148                              &GetExtensionSpecificsFromEntityOfApp);
149 }
150 
151 }  // namespace browser_sync
152