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