| 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/extensions/crx_installer.h" | 5 #include "chrome/browser/extensions/crx_installer.h" |
| 6 | 6 |
| 7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
| 8 #include "base/files/file_path.h" |
| 8 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/download/download_crx_util.h" | 11 #include "chrome/browser/download/download_crx_util.h" |
| 11 #include "chrome/browser/extensions/browser_action_test_util.h" | 12 #include "chrome/browser/extensions/browser_action_test_util.h" |
| 12 #include "chrome/browser/extensions/extension_browsertest.h" | 13 #include "chrome/browser/extensions/extension_browsertest.h" |
| 13 #include "chrome/browser/extensions/extension_install_prompt.h" | 14 #include "chrome/browser/extensions/extension_install_prompt.h" |
| 14 #include "chrome/browser/extensions/extension_install_prompt_show_params.h" | 15 #include "chrome/browser/extensions/extension_install_prompt_show_params.h" |
| 15 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/extensions/extension_util.h" | 17 #include "chrome/browser/extensions/extension_util.h" |
| 17 #include "chrome/browser/extensions/fake_safe_browsing_database_manager.h" | 18 #include "chrome/browser/extensions/fake_safe_browsing_database_manager.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 30 #include "content/public/test/test_utils.h" | 31 #include "content/public/test/test_utils.h" |
| 31 #include "extensions/browser/extension_prefs.h" | 32 #include "extensions/browser/extension_prefs.h" |
| 32 #include "extensions/browser/extension_registry.h" | 33 #include "extensions/browser/extension_registry.h" |
| 33 #include "extensions/browser/extension_system.h" | 34 #include "extensions/browser/extension_system.h" |
| 34 #include "extensions/browser/install/crx_installer_error.h" | 35 #include "extensions/browser/install/crx_installer_error.h" |
| 35 #include "extensions/browser/management_policy.h" | 36 #include "extensions/browser/management_policy.h" |
| 36 #include "extensions/browser/notification_types.h" | 37 #include "extensions/browser/notification_types.h" |
| 37 #include "extensions/common/extension.h" | 38 #include "extensions/common/extension.h" |
| 38 #include "extensions/common/feature_switch.h" | 39 #include "extensions/common/feature_switch.h" |
| 39 #include "extensions/common/file_util.h" | 40 #include "extensions/common/file_util.h" |
| 41 #include "extensions/common/permissions/api_permission.h" |
| 40 #include "extensions/common/permissions/permission_set.h" | 42 #include "extensions/common/permissions/permission_set.h" |
| 43 #include "extensions/common/permissions/permissions_data.h" |
| 41 #include "extensions/common/switches.h" | 44 #include "extensions/common/switches.h" |
| 42 #include "ui/base/l10n/l10n_util.h" | 45 #include "ui/base/l10n/l10n_util.h" |
| 43 | 46 |
| 44 #if defined(OS_CHROMEOS) | 47 #if defined(OS_CHROMEOS) |
| 45 #include "chrome/browser/chromeos/login/users/fake_user_manager.h" | 48 #include "chrome/browser/chromeos/login/users/fake_user_manager.h" |
| 46 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | 49 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
| 47 #include "chrome/browser/extensions/extension_assets_manager_chromeos.h" | 50 #include "chrome/browser/extensions/extension_assets_manager_chromeos.h" |
| 48 #include "chromeos/chromeos_switches.h" | 51 #include "chromeos/chromeos_switches.h" |
| 49 #endif | 52 #endif |
| 50 | 53 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 return "ManagementPolicyMock"; | 155 return "ManagementPolicyMock"; |
| 153 } | 156 } |
| 154 | 157 |
| 155 bool UserMayLoad(const Extension* extension, | 158 bool UserMayLoad(const Extension* extension, |
| 156 base::string16* error) const override { | 159 base::string16* error) const override { |
| 157 *error = base::UTF8ToUTF16("Dummy error message"); | 160 *error = base::UTF8ToUTF16("Dummy error message"); |
| 158 return false; | 161 return false; |
| 159 } | 162 } |
| 160 }; | 163 }; |
| 161 | 164 |
| 165 // Appends "enable-experimental-extension-apis" to the command line for the |
| 166 // lifetime of this class. |
| 167 class ScopedExperimentalCommandLine { |
| 168 public: |
| 169 ScopedExperimentalCommandLine() : saved_(*CommandLine::ForCurrentProcess()) { |
| 170 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 171 switches::kEnableExperimentalExtensionApis); |
| 172 } |
| 173 |
| 174 ~ScopedExperimentalCommandLine() { |
| 175 *CommandLine::ForCurrentProcess() = saved_; |
| 176 } |
| 177 |
| 178 private: |
| 179 CommandLine saved_; |
| 180 }; |
| 181 |
| 162 } // namespace | 182 } // namespace |
| 163 | 183 |
| 164 class ExtensionCrxInstallerTest : public ExtensionBrowserTest { | 184 class ExtensionCrxInstallerTest : public ExtensionBrowserTest { |
| 165 public: | 185 protected: |
| 166 scoped_ptr<WebstoreInstaller::Approval> GetApproval( | 186 scoped_ptr<WebstoreInstaller::Approval> GetApproval( |
| 167 const char* manifest_dir, | 187 const char* manifest_dir, |
| 168 const std::string& id, | 188 const std::string& id, |
| 169 bool strict_manifest_checks) { | 189 bool strict_manifest_checks) { |
| 170 scoped_ptr<WebstoreInstaller::Approval> result; | 190 scoped_ptr<WebstoreInstaller::Approval> result; |
| 171 | 191 |
| 172 base::FilePath ext_path = test_data_dir_.AppendASCII(manifest_dir); | 192 base::FilePath ext_path = test_data_dir_.AppendASCII(manifest_dir); |
| 173 std::string error; | 193 std::string error; |
| 174 scoped_ptr<base::DictionaryValue> parsed_manifest( | 194 scoped_ptr<base::DictionaryValue> parsed_manifest( |
| 175 file_util::LoadManifest(ext_path, &error)); | 195 file_util::LoadManifest(ext_path, &error)); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 RunCrxInstaller(approval.get(), mock_install_prompt->CreatePrompt(), | 232 RunCrxInstaller(approval.get(), mock_install_prompt->CreatePrompt(), |
| 213 crx_path); | 233 crx_path); |
| 214 | 234 |
| 215 EXPECT_TRUE(mock_install_prompt->did_succeed()); | 235 EXPECT_TRUE(mock_install_prompt->did_succeed()); |
| 216 } | 236 } |
| 217 | 237 |
| 218 // Installs an extension and checks that it has scopes granted IFF | 238 // Installs an extension and checks that it has scopes granted IFF |
| 219 // |record_oauth2_grant| is true. | 239 // |record_oauth2_grant| is true. |
| 220 void CheckHasEmptyScopesAfterInstall(const std::string& ext_relpath, | 240 void CheckHasEmptyScopesAfterInstall(const std::string& ext_relpath, |
| 221 bool record_oauth2_grant) { | 241 bool record_oauth2_grant) { |
| 222 CommandLine::ForCurrentProcess()->AppendSwitch( | 242 ScopedExperimentalCommandLine scope; |
| 223 switches::kEnableExperimentalExtensionApis); | |
| 224 | 243 |
| 225 scoped_refptr<MockPromptProxy> mock_prompt = | 244 scoped_refptr<MockPromptProxy> mock_prompt = |
| 226 CreateMockPromptProxyForBrowser(browser()); | 245 CreateMockPromptProxyForBrowser(browser()); |
| 227 | 246 |
| 228 mock_prompt->set_record_oauth2_grant(record_oauth2_grant); | 247 mock_prompt->set_record_oauth2_grant(record_oauth2_grant); |
| 229 InstallWithPrompt("browsertest/scopes", std::string(), mock_prompt); | 248 InstallWithPrompt("browsertest/scopes", std::string(), mock_prompt); |
| 230 | 249 |
| 231 scoped_refptr<PermissionSet> permissions = | 250 scoped_refptr<PermissionSet> permissions = |
| 232 ExtensionPrefs::Get(browser()->profile()) | 251 ExtensionPrefs::Get(browser()->profile()) |
| 233 ->GetGrantedPermissions(mock_prompt->extension_id()); | 252 ->GetGrantedPermissions(mock_prompt->extension_id()); |
| 234 ASSERT_TRUE(permissions.get()); | 253 ASSERT_TRUE(permissions.get()); |
| 235 } | 254 } |
| 255 |
| 256 // Returns a FilePath to an unpacked "experimental" extension (a test |
| 257 // Extension which requests the "experimental" permission). |
| 258 base::FilePath PackExperimentalExtension() { |
| 259 // We must modify the command line temporarily in order to pack an |
| 260 // extension that requests the experimental permission. |
| 261 ScopedExperimentalCommandLine scope; |
| 262 base::FilePath test_path = test_data_dir_.AppendASCII("experimental"); |
| 263 base::FilePath crx_path = PackExtension(test_path); |
| 264 CHECK(!crx_path.empty()) << "Extension not found at " << test_path.value(); |
| 265 return crx_path; |
| 266 } |
| 236 }; | 267 }; |
| 237 | 268 |
| 238 // This test is skipped on ChromeOS because it requires the NPAPI, | 269 // This test is skipped on ChromeOS because it requires the NPAPI, |
| 239 // which is not available on that platform. | 270 // which is not available on that platform. |
| 240 #if !defined(OS_CHROMEOS) | 271 #if !defined(OS_CHROMEOS) |
| 241 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, Whitelisting) { | 272 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, Whitelisting) { |
| 242 std::string id = "hdgllgikmikobbofgnabhfimcfoopgnd"; | 273 std::string id = "hdgllgikmikobbofgnabhfimcfoopgnd"; |
| 243 ExtensionRegistry* registry = ExtensionRegistry::Get( | 274 ExtensionRegistry* registry = ExtensionRegistry::Get( |
| 244 browser()->profile()); | 275 browser()->profile()); |
| 245 | 276 |
| 246 // Even whitelisted extensions with NPAPI should not prompt. | 277 // Even whitelisted extensions with NPAPI should not prompt. |
| 247 scoped_refptr<MockPromptProxy> mock_prompt = | 278 scoped_refptr<MockPromptProxy> mock_prompt = |
| 248 CreateMockPromptProxyForBrowser(browser()); | 279 CreateMockPromptProxyForBrowser(browser()); |
| 249 InstallWithPrompt("uitest/plugins", id, mock_prompt); | 280 InstallWithPrompt("uitest/plugins", id, mock_prompt); |
| 250 EXPECT_FALSE(mock_prompt->confirmation_requested()); | 281 EXPECT_FALSE(mock_prompt->confirmation_requested()); |
| 251 EXPECT_TRUE(registry->enabled_extensions().GetByID(id)); | 282 EXPECT_TRUE(registry->enabled_extensions().GetByID(id)); |
| 252 } | 283 } |
| 253 #endif | 284 #endif |
| 254 | 285 |
| 255 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, | 286 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, |
| 256 GalleryInstallGetsExperimental) { | 287 ExperimentalExtensionFromGallery) { |
| 257 // We must modify the command line temporarily in order to pack an extension | 288 // Gallery-installed extensions should have their experimental permission |
| 258 // that requests the experimental permission. | 289 // preserved, since we allow the Webstore to make that decision. |
| 259 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 290 base::FilePath crx_path = PackExperimentalExtension(); |
| 260 CommandLine old_command_line = *command_line; | 291 const Extension* extension = InstallExtensionFromWebstore(crx_path, 1); |
| 261 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); | 292 ASSERT_TRUE(extension); |
| 262 base::FilePath crx_path = PackExtension( | 293 EXPECT_TRUE(extension->permissions_data()->HasAPIPermission( |
| 263 test_data_dir_.AppendASCII("experimental")); | 294 APIPermission::kExperimental)); |
| 264 ASSERT_FALSE(crx_path.empty()); | 295 } |
| 265 | 296 |
| 266 // Now reset the command line so that we are testing specifically whether | 297 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, |
| 267 // installing from webstore enables experimental permissions. | 298 ExperimentalExtensionFromOutsideGallery) { |
| 268 *(CommandLine::ForCurrentProcess()) = old_command_line; | 299 // Non-gallery-installed extensions should lose their experimental |
| 300 // permission if the flag isn't enabled. |
| 301 base::FilePath crx_path = PackExperimentalExtension(); |
| 302 const Extension* extension = InstallExtension(crx_path, 1); |
| 303 ASSERT_TRUE(extension); |
| 304 EXPECT_FALSE(extension->permissions_data()->HasAPIPermission( |
| 305 APIPermission::kExperimental)); |
| 306 } |
| 269 | 307 |
| 270 EXPECT_FALSE(InstallExtension(crx_path, 0)); | 308 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, |
| 271 EXPECT_TRUE(InstallExtensionFromWebstore(crx_path, 1)); | 309 ExperimentalExtensionFromOutsideGalleryWithFlag) { |
| 310 // Non-gallery-installed extensions should maintain their experimental |
| 311 // permission if the flag is enabled. |
| 312 base::FilePath crx_path = PackExperimentalExtension(); |
| 313 ScopedExperimentalCommandLine scope; |
| 314 const Extension* extension = InstallExtension(crx_path, 1); |
| 315 ASSERT_TRUE(extension); |
| 316 EXPECT_TRUE(extension->permissions_data()->HasAPIPermission( |
| 317 APIPermission::kExperimental)); |
| 272 } | 318 } |
| 273 | 319 |
| 274 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, PlatformAppCrx) { | 320 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, PlatformAppCrx) { |
| 275 CommandLine::ForCurrentProcess()->AppendSwitch( | 321 ScopedExperimentalCommandLine scope; |
| 276 switches::kEnableExperimentalExtensionApis); | |
| 277 EXPECT_TRUE(InstallExtension( | 322 EXPECT_TRUE(InstallExtension( |
| 278 test_data_dir_.AppendASCII("minimal_platform_app.crx"), 1)); | 323 test_data_dir_.AppendASCII("minimal_platform_app.crx"), 1)); |
| 279 } | 324 } |
| 280 | 325 |
| 281 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, PackAndInstallExtension) { | 326 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, PackAndInstallExtension) { |
| 282 if (!FeatureSwitch::easy_off_store_install()->IsEnabled()) | 327 if (!FeatureSwitch::easy_off_store_install()->IsEnabled()) |
| 283 return; | 328 return; |
| 284 | 329 |
| 285 const int kNumDownloadsExpected = 1; | 330 const int kNumDownloadsExpected = 1; |
| 286 | 331 |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 595 // because previously withheld permissions are now being requested. | 640 // because previously withheld permissions are now being requested. |
| 596 enable_scripts_switch.reset(); | 641 enable_scripts_switch.reset(); |
| 597 extension = InstallExtension(crx_path, -1); | 642 extension = InstallExtension(crx_path, -1); |
| 598 EXPECT_FALSE(registry->enabled_extensions().GetByID(extension_id)); | 643 EXPECT_FALSE(registry->enabled_extensions().GetByID(extension_id)); |
| 599 EXPECT_TRUE(registry->disabled_extensions().GetByID(extension_id)); | 644 EXPECT_TRUE(registry->disabled_extensions().GetByID(extension_id)); |
| 600 EXPECT_TRUE(ExtensionPrefs::Get(browser()->profile())->GetDisableReasons( | 645 EXPECT_TRUE(ExtensionPrefs::Get(browser()->profile())->GetDisableReasons( |
| 601 extension_id) & Extension::DISABLE_PERMISSIONS_INCREASE); | 646 extension_id) & Extension::DISABLE_PERMISSIONS_INCREASE); |
| 602 } | 647 } |
| 603 | 648 |
| 604 } // namespace extensions | 649 } // namespace extensions |
| OLD | NEW |