summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/glue/extension_change_processor.cc
blob: 9d6ed26791691553a4b573cd8ebca0fbb5cac215 (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
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
// Copyright (c) 2011 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 "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_sync_data.h"
#include "chrome/browser/profiles/profile.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/chrome_notification_types.h"
#include "chrome/common/extensions/extension.h"
#include "content/browser/browser_thread.h"
#include "content/common/notification_details.h"
#include "content/common/notification_source.h"

namespace browser_sync {

ExtensionChangeProcessor::ExtensionChangeProcessor(
    UnrecoverableErrorHandler* error_handler)
    : ChangeProcessor(error_handler),
      traits_(GetExtensionSyncTraits()),
      profile_(NULL),
      extension_service_(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(int type,
                                       const NotificationSource& source,
                                       const NotificationDetails& details) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  DCHECK(running());
  DCHECK(profile_);
  if ((type != chrome::NOTIFICATION_EXTENSION_LOADED) &&
      (type != chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED) &&
      (type != chrome::NOTIFICATION_EXTENSION_UNLOADED)) {
    LOG(DFATAL) << "Received unexpected notification of type "
                << type;
    return;
  }

  // Filter out unhandled extensions first.
  DCHECK_EQ(Source<Profile>(source).ptr(), profile_);
  const Extension& extension =
      (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) ?
      *Details<UnloadedExtensionInfo>(details)->extension :
      *Details<const Extension>(details).ptr();
  if (!traits_.is_valid_and_syncable(extension)) {
    return;
  }

  const std::string& id = extension.id();

  // Then handle extension uninstalls.
  if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) {
    const UnloadedExtensionInfo& info =
        *Details<UnloadedExtensionInfo>(details).ptr();
    if (info.reason == UnloadedExtensionInfo::UNINSTALL) {
      VLOG(1) << "Removing server data for uninstalled extension " << id
              << " of type " << info.extension->GetType();
      RemoveServerData(traits_, id, share_handle());
      return;
    }
  }

  VLOG(1) << "Updating server data for extension " << id
          << " (notification type = " << type << ")";
  std::string error;
  if (!UpdateServerData(traits_, extension, *extension_service_,
                        share_handle(), &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;
  }
  for (int i = 0; i < change_count; ++i) {
    const sync_api::SyncManager::ChangeRecord& change = changes[i];
    sync_pb::ExtensionSpecifics specifics;
    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);
        specifics = (*traits_.extension_specifics_getter)(node);
        break;
      }
      case sync_api::SyncManager::ChangeRecord::ACTION_DELETE: {
        if (!(*traits_.extension_specifics_entity_getter)(
                change.specifics, &specifics)) {
          std::stringstream error;
          error << "Could not get extension specifics from deleted node "
                << change.id;
          error_handler()->OnUnrecoverableError(FROM_HERE, error.str());
          LOG(DFATAL) << error.str();
        }
        break;
      }
    }
    ExtensionSyncData sync_data;
    if (!SpecificsToSyncData(specifics, &sync_data)) {
      // TODO(akalin): Should probably recover or drop.
      std::string error =
          std::string("Invalid server specifics: ") +
          ExtensionSpecificsToString(specifics);
      error_handler()->OnUnrecoverableError(FROM_HERE, error);
      return;
    }
    sync_data.uninstalled =
        (change.action == sync_api::SyncManager::ChangeRecord::ACTION_DELETE);
    StopObserving();
    extension_service_->ProcessSyncData(sync_data,
                                        traits_.is_valid_and_syncable);
    StartObserving();
  }
}

void ExtensionChangeProcessor::StartImpl(Profile* profile) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  profile_ = profile;
  extension_service_ = profile_->GetExtensionService();
  DCHECK(profile_);
  DCHECK(extension_service_);
  StartObserving();
}

void ExtensionChangeProcessor::StopImpl() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  StopObserving();
  profile_ = NULL;
  extension_service_ = NULL;
}

void ExtensionChangeProcessor::StartObserving() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  DCHECK(profile_);

  notification_registrar_.Add(
      this, chrome::NOTIFICATION_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, chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
      Source<Profile>(profile_));

  notification_registrar_.Add(
      this, chrome::NOTIFICATION_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