| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/chromeos/extensions/public_session_permission_helper.h" | 5 #include "chrome/browser/chromeos/extensions/public_session_permission_helper.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "chrome/browser/extensions/extension_install_prompt.h" | 16 #include "chrome/browser/extensions/extension_install_prompt.h" |
| 17 #include "chrome/common/extensions/extension_test_util.h" | 17 #include "chrome/common/extensions/extension_test_util.h" |
| 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 19 #include "chromeos/login/scoped_test_public_session_login_state.h" |
| 19 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 20 #include "extensions/browser/extension_dialog_auto_confirm.h" | 21 #include "extensions/browser/extension_dialog_auto_confirm.h" |
| 21 #include "extensions/common/extension.h" | 22 #include "extensions/common/extension.h" |
| 22 #include "extensions/common/manifest.h" | 23 #include "extensions/common/manifest.h" |
| 23 #include "extensions/common/permissions/api_permission.h" | 24 #include "extensions/common/permissions/api_permission.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 26 |
| 26 using extension_test_util::LoadManifestUnchecked; | 27 using extension_test_util::LoadManifestUnchecked; |
| 27 using content::WebContents; | 28 using content::WebContents; |
| 28 using extensions::APIPermission; | 29 using extensions::APIPermission; |
| 29 using extensions::Extension; | 30 using extensions::Extension; |
| 30 using extensions::Manifest; | 31 using extensions::Manifest; |
| 31 using Result = ExtensionInstallPrompt::Result; | 32 using Result = ExtensionInstallPrompt::Result; |
| 32 | 33 |
| 34 namespace extensions { |
| 35 namespace permission_helper { |
| 33 namespace { | 36 namespace { |
| 34 | 37 |
| 35 auto permission_a = APIPermission::kAudio; | 38 auto permission_a = APIPermission::kAudio; |
| 36 auto permission_b = APIPermission::kBookmark; | 39 auto permission_b = APIPermission::kBookmark; |
| 37 bool did_show_dialog; | 40 bool did_show_dialog; |
| 38 | 41 |
| 42 const char kWhitelistedId[] = "cbkkbcmdlboombapidmoeolnmdacpkch"; |
| 43 const char kNonWhitelistedId[] = "bogus"; |
| 44 |
| 39 scoped_refptr<Extension> LoadManifestHelper(const std::string& id) { | 45 scoped_refptr<Extension> LoadManifestHelper(const std::string& id) { |
| 40 std::string error; | 46 std::string error; |
| 41 scoped_refptr<Extension> extension = LoadManifestUnchecked( | 47 scoped_refptr<Extension> extension = LoadManifestUnchecked( |
| 42 "common/background_page", "manifest.json", Manifest::INVALID_LOCATION, | 48 "common/background_page", "manifest.json", Manifest::INVALID_LOCATION, |
| 43 Extension::NO_FLAGS, id, &error); | 49 Extension::NO_FLAGS, id, &error); |
| 44 EXPECT_TRUE(extension.get()) << error; | 50 EXPECT_TRUE(extension.get()) << error; |
| 45 return extension; | 51 return extension; |
| 46 } | 52 } |
| 47 | 53 |
| 48 bool get_did_show_dialog_and_reset() { | 54 bool get_did_show_dialog_and_reset() { |
| 49 bool tmp = did_show_dialog; | 55 bool tmp = did_show_dialog; |
| 50 did_show_dialog = false; | 56 did_show_dialog = false; |
| 51 return tmp; | 57 return tmp; |
| 52 } | 58 } |
| 53 | 59 |
| 60 base::Callback<void(const PermissionIDSet&)> BindQuitLoop(base::RunLoop* loop) { |
| 61 return base::Bind( |
| 62 [](base::RunLoop* loop, const PermissionIDSet&) { loop->Quit(); }, loop); |
| 63 } |
| 64 |
| 54 class ProgrammableInstallPrompt | 65 class ProgrammableInstallPrompt |
| 55 : public ExtensionInstallPrompt, | 66 : public ExtensionInstallPrompt, |
| 56 public base::SupportsWeakPtr<ProgrammableInstallPrompt> { | 67 public base::SupportsWeakPtr<ProgrammableInstallPrompt> { |
| 57 public: | 68 public: |
| 58 explicit ProgrammableInstallPrompt(WebContents* contents) | 69 explicit ProgrammableInstallPrompt(WebContents* contents) |
| 59 : ExtensionInstallPrompt(contents) {} | 70 : ExtensionInstallPrompt(contents) {} |
| 60 | 71 |
| 61 ~ProgrammableInstallPrompt() override {} | 72 ~ProgrammableInstallPrompt() override {} |
| 62 | 73 |
| 63 void ShowDialog( | 74 void ShowDialog( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 76 } | 87 } |
| 77 | 88 |
| 78 private: | 89 private: |
| 79 ExtensionInstallPrompt::DoneCallback done_callback_; | 90 ExtensionInstallPrompt::DoneCallback done_callback_; |
| 80 | 91 |
| 81 DISALLOW_COPY_AND_ASSIGN(ProgrammableInstallPrompt); | 92 DISALLOW_COPY_AND_ASSIGN(ProgrammableInstallPrompt); |
| 82 }; | 93 }; |
| 83 | 94 |
| 84 } // namespace | 95 } // namespace |
| 85 | 96 |
| 86 namespace extensions { | |
| 87 namespace permission_helper { | |
| 88 | |
| 89 class PublicSessionPermissionHelperTest | 97 class PublicSessionPermissionHelperTest |
| 90 : public ChromeRenderViewHostTestHarness { | 98 : public ChromeRenderViewHostTestHarness { |
| 91 public: | 99 public: |
| 92 PublicSessionPermissionHelperTest() {} | 100 PublicSessionPermissionHelperTest() {} |
| 93 ~PublicSessionPermissionHelperTest() override {} | 101 ~PublicSessionPermissionHelperTest() override {} |
| 94 | 102 |
| 95 // testing::Test | 103 // testing::Test |
| 96 void SetUp() override; | 104 void SetUp() override; |
| 97 void TearDown() override; | 105 void TearDown() override; |
| 98 | 106 |
| 99 // Class helpers. | 107 // Class helpers. |
| 100 void RequestResolved(const PermissionIDSet& allowed_permissions); | 108 void RequestResolved(const PermissionIDSet& allowed_permissions); |
| 101 std::unique_ptr<ExtensionInstallPrompt> ReturnPrompt( | 109 std::unique_ptr<ExtensionInstallPrompt> ReturnPrompt( |
| 102 std::unique_ptr<ExtensionInstallPrompt> prompt, | 110 std::unique_ptr<ExtensionInstallPrompt> prompt, |
| 103 WebContents* web_contents); | 111 WebContents* web_contents); |
| 104 base::WeakPtr<ProgrammableInstallPrompt> CallHandlePermissionRequest( | 112 base::WeakPtr<ProgrammableInstallPrompt> CallHandlePermissionRequest( |
| 105 const scoped_refptr<Extension>& extension, | 113 const scoped_refptr<Extension>& extension, |
| 106 const PermissionIDSet& permissions); | 114 const PermissionIDSet& permissions); |
| 107 | 115 |
| 108 protected: | 116 protected: |
| 109 scoped_refptr<Extension> extension_a_; | 117 scoped_refptr<Extension> extension_a_; |
| 110 scoped_refptr<Extension> extension_b_; | 118 scoped_refptr<Extension> extension_b_; |
| 111 | 119 |
| 112 std::vector<PermissionIDSet> allowed_permissions_; | 120 std::vector<PermissionIDSet> allowed_permissions_; |
| 113 | 121 |
| 122 chromeos::ScopedTestPublicSessionLoginState login_state_; |
| 123 |
| 114 private: | 124 private: |
| 115 DISALLOW_COPY_AND_ASSIGN(PublicSessionPermissionHelperTest); | 125 DISALLOW_COPY_AND_ASSIGN(PublicSessionPermissionHelperTest); |
| 116 }; | 126 }; |
| 117 | 127 |
| 118 void PublicSessionPermissionHelperTest::SetUp() { | 128 void PublicSessionPermissionHelperTest::SetUp() { |
| 119 ChromeRenderViewHostTestHarness::SetUp(); | 129 ChromeRenderViewHostTestHarness::SetUp(); |
| 120 extension_a_ = LoadManifestHelper("extension_a"); | 130 extension_a_ = LoadManifestHelper("extension_a"); |
| 121 extension_b_ = LoadManifestHelper("extension_b"); | 131 extension_b_ = LoadManifestHelper("extension_b"); |
| 122 } | 132 } |
| 123 | 133 |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 auto prompt2 = | 254 auto prompt2 = |
| 245 CallHandlePermissionRequest(extension_a_, {permission_a, permission_b}); | 255 CallHandlePermissionRequest(extension_a_, {permission_a, permission_b}); |
| 246 EXPECT_TRUE(prompt2); | 256 EXPECT_TRUE(prompt2); |
| 247 EXPECT_TRUE(get_did_show_dialog_and_reset()); | 257 EXPECT_TRUE(get_did_show_dialog_and_reset()); |
| 248 prompt1->Resolve(Result::USER_CANCELED); | 258 prompt1->Resolve(Result::USER_CANCELED); |
| 249 EXPECT_TRUE(allowed_permissions_.at(0).Equals({})); | 259 EXPECT_TRUE(allowed_permissions_.at(0).Equals({})); |
| 250 prompt2->Resolve(Result::ACCEPTED); | 260 prompt2->Resolve(Result::ACCEPTED); |
| 251 EXPECT_TRUE(allowed_permissions_.at(1).Equals({permission_b})); | 261 EXPECT_TRUE(allowed_permissions_.at(1).Equals({permission_b})); |
| 252 } | 262 } |
| 253 | 263 |
| 264 TEST_F(PublicSessionPermissionHelperTest, WhitelistedExtension) { |
| 265 auto extension = LoadManifestHelper(kWhitelistedId); |
| 266 // Whitelisted extension can use any permission. |
| 267 EXPECT_TRUE(PermissionAllowed(extension.get(), permission_a)); |
| 268 EXPECT_TRUE(PermissionAllowed(extension.get(), permission_b)); |
| 269 // Whitelisted extension is already handled (no permission prompt needed). |
| 270 EXPECT_TRUE(HandlePermissionRequest(*extension, {permission_a}, |
| 271 web_contents(), RequestResolvedCallback(), |
| 272 PromptFactory())); |
| 273 EXPECT_TRUE(PermissionAllowed(extension.get(), permission_a)); |
| 274 EXPECT_TRUE(PermissionAllowed(extension.get(), permission_b)); |
| 275 } |
| 276 |
| 277 TEST_F(PublicSessionPermissionHelperTest, NonWhitelistedExtension) { |
| 278 auto extension = LoadManifestHelper(kNonWhitelistedId); |
| 279 EXPECT_FALSE(PermissionAllowed(extension.get(), permission_a)); |
| 280 EXPECT_FALSE(PermissionAllowed(extension.get(), permission_b)); |
| 281 // Prompt for permission_a, grant it, verify. |
| 282 { |
| 283 ScopedTestDialogAutoConfirm auto_confirm( |
| 284 ScopedTestDialogAutoConfirm::ACCEPT); |
| 285 // Permission not handled yet, need to show a prompt. |
| 286 base::RunLoop loop; |
| 287 EXPECT_FALSE(HandlePermissionRequest(*extension, {permission_a}, |
| 288 web_contents(), BindQuitLoop(&loop), |
| 289 PromptFactory())); |
| 290 loop.Run(); |
| 291 EXPECT_TRUE(PermissionAllowed(extension.get(), permission_a)); |
| 292 EXPECT_FALSE(PermissionAllowed(extension.get(), permission_b)); |
| 293 } |
| 294 // Already handled (allow), doesn't show a prompt. |
| 295 EXPECT_TRUE(HandlePermissionRequest(*extension, {permission_a}, |
| 296 web_contents(), RequestResolvedCallback(), |
| 297 PromptFactory())); |
| 298 // Prompt for permission_b, deny it, verify. |
| 299 { |
| 300 ScopedTestDialogAutoConfirm auto_confirm( |
| 301 ScopedTestDialogAutoConfirm::CANCEL); |
| 302 // Permission not handled yet, need to show a prompt. |
| 303 base::RunLoop loop; |
| 304 EXPECT_FALSE(HandlePermissionRequest(*extension, {permission_b}, |
| 305 web_contents(), BindQuitLoop(&loop), |
| 306 PromptFactory())); |
| 307 loop.Run(); |
| 308 EXPECT_TRUE(PermissionAllowed(extension.get(), permission_a)); |
| 309 EXPECT_FALSE(PermissionAllowed(extension.get(), permission_b)); |
| 310 } |
| 311 // Already handled (deny), doesn't show a prompt. |
| 312 EXPECT_TRUE(HandlePermissionRequest(*extension, {permission_b}, |
| 313 web_contents(), RequestResolvedCallback(), |
| 314 PromptFactory())); |
| 315 } |
| 316 |
| 254 } // namespace permission_helper | 317 } // namespace permission_helper |
| 255 } // namespace extensions | 318 } // namespace extensions |
| OLD | NEW |