[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | cccb5cf | 2012-06-06 22:20:04 | [diff] [blame] | 5 | #include "content/shell/layout_test_controller.h" |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 6 | |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 7 | #include "base/stringprintf.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 8 | #include "content/public/renderer/render_view.h" |
| 9 | #include "content/shell/shell_messages.h" |
| 10 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h" |
| 11 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 12 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 13 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 14 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 15 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 16 | |
| 17 | using WebKit::WebFrame; |
| 18 | using WebKit::WebElement; |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 19 | using WebKit::WebSize; |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 20 | |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 21 | namespace content { |
| 22 | |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | std::string DumpDocumentText(WebFrame* frame) { |
| 26 | // We use the document element's text instead of the body text here because |
| 27 | // not all documents have a body, such as XML documents. |
| 28 | WebElement documentElement = frame->document().documentElement(); |
| 29 | if (documentElement.isNull()) |
| 30 | return std::string(); |
| 31 | return documentElement.innerText().utf8(); |
| 32 | } |
| 33 | |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 34 | std::string DumpDocumentPrintedText(WebFrame* frame) { |
| 35 | return frame->renderTreeAsText(WebFrame::RenderAsTextPrinting).utf8(); |
| 36 | } |
| 37 | |
| 38 | std::string DumpFramesAsText(WebFrame* frame, bool printing, bool recursive) { |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 39 | std::string result; |
| 40 | |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 41 | // Cannot do printed format for anything other than HTML. |
| 42 | if (printing && !frame->document().isHTMLDocument()) |
| 43 | return std::string(); |
| 44 | |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 45 | // Add header for all but the main frame. Skip emtpy frames. |
| 46 | if (frame->parent() && !frame->document().documentElement().isNull()) { |
| 47 | result.append("\n--------\nFrame: '"); |
| 48 | result.append(frame->name().utf8().data()); |
| 49 | result.append("'\n--------\n"); |
| 50 | } |
| 51 | |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 52 | result.append( |
| 53 | printing ? DumpDocumentPrintedText(frame) : DumpDocumentText(frame)); |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 54 | result.append("\n"); |
| 55 | |
| 56 | if (recursive) { |
| 57 | for (WebFrame* child = frame->firstChild(); child; |
| 58 | child = child->nextSibling()) { |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 59 | result.append(DumpFramesAsText(child, printing, recursive)); |
| 60 | } |
| 61 | } |
| 62 | return result; |
| 63 | } |
| 64 | |
| 65 | std::string DumpFrameScrollPosition(WebFrame* frame, bool recursive) { |
| 66 | std::string result; |
| 67 | |
| 68 | WebSize offset = frame->scrollOffset(); |
| 69 | if (offset.width > 0 || offset.height > 0) { |
| 70 | if (frame->parent()) { |
| 71 | result.append( |
| 72 | base::StringPrintf("frame '%s' ", frame->name().utf8().data())); |
| 73 | } |
| 74 | result.append( |
| 75 | base::StringPrintf("scrolled to %d,%d\n", offset.width, offset.height)); |
| 76 | } |
| 77 | |
| 78 | if (recursive) { |
| 79 | for (WebFrame* child = frame->firstChild(); child; |
| 80 | child = child->nextSibling()) { |
| 81 | result.append(DumpFrameScrollPosition(child, recursive)); |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | return result; |
| 85 | } |
| 86 | |
| 87 | } // namespace |
[email protected] | cccb5cf | 2012-06-06 22:20:04 | [diff] [blame] | 88 | |
| 89 | LayoutTestController::LayoutTestController(RenderView* render_view) |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 90 | : RenderViewObserver(render_view) { |
| 91 | } |
| 92 | |
[email protected] | cccb5cf | 2012-06-06 22:20:04 | [diff] [blame] | 93 | LayoutTestController::~LayoutTestController() { |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | cccb5cf | 2012-06-06 22:20:04 | [diff] [blame] | 96 | void LayoutTestController::DidFinishLoad(WebFrame* frame) { |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 97 | if (!frame->parent()) |
| 98 | Send(new ShellViewHostMsg_DidFinishLoad(routing_id())); |
| 99 | } |
| 100 | |
[email protected] | cccb5cf | 2012-06-06 22:20:04 | [diff] [blame] | 101 | bool LayoutTestController::OnMessageReceived(const IPC::Message& message) { |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 102 | bool handled = true; |
[email protected] | cccb5cf | 2012-06-06 22:20:04 | [diff] [blame] | 103 | IPC_BEGIN_MESSAGE_MAP(LayoutTestController, message) |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 104 | IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump) |
| 105 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 106 | IPC_END_MESSAGE_MAP() |
| 107 | |
| 108 | return handled; |
| 109 | } |
| 110 | |
[email protected] | cccb5cf | 2012-06-06 22:20:04 | [diff] [blame] | 111 | void LayoutTestController::OnCaptureTextDump(bool as_text, |
| 112 | bool printing, |
| 113 | bool recursive) { |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 114 | WebFrame* frame = render_view()->GetWebView()->mainFrame(); |
| 115 | std::string dump; |
| 116 | if (as_text) { |
| 117 | dump = DumpFramesAsText(frame, printing, recursive); |
| 118 | } else { |
| 119 | WebFrame::RenderAsTextControls render_text_behavior = |
| 120 | WebFrame::RenderAsTextNormal; |
| 121 | if (printing) |
| 122 | render_text_behavior |= WebFrame::RenderAsTextPrinting; |
| 123 | dump = frame->renderTreeAsText(render_text_behavior).utf8(); |
| 124 | dump.append(DumpFrameScrollPosition(frame, recursive)); |
| 125 | } |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 126 | Send(new ShellViewHostMsg_TextDump(routing_id(), dump)); |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | } // namespace content |