diff options
author | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-29 10:57:33 +0000 |
---|---|---|
committer | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-29 10:57:33 +0000 |
commit | efb5f5799f2c3aab8c3ddf3558320b7c3d587c99 (patch) | |
tree | b705fe39bd9649a1153d26b6af1048108055e81f /content | |
parent | 9d806e8602b9330d6a175787ea41b08278827c93 (diff) | |
download | chromium_src-efb5f5799f2c3aab8c3ddf3558320b7c3d587c99.zip chromium_src-efb5f5799f2c3aab8c3ddf3558320b7c3d587c99.tar.gz chromium_src-efb5f5799f2c3aab8c3ddf3558320b7c3d587c99.tar.bz2 |
Add option --dump-render-tree to content_shell to dump the render tree as text.
BUG=111316
TEST=run out/Debug/content_shell --dump-render-tree file://$(pwd)/third_party/WebKit/LayoutTests/fast/html/section-element.html > section-element-actual.txt - should be the same as section-element-expected.txt
Review URL: http://codereview.chromium.org/9289045
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@119642 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
-rw-r--r-- | content/content_shell.gypi | 6 | ||||
-rw-r--r-- | content/shell/shell.cc | 23 | ||||
-rw-r--r-- | content/shell/shell.h | 11 | ||||
-rw-r--r-- | content/shell/shell_content_browser_client.cc | 2 | ||||
-rw-r--r-- | content/shell/shell_messages.cc | 33 | ||||
-rw-r--r-- | content/shell/shell_messages.h | 19 | ||||
-rw-r--r-- | content/shell/shell_render_view_host_observer.cc | 35 | ||||
-rw-r--r-- | content/shell/shell_render_view_host_observer.h | 32 | ||||
-rw-r--r-- | content/shell/shell_render_view_observer.cc | 60 | ||||
-rw-r--r-- | content/shell/shell_render_view_observer.h | 3 | ||||
-rw-r--r-- | content/shell/shell_switches.cc | 12 | ||||
-rw-r--r-- | content/shell/shell_switches.h | 17 |
12 files changed, 247 insertions, 6 deletions
diff --git a/content/content_shell.gypi b/content/content_shell.gypi index fd9cd0b..687bdf3 100644 --- a/content/content_shell.gypi +++ b/content/content_shell.gypi @@ -69,10 +69,16 @@ 'shell/shell_download_manager_delegate.h', 'shell/shell_main_delegate.cc', 'shell/shell_main_delegate.h', + 'shell/shell_messages.cc', + 'shell/shell_messages.h', + 'shell/shell_render_view_host_observer.cc', + 'shell/shell_render_view_host_observer.h', 'shell/shell_render_view_observer.cc', 'shell/shell_render_view_observer.h', 'shell/shell_resource_context.cc', 'shell/shell_resource_context.h', + 'shell/shell_switches.cc', + 'shell/shell_switches.h', 'shell/shell_url_request_context_getter.cc', 'shell/shell_url_request_context_getter.h', ], diff --git a/content/shell/shell.cc b/content/shell/shell.cc index fcf684c..34e0e44 100644 --- a/content/shell/shell.cc +++ b/content/shell/shell.cc @@ -4,11 +4,15 @@ #include "content/shell/shell.h" +#include "base/command_line.h" #include "base/message_loop.h" #include "base/path_service.h" #include "base/string_util.h" +#include "content/browser/renderer_host/render_view_host.h" #include "content/browser/tab_contents/navigation_controller_impl.h" #include "content/browser/tab_contents/tab_contents.h" +#include "content/shell/shell_messages.h" +#include "content/shell/shell_switches.h" #include "ui/gfx/size.h" // Content area size for newly created windows. @@ -19,8 +23,9 @@ namespace content { std::vector<Shell*> Shell::windows_; -Shell::Shell() - : window_(NULL), +Shell::Shell(TabContents* tab_contents) + : WebContentsObserver(tab_contents), + window_(NULL), url_edit_view_(NULL) #if defined(OS_WIN) , default_edit_wnd_proc_(0) @@ -41,7 +46,7 @@ Shell::~Shell() { } Shell* Shell::CreateShell(TabContents* tab_contents) { - Shell* shell = new Shell(); + Shell* shell = new Shell(tab_contents); shell->PlatformCreateWindow(kTestWindowWidth, kTestWindowHeight); shell->tab_contents_.reset(tab_contents); @@ -130,4 +135,16 @@ void Shell::UpdatePreferredSize(WebContents* source, PlatformSizeTo(pref_size.width(), pref_size.height()); } +void Shell::DidFinishLoad(int64 frame_id, + const GURL& validated_url, + bool is_main_frame) { + if (!is_main_frame) + return; + if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) + return; + RenderViewHost* render_view_host = tab_contents_->GetRenderViewHost(); + render_view_host->Send( + new ShellViewMsg_CaptureTextDump(render_view_host->routing_id(), false)); +} + } // namespace content diff --git a/content/shell/shell.h b/content/shell/shell.h index c4b09f7..bae8ef2 100644 --- a/content/shell/shell.h +++ b/content/shell/shell.h @@ -13,6 +13,7 @@ #include "base/memory/scoped_ptr.h" #include "base/string_piece.h" #include "content/public/browser/web_contents_delegate.h" +#include "content/public/browser/web_contents_observer.h" #include "ui/gfx/native_widget_types.h" #if defined(OS_LINUX) @@ -31,7 +32,8 @@ class SiteInstance; // This represents one window of the Content Shell, i.e. all the UI including // buttons and url bar, as well as the web content area. -class Shell : public WebContentsDelegate { +class Shell : public WebContentsDelegate, + public WebContentsObserver { public: virtual ~Shell(); @@ -71,7 +73,7 @@ class Shell : public WebContentsDelegate { STOP_BUTTON }; - Shell(); + explicit Shell(TabContents* tab_contents); // Helper to create a new Shell given a newly created TabContents. static Shell* CreateShell(TabContents* tab_contents); @@ -107,6 +109,11 @@ class Shell : public WebContentsDelegate { virtual void UpdatePreferredSize(WebContents* source, const gfx::Size& pref_size) OVERRIDE; + // content::WebContentsObserver + virtual void DidFinishLoad(int64 frame_id, + const GURL& validated_url, + bool is_main_frame) OVERRIDE; + #if defined(OS_WIN) static ATOM RegisterWindowClass(); static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); diff --git a/content/shell/shell_content_browser_client.cc b/content/shell/shell_content_browser_client.cc index 77d87ab..01296bb 100644 --- a/content/shell/shell_content_browser_client.cc +++ b/content/shell/shell_content_browser_client.cc @@ -8,6 +8,7 @@ #include "content/shell/shell.h" #include "content/shell/shell_browser_main.h" #include "content/shell/shell_devtools_delegate.h" +#include "content/shell/shell_render_view_host_observer.h" #include "googleurl/src/gurl.h" #include "third_party/skia/include/core/SkBitmap.h" #include "webkit/glue/webpreferences.h" @@ -52,6 +53,7 @@ WebContentsView* ShellContentBrowserClient::CreateWebContentsView( void ShellContentBrowserClient::RenderViewHostCreated( RenderViewHost* render_view_host) { + new ShellRenderViewHostObserver(render_view_host); } void ShellContentBrowserClient::RenderProcessHostCreated( diff --git a/content/shell/shell_messages.cc b/content/shell/shell_messages.cc new file mode 100644 index 0000000..422299f --- /dev/null +++ b/content/shell/shell_messages.cc @@ -0,0 +1,33 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Get basic type definitions. +#define IPC_MESSAGE_IMPL +#include "content/shell/shell_messages.h" + +// Generate constructors. +#include "ipc/struct_constructor_macros.h" +#include "content/shell/shell_messages.h" + +// Generate destructors. +#include "ipc/struct_destructor_macros.h" +#include "content/shell/shell_messages.h" + +// Generate param traits write methods. +#include "ipc/param_traits_write_macros.h" +namespace IPC { +#include "content/shell/shell_messages.h" +} // namespace IPC + +// Generate param traits read methods. +#include "ipc/param_traits_read_macros.h" +namespace IPC { +#include "content/shell/shell_messages.h" +} // namespace IPC + +// Generate param traits log methods. +#include "ipc/param_traits_log_macros.h" +namespace IPC { +#include "content/shell/shell_messages.h" +} // namespace IPC diff --git a/content/shell/shell_messages.h b/content/shell/shell_messages.h new file mode 100644 index 0000000..5e3c741 --- /dev/null +++ b/content/shell/shell_messages.h @@ -0,0 +1,19 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Multiply-included file, no traditional include guard. +#include <string> + +#include "ipc/ipc_message_macros.h" + +#define IPC_MESSAGE_START ShellMsgStart + +// Tells the render view to capture a text dump of the page. The render view +// responds with a ShellViewHostMsg_TextDump. +IPC_MESSAGE_ROUTED1(ShellViewMsg_CaptureTextDump, + bool /* recursive */) + +// Send a text dump of the tab contents to the render host. +IPC_MESSAGE_ROUTED1(ShellViewHostMsg_TextDump, + std::string /* dump */) diff --git a/content/shell/shell_render_view_host_observer.cc b/content/shell/shell_render_view_host_observer.cc new file mode 100644 index 0000000..f613851 --- /dev/null +++ b/content/shell/shell_render_view_host_observer.cc @@ -0,0 +1,35 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/shell/shell_render_view_host_observer.h" + +#include <iostream> + +#include "content/shell/shell_messages.h" + +namespace content { + +ShellRenderViewHostObserver::ShellRenderViewHostObserver( + RenderViewHost* render_view_host) + : RenderViewHostObserver(render_view_host) { +} + +ShellRenderViewHostObserver::~ShellRenderViewHostObserver() { +} + +bool ShellRenderViewHostObserver::OnMessageReceived( + const IPC::Message& message) { + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(ShellRenderViewHostObserver, message) + IPC_MESSAGE_HANDLER(ShellViewHostMsg_TextDump, OnTextDump) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + return handled; +} + +void ShellRenderViewHostObserver::OnTextDump(const std::string& dump) { + std::cout << dump; +} + +} // namespace content diff --git a/content/shell/shell_render_view_host_observer.h b/content/shell/shell_render_view_host_observer.h new file mode 100644 index 0000000..f1ba449 --- /dev/null +++ b/content/shell/shell_render_view_host_observer.h @@ -0,0 +1,32 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_SHELL_SHELL_RENDER_VIEW_HOST_OBSERVER_H_ +#define CONTENT_SHELL_SHELL_RENDER_VIEW_HOST_OBSERVER_H_ +#pragma once + +#include <string> + +#include "content/public/browser/render_view_host_observer.h" + +namespace content { + +class ShellRenderViewHostObserver : public RenderViewHostObserver { + public: + explicit ShellRenderViewHostObserver(RenderViewHost* render_view_host); + virtual ~ShellRenderViewHostObserver(); + + // RenderViewHostObserver implementation. + virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; + + private: + // Message handlers. + void OnTextDump(const std::string& dump); + + DISALLOW_COPY_AND_ASSIGN(ShellRenderViewHostObserver); +}; + +} // namespace content + +#endif // CONTENT_SHELL_SHELL_RENDER_VIEW_HOST_OBSERVER_H_ diff --git a/content/shell/shell_render_view_observer.cc b/content/shell/shell_render_view_observer.cc index 942f601..065551e 100644 --- a/content/shell/shell_render_view_observer.cc +++ b/content/shell/shell_render_view_observer.cc @@ -4,8 +4,54 @@ #include "content/shell/shell_render_view_observer.h" +#include "content/public/renderer/render_view.h" +#include "content/shell/shell_messages.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" + +using WebKit::WebFrame; +using WebKit::WebElement; + namespace content { +namespace { + +std::string DumpDocumentText(WebFrame* frame) { + // We use the document element's text instead of the body text here because + // not all documents have a body, such as XML documents. + WebElement documentElement = frame->document().documentElement(); + if (documentElement.isNull()) + return std::string(); + return documentElement.innerText().utf8(); +} + +std::string DumpFramesAsText(WebFrame* frame, bool recursive) { + std::string result; + + // Add header for all but the main frame. Skip emtpy frames. + if (frame->parent() && !frame->document().documentElement().isNull()) { + result.append("\n--------\nFrame: '"); + result.append(frame->name().utf8().data()); + result.append("'\n--------\n"); + } + + result.append(DumpDocumentText(frame)); + result.append("\n"); + + if (recursive) { + for (WebFrame* child = frame->firstChild(); child; + child = child->nextSibling()) { + result.append(DumpFramesAsText(child, recursive)); + } + } + return result; +} + +} // namespace ShellRenderViewObserver::ShellRenderViewObserver(RenderView* render_view) : RenderViewObserver(render_view) { } @@ -14,7 +60,19 @@ ShellRenderViewObserver::~ShellRenderViewObserver() { } bool ShellRenderViewObserver::OnMessageReceived(const IPC::Message& message) { - return false; + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(ShellRenderViewObserver, message) + IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + + return handled; +} + +void ShellRenderViewObserver::OnCaptureTextDump(bool recursive) { + std::string dump = + DumpFramesAsText(render_view()->GetWebView()->mainFrame(), recursive); + Send(new ShellViewHostMsg_TextDump(routing_id(), dump)); } } // namespace content diff --git a/content/shell/shell_render_view_observer.h b/content/shell/shell_render_view_observer.h index c1d963f..ad5466b 100644 --- a/content/shell/shell_render_view_observer.h +++ b/content/shell/shell_render_view_observer.h @@ -21,6 +21,9 @@ class ShellRenderViewObserver : public RenderViewObserver { virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; private: + // Message handlers. + void OnCaptureTextDump(bool recursive); + DISALLOW_COPY_AND_ASSIGN(ShellRenderViewObserver); }; diff --git a/content/shell/shell_switches.cc b/content/shell/shell_switches.cc new file mode 100644 index 0000000..0411cf2 --- /dev/null +++ b/content/shell/shell_switches.cc @@ -0,0 +1,12 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/shell/shell_switches.h" + +namespace switches { + +// Request pages to be dumped as text once they finished loading. +const char kDumpRenderTree[] = "dump-render-tree"; + +} // namespace switches diff --git a/content/shell/shell_switches.h b/content/shell/shell_switches.h new file mode 100644 index 0000000..9e4e838 --- /dev/null +++ b/content/shell/shell_switches.h @@ -0,0 +1,17 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Defines all the "content_shell" command-line switches. + +#ifndef CONTENT_SHELL_SHELL_SWITCHES_H_ +#define CONTENT_SHELL_SHELL_SWITCHES_H_ +#pragma once + +namespace switches { + +extern const char kDumpRenderTree[]; + +} // namespace switches + +#endif // CONTENT_SHELL_SHELL_SWITCHES_H_ |