Chromium Code Reviews| Index: content/public/test/render_view_test.cc |
| diff --git a/content/public/test/render_view_test.cc b/content/public/test/render_view_test.cc |
| index 0cb39a4b6617bb126d78529b8fd960df8f35f157..696e749ce17b58cf3dc6cf5c5bd799cc05dfea5e 100644 |
| --- a/content/public/test/render_view_test.cc |
| +++ b/content/public/test/render_view_test.cc |
| @@ -33,6 +33,7 @@ |
| #include "content/test/test_content_client.h" |
| #include "content/test/test_render_frame.h" |
| #include "third_party/WebKit/public/platform/WebScreenInfo.h" |
| +#include "third_party/WebKit/public/platform/WebURLLoader.h" |
| #include "third_party/WebKit/public/platform/WebURLRequest.h" |
| #include "third_party/WebKit/public/web/WebDocument.h" |
| #include "third_party/WebKit/public/web/WebHistoryItem.h" |
| @@ -56,6 +57,7 @@ using blink::WebLocalFrame; |
| using blink::WebMouseEvent; |
| using blink::WebScriptSource; |
| using blink::WebString; |
| +using blink::WebURLLoader; |
| using blink::WebURLRequest; |
| namespace { |
| @@ -96,37 +98,105 @@ bool GetWindowsKeyCode(char ascii_character, int* key_code) { |
| } |
| } |
| +WebURLRequest createDataRequest(const std::string& html) { |
| + std::string url_str = "data:text/html;charset=utf-8,"; |
| + url_str.append(html); |
| + GURL url(url_str); |
| + WebURLRequest request(url); |
| + request.setCheckForBrowserSideNavigation(false); |
| + return request; |
| +} |
| + |
| } // namespace |
| namespace content { |
| -class RendererBlinkPlatformImplNoSandboxImpl |
| +const char kWrappedHTMLDataHeader[] = "X-WrappedHTMLData"; |
| + |
| +// This loader checks all requests for the presence of the X-WrappedHTMLData |
| +// header and, if it's found, substitutes a data: url with the header's value |
| +// instead of loading the original request. It is used to implement |
| +// LoadHTMLWithURLOverride. |
| +class WebURLLoaderWrapper : public WebURLLoader { |
| +public: |
| + WebURLLoaderWrapper(WebURLLoader* wrapped_loader) |
| + : wrapped_loader_(wrapped_loader) { } |
| + |
| + virtual void loadSynchronously(const WebURLRequest& request, |
| + blink::WebURLResponse& response, |
| + blink::WebURLError& error, |
| + blink::WebData& data) override { |
| + std::string html = request.httpHeaderField(kWrappedHTMLDataHeader).utf8(); |
| + if (html.empty()) |
| + wrapped_loader_->loadSynchronously(request, response, error, data); |
|
Evan Stade
2015/12/11 23:44:45
ternary operator
|
| + else |
| + wrapped_loader_->loadSynchronously(createDataRequest(html), |
| + response, |
| + error, |
| + data); |
| + } |
| + |
| + virtual void loadAsynchronously(const WebURLRequest& request, |
| + blink::WebURLLoaderClient* client) override { |
| + std::string html = request.httpHeaderField(kWrappedHTMLDataHeader).utf8(); |
| + if (html.empty()) |
|
Evan Stade
2015/12/11 23:44:45
ternary operator
|
| + wrapped_loader_->loadAsynchronously(request, client); |
| + else |
| + wrapped_loader_->loadAsynchronously(createDataRequest(html), client); |
| + } |
| + |
| + virtual void cancel() override { |
| + wrapped_loader_->cancel(); |
| + } |
| + |
| + virtual void setDefersLoading(bool defer) { |
| + wrapped_loader_->setDefersLoading(defer); |
| + } |
| + |
| + virtual void setLoadingTaskRunner(blink::WebTaskRunner* runner) override { |
| + wrapped_loader_->setLoadingTaskRunner(runner); |
| + } |
| + |
| +private: |
| + std::unique_ptr<WebURLLoader> wrapped_loader_; |
| +}; |
| + |
| +class RendererBlinkPlatformImplTestOverrideImpl |
| : public RendererBlinkPlatformImpl { |
| public: |
| - RendererBlinkPlatformImplNoSandboxImpl( |
| + RendererBlinkPlatformImplTestOverrideImpl( |
| scheduler::RendererScheduler* scheduler) |
| : RendererBlinkPlatformImpl(scheduler) {} |
| + // Get rid of the dependency to the sandbox, which is not available in |
| + // RenderViewTest. |
| blink::WebSandboxSupport* sandboxSupport() override { return NULL; } |
| + |
| + // Inject a WebURLLoader which rewrites requests that have the |
| + // X-WrappedHTMLData header. |
| + WebURLLoader* createURLLoader() override { |
| + return new WebURLLoaderWrapper( |
| + RendererBlinkPlatformImpl::createURLLoader()); |
| + } |
| }; |
| -RenderViewTest::RendererBlinkPlatformImplNoSandbox:: |
| - RendererBlinkPlatformImplNoSandbox() { |
| +RenderViewTest::RendererBlinkPlatformImplTestOverride:: |
| + RendererBlinkPlatformImplTestOverride() { |
| renderer_scheduler_ = scheduler::RendererScheduler::Create(); |
| blink_platform_impl_.reset( |
| - new RendererBlinkPlatformImplNoSandboxImpl(renderer_scheduler_.get())); |
| + new RendererBlinkPlatformImplTestOverrideImpl(renderer_scheduler_.get())); |
| } |
| -RenderViewTest::RendererBlinkPlatformImplNoSandbox:: |
| - ~RendererBlinkPlatformImplNoSandbox() { |
| +RenderViewTest::RendererBlinkPlatformImplTestOverride:: |
| + ~RendererBlinkPlatformImplTestOverride() { |
| } |
| blink::Platform* |
| - RenderViewTest::RendererBlinkPlatformImplNoSandbox::Get() const { |
| + RenderViewTest::RendererBlinkPlatformImplTestOverride::Get() const { |
| return blink_platform_impl_.get(); |
| } |
| -void RenderViewTest::RendererBlinkPlatformImplNoSandbox::Shutdown() { |
| +void RenderViewTest::RendererBlinkPlatformImplTestOverride::Shutdown() { |
| renderer_scheduler_->Shutdown(); |
| blink_platform_impl_->Shutdown(); |
| } |
| @@ -169,11 +239,19 @@ bool RenderViewTest::ExecuteJavaScriptAndReturnIntValue( |
| } |
| void RenderViewTest::LoadHTML(const char* html) { |
| - std::string url_str = "data:text/html;charset=utf-8,"; |
| - url_str.append(html); |
| - GURL url(url_str); |
| + GetMainFrame()->loadRequest(createDataRequest(html)); |
| + // The load actually happens asynchronously, so we pump messages to process |
| + // the pending continuation. |
| + FrameLoadWaiter(view_->GetMainRenderFrame()).Wait(); |
| +} |
| + |
| +void RenderViewTest::LoadHTMLWithUrlOverride(const char* html, |
| + const char* url_override) { |
| + GURL url(url_override); |
| WebURLRequest request(url); |
| request.setCheckForBrowserSideNavigation(false); |
| + request.addHTTPHeaderField(kWrappedHTMLDataHeader, WebString::fromUTF8(html)); |
| + |
| GetMainFrame()->loadRequest(request); |
| // The load actually happens asynchronously, so we pump messages to process |
| // the pending continuation. |