| 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/download/download_crx_util.h" | 5 #include "chrome/browser/download/download_crx_util.h" |
| 6 #include "chrome/browser/download/download_service.h" | 6 #include "chrome/browser/download/download_service.h" |
| 7 #include "chrome/browser/download/download_service_factory.h" | 7 #include "chrome/browser/download/download_service_factory.h" |
| 8 #include "chrome/browser/download/download_test_observer.h" | 8 #include "chrome/browser/download/download_test_observer.h" |
| 9 #include "chrome/browser/extensions/crx_installer.h" | 9 #include "chrome/browser/extensions/crx_installer.h" |
| 10 #include "chrome/browser/extensions/extension_browsertest.h" | 10 #include "chrome/browser/extensions/extension_browsertest.h" |
| 11 #include "chrome/browser/extensions/extension_install_prompt.h" | 11 #include "chrome/browser/extensions/extension_install_prompt.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
| 16 #include "chrome/common/extensions/extension.h" | |
| 17 #include "chrome/common/extensions/extension_file_util.h" | 16 #include "chrome/common/extensions/extension_file_util.h" |
| 18 #include "chrome/common/extensions/extension_switch_utils.h" | 17 #include "chrome/common/extensions/extension_switch_utils.h" |
| 19 #include "chrome/common/extensions/permissions/permission_set.h" | |
| 20 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
| 21 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
| 23 | 21 |
| 24 class SkBitmap; | 22 class SkBitmap; |
| 25 | 23 |
| 26 using extensions::Extension; | |
| 27 | |
| 28 namespace { | 24 namespace { |
| 29 | 25 |
| 30 // Observer waits for exactly one download to finish. | 26 // Observer waits for exactly one download to finish. |
| 31 | 27 |
| 32 class MockInstallPrompt : public ExtensionInstallPrompt { | 28 class MockInstallPrompt : public ExtensionInstallPrompt { |
| 33 public: | 29 public: |
| 34 explicit MockInstallPrompt(Browser* browser) : | 30 explicit MockInstallPrompt(Browser* browser) : |
| 35 ExtensionInstallPrompt(browser), | 31 ExtensionInstallPrompt(browser), |
| 36 confirmation_requested_(false), | 32 did_succeed_(false), |
| 37 extension_(NULL) {} | 33 confirmation_requested_(false) {} |
| 38 | 34 |
| 39 bool did_succeed() const { return !!extension_; } | 35 bool did_succeed() const { return did_succeed_; } |
| 40 const Extension* extension() const { return extension_; } | |
| 41 bool confirmation_requested() const { return confirmation_requested_; } | 36 bool confirmation_requested() const { return confirmation_requested_; } |
| 42 const string16& error() const { return error_; } | 37 const string16& error() const { return error_; } |
| 43 void set_record_oauth2_grant(bool record) { record_oauth2_grant_ = record; } | |
| 44 | 38 |
| 45 // Overriding some of the ExtensionInstallUI API. | 39 // Overriding some of the ExtensionInstallUI API. |
| 46 void ConfirmInstall(Delegate* delegate, | 40 void ConfirmInstall(Delegate* delegate, |
| 47 const Extension* extension) { | 41 const extensions::Extension* extension) { |
| 48 confirmation_requested_ = true; | 42 confirmation_requested_ = true; |
| 49 delegate->InstallUIProceed(); | 43 delegate->InstallUIProceed(); |
| 50 } | 44 } |
| 51 void OnInstallSuccess(const Extension* extension, | 45 void OnInstallSuccess(const extensions::Extension* extension, |
| 52 SkBitmap* icon) { | 46 SkBitmap* icon) { |
| 53 extension_ = extension; | 47 did_succeed_ = true; |
| 54 MessageLoopForUI::current()->Quit(); | 48 MessageLoopForUI::current()->Quit(); |
| 55 } | 49 } |
| 56 void OnInstallFailure(const CrxInstallerError& error) { | 50 void OnInstallFailure(const CrxInstallerError& error) { |
| 57 error_ = error.message(); | 51 error_ = error.message(); |
| 58 MessageLoopForUI::current()->Quit(); | 52 MessageLoopForUI::current()->Quit(); |
| 59 } | 53 } |
| 60 | 54 |
| 61 private: | 55 private: |
| 56 bool did_succeed_; |
| 62 bool confirmation_requested_; | 57 bool confirmation_requested_; |
| 63 string16 error_; | 58 string16 error_; |
| 64 const Extension* extension_; | |
| 65 }; | 59 }; |
| 66 | 60 |
| 67 } // namespace | 61 } // namespace |
| 68 | 62 |
| 69 class ExtensionCrxInstallerTest : public ExtensionBrowserTest { | 63 class ExtensionCrxInstallerTest : public ExtensionBrowserTest { |
| 70 public: | 64 public: |
| 71 // Installs a crx from |crx_relpath| (a path relative to the extension test | 65 // Installs a crx from |crx_relpath| (a path relative to the extension test |
| 72 // data dir) with expected id |id|. Returns the installer. | 66 // data dir) with expected id |id|. Returns whether a confirmation prompt |
| 73 scoped_refptr<CrxInstaller> InstallWithPrompt( | 67 // happened or not. |
| 74 const std::string& ext_relpath, | 68 bool DidWhitelistInstallPrompt(const std::string& ext_relpath, |
| 75 const std::string& id, | 69 const std::string& id) { |
| 76 MockInstallPrompt* mock_install_prompt) { | |
| 77 ExtensionService* service = browser()->profile()->GetExtensionService(); | 70 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 71 MockInstallPrompt* mock_install_prompt = new MockInstallPrompt(browser()); |
| 78 FilePath ext_path = test_data_dir_.AppendASCII(ext_relpath); | 72 FilePath ext_path = test_data_dir_.AppendASCII(ext_relpath); |
| 79 | 73 |
| 80 std::string error; | 74 std::string error; |
| 81 base::DictionaryValue* parsed_manifest = | 75 base::DictionaryValue* parsed_manifest = |
| 82 extension_file_util::LoadManifest(ext_path, &error); | 76 extension_file_util::LoadManifest(ext_path, &error); |
| 83 if (!parsed_manifest) | 77 if (!parsed_manifest) |
| 84 return scoped_refptr<CrxInstaller>(); | 78 return false; |
| 85 | 79 |
| 86 scoped_ptr<WebstoreInstaller::Approval> approval; | 80 scoped_ptr<WebstoreInstaller::Approval> approval( |
| 87 if (!id.empty()) { | 81 WebstoreInstaller::Approval::CreateWithNoInstallPrompt( |
| 88 approval = WebstoreInstaller::Approval::CreateWithNoInstallPrompt( | 82 browser()->profile(), |
| 89 browser()->profile(), | 83 id, |
| 90 id, | 84 scoped_ptr<base::DictionaryValue>(parsed_manifest))); |
| 91 scoped_ptr<base::DictionaryValue>(parsed_manifest)); | |
| 92 } | |
| 93 | 85 |
| 94 scoped_refptr<CrxInstaller> installer( | 86 scoped_refptr<CrxInstaller> installer( |
| 95 CrxInstaller::Create(service, | 87 CrxInstaller::Create(service, |
| 96 mock_install_prompt, /* ownership transferred */ | 88 mock_install_prompt, /* ownership transferred */ |
| 97 approval.get() /* keep ownership */)); | 89 approval.get() /* keep ownership */)); |
| 98 installer->set_allow_silent_install(true); | 90 installer->set_allow_silent_install(true); |
| 99 installer->set_is_gallery_install(true); | 91 installer->set_is_gallery_install(true); |
| 100 installer->InstallCrx(PackExtension(ext_path)); | 92 installer->InstallCrx(PackExtension(ext_path)); |
| 101 ui_test_utils::RunMessageLoop(); | 93 ui_test_utils::RunMessageLoop(); |
| 102 | 94 |
| 103 EXPECT_TRUE(mock_install_prompt->did_succeed()); | 95 EXPECT_TRUE(mock_install_prompt->did_succeed()); |
| 104 return installer; | 96 return mock_install_prompt->confirmation_requested(); |
| 105 } | |
| 106 | |
| 107 // Installs an extension and checks that it has scopes granted IFF | |
| 108 // |record_oauth2_grant| is true. | |
| 109 void CheckHasEmptyScopesAfterInstall(const std::string& ext_relpath, | |
| 110 bool record_oauth2_grant) { | |
| 111 CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 112 switches::kEnableExperimentalExtensionApis); | |
| 113 | |
| 114 ExtensionService* service = browser()->profile()->GetExtensionService(); | |
| 115 | |
| 116 MockInstallPrompt* mock_prompt = new MockInstallPrompt(browser()); | |
| 117 mock_prompt->set_record_oauth2_grant(record_oauth2_grant); | |
| 118 scoped_refptr<CrxInstaller> installer = | |
| 119 InstallWithPrompt("browsertest/scopes", std::string(), mock_prompt); | |
| 120 | |
| 121 scoped_refptr<extensions::PermissionSet> permissions = | |
| 122 service->extension_prefs()->GetGrantedPermissions( | |
| 123 mock_prompt->extension()->id()); | |
| 124 ASSERT_TRUE(permissions.get()); | |
| 125 EXPECT_NE(record_oauth2_grant, permissions->scopes().empty()); | |
| 126 } | 97 } |
| 127 }; | 98 }; |
| 128 | 99 |
| 129 #if defined(OS_CHROMEOS) | 100 #if defined(OS_CHROMEOS) |
| 130 #define MAYBE_Whitelisting DISABLED_Whitelisting | 101 #define MAYBE_Whitelisting DISABLED_Whitelisting |
| 131 #else | 102 #else |
| 132 #define MAYBE_Whitelisting Whitelisting | 103 #define MAYBE_Whitelisting Whitelisting |
| 133 #endif | 104 #endif |
| 134 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, MAYBE_Whitelisting) { | 105 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, MAYBE_Whitelisting) { |
| 135 std::string id = "hdgllgikmikobbofgnabhfimcfoopgnd"; | 106 std::string id = "hdgllgikmikobbofgnabhfimcfoopgnd"; |
| 136 ExtensionService* service = browser()->profile()->GetExtensionService(); | 107 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 137 | 108 |
| 138 // Even whitelisted extensions with NPAPI should not prompt. | 109 // Even whitelisted extensions with NPAPI should not prompt. |
| 139 MockInstallPrompt* mock_prompt = new MockInstallPrompt(browser()); | 110 EXPECT_FALSE(DidWhitelistInstallPrompt("uitest/plugins", id)); |
| 140 scoped_refptr<CrxInstaller> installer = | |
| 141 InstallWithPrompt("uitest/plugins", id, mock_prompt); | |
| 142 EXPECT_FALSE(mock_prompt->confirmation_requested()); | |
| 143 EXPECT_TRUE(service->GetExtensionById(id, false)); | 111 EXPECT_TRUE(service->GetExtensionById(id, false)); |
| 144 } | 112 } |
| 145 | 113 |
| 146 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, | 114 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, |
| 147 GalleryInstallGetsExperimental) { | 115 GalleryInstallGetsExperimental) { |
| 148 // We must modify the command line temporarily in order to pack an extension | 116 // We must modify the command line temporarily in order to pack an extension |
| 149 // that requests the experimental permission. | 117 // that requests the experimental permission. |
| 150 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 118 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 151 CommandLine old_command_line = *command_line; | 119 CommandLine old_command_line = *command_line; |
| 152 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); | 120 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); |
| 153 FilePath crx_path = PackExtension( | 121 FilePath crx_path = PackExtension( |
| 154 test_data_dir_.AppendASCII("experimental")); | 122 test_data_dir_.AppendASCII("experimental")); |
| 155 ASSERT_FALSE(crx_path.empty()); | 123 ASSERT_FALSE(crx_path.empty()); |
| 156 | 124 |
| 157 // Now reset the command line so that we are testing specifically whether | 125 // Now reset the command line so that we are testing specifically whether |
| 158 // installing from webstore enables experimental permissions. | 126 // installing from webstore enables experimental permissions. |
| 159 *(CommandLine::ForCurrentProcess()) = old_command_line; | 127 *(CommandLine::ForCurrentProcess()) = old_command_line; |
| 160 | 128 |
| 161 EXPECT_FALSE(InstallExtension(crx_path, 0)); | 129 EXPECT_FALSE(InstallExtension(crx_path, 0)); |
| 162 EXPECT_TRUE(InstallExtensionFromWebstore(crx_path, 1)); | 130 EXPECT_TRUE(InstallExtensionFromWebstore(crx_path, 1)); |
| 163 } | 131 } |
| 164 | 132 |
| 165 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, PlatformAppCrx) { | 133 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, PlatformAppCrx) { |
| 166 CommandLine::ForCurrentProcess()->AppendSwitch( | 134 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 167 switches::kEnableExperimentalExtensionApis); | 135 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); |
| 168 EXPECT_TRUE(InstallExtension( | 136 EXPECT_TRUE(InstallExtension( |
| 169 test_data_dir_.AppendASCII("minimal_platform_app.crx"), 1)); | 137 test_data_dir_.AppendASCII("minimal_platform_app.crx"), 1)); |
| 170 } | 138 } |
| 171 | 139 |
| 172 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, PackAndInstallExtension) { | 140 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, PackAndInstallExtension) { |
| 173 if (!extensions::switch_utils::IsEasyOffStoreInstallEnabled()) | 141 if (!extensions::switch_utils::IsEasyOffStoreInstallEnabled()) |
| 174 return; | 142 return; |
| 175 | 143 |
| 176 const int kNumDownloadsExpected = 1; | 144 const int kNumDownloadsExpected = 1; |
| 177 const bool kExpectFileSelectDialog = false; | 145 const bool kExpectFileSelectDialog = false; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 198 LOG(ERROR) << "PackAndInstallExtension: Navigating to URL"; | 166 LOG(ERROR) << "PackAndInstallExtension: Navigating to URL"; |
| 199 ui_test_utils::NavigateToURLWithDisposition(browser(), url, CURRENT_TAB, | 167 ui_test_utils::NavigateToURLWithDisposition(browser(), url, CURRENT_TAB, |
| 200 ui_test_utils::BROWSER_TEST_NONE); | 168 ui_test_utils::BROWSER_TEST_NONE); |
| 201 | 169 |
| 202 EXPECT_TRUE(WaitForCrxInstallerDone()); | 170 EXPECT_TRUE(WaitForCrxInstallerDone()); |
| 203 LOG(ERROR) << "PackAndInstallExtension: Extension install"; | 171 LOG(ERROR) << "PackAndInstallExtension: Extension install"; |
| 204 EXPECT_TRUE(mock_prompt->confirmation_requested()); | 172 EXPECT_TRUE(mock_prompt->confirmation_requested()); |
| 205 LOG(ERROR) << "PackAndInstallExtension: Extension install confirmed"; | 173 LOG(ERROR) << "PackAndInstallExtension: Extension install confirmed"; |
| 206 } | 174 } |
| 207 | 175 |
| 208 // Tests that scopes are only granted if |record_oauth2_grant_| on the prompt is | |
| 209 // true. | |
| 210 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, GrantScopes) { | |
| 211 EXPECT_NO_FATAL_FAILURE(CheckHasEmptyScopesAfterInstall("browsertest/scopes", | |
| 212 true)); | |
| 213 } | |
| 214 | |
| 215 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, DoNotGrantScopes) { | |
| 216 EXPECT_NO_FATAL_FAILURE(CheckHasEmptyScopesAfterInstall("browsertest/scopes", | |
| 217 false)); | |
| 218 } | |
| 219 | |
| 220 // Off-store install cannot yet be disabled on Aura. | 176 // Off-store install cannot yet be disabled on Aura. |
| 221 #if defined(USE_AURA) | 177 #if defined(USE_AURA) |
| 222 #define MAYBE_AllowOffStore DISABLED_AllowOffStore | 178 #define MAYBE_AllowOffStore DISABLED_AllowOffStore |
| 223 #else | 179 #else |
| 224 #define MAYBE_AllowOffStore AllowOffStore | 180 #define MAYBE_AllowOffStore AllowOffStore |
| 225 #endif | 181 #endif |
| 226 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, MAYBE_AllowOffStore) { | 182 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, MAYBE_AllowOffStore) { |
| 227 ExtensionService* service = browser()->profile()->GetExtensionService(); | 183 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 228 const bool kTestData[] = {false, true}; | 184 const bool kTestData[] = {false, true}; |
| 229 | 185 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 246 kTestData[i]; | 202 kTestData[i]; |
| 247 if (kTestData[i]) { | 203 if (kTestData[i]) { |
| 248 EXPECT_EQ(string16(), mock_prompt->error()) << kTestData[i]; | 204 EXPECT_EQ(string16(), mock_prompt->error()) << kTestData[i]; |
| 249 } else { | 205 } else { |
| 250 EXPECT_EQ(l10n_util::GetStringUTF16( | 206 EXPECT_EQ(l10n_util::GetStringUTF16( |
| 251 IDS_EXTENSION_INSTALL_DISALLOWED_ON_SITE), | 207 IDS_EXTENSION_INSTALL_DISALLOWED_ON_SITE), |
| 252 mock_prompt->error()) << kTestData[i]; | 208 mock_prompt->error()) << kTestData[i]; |
| 253 } | 209 } |
| 254 } | 210 } |
| 255 } | 211 } |
| OLD | NEW |