| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/test/integration/sync_extension_helper.h" | 5 #include "chrome/browser/sync/test/integration/sync_extension_helper.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/extensions/extension_system.h" | 12 #include "chrome/browser/extensions/extension_system.h" |
| 13 #include "chrome/browser/extensions/pending_extension_info.h" | 13 #include "chrome/browser/extensions/pending_extension_info.h" |
| 14 #include "chrome/browser/extensions/pending_extension_manager.h" | 14 #include "chrome/browser/extensions/pending_extension_manager.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" | 16 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" |
| 17 #include "chrome/browser/sync/test/integration/sync_test.h" | 17 #include "chrome/browser/sync/test/integration/sync_test.h" |
| 18 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
| 19 #include "chrome/common/extensions/extension_manifest_constants.h" | 19 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 20 #include "extensions/common/id_util.h" |
| 20 #include "sync/api/string_ordinal.h" | 21 #include "sync/api/string_ordinal.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 23 |
| 23 using extensions::Extension; | 24 using extensions::Extension; |
| 24 using extensions::Manifest; | 25 using extensions::Manifest; |
| 25 | 26 |
| 26 SyncExtensionHelper::ExtensionState::ExtensionState() | 27 SyncExtensionHelper::ExtensionState::ExtensionState() |
| 27 : enabled_state(ENABLED), incognito_enabled(false) {} | 28 : enabled_state(ENABLED), incognito_enabled(false) {} |
| 28 | 29 |
| 29 SyncExtensionHelper::ExtensionState::~ExtensionState() {} | 30 SyncExtensionHelper::ExtensionState::~ExtensionState() {} |
| 30 | 31 |
| 31 bool SyncExtensionHelper::ExtensionState::Equals( | 32 bool SyncExtensionHelper::ExtensionState::Equals( |
| 32 const SyncExtensionHelper::ExtensionState &other) const { | 33 const SyncExtensionHelper::ExtensionState &other) const { |
| 33 return ((enabled_state == other.enabled_state) && | 34 return ((enabled_state == other.enabled_state) && |
| 34 (incognito_enabled == other.incognito_enabled)); | 35 (incognito_enabled == other.incognito_enabled)); |
| 35 } | 36 } |
| 36 | 37 |
| 37 // static | 38 // static |
| 38 SyncExtensionHelper* SyncExtensionHelper::GetInstance() { | 39 SyncExtensionHelper* SyncExtensionHelper::GetInstance() { |
| 39 SyncExtensionHelper* instance = Singleton<SyncExtensionHelper>::get(); | 40 SyncExtensionHelper* instance = Singleton<SyncExtensionHelper>::get(); |
| 40 instance->SetupIfNecessary(sync_datatype_helper::test()); | 41 instance->SetupIfNecessary(sync_datatype_helper::test()); |
| 41 return instance; | 42 return instance; |
| 42 } | 43 } |
| 43 | 44 |
| 44 SyncExtensionHelper::SyncExtensionHelper() : setup_completed_(false) {} | 45 SyncExtensionHelper::SyncExtensionHelper() : setup_completed_(false) {} |
| 45 | 46 |
| 46 SyncExtensionHelper::~SyncExtensionHelper() {} | 47 SyncExtensionHelper::~SyncExtensionHelper() {} |
| 47 | 48 |
| 48 // static | |
| 49 std::string SyncExtensionHelper::NameToId(const std::string& name) { | |
| 50 std::string id; | |
| 51 EXPECT_TRUE(Extension::GenerateId(name, &id)); | |
| 52 return id; | |
| 53 } | |
| 54 | |
| 55 void SyncExtensionHelper::SetupIfNecessary(SyncTest* test) { | 49 void SyncExtensionHelper::SetupIfNecessary(SyncTest* test) { |
| 56 if (setup_completed_) | 50 if (setup_completed_) |
| 57 return; | 51 return; |
| 58 | 52 |
| 59 for (int i = 0; i < test->num_clients(); ++i) { | 53 for (int i = 0; i < test->num_clients(); ++i) { |
| 60 SetupProfile(test->GetProfile(i)); | 54 SetupProfile(test->GetProfile(i)); |
| 61 } | 55 } |
| 62 SetupProfile(test->verifier()); | 56 SetupProfile(test->verifier()); |
| 63 | 57 |
| 64 setup_completed_ = true; | 58 setup_completed_ = true; |
| 65 } | 59 } |
| 66 | 60 |
| 67 std::string SyncExtensionHelper::InstallExtension( | 61 std::string SyncExtensionHelper::InstallExtension( |
| 68 Profile* profile, const std::string& name, Manifest::Type type) { | 62 Profile* profile, const std::string& name, Manifest::Type type) { |
| 69 scoped_refptr<Extension> extension = GetExtension(profile, name, type); | 63 scoped_refptr<Extension> extension = GetExtension(profile, name, type); |
| 70 if (!extension.get()) { | 64 if (!extension.get()) { |
| 71 NOTREACHED() << "Could not install extension " << name; | 65 NOTREACHED() << "Could not install extension " << name; |
| 72 return ""; | 66 return ""; |
| 73 } | 67 } |
| 74 profile->GetExtensionService()->OnExtensionInstalled( | 68 profile->GetExtensionService()->OnExtensionInstalled( |
| 75 extension, syncer::StringOrdinal(), false /* no requirement errors */, | 69 extension, syncer::StringOrdinal(), false /* no requirement errors */, |
| 76 false /* don't wait for idle to install */); | 70 false /* don't wait for idle to install */); |
| 77 return extension->id(); | 71 return extension->id(); |
| 78 } | 72 } |
| 79 | 73 |
| 80 void SyncExtensionHelper::UninstallExtension( | 74 void SyncExtensionHelper::UninstallExtension( |
| 81 Profile* profile, const std::string& name) { | 75 Profile* profile, const std::string& name) { |
| 82 ExtensionService::UninstallExtensionHelper(profile->GetExtensionService(), | 76 ExtensionService::UninstallExtensionHelper( |
| 83 NameToId(name)); | 77 profile->GetExtensionService(), |
| 78 extensions::id_util::GenerateId(name)); |
| 84 } | 79 } |
| 85 | 80 |
| 86 std::vector<std::string> SyncExtensionHelper::GetInstalledExtensionNames( | 81 std::vector<std::string> SyncExtensionHelper::GetInstalledExtensionNames( |
| 87 Profile* profile) const { | 82 Profile* profile) const { |
| 88 std::vector<std::string> names; | 83 std::vector<std::string> names; |
| 89 ExtensionService* extension_service = profile->GetExtensionService(); | 84 ExtensionService* extension_service = profile->GetExtensionService(); |
| 90 | 85 |
| 91 scoped_ptr<const ExtensionSet> extensions( | 86 scoped_ptr<const ExtensionSet> extensions( |
| 92 extension_service->GenerateInstalledExtensionsSet()); | 87 extension_service->GenerateInstalledExtensionsSet()); |
| 93 for (ExtensionSet::const_iterator it = extensions->begin(); | 88 for (ExtensionSet::const_iterator it = extensions->begin(); |
| 94 it != extensions->end(); ++it) { | 89 it != extensions->end(); ++it) { |
| 95 names.push_back((*it)->name()); | 90 names.push_back((*it)->name()); |
| 96 } | 91 } |
| 97 | 92 |
| 98 return names; | 93 return names; |
| 99 } | 94 } |
| 100 | 95 |
| 101 void SyncExtensionHelper::EnableExtension(Profile* profile, | 96 void SyncExtensionHelper::EnableExtension(Profile* profile, |
| 102 const std::string& name) { | 97 const std::string& name) { |
| 103 profile->GetExtensionService()->EnableExtension(NameToId(name)); | 98 profile->GetExtensionService()->EnableExtension( |
| 99 extensions::id_util::GenerateId(name)); |
| 104 } | 100 } |
| 105 | 101 |
| 106 void SyncExtensionHelper::DisableExtension(Profile* profile, | 102 void SyncExtensionHelper::DisableExtension(Profile* profile, |
| 107 const std::string& name) { | 103 const std::string& name) { |
| 108 profile->GetExtensionService()->DisableExtension( | 104 profile->GetExtensionService()->DisableExtension( |
| 109 NameToId(name), Extension::DISABLE_USER_ACTION); | 105 extensions::id_util::GenerateId(name), Extension::DISABLE_USER_ACTION); |
| 110 } | 106 } |
| 111 | 107 |
| 112 bool SyncExtensionHelper::IsExtensionEnabled( | 108 bool SyncExtensionHelper::IsExtensionEnabled( |
| 113 Profile* profile, const std::string& name) const { | 109 Profile* profile, const std::string& name) const { |
| 114 return profile->GetExtensionService()->IsExtensionEnabled(NameToId(name)); | 110 return profile->GetExtensionService()->IsExtensionEnabled( |
| 111 extensions::id_util::GenerateId(name)); |
| 115 } | 112 } |
| 116 | 113 |
| 117 void SyncExtensionHelper::IncognitoEnableExtension( | 114 void SyncExtensionHelper::IncognitoEnableExtension( |
| 118 Profile* profile, const std::string& name) { | 115 Profile* profile, const std::string& name) { |
| 119 profile->GetExtensionService()->SetIsIncognitoEnabled(NameToId(name), true); | 116 profile->GetExtensionService()->SetIsIncognitoEnabled( |
| 117 extensions::id_util::GenerateId(name), true); |
| 120 } | 118 } |
| 121 | 119 |
| 122 void SyncExtensionHelper::IncognitoDisableExtension( | 120 void SyncExtensionHelper::IncognitoDisableExtension( |
| 123 Profile* profile, const std::string& name) { | 121 Profile* profile, const std::string& name) { |
| 124 profile->GetExtensionService()->SetIsIncognitoEnabled(NameToId(name), false); | 122 profile->GetExtensionService()->SetIsIncognitoEnabled( |
| 123 extensions::id_util::GenerateId(name), false); |
| 125 } | 124 } |
| 126 | 125 |
| 127 bool SyncExtensionHelper::IsIncognitoEnabled( | 126 bool SyncExtensionHelper::IsIncognitoEnabled( |
| 128 Profile* profile, const std::string& name) const { | 127 Profile* profile, const std::string& name) const { |
| 129 return profile->GetExtensionService()->IsIncognitoEnabled(NameToId(name)); | 128 return profile->GetExtensionService()->IsIncognitoEnabled( |
| 129 extensions::id_util::GenerateId(name)); |
| 130 } | 130 } |
| 131 | 131 |
| 132 | 132 |
| 133 bool SyncExtensionHelper::IsExtensionPendingInstallForSync( | 133 bool SyncExtensionHelper::IsExtensionPendingInstallForSync( |
| 134 Profile* profile, const std::string& id) const { | 134 Profile* profile, const std::string& id) const { |
| 135 const extensions::PendingExtensionManager* pending_extension_manager = | 135 const extensions::PendingExtensionManager* pending_extension_manager = |
| 136 profile->GetExtensionService()->pending_extension_manager(); | 136 profile->GetExtensionService()->pending_extension_manager(); |
| 137 const extensions::PendingExtensionInfo* info = | 137 const extensions::PendingExtensionInfo* info = |
| 138 pending_extension_manager->GetById(id); | 138 pending_extension_manager->GetById(id); |
| 139 if (!info) | 139 if (!info) |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 return it2->second; | 354 return it2->second; |
| 355 } | 355 } |
| 356 | 356 |
| 357 scoped_refptr<Extension> extension = | 357 scoped_refptr<Extension> extension = |
| 358 CreateExtension(profile->GetExtensionService()->install_directory(), | 358 CreateExtension(profile->GetExtensionService()->install_directory(), |
| 359 name, type); | 359 name, type); |
| 360 if (!extension.get()) { | 360 if (!extension.get()) { |
| 361 ADD_FAILURE(); | 361 ADD_FAILURE(); |
| 362 return NULL; | 362 return NULL; |
| 363 } | 363 } |
| 364 const std::string& expected_id = NameToId(name); | 364 const std::string& expected_id = extensions::id_util::GenerateId(name); |
| 365 if (extension->id() != expected_id) { | 365 if (extension->id() != expected_id) { |
| 366 EXPECT_EQ(expected_id, extension->id()); | 366 EXPECT_EQ(expected_id, extension->id()); |
| 367 return NULL; | 367 return NULL; |
| 368 } | 368 } |
| 369 DVLOG(2) << "created extension with name = " | 369 DVLOG(2) << "created extension with name = " |
| 370 << name << ", id = " << expected_id; | 370 << name << ", id = " << expected_id; |
| 371 (it->second)[name] = extension; | 371 (it->second)[name] = extension; |
| 372 id_to_name_[expected_id] = name; | 372 id_to_name_[expected_id] = name; |
| 373 id_to_type_[expected_id] = type; | 373 id_to_type_[expected_id] = type; |
| 374 return extension; | 374 return extension; |
| 375 } | 375 } |
| OLD | NEW |