summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/glue/extension_sync_traits.cc
blob: 74e291e9442d616f64fbe9a3da21fa12cec2f0b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/sync/glue/extension_sync_traits.h"

#include "base/string_piece.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/sync/engine/syncapi.h"
#include "chrome/browser/sync/protocol/app_specifics.pb.h"
#include "chrome/browser/sync/protocol/extension_specifics.pb.h"

namespace browser_sync {

ExtensionSyncTraits::ExtensionSyncTraits(
    syncable::ModelType model_type,
    PendingExtensionInfo::ExpectedCrxType expected_crx_type,
    const char* root_node_tag,
    const ExtensionTypeSet& allowed_extension_types,
    ExtensionSpecificsGetter extension_specifics_getter,
    ExtensionSpecificsSetter extension_specifics_setter,
    ExtensionSpecificsEntityGetter extension_specifics_entity_getter)
    : model_type(model_type),
      expected_crx_type(expected_crx_type),
      root_node_tag(root_node_tag),
      allowed_extension_types(allowed_extension_types),
      extension_specifics_getter(extension_specifics_getter),
      extension_specifics_setter(extension_specifics_setter),
      extension_specifics_entity_getter(extension_specifics_entity_getter) {}

ExtensionSyncTraits::~ExtensionSyncTraits() {}

namespace {

const sync_pb::ExtensionSpecifics& GetExtensionSpecifics(
        const sync_api::BaseNode& node) {
  return node.GetExtensionSpecifics();
}

void SetExtensionSpecifics(
    const sync_pb::ExtensionSpecifics& extension_specifics,
    sync_api::WriteNode* node) {
  node->SetTitle(UTF8ToWide(extension_specifics.name()));
  node->SetExtensionSpecifics(extension_specifics);
}

bool GetExtensionSpecificsFromEntity(
    const sync_pb::EntitySpecifics& entity_specifics,
    sync_pb::ExtensionSpecifics* extension_specifics) {
  if (!entity_specifics.HasExtension(sync_pb::extension)) {
    return false;
  }
  *extension_specifics = entity_specifics.GetExtension(sync_pb::extension);
  return true;
}

}  // namespace

ExtensionSyncTraits GetExtensionSyncTraits() {
  ExtensionTypeSet allowed_extension_types;
  allowed_extension_types.insert(EXTENSION);
  allowed_extension_types.insert(UPDATEABLE_USER_SCRIPT);
  return ExtensionSyncTraits(syncable::EXTENSIONS,
                             PendingExtensionInfo::EXTENSION,
                             "google_chrome_extensions",
                             allowed_extension_types,
                             &GetExtensionSpecifics,
                             &SetExtensionSpecifics,
                             &GetExtensionSpecificsFromEntity);
}

namespace {

const sync_pb::ExtensionSpecifics& GetExtensionSpecificsOfApp(
        const sync_api::BaseNode& node) {
  return node.GetAppSpecifics().extension();
}

void SetExtensionSpecificsOfApp(
    const sync_pb::ExtensionSpecifics& extension_specifics,
    sync_api::WriteNode* node) {
  node->SetTitle(UTF8ToWide(extension_specifics.name()));
  sync_pb::AppSpecifics app_specifics;
  *app_specifics.mutable_extension() = extension_specifics;
  node->SetAppSpecifics(app_specifics);
}

bool GetExtensionSpecificsFromEntityOfApp(
    const sync_pb::EntitySpecifics& entity_specifics,
    sync_pb::ExtensionSpecifics* extension_specifics) {
  if (!entity_specifics.HasExtension(sync_pb::app)) {
    return false;
  }
  *extension_specifics =
      entity_specifics.GetExtension(sync_pb::app).extension();
  return true;
}

}  // namespace

ExtensionSyncTraits GetAppSyncTraits() {
  ExtensionTypeSet allowed_extension_types;
  allowed_extension_types.insert(APP);
  return ExtensionSyncTraits(syncable::APPS,
                             PendingExtensionInfo::APP,
                             "google_chrome_apps",
                             allowed_extension_types,
                             &GetExtensionSpecificsOfApp,
                             &SetExtensionSpecificsOfApp,
                             &GetExtensionSpecificsFromEntityOfApp);
}

}  // namespace browser_sync