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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
// 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_change_processor.h"
#include <sstream>
#include <string>
#include "base/logging.h"
#include "base/stl_util-inl.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/sync/glue/extension_sync.h"
#include "chrome/browser/sync/glue/extension_util.h"
#include "chrome/browser/sync/protocol/extension_specifics.pb.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/notification_details.h"
#include "chrome/common/notification_source.h"
#include "content/browser/browser_thread.h"
namespace browser_sync {
ExtensionChangeProcessor::ExtensionChangeProcessor(
const ExtensionSyncTraits& traits,
UnrecoverableErrorHandler* error_handler)
: ChangeProcessor(error_handler),
traits_(traits),
profile_(NULL) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(error_handler);
}
ExtensionChangeProcessor::~ExtensionChangeProcessor() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
// TODO(akalin): We need to make sure events we receive from either
// the browser or the syncapi are done in order; this is tricky since
// some events (e.g., extension installation) are done asynchronously.
void ExtensionChangeProcessor::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(running());
DCHECK(profile_);
if ((type != NotificationType::EXTENSION_INSTALLED) &&
(type != NotificationType::EXTENSION_UNINSTALLED) &&
(type != NotificationType::EXTENSION_LOADED) &&
(type != NotificationType::EXTENSION_UPDATE_DISABLED) &&
(type != NotificationType::EXTENSION_UNLOADED)) {
LOG(DFATAL) << "Received unexpected notification of type "
<< type.value;
return;
}
DCHECK_EQ(Source<Profile>(source).ptr(), profile_);
if (type == NotificationType::EXTENSION_UNINSTALLED) {
const UninstalledExtensionInfo* uninstalled_extension_info =
Details<UninstalledExtensionInfo>(details).ptr();
CHECK(uninstalled_extension_info);
if (traits_.should_handle_extension_uninstall(
*uninstalled_extension_info)) {
const std::string& id = uninstalled_extension_info->extension_id;
VLOG(1) << "Removing server data for uninstalled extension " << id
<< " of type " << uninstalled_extension_info->extension_type;
RemoveServerData(traits_, id, profile_->GetProfileSyncService());
}
} else {
const Extension* extension = NULL;
if (type == NotificationType::EXTENSION_UNLOADED) {
extension = Details<UnloadedExtensionInfo>(details)->extension;
} else {
extension = Details<const Extension>(details).ptr();
}
CHECK(extension);
VLOG(1) << "Updating server data for extension " << extension->id()
<< " (notification type = " << type.value << ")";
if (!traits_.is_valid_and_syncable(*extension)) {
return;
}
std::string error;
if (!UpdateServerData(traits_, *extension,
profile_->GetProfileSyncService(), &error)) {
error_handler()->OnUnrecoverableError(FROM_HERE, error);
}
}
}
void ExtensionChangeProcessor::ApplyChangesFromSyncModel(
const sync_api::BaseTransaction* trans,
const sync_api::SyncManager::ChangeRecord* changes,
int change_count) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!running()) {
return;
}
ExtensionService* extensions_service =
GetExtensionServiceFromProfile(profile_);
for (int i = 0; i < change_count; ++i) {
const sync_api::SyncManager::ChangeRecord& change = changes[i];
switch (change.action) {
case sync_api::SyncManager::ChangeRecord::ACTION_ADD:
case sync_api::SyncManager::ChangeRecord::ACTION_UPDATE: {
sync_api::ReadNode node(trans);
if (!node.InitByIdLookup(change.id)) {
std::stringstream error;
error << "Extension node lookup failed for change " << change.id
<< " of action type " << change.action;
error_handler()->OnUnrecoverableError(FROM_HERE, error.str());
return;
}
DCHECK_EQ(node.GetModelType(), traits_.model_type);
const sync_pb::ExtensionSpecifics& specifics =
(*traits_.extension_specifics_getter)(node);
if (!IsExtensionSpecificsValid(specifics)) {
std::string error =
std::string("Invalid server specifics: ") +
ExtensionSpecificsToString(specifics);
error_handler()->OnUnrecoverableError(FROM_HERE, error);
return;
}
StopObserving();
UpdateClient(traits_, specifics, extensions_service);
StartObserving();
break;
}
case sync_api::SyncManager::ChangeRecord::ACTION_DELETE: {
sync_pb::ExtensionSpecifics specifics;
if ((*traits_.extension_specifics_entity_getter)(
change.specifics, &specifics)) {
StopObserving();
RemoveFromClient(traits_, specifics.id(), extensions_service);
StartObserving();
} else {
std::stringstream error;
error << "Could not get extension ID for deleted node "
<< change.id;
error_handler()->OnUnrecoverableError(FROM_HERE, error.str());
LOG(DFATAL) << error.str();
}
break;
}
}
}
}
void ExtensionChangeProcessor::StartImpl(Profile* profile) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(profile);
profile_ = profile;
StartObserving();
}
void ExtensionChangeProcessor::StopImpl() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
StopObserving();
profile_ = NULL;
}
void ExtensionChangeProcessor::StartObserving() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(profile_);
notification_registrar_.Add(
this, NotificationType::EXTENSION_INSTALLED,
Source<Profile>(profile_));
notification_registrar_.Add(
this, NotificationType::EXTENSION_UNINSTALLED,
Source<Profile>(profile_));
notification_registrar_.Add(
this, NotificationType::EXTENSION_LOADED,
Source<Profile>(profile_));
// Despite the name, this notification is exactly like
// EXTENSION_LOADED but with an initial state of DISABLED.
notification_registrar_.Add(
this, NotificationType::EXTENSION_UPDATE_DISABLED,
Source<Profile>(profile_));
notification_registrar_.Add(
this, NotificationType::EXTENSION_UNLOADED,
Source<Profile>(profile_));
}
void ExtensionChangeProcessor::StopObserving() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(profile_);
VLOG(1) << "Unobserving all notifications";
notification_registrar_.RemoveAll();
}
} // namespace browser_sync
|