summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/sync_bundle.cc
blob: dd793375c6e9163b7d362fa2391a48548382a57e (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
114
115
116
117
118
119
120
121
122
123
// Copyright 2015 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/extensions/sync_bundle.h"

#include "base/location.h"
#include "chrome/browser/extensions/extension_sync_data.h"
#include "chrome/browser/extensions/extension_sync_service.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/profiles/profile.h"
#include "extensions/common/extension.h"

namespace extensions {

SyncBundle::SyncBundle() {}
SyncBundle::~SyncBundle() {}

void SyncBundle::StartSyncing(
    scoped_ptr<syncer::SyncChangeProcessor> sync_processor) {
  sync_processor_.reset(sync_processor.release());
}

void SyncBundle::Reset() {
  sync_processor_.reset();
  synced_extensions_.clear();
  pending_sync_data_.clear();
}

bool SyncBundle::IsSyncing() const {
  return sync_processor_ != nullptr;
}

void SyncBundle::PushSyncDataList(
    const syncer::SyncDataList& sync_data_list) {
  syncer::SyncChangeList sync_change_list;
  for (const syncer::SyncData& sync_data : sync_data_list) {
    const syncer::SyncDataLocal sync_data_local(sync_data);
    const std::string& extension_id = sync_data_local.GetTag();

    sync_change_list.push_back(CreateSyncChange(extension_id, sync_data));

    AddSyncedExtension(extension_id);
  }

  PushSyncChanges(sync_change_list);
}

void SyncBundle::PushSyncDeletion(const std::string& extension_id,
                                  const syncer::SyncData& sync_data) {
  if (!HasSyncedExtension(extension_id))
    return;

  RemoveSyncedExtension(extension_id);
  PushSyncChanges(syncer::SyncChangeList(1,
      syncer::SyncChange(FROM_HERE,
                         syncer::SyncChange::ACTION_DELETE,
                         sync_data)));
}

void SyncBundle::PushSyncAddOrUpdate(const std::string& extension_id,
                                     const syncer::SyncData& sync_data) {
  PushSyncChanges(syncer::SyncChangeList(
      1, CreateSyncChange(extension_id, sync_data)));
  AddSyncedExtension(extension_id);
  // Now sync and local state agree. If we had a pending install from sync,
  // clear it now.
  pending_sync_data_.erase(extension_id);
}

void SyncBundle::ApplySyncData(const ExtensionSyncData& extension_sync_data) {
  if (extension_sync_data.uninstalled())
    RemoveSyncedExtension(extension_sync_data.id());
  else
    AddSyncedExtension(extension_sync_data.id());
}

bool SyncBundle::HasPendingExtensionData(const std::string& id) const {
  return pending_sync_data_.find(id) != pending_sync_data_.end();
}

void SyncBundle::AddPendingExtensionData(
    const ExtensionSyncData& extension_sync_data) {
  pending_sync_data_.insert(std::make_pair(extension_sync_data.id(),
                                           extension_sync_data));
}

std::vector<ExtensionSyncData> SyncBundle::GetPendingExtensionData() const {
  std::vector<ExtensionSyncData> pending_extensions;
  for (const auto& data : pending_sync_data_)
    pending_extensions.push_back(data.second);

  return pending_extensions;
}

syncer::SyncChange SyncBundle::CreateSyncChange(
    const std::string& extension_id,
    const syncer::SyncData& sync_data) const {
  return syncer::SyncChange(
      FROM_HERE,
      HasSyncedExtension(extension_id) ? syncer::SyncChange::ACTION_UPDATE
                                       : syncer::SyncChange::ACTION_ADD,
      sync_data);
}

void SyncBundle::PushSyncChanges(
    const syncer::SyncChangeList& sync_change_list) {
  sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list);
}

void SyncBundle::AddSyncedExtension(const std::string& id) {
  synced_extensions_.insert(id);
}

void SyncBundle::RemoveSyncedExtension(const std::string& id) {
  synced_extensions_.erase(id);
}

bool SyncBundle::HasSyncedExtension(const std::string& id) const {
  return synced_extensions_.find(id) != synced_extensions_.end();
}

}  // namespace extensions