| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/memory/ptr_util.h" |
| 5 #include "base/strings/stringprintf.h" | 6 #include "base/strings/stringprintf.h" |
| 6 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 7 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 7 #include "components/crx_file/id_util.h" | 8 #include "components/crx_file/id_util.h" |
| 8 #include "content/public/browser/content_browser_client.h" | 9 #include "content/public/browser/content_browser_client.h" |
| 9 #include "content/public/browser/navigation_handle.h" | 10 #include "content/public/browser/navigation_handle.h" |
| 10 #include "content/public/common/content_client.h" | 11 #include "content/public/common/content_client.h" |
| 11 #include "content/public/test/test_renderer_host.h" | 12 #include "content/public/test/test_renderer_host.h" |
| 12 #include "content/public/test/web_contents_tester.h" | 13 #include "content/public/test/web_contents_tester.h" |
| 13 #include "extensions/browser/extension_navigation_throttle.h" | 14 #include "extensions/browser/extension_navigation_throttle.h" |
| 14 #include "extensions/browser/extension_registry.h" | 15 #include "extensions/browser/extension_registry.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 29 const char kAccessibleDir[] = "accessible_dir/*"; | 30 const char kAccessibleDir[] = "accessible_dir/*"; |
| 30 const char kAccessibleDirResource[] = "accessible_dir/foo.html"; | 31 const char kAccessibleDirResource[] = "accessible_dir/foo.html"; |
| 31 | 32 |
| 32 class MockBrowserClient : public content::ContentBrowserClient { | 33 class MockBrowserClient : public content::ContentBrowserClient { |
| 33 public: | 34 public: |
| 34 MockBrowserClient() {} | 35 MockBrowserClient() {} |
| 35 ~MockBrowserClient() override {} | 36 ~MockBrowserClient() override {} |
| 36 | 37 |
| 37 // Only construct an ExtensionNavigationThrottle so that we can test it in | 38 // Only construct an ExtensionNavigationThrottle so that we can test it in |
| 38 // isolation. | 39 // isolation. |
| 39 ScopedVector<NavigationThrottle> CreateThrottlesForNavigation( | 40 std::vector<std::unique_ptr<NavigationThrottle>> CreateThrottlesForNavigation( |
| 40 content::NavigationHandle* handle) override { | 41 content::NavigationHandle* handle) override { |
| 41 ScopedVector<NavigationThrottle> throttles; | 42 std::vector<std::unique_ptr<NavigationThrottle>> throttles; |
| 42 if (!handle->IsInMainFrame()) // Mirrors ChromeContentBrowserClient. | 43 if (!handle->IsInMainFrame()) { // Mirrors ChromeContentBrowserClient. |
| 43 throttles.push_back(new ExtensionNavigationThrottle(handle)); | 44 throttles.push_back( |
| 45 base::MakeUnique<ExtensionNavigationThrottle>(handle)); |
| 46 } |
| 44 return throttles; | 47 return throttles; |
| 45 } | 48 } |
| 46 }; | 49 }; |
| 47 | 50 |
| 48 } // namespace | 51 } // namespace |
| 49 | 52 |
| 50 class ExtensionNavigationThrottleUnitTest | 53 class ExtensionNavigationThrottleUnitTest |
| 51 : public ChromeRenderViewHostTestHarness { | 54 : public ChromeRenderViewHostTestHarness { |
| 52 public: | 55 public: |
| 53 ExtensionNavigationThrottleUnitTest() {} | 56 ExtensionNavigationThrottleUnitTest() {} |
| 54 void SetUp() override { | 57 void SetUp() override { |
| 55 ChromeRenderViewHostTestHarness::SetUp(); | 58 ChromeRenderViewHostTestHarness::SetUp(); |
| 56 original_client_ = content::SetBrowserClientForTesting(&client_); | 59 original_client_ = content::SetBrowserClientForTesting(&client_); |
| 57 AddExtension(); | 60 AddExtension(); |
| 58 } | 61 } |
| 59 | 62 |
| 60 void TearDown() override { | 63 void TearDown() override { |
| 61 content::SetBrowserClientForTesting(original_client_); | 64 content::SetBrowserClientForTesting(original_client_); |
| 62 ChromeRenderViewHostTestHarness::TearDown(); | 65 ChromeRenderViewHostTestHarness::TearDown(); |
| 63 } | 66 } |
| 64 | 67 |
| 65 // Checks that trying to navigate the given |host| to |url| results in the | 68 // Checks that trying to navigate the given |host| to |url| results in the |
| 66 // |expected_result|. | 69 // |expected_result|. |
| 67 void CheckTestCase(content::RenderFrameHost* host, | 70 void CheckTestCase(content::RenderFrameHost* host, |
| 68 const GURL& url, | 71 const GURL& url, |
| 69 NavigationThrottle::ThrottleCheckResult expected_result) { | 72 NavigationThrottle::ThrottleCheckResult expected_result) { |
| 70 std::unique_ptr<content::NavigationHandle> handle = | 73 std::unique_ptr<content::NavigationHandle> handle = |
| 71 content::NavigationHandle::CreateNavigationHandleForTesting(url, host); | 74 content::NavigationHandle::CreateNavigationHandleForTesting(url, host); |
| 72 EXPECT_EQ(expected_result, handle->CallWillStartRequestForTesting( | 75 EXPECT_EQ(expected_result, |
| 73 false, //not post | 76 handle->CallWillStartRequestForTesting( |
| 74 content::Referrer(), false, | 77 false, // not post |
| 75 ui::PAGE_TRANSITION_LINK, false)) | 78 content::Referrer(), false, ui::PAGE_TRANSITION_LINK, false)) |
| 76 << url; | 79 << url; |
| 77 } | 80 } |
| 78 | 81 |
| 79 const Extension* extension() { return extension_.get(); } | 82 const Extension* extension() { return extension_.get(); } |
| 80 content::WebContentsTester* web_contents_tester() { | 83 content::WebContentsTester* web_contents_tester() { |
| 81 return content::WebContentsTester::For(web_contents()); | 84 return content::WebContentsTester::For(web_contents()); |
| 82 } | 85 } |
| 83 content::RenderFrameHostTester* render_frame_host_tester( | 86 content::RenderFrameHostTester* render_frame_host_tester( |
| 84 content::RenderFrameHost* host) { | 87 content::RenderFrameHost* host) { |
| 85 return content::RenderFrameHostTester::For(host); | 88 return content::RenderFrameHostTester::For(host); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 | 188 |
| 186 std::string second_id = crx_file::id_util::GenerateId("bar"); | 189 std::string second_id = crx_file::id_util::GenerateId("bar"); |
| 187 ASSERT_NE(second_id, extension()->id()); | 190 ASSERT_NE(second_id, extension()->id()); |
| 188 GURL invalid_url(base::StringPrintf("chrome-extension://%s/accessible.html", | 191 GURL invalid_url(base::StringPrintf("chrome-extension://%s/accessible.html", |
| 189 second_id.c_str())); | 192 second_id.c_str())); |
| 190 // Requests to non-existent extensions should be blocked. | 193 // Requests to non-existent extensions should be blocked. |
| 191 CheckTestCase(child, invalid_url, NavigationThrottle::BLOCK_REQUEST); | 194 CheckTestCase(child, invalid_url, NavigationThrottle::BLOCK_REQUEST); |
| 192 } | 195 } |
| 193 | 196 |
| 194 } // namespace extensions | 197 } // namespace extensions |
| OLD | NEW |