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 "chrome/common/extensions/manifest.h" 16 17 using sync_datatype_helper::test; 18 19 namespace extensions_helper { 20 21 const char extension_name_prefix[] = "fakeextension"; 22 23 bool HasSameExtensionsAsVerifier(int index) { 24 return SyncExtensionHelper::GetInstance()->ExtensionStatesMatch( 25 test()->GetProfile(index), test()->verifier()); 26 } 27 28 bool 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 39 bool 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 52 std::string InstallExtension(Profile* profile, int index) { 53 return SyncExtensionHelper::GetInstance()->InstallExtension( 54 profile, 55 CreateFakeExtensionName(index), 56 extensions::Manifest::TYPE_EXTENSION); 57 } 58 59 std::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 65 void UninstallExtension(Profile* profile, int index) { 66 return SyncExtensionHelper::GetInstance()->UninstallExtension( 67 profile, CreateFakeExtensionName(index)); 68 } 69 70 std::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 84 void EnableExtension(Profile* profile, int index) { 85 return SyncExtensionHelper::GetInstance()->EnableExtension( 86 profile, CreateFakeExtensionName(index)); 87 } 88 89 void DisableExtension(Profile* profile, int index) { 90 return SyncExtensionHelper::GetInstance()->DisableExtension( 91 profile, CreateFakeExtensionName(index)); 92 } 93 94 bool IsExtensionEnabled(Profile* profile, int index) { 95 return SyncExtensionHelper::GetInstance()->IsExtensionEnabled( 96 profile, CreateFakeExtensionName(index)); 97 } 98 99 void IncognitoEnableExtension(Profile* profile, int index) { 100 return SyncExtensionHelper::GetInstance()->IncognitoEnableExtension( 101 profile, CreateFakeExtensionName(index)); 102 } 103 104 void IncognitoDisableExtension(Profile* profile, int index) { 105 return SyncExtensionHelper::GetInstance()->IncognitoDisableExtension( 106 profile, CreateFakeExtensionName(index)); 107 } 108 109 bool IsIncognitoEnabled(Profile* profile, int index) { 110 return SyncExtensionHelper::GetInstance()->IsIncognitoEnabled( 111 profile, CreateFakeExtensionName(index)); 112 } 113 114 void InstallExtensionsPendingForSync(Profile* profile) { 115 SyncExtensionHelper::GetInstance()->InstallExtensionsPendingForSync(profile); 116 } 117 118 std::string CreateFakeExtensionName(int index) { 119 return extension_name_prefix + base::IntToString(index); 120 } 121 122 bool ExtensionNameToIndex(const std::string& name, int* index) { 123 if (!StartsWithASCII(name, extension_name_prefix, true) || 124 !base::StringToInt(name.substr(strlen(extension_name_prefix)), index)) { 125 LOG(WARNING) << "Unable to convert extension name \"" << name 126 << "\" to index"; 127 return false; 128 } 129 return true; 130 } 131 132 } // namespace extensions_helper 133