File: sync_extension_helper.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (430 lines) | stat: -rw-r--r-- 15,863 bytes parent folder | download | duplicates (3)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Copyright 2012 The Chromium Authors
// 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 <list>
#include <memory>
#include <utility>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/threading/thread_restrictions.h"
#include "base/uuid.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "components/crx_file/id_util.h"
#include "components/sync/model/string_ordinal.h"
#include "extensions/browser/disable_reason.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/extension_util.h"
#include "extensions/browser/install_flag.h"
#include "extensions/browser/pending_extension_info.h"
#include "extensions/browser/pending_extension_manager.h"
#include "extensions/browser/uninstall_reason.h"
#include "extensions/buildflags/buildflags.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_set.h"
#include "extensions/common/manifest_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

static_assert(BUILDFLAG(ENABLE_EXTENSIONS_CORE));

using extensions::Extension;
using extensions::ExtensionPrefs;
using extensions::Manifest;

const char kFakeExtensionPrefix[] = "fakeextension";

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

SyncExtensionHelper::SyncExtensionHelper() = default;

SyncExtensionHelper::~SyncExtensionHelper() = default;

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

  extension_name_prefix_ =
      kFakeExtensionPrefix + base::Uuid::GenerateRandomV4().AsLowercaseString();
  for (int i = 0; i < test->num_clients(); ++i) {
    SetupProfile(test->GetProfile(i));
  }
  if (test->UseVerifier()) {
    SetupProfile(test->verifier());
  }

  setup_completed_ = true;
}

std::string SyncExtensionHelper::InstallExtension(Profile* profile,
                                                  const std::string& name,
                                                  Manifest::Type type) {
  base::ScopedAllowBlockingForTesting allow_blocking;
  scoped_refptr<Extension> extension = GetExtension(profile, name, type);
  if (!extension.get()) {
    NOTREACHED() << "Could not install extension " << name;
  }
  extensions::ExtensionRegistrar::Get(profile)->OnExtensionInstalled(
      extension.get(), syncer::StringOrdinal(),
      extensions::kInstallFlagInstallImmediately);
  return extension->id();
}

void SyncExtensionHelper::UninstallExtension(Profile* profile,
                                             const std::string& name) {
  extensions::ExtensionRegistrar::Get(profile)->UninstallExtension(
      crx_file::id_util::GenerateId(name), extensions::UNINSTALL_REASON_SYNC,
      nullptr /* error */);
}

std::vector<std::string> SyncExtensionHelper::GetInstalledExtensionNames(
    Profile* profile) const {
  std::vector<std::string> names;

  const extensions::ExtensionSet extensions =
      extensions::ExtensionRegistry::Get(profile)
          ->GenerateInstalledExtensionsSet();
  for (const auto& extension : extensions) {
    names.push_back(extension->name());
  }

  return names;
}

void SyncExtensionHelper::EnableExtension(Profile* profile,
                                          const std::string& name) {
  extensions::ExtensionRegistrar::Get(profile)->EnableExtension(
      crx_file::id_util::GenerateId(name));
}

void SyncExtensionHelper::DisableExtension(Profile* profile,
                                           const std::string& name) {
  extensions::ExtensionRegistrar::Get(profile)->DisableExtension(
      crx_file::id_util::GenerateId(name),
      {extensions::disable_reason::DISABLE_USER_ACTION});
}

bool SyncExtensionHelper::IsExtensionEnabled(Profile* profile,
                                             const std::string& name) const {
  return extensions::ExtensionRegistrar::Get(profile)->IsExtensionEnabled(
      crx_file::id_util::GenerateId(name));
}

void SyncExtensionHelper::IncognitoEnableExtension(Profile* profile,
                                                   const std::string& name) {
  extensions::util::SetIsIncognitoEnabled(crx_file::id_util::GenerateId(name),
                                          profile, true);
}

void SyncExtensionHelper::IncognitoDisableExtension(Profile* profile,
                                                    const std::string& name) {
  extensions::util::SetIsIncognitoEnabled(crx_file::id_util::GenerateId(name),
                                          profile, false);
}

bool SyncExtensionHelper::IsIncognitoEnabled(Profile* profile,
                                             const std::string& name) const {
  return extensions::util::IsIncognitoEnabled(
      crx_file::id_util::GenerateId(name), profile);
}

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

void SyncExtensionHelper::InstallExtensionsPendingForSync(Profile* profile) {
  // 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 extensions::PendingExtensionManager* pending_extension_manager =
      extensions::PendingExtensionManager::Get(profile);

  std::list<std::string> pending_crx_ids =
      pending_extension_manager->GetPendingIdsForUpdateCheck();

  const extensions::PendingExtensionInfo* info = nullptr;
  for (const std::string& pending_crx_id : pending_crx_ids) {
    ASSERT_TRUE(info = pending_extension_manager->GetById(pending_crx_id));
    if (!info->is_from_sync()) {
      continue;
    }

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

SyncExtensionHelper::ExtensionState::ExtensionState(
    EnabledState state,
    const extensions::DisableReasonSet& reasons,
    bool incognito_enabled)
    : enabled_state(state),
      disable_reasons(reasons),
      incognito_enabled(incognito_enabled) {}

SyncExtensionHelper::ExtensionState::ExtensionState(ExtensionState&& other) =
    default;

SyncExtensionHelper::ExtensionState::~ExtensionState() = default;

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

  ExtensionStateMap extension_state_map;

  const extensions::ExtensionSet extensions =
      extensions::ExtensionRegistry::Get(profile)
          ->GenerateInstalledExtensionsSet();

  auto* extension_registrar = extensions::ExtensionRegistrar::Get(profile);
  for (const scoped_refptr<const Extension>& extension : extensions) {
    const std::string& id = extension->id();
    extension_state_map.emplace(
        id, ExtensionState{extension_registrar->IsExtensionEnabled(id)
                               ? ExtensionState::ENABLED
                               : ExtensionState::DISABLED,
                           ExtensionPrefs::Get(profile)->GetDisableReasons(id),
                           extensions::util::IsIncognitoEnabled(id, profile)});
    DVLOG(2) << "Extension " << id << " in profile " << profile_debug_name
             << " is "
             << (extension_registrar->IsExtensionEnabled(id) ? "enabled"
                                                             : "disabled");
  }

  const extensions::PendingExtensionManager* pending_extension_manager =
      extensions::PendingExtensionManager::Get(profile);

  std::list<std::string> pending_crx_ids =
      pending_extension_manager->GetPendingIdsForUpdateCheck();

  for (const std::string& id : pending_crx_ids) {
    extension_state_map.emplace(
        id, ExtensionState{ExtensionState::PENDING,
                           ExtensionPrefs::Get(profile)->GetDisableReasons(id),
                           extensions::util::IsIncognitoEnabled(id, profile)});
    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;
  }

  auto it1 = state_map1.begin();
  auto it2 = state_map2.begin();
  while (it1 != state_map1.end()) {
    const auto& [app_id1, app_state1] = *it1;
    const auto& [app_id2, app_state2] = *it2;
    if (app_id1 != app_id2) {
      DVLOG(1) << "Extensions for profile " << profile1->GetDebugName()
               << " do not match profile " << profile2->GetDebugName();
      return false;
    } else if (app_state1 != app_state2) {
      DVLOG(1) << "Extension states for profile " << profile1->GetDebugName()
               << " do not match profile " << profile2->GetDebugName();
      return false;
    }
    ++it1;
    ++it2;
  }
  return true;
}

std::string SyncExtensionHelper::CreateFakeExtensionName(int index) {
  return extension_name_prefix_ + base::NumberToString(index);
}

bool SyncExtensionHelper::ExtensionNameToIndex(const std::string& name,
                                               int* index) {
  if (!(base::StartsWith(name, extension_name_prefix_,
                         base::CompareCase::SENSITIVE) &&
        base::StringToInt(name.substr(extension_name_prefix_.size()), index))) {
    LOG(WARNING) << "Unable to convert extension name \"" << name
                 << "\" to index";
    return false;
  }
  return true;
}

void SyncExtensionHelper::SetupProfile(Profile* profile) {
  extensions::ExtensionSystem::Get(profile)->InitForRegularProfile(
      true /* extensions_enabled */);
  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 base::FilePath& base_dir,
                                         const std::string& name,
                                         Manifest::Type type) {
  base::Value::Dict source;
  source.SetByDottedPath(extensions::manifest_keys::kName, name);
  const std::string& public_key = NameToPublicKey(name);
  source.SetByDottedPath(extensions::manifest_keys::kPublicKey, public_key);
  source.SetByDottedPath(extensions::manifest_keys::kVersion, "0.0.0.0");
  source.SetByDottedPath(extensions::manifest_keys::kManifestVersion, 2);
  switch (type) {
    case Manifest::TYPE_EXTENSION:
      // Do nothing.
      break;
    case Manifest::TYPE_THEME:
      source.SetByDottedPath(extensions::manifest_keys::kTheme,
                             base::Value::Dict());
      break;
    case Manifest::TYPE_HOSTED_APP:
    case Manifest::TYPE_LEGACY_PACKAGED_APP:
      source.SetByDottedPath(extensions::manifest_keys::kApp,
                             base::Value::Dict());
      source.SetByDottedPath(extensions::manifest_keys::kLaunchWebURL,
                             "http://www.example.com");
      break;
    case Manifest::TYPE_PLATFORM_APP: {
      source.SetByDottedPath(extensions::manifest_keys::kApp,
                             base::Value::Dict());
      source.SetByDottedPath(extensions::manifest_keys::kPlatformAppBackground,
                             base::Value::Dict());
      base::Value::List scripts;
      scripts.Append("main.js");
      source.SetByDottedPath(
          extensions::manifest_keys::kPlatformAppBackgroundScripts,
          std::move(scripts));
      break;
    }
    default:
      ADD_FAILURE();
      return nullptr;
  }
  const base::FilePath sub_dir = base::FilePath().AppendASCII(name);
  base::FilePath extension_dir;
  if (!base::PathExists(base_dir) && !base::CreateDirectory(base_dir)) {
    ADD_FAILURE();
    return nullptr;
  }
  if (!base::CreateTemporaryDirInDir(base_dir, sub_dir.value(),
                                     &extension_dir)) {
    ADD_FAILURE();
    return nullptr;
  }
  std::string error;
  scoped_refptr<Extension> extension = Extension::Create(
      extension_dir, extensions::mojom::ManifestLocation::kInternal, source,
      Extension::NO_FLAGS, &error);
  if (!error.empty()) {
    ADD_FAILURE() << error;
    return nullptr;
  }
  if (!extension.get()) {
    ADD_FAILURE();
    return nullptr;
  }
  if (extension->name() != name) {
    EXPECT_EQ(name, extension->name());
    return nullptr;
  }
  if (extension->GetType() != type) {
    EXPECT_EQ(type, extension->GetType());
    return nullptr;
  }
  return extension;
}

}  // namespace

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

  scoped_refptr<Extension> extension = CreateExtension(
      extensions::ExtensionRegistrar::Get(profile)->install_directory(), name,
      type);
  if (!extension.get()) {
    ADD_FAILURE();
    return nullptr;
  }
  const std::string& expected_id = crx_file::id_util::GenerateId(name);
  if (extension->id() != expected_id) {
    EXPECT_EQ(expected_id, extension->id());
    return nullptr;
  }
  DVLOG(2) << "created extension with name = " << name
           << ", id = " << expected_id;
  (it->second)[name] = extension;
  id_to_name_[expected_id] = name;
  id_to_type_[expected_id] = type;
  return extension;
}