OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 #include "platform/UserGestureIndicator.h" | 83 #include "platform/UserGestureIndicator.h" |
84 #include "platform/geometry/FloatRect.h" | 84 #include "platform/geometry/FloatRect.h" |
85 #include "platform/network/ResourceError.h" | 85 #include "platform/network/ResourceError.h" |
86 #include "platform/scroll/ScrollbarTheme.h" | 86 #include "platform/scroll/ScrollbarTheme.h" |
87 #include "platform/testing/URLTestHelpers.h" | 87 #include "platform/testing/URLTestHelpers.h" |
88 #include "platform/testing/UnitTestHelpers.h" | 88 #include "platform/testing/UnitTestHelpers.h" |
89 #include "platform/weborigin/SchemeRegistry.h" | 89 #include "platform/weborigin/SchemeRegistry.h" |
90 #include "platform/weborigin/SecurityOrigin.h" | 90 #include "platform/weborigin/SecurityOrigin.h" |
91 #include "public/platform/Platform.h" | 91 #include "public/platform/Platform.h" |
92 #include "public/platform/WebCachePolicy.h" | 92 #include "public/platform/WebCachePolicy.h" |
93 #include "public/platform/WebDocumentSubresourceFilter.h" | |
93 #include "public/platform/WebFloatRect.h" | 94 #include "public/platform/WebFloatRect.h" |
94 #include "public/platform/WebSecurityOrigin.h" | 95 #include "public/platform/WebSecurityOrigin.h" |
95 #include "public/platform/WebThread.h" | 96 #include "public/platform/WebThread.h" |
96 #include "public/platform/WebURL.h" | 97 #include "public/platform/WebURL.h" |
97 #include "public/platform/WebURLLoaderMockFactory.h" | 98 #include "public/platform/WebURLLoaderMockFactory.h" |
98 #include "public/platform/WebURLResponse.h" | 99 #include "public/platform/WebURLResponse.h" |
99 #include "public/web/WebCache.h" | 100 #include "public/web/WebCache.h" |
100 #include "public/web/WebConsoleMessage.h" | 101 #include "public/web/WebConsoleMessage.h" |
101 #include "public/web/WebDataSource.h" | 102 #include "public/web/WebDataSource.h" |
102 #include "public/web/WebDeviceEmulationParams.h" | 103 #include "public/web/WebDeviceEmulationParams.h" |
(...skipping 22 matching lines...) Expand all Loading... | |
125 #include "web/TextFinder.h" | 126 #include "web/TextFinder.h" |
126 #include "web/WebLocalFrameImpl.h" | 127 #include "web/WebLocalFrameImpl.h" |
127 #include "web/WebRemoteFrameImpl.h" | 128 #include "web/WebRemoteFrameImpl.h" |
128 #include "web/WebViewImpl.h" | 129 #include "web/WebViewImpl.h" |
129 #include "web/tests/FrameTestHelpers.h" | 130 #include "web/tests/FrameTestHelpers.h" |
130 #include "wtf/Forward.h" | 131 #include "wtf/Forward.h" |
131 #include "wtf/dtoa/utils.h" | 132 #include "wtf/dtoa/utils.h" |
132 #include <map> | 133 #include <map> |
133 #include <stdarg.h> | 134 #include <stdarg.h> |
134 #include <v8.h> | 135 #include <v8.h> |
136 #include <vector> | |
135 | 137 |
136 using blink::URLTestHelpers::toKURL; | 138 using blink::URLTestHelpers::toKURL; |
137 using blink::testing::runPendingTasks; | 139 using blink::testing::runPendingTasks; |
138 using testing::ElementsAre; | 140 using testing::ElementsAre; |
139 using testing::Mock; | 141 using testing::Mock; |
140 using testing::_; | 142 using testing::_; |
141 | 143 |
142 namespace blink { | 144 namespace blink { |
143 | 145 |
144 ::std::ostream& operator<<(::std::ostream& os, const WebFloatSize& size) | 146 ::std::ostream& operator<<(::std::ostream& os, const WebFloatSize& size) |
(...skipping 8536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
8681 | 8683 |
8682 WebLocalFrame* mainFrame = helper.webView()->mainFrame()->toWebLocalFrame(); | 8684 WebLocalFrame* mainFrame = helper.webView()->mainFrame()->toWebLocalFrame(); |
8683 v8::HandleScope scope(v8::Isolate::GetCurrent()); | 8685 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
8684 mainFrame->executeScript(WebScriptSource("hello = 'world';")); | 8686 mainFrame->executeScript(WebScriptSource("hello = 'world';")); |
8685 FrameTestHelpers::loadFrame(mainFrame, "data:text/html,new page"); | 8687 FrameTestHelpers::loadFrame(mainFrame, "data:text/html,new page"); |
8686 v8::Local<v8::Value> result = mainFrame->executeScriptAndReturnValue(WebScri ptSource("hello")); | 8688 v8::Local<v8::Value> result = mainFrame->executeScriptAndReturnValue(WebScri ptSource("hello")); |
8687 ASSERT_TRUE(result->IsString()); | 8689 ASSERT_TRUE(result->IsString()); |
8688 EXPECT_EQ("world", toCoreString(result->ToString(mainFrame->mainWorldScriptC ontext()).ToLocalChecked())); | 8690 EXPECT_EQ("world", toCoreString(result->ToString(mainFrame->mainWorldScriptC ontext()).ToLocalChecked())); |
8689 } | 8691 } |
8690 | 8692 |
8693 class TestDocumentSubresourceFilter : public WebDocumentSubresourceFilter { | |
battre
2016/05/30 15:29:26
Should this be called BlockEverysthingSubresourceF
engedy
2016/05/30 20:54:01
Yes, I was considering that too, but naming classe
| |
8694 public: | |
8695 TestDocumentSubresourceFilter() {} | |
8696 ~TestDocumentSubresourceFilter() override {} | |
8697 | |
8698 bool allowLoad(const WebURL& resourceUrl, WebURLRequest::RequestContext) ove rride | |
8699 { | |
8700 std::string resourcePath = WebString(KURL(resourceUrl).path()).utf8(); | |
8701 if (std::find(m_requestedSubresourcePaths.begin(), m_requestedSubresourc ePaths.end(), resourcePath) == m_requestedSubresourcePaths.end()) | |
8702 m_requestedSubresourcePaths.push_back(resourcePath); | |
8703 return false; | |
8704 } | |
8705 | |
8706 const std::vector<std::string>& requestedSubresourcePaths() const { return m _requestedSubresourcePaths; } | |
8707 | |
8708 private: | |
8709 std::vector<std::string> m_requestedSubresourcePaths; | |
8710 }; | |
8711 | |
8712 class SubresourceFilteringWebFrameClient : public FrameTestHelpers::TestWebFrame Client { | |
8713 public: | |
8714 void didStartProvisionalLoad(WebLocalFrame* localFrame, double) override | |
8715 { | |
8716 // Normally, the filter should be set when the load is committed. For | |
8717 // the sake of this test, however, inject it earlier to verify that it | |
8718 // is not consulted for the main/imported resource load. | |
8719 m_subresourceFilter = new TestDocumentSubresourceFilter(); | |
8720 localFrame->provisionalDataSource()->setSubresourceFilter(m_subresourceF ilter); | |
8721 } | |
8722 | |
8723 const TestDocumentSubresourceFilter* subresourceFilter() const | |
8724 { | |
8725 return m_subresourceFilter; | |
8726 } | |
8727 | |
8728 private: | |
8729 // Weak, owned by WebDataSource. | |
8730 TestDocumentSubresourceFilter* m_subresourceFilter = nullptr; | |
8731 }; | |
8732 | |
8733 class WebFrameSubresourceFilterTest : public WebFrameTest { | |
8734 public: | |
8735 WebFrameSubresourceFilterTest() | |
8736 { | |
8737 registerMockedHttpURLLoad("include-script.html"); | |
8738 registerMockedHttpURLLoad("included-script.js"); | |
8739 m_webViewhelper.initialize(true /* enableJavascript */, &m_client); | |
8740 } | |
8741 | |
8742 protected: | |
8743 void expectScriptDisallowed() | |
8744 { | |
8745 EXPECT_FALSE(evaluateBoolean("document.scriptExecuted")); | |
8746 EXPECT_FALSE(evaluateBoolean("document.scriptLoaded")); | |
8747 EXPECT_TRUE(evaluateBoolean("document.scriptFailed")); | |
8748 } | |
8749 | |
8750 WebFrame* mainFrame() { return m_webViewhelper.webViewImpl()->mainFrame(); } | |
8751 const std::vector<std::string>& requestedSubresourcePaths() const { return m _client.subresourceFilter()->requestedSubresourcePaths(); } | |
8752 | |
8753 private: | |
8754 bool evaluateBoolean(const std::string& expression) | |
8755 { | |
8756 v8::HandleScope scope(v8::Isolate::GetCurrent()); | |
8757 v8::Local<v8::Value> result = mainFrame()->executeScriptAndReturnValue(W ebScriptSource(WebString::fromUTF8(expression))); | |
8758 return result.As<v8::Boolean>()->Value(); | |
8759 } | |
8760 | |
8761 SubresourceFilteringWebFrameClient m_client; | |
8762 FrameTestHelpers::WebViewHelper m_webViewhelper; | |
8763 }; | |
8764 | |
8765 TEST_F(WebFrameSubresourceFilterTest, MainDocument) | |
8766 { | |
8767 FrameTestHelpers::loadFrame(mainFrame(), m_baseURL + "include-script.html"); | |
8768 EXPECT_THAT(requestedSubresourcePaths(), ElementsAre("/included-script.js")) ; | |
8769 expectScriptDisallowed(); | |
8770 } | |
8771 | |
8772 TEST_F(WebFrameSubresourceFilterTest, ImportDocument) | |
8773 { | |
8774 const char kHostDocument[] = "<html><head><link id=\"importLink\" rel=\"impo rt\" href=\"include-script.html\"></head></html>"; | |
8775 FrameTestHelpers::loadHTMLString(mainFrame(), kHostDocument, toKURL("http:// internal.test/host-include-script.html")); | |
8776 EXPECT_THAT(requestedSubresourcePaths(), ElementsAre("/included-script.js")) ; | |
8777 expectScriptDisallowed(); | |
8778 } | |
8779 | |
8691 } // namespace blink | 8780 } // namespace blink |
OLD | NEW |