summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/test/integration/sync_extension_helper.cc
blob: f32989acae9a2073b3ce0526b39b5c5c4439318e (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright (c) 2012 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/test/integration/sync_extension_helper.h"

#include "base/file_path.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/extensions/pending_extension_info.h"
#include "chrome/browser/extensions/pending_extension_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_manifest_constants.h"
#include "chrome/common/string_ordinal.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
#include "testing/gtest/include/gtest/gtest.h"

using extensions::Extension;

SyncExtensionHelper::ExtensionState::ExtensionState()
    : enabled_state(ENABLED), incognito_enabled(false) {}

SyncExtensionHelper::ExtensionState::~ExtensionState() {}

bool SyncExtensionHelper::ExtensionState::Equals(
    const SyncExtensionHelper::ExtensionState &other) const {
  return ((enabled_state == other.enabled_state) &&
          (incognito_enabled == other.incognito_enabled));
}

// static
SyncExtensionHelper* SyncExtensionHelper::GetInstance() {
  SyncExtensionHelper* instance = Singleton<SyncExtensionHelper>::get();
  instance->SetupIfNecessary(sync_datatype_helper::test());
  return instance;
}

SyncExtensionHelper::SyncExtensionHelper() : setup_completed_(false) {}

SyncExtensionHelper::~SyncExtensionHelper() {}

// static
std::string SyncExtensionHelper::NameToId(const std::string& name) {
  std::string id;
  EXPECT_TRUE(Extension::GenerateId(name, &id));
  return id;
}

void SyncExtensionHelper::SetupIfNecessary(SyncTest* test) {
  if (setup_completed_)
    return;

  for (int i = 0; i < test->num_clients(); ++i) {
    SetupProfile(test->GetProfile(i));
  }
  SetupProfile(test->verifier());

  setup_completed_ = true;
}

std::string SyncExtensionHelper::InstallExtension(
    Profile* profile, const std::string& name, Extension::Type type) {
  scoped_refptr<Extension> extension = GetExtension(profile, name, type);
  if (!extension.get()) {
    NOTREACHED() << "Could not install extension " << name;
    return "";
  }
  profile->GetExtensionService()->OnExtensionInstalled(
      extension, extension->UpdatesFromGallery(), StringOrdinal());
  return extension->id();
}

void SyncExtensionHelper::UninstallExtension(
    Profile* profile, const std::string& name) {
  ExtensionService::UninstallExtensionHelper(profile->GetExtensionService(),
                                             NameToId(name));
}

std::vector<std::string> SyncExtensionHelper::GetInstalledExtensionNames(
    Profile* profile) const {
  std::vector<std::string> names;
  ExtensionService* extension_service = profile->GetExtensionService();

  scoped_ptr<const ExtensionSet> extensions(
      extension_service->GenerateInstalledExtensionsSet());
  for (ExtensionSet::const_iterator it = extensions->begin();
       it != extensions->end(); ++it) {
    names.push_back((*it)->name());
  }

  return names;
}

void SyncExtensionHelper::EnableExtension(Profile* profile,
                                          const std::string& name) {
  profile->GetExtensionService()->EnableExtension(NameToId(name));
}

void SyncExtensionHelper::DisableExtension(Profile* profile,
                                           const std::string& name) {
  profile->GetExtensionService()->DisableExtension(
      NameToId(name), Extension::DISABLE_USER_ACTION);
}

bool SyncExtensionHelper::IsExtensionEnabled(
    Profile* profile, const std::string& name) const {
  return profile->GetExtensionService()->IsExtensionEnabled(NameToId(name));
}

void SyncExtensionHelper::IncognitoEnableExtension(
    Profile* profile, const std::string& name) {
  profile->GetExtensionService()->SetIsIncognitoEnabled(NameToId(name), true);
}

void SyncExtensionHelper::IncognitoDisableExtension(
    Profile* profile, const std::string& name) {
  profile->GetExtensionService()->SetIsIncognitoEnabled(NameToId(name), false);
}

bool SyncExtensionHelper::IsIncognitoEnabled(
    Profile* profile, const std::string& name) const {
  return profile->GetExtensionService()->IsIncognitoEnabled(NameToId(name));
}


bool SyncExtensionHelper::IsExtensionPendingInstallForSync(
    Profile* profile, const std::string& id) const {
  const PendingExtensionManager* pending_extension_manager =
      profile->GetExtensionService()->pending_extension_manager();
  PendingExtensionInfo info;
  if (!pending_extension_manager->GetById(id, &info)) {
    return false;
  }
  return info.is_from_sync();
}

void SyncExtensionHelper::InstallExtensionsPendingForSync(
    Profile* profile, Extension::Type type) {
  // TODO(akalin): Mock out the servers that the extensions auto-update
  // mechanism talk to so as to more closely match what actually happens.
  // Background networking will need to be re-enabled for extensions tests.

  // We make a copy here since InstallExtension() removes the
  // extension from the extensions service's copy.
  const PendingExtensionManager* pending_extension_manager =
      profile->GetExtensionService()->pending_extension_manager();

  std::set<std::string> pending_crx_ids;
  pending_extension_manager->GetPendingIdsForUpdateCheck(&pending_crx_ids);

  std::set<std::string>::const_iterator id;
  PendingExtensionInfo info;
  for (id = pending_crx_ids.begin(); id != pending_crx_ids.end(); ++id) {
    ASSERT_TRUE(pending_extension_manager->GetById(*id, &info));
    if (!info.is_from_sync())
      continue;

    StringMap::const_iterator it2 = id_to_name_.find(*id);
    if (it2 == id_to_name_.end()) {
      ADD_FAILURE() << "Could not get name for id " << *id
                    << " (profile = " << profile->GetDebugName() << ")";
      continue;
    }
    InstallExtension(profile, it2->second, type);
  }
}

SyncExtensionHelper::ExtensionStateMap
    SyncExtensionHelper::GetExtensionStates(Profile* profile) {
  const std::string& profile_debug_name = profile->GetDebugName();

  ExtensionStateMap extension_state_map;

  ExtensionService* extension_service = profile->GetExtensionService();

  scoped_ptr<const ExtensionSet> extensions(
      extension_service->GenerateInstalledExtensionsSet());
  for (ExtensionSet::const_iterator it = extensions->begin();
       it != extensions->end(); ++it) {
    const std::string& id = (*it)->id();
    extension_state_map[id].enabled_state =
        extension_service->IsExtensionEnabled(id) ?
        ExtensionState::ENABLED :
        ExtensionState::DISABLED;
    extension_state_map[id].incognito_enabled =
        extension_service->IsIncognitoEnabled(id);

    DVLOG(2) << "Extension " << (*it)->id() << " in profile "
             << profile_debug_name << " is "
             << (extension_service->IsExtensionEnabled(id) ?
                 "enabled" : "disabled");
  }

  const PendingExtensionManager* pending_extension_manager =
      extension_service->pending_extension_manager();

  std::set<std::string> pending_crx_ids;
  pending_extension_manager->GetPendingIdsForUpdateCheck(&pending_crx_ids);

  std::set<std::string>::const_iterator id;
  for (id = pending_crx_ids.begin(); id != pending_crx_ids.end(); ++id) {
    extension_state_map[*id].enabled_state = ExtensionState::PENDING;
    extension_state_map[*id].incognito_enabled =
        extension_service->IsIncognitoEnabled(*id);
    DVLOG(2) << "Extension " << *id << " in profile "
             << profile_debug_name << " is pending";
  }

  return extension_state_map;
}

bool SyncExtensionHelper::ExtensionStatesMatch(
    Profile* profile1, Profile* profile2) {
  const ExtensionStateMap& state_map1 = GetExtensionStates(profile1);
  const ExtensionStateMap& state_map2 = GetExtensionStates(profile2);
  if (state_map1.size() != state_map2.size()) {
    DVLOG(1) << "Number of extensions for profile " << profile1->GetDebugName()
             << " does not match profile " << profile2->GetDebugName();
    return false;
  }

  ExtensionStateMap::const_iterator it1 = state_map1.begin();
  ExtensionStateMap::const_iterator it2 = state_map2.begin();
  while (it1 != state_map1.end()) {
    if (it1->first != it2->first) {
      DVLOG(1) << "Extensions for profile " << profile1->GetDebugName()
               << " do not match profile " << profile2->GetDebugName();
      return false;
    } else if (!it1->second.Equals(it2->second)) {
      DVLOG(1) << "Extension states for profile " << profile1->GetDebugName()
               << " do not match profile " << profile2->GetDebugName();
      return false;
    }
    ++it1;
    ++it2;
  }
  return true;
}

void SyncExtensionHelper::SetupProfile(Profile* profile) {
  ExtensionSystem::Get(profile)->Init(true);
  profile_extensions_.insert(make_pair(profile, ExtensionNameMap()));
}

namespace {

std::string NameToPublicKey(const std::string& name) {
  std::string public_key;
  std::string pem;
  EXPECT_TRUE(Extension::ProducePEM(name, &pem) &&
              Extension::FormatPEMForFileOutput(pem, &public_key,
                                                true /* is_public */));
  return public_key;
}

// TODO(akalin): Somehow unify this with MakeExtension() in
// extension_util_unittest.cc.
scoped_refptr<Extension> CreateExtension(
    const FilePath& base_dir, const std::string& name,
    Extension::Type type) {
  DictionaryValue source;
  source.SetString(extension_manifest_keys::kName, name);
  const std::string& public_key = NameToPublicKey(name);
  source.SetString(extension_manifest_keys::kPublicKey, public_key);
  source.SetString(extension_manifest_keys::kVersion, "0.0.0.0");
  switch (type) {
    case Extension::TYPE_EXTENSION:
      // Do nothing.
      break;
    case Extension::TYPE_THEME:
      source.Set(extension_manifest_keys::kTheme, new DictionaryValue());
      break;
    case Extension::TYPE_HOSTED_APP:
    case Extension::TYPE_PACKAGED_APP:
      source.Set(extension_manifest_keys::kApp, new DictionaryValue());
      source.SetString(extension_manifest_keys::kLaunchWebURL,
                       "http://www.example.com");
      break;
    default:
      ADD_FAILURE();
      return NULL;
  }
  const FilePath sub_dir = FilePath().AppendASCII(name);
  FilePath extension_dir;
  if (!file_util::PathExists(base_dir) &&
      !file_util::CreateDirectory(base_dir) &&
      !file_util::CreateTemporaryDirInDir(
          base_dir, sub_dir.value(), &extension_dir)) {
    ADD_FAILURE();
    return NULL;
  }
  std::string error;
  scoped_refptr<Extension> extension =
      Extension::Create(extension_dir, Extension::INTERNAL,
                        source, Extension::STRICT_ERROR_CHECKS, &error);
  if (!error.empty()) {
    ADD_FAILURE() << error;
    return NULL;
  }
  if (!extension.get()) {
    ADD_FAILURE();
    return NULL;
  }
  if (extension->name() != name) {
    EXPECT_EQ(name, extension->name());
    return NULL;
  }
  if (extension->GetType() != type) {
    EXPECT_EQ(type, extension->GetType());
    return NULL;
  }
  return extension;
}

}  // namespace

scoped_refptr<Extension> SyncExtensionHelper::GetExtension(
    Profile* profile, const std::string& name,
    Extension::Type type) {
  if (name.empty()) {
    ADD_FAILURE();
    return NULL;
  }
  ProfileExtensionNameMap::iterator it = profile_extensions_.find(profile);
  if (it == profile_extensions_.end()) {
    ADD_FAILURE();
    return NULL;
  }
  ExtensionNameMap::const_iterator it2 = it->second.find(name);
  if (it2 != it->second.end()) {
    return it2->second;
  }

  scoped_refptr<Extension> extension =
      CreateExtension(profile->GetExtensionService()->install_directory(),
                      name, type);
  if (!extension.get()) {
    ADD_FAILURE();
    return NULL;
  }
  const std::string& expected_id = NameToId(name);
  if (extension->id() != expected_id) {
    EXPECT_EQ(expected_id, extension->id());
    return NULL;
  }
  DVLOG(2) << "created extension with name = "
           << name << ", id = " << expected_id;
  (it->second)[name] = extension;
  id_to_name_[expected_id] = name;
  return extension;
}