extensions_helper.cc revision 1e9bf3e0803691d0a228da41fc608347b6db4340
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/sync/test/integration/extensions_helper.h"
6
7#include <cstring>
8
9#include "base/logging.h"
10#include "base/strings/string_number_conversions.h"
11#include "base/strings/string_util.h"
12#include "chrome/browser/profiles/profile.h"
13#include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
14#include "chrome/browser/sync/test/integration/sync_extension_helper.h"
15#include "extensions/common/manifest.h"
16
17using sync_datatype_helper::test;
18
19namespace extensions_helper {
20
21const char extension_name_prefix[] = "fakeextension";
22
23bool HasSameExtensionsAsVerifier(int index) {
24  return SyncExtensionHelper::GetInstance()->ExtensionStatesMatch(
25      test()->GetProfile(index), test()->verifier());
26}
27
28bool AllProfilesHaveSameExtensionsAsVerifier() {
29  for (int i = 0; i < test()->num_clients(); ++i) {
30    if (!HasSameExtensionsAsVerifier(i)) {
31      LOG(ERROR) << "Profile " << i << " doesn't have the same extensions as"
32                                       " the verifier profile.";
33      return false;
34    }
35  }
36  return true;
37}
38
39bool AllProfilesHaveSameExtensions() {
40  for (int i = 1; i < test()->num_clients(); ++i) {
41    if (!SyncExtensionHelper::GetInstance()->ExtensionStatesMatch(
42        test()->GetProfile(0), test()->GetProfile(i))) {
43      LOG(ERROR) << "Profile " << i << " doesnt have the same extensions as"
44                                       " profile 0.";
45      return false;
46    }
47  }
48  return true;
49}
50
51
52std::string InstallExtension(Profile* profile, int index) {
53  return SyncExtensionHelper::GetInstance()->InstallExtension(
54      profile,
55      CreateFakeExtensionName(index),
56      extensions::Manifest::TYPE_EXTENSION);
57}
58
59std::string InstallExtensionForAllProfiles(int index) {
60  for (int i = 0; i < test()->num_clients(); ++i)
61    InstallExtension(test()->GetProfile(i), index);
62  return InstallExtension(test()->verifier(), index);
63}
64
65void UninstallExtension(Profile* profile, int index) {
66  return SyncExtensionHelper::GetInstance()->UninstallExtension(
67      profile, CreateFakeExtensionName(index));
68}
69
70std::vector<int> GetInstalledExtensions(Profile* profile) {
71  std::vector<int> indices;
72  std::vector<std::string> names =
73      SyncExtensionHelper::GetInstance()->GetInstalledExtensionNames(profile);
74  for (std::vector<std::string>::const_iterator it = names.begin();
75       it != names.end(); ++it) {
76    int index;
77    if (ExtensionNameToIndex(*it, &index)) {
78      indices.push_back(index);
79    }
80  }
81  return indices;
82}
83
84bool IsExtensionEnabled(Profile* profile, int index) {
85  return SyncExtensionHelper::GetInstance()->IsExtensionEnabled(
86      profile, CreateFakeExtensionName(index));
87}
88
89void IncognitoEnableExtension(Profile* profile, int index) {
90  return SyncExtensionHelper::GetInstance()->IncognitoEnableExtension(
91      profile, CreateFakeExtensionName(index));
92}
93
94void IncognitoDisableExtension(Profile* profile, int index) {
95  return SyncExtensionHelper::GetInstance()->IncognitoDisableExtension(
96      profile, CreateFakeExtensionName(index));
97}
98
99bool IsIncognitoEnabled(Profile* profile, int index) {
100  return SyncExtensionHelper::GetInstance()->IsIncognitoEnabled(
101      profile, CreateFakeExtensionName(index));
102}
103
104void InstallExtensionsPendingForSync(Profile* profile) {
105  SyncExtensionHelper::GetInstance()->InstallExtensionsPendingForSync(profile);
106}
107
108std::string CreateFakeExtensionName(int index) {
109  return extension_name_prefix + base::IntToString(index);
110}
111
112bool ExtensionNameToIndex(const std::string& name, int* index) {
113  if (!StartsWithASCII(name, extension_name_prefix, true) ||
114      !base::StringToInt(name.substr(strlen(extension_name_prefix)), index)) {
115    LOG(WARNING) << "Unable to convert extension name \"" << name
116                 << "\" to index";
117    return false;
118  }
119  return true;
120}
121
122}  // namespace extensions_helper
123