OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "net/base/filename_util.h" | 27 #include "net/base/filename_util.h" |
28 #include "net/url_request/url_request_context.h" | 28 #include "net/url_request/url_request_context.h" |
29 #include "third_party/WebKit/public/platform/WebCString.h" | 29 #include "third_party/WebKit/public/platform/WebCString.h" |
30 #include "third_party/WebKit/public/platform/WebData.h" | 30 #include "third_party/WebKit/public/platform/WebData.h" |
31 #include "third_party/WebKit/public/platform/WebString.h" | 31 #include "third_party/WebKit/public/platform/WebString.h" |
32 #include "third_party/WebKit/public/platform/WebURL.h" | 32 #include "third_party/WebKit/public/platform/WebURL.h" |
33 #include "third_party/WebKit/public/platform/WebVector.h" | 33 #include "third_party/WebKit/public/platform/WebVector.h" |
34 #include "third_party/WebKit/public/web/WebDocument.h" | 34 #include "third_party/WebKit/public/web/WebDocument.h" |
35 #include "third_party/WebKit/public/web/WebElement.h" | 35 #include "third_party/WebKit/public/web/WebElement.h" |
36 #include "third_party/WebKit/public/web/WebElementCollection.h" | 36 #include "third_party/WebKit/public/web/WebElementCollection.h" |
| 37 #include "third_party/WebKit/public/web/WebFrameSerializer.h" |
| 38 #include "third_party/WebKit/public/web/WebFrameSerializerClient.h" |
37 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 39 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
38 #include "third_party/WebKit/public/web/WebMetaElement.h" | 40 #include "third_party/WebKit/public/web/WebMetaElement.h" |
39 #include "third_party/WebKit/public/web/WebNode.h" | 41 #include "third_party/WebKit/public/web/WebNode.h" |
40 #include "third_party/WebKit/public/web/WebPageSerializer.h" | |
41 #include "third_party/WebKit/public/web/WebPageSerializerClient.h" | |
42 #include "third_party/WebKit/public/web/WebView.h" | 42 #include "third_party/WebKit/public/web/WebView.h" |
43 | 43 |
44 using blink::WebCString; | 44 using blink::WebCString; |
45 using blink::WebData; | 45 using blink::WebData; |
46 using blink::WebDocument; | 46 using blink::WebDocument; |
47 using blink::WebElement; | 47 using blink::WebElement; |
48 using blink::WebMetaElement; | 48 using blink::WebMetaElement; |
49 using blink::WebElementCollection; | 49 using blink::WebElementCollection; |
50 using blink::WebFrame; | 50 using blink::WebFrame; |
| 51 using blink::WebFrameSerializer; |
| 52 using blink::WebFrameSerializerClient; |
51 using blink::WebLocalFrame; | 53 using blink::WebLocalFrame; |
52 using blink::WebNode; | 54 using blink::WebNode; |
53 using blink::WebPageSerializer; | |
54 using blink::WebPageSerializerClient; | |
55 using blink::WebString; | 55 using blink::WebString; |
56 using blink::WebURL; | 56 using blink::WebURL; |
57 using blink::WebView; | 57 using blink::WebView; |
58 using blink::WebVector; | 58 using blink::WebVector; |
59 | 59 |
60 namespace content { | 60 namespace content { |
61 | 61 |
62 bool HasDocType(const WebDocument& doc) { | 62 bool HasDocType(const WebDocument& doc) { |
63 return doc.firstChild().isDocumentTypeNode(); | 63 return doc.firstChild().isDocumentTypeNode(); |
64 } | 64 } |
65 | 65 |
66 class LoadObserver : public RenderViewObserver { | 66 class LoadObserver : public RenderViewObserver { |
67 public: | 67 public: |
68 LoadObserver(RenderView* render_view, const base::Closure& quit_closure) | 68 LoadObserver(RenderView* render_view, const base::Closure& quit_closure) |
69 : RenderViewObserver(render_view), | 69 : RenderViewObserver(render_view), |
70 quit_closure_(quit_closure) {} | 70 quit_closure_(quit_closure) {} |
71 | 71 |
72 void DidFinishLoad(blink::WebLocalFrame* frame) override { | 72 void DidFinishLoad(blink::WebLocalFrame* frame) override { |
73 if (frame == render_view()->GetWebView()->mainFrame()) | 73 if (frame == render_view()->GetWebView()->mainFrame()) |
74 quit_closure_.Run(); | 74 quit_closure_.Run(); |
75 } | 75 } |
76 | 76 |
77 private: | 77 private: |
78 base::Closure quit_closure_; | 78 base::Closure quit_closure_; |
79 }; | 79 }; |
80 | 80 |
81 class DomSerializerTests : public ContentBrowserTest, | 81 class DomSerializerTests : public ContentBrowserTest, |
82 public WebPageSerializerClient { | 82 public WebFrameSerializerClient { |
83 public: | 83 public: |
84 DomSerializerTests() : serialization_reported_end_of_data_(false) {} | 84 DomSerializerTests() : serialization_reported_end_of_data_(false) {} |
85 | 85 |
86 void SetUpCommandLine(base::CommandLine* command_line) override { | 86 void SetUpCommandLine(base::CommandLine* command_line) override { |
87 command_line->AppendSwitch(switches::kSingleProcess); | 87 command_line->AppendSwitch(switches::kSingleProcess); |
88 #if defined(OS_WIN) | 88 #if defined(OS_WIN) |
89 // Don't want to try to create a GPU process. | 89 // Don't want to try to create a GPU process. |
90 command_line->AppendSwitch(switches::kDisableGpu); | 90 command_line->AppendSwitch(switches::kDisableGpu); |
91 #endif | 91 #endif |
92 } | 92 } |
93 | 93 |
94 void SetUpOnMainThread() override { | 94 void SetUpOnMainThread() override { |
95 render_view_routing_id_ = | 95 render_view_routing_id_ = |
96 shell()->web_contents()->GetRenderViewHost()->GetRoutingID(); | 96 shell()->web_contents()->GetRenderViewHost()->GetRoutingID(); |
97 } | 97 } |
98 | 98 |
99 // DomSerializerDelegate. | 99 // DomSerializerDelegate. |
100 void didSerializeDataForFrame(const WebCString& data, | 100 void didSerializeDataForFrame(const WebCString& data, |
101 PageSerializationStatus status) override { | 101 FrameSerializationStatus status) override { |
102 // Check finish status of current frame. | 102 // Check finish status of current frame. |
103 ASSERT_FALSE(serialization_reported_end_of_data_); | 103 ASSERT_FALSE(serialization_reported_end_of_data_); |
104 | 104 |
105 // Add data to corresponding frame's content. | 105 // Add data to corresponding frame's content. |
106 serialized_contents_ += data; | 106 serialized_contents_ += data; |
107 | 107 |
108 // Current frame is completed saving, change the finish status. | 108 // Current frame is completed saving, change the finish status. |
109 if (status == WebPageSerializerClient::CurrentFrameIsFinished) | 109 if (status == WebFrameSerializerClient::CurrentFrameIsFinished) |
110 serialization_reported_end_of_data_ = true; | 110 serialization_reported_end_of_data_ = true; |
111 } | 111 } |
112 | 112 |
113 RenderView* GetRenderView() { | 113 RenderView* GetRenderView() { |
114 return RenderView::FromRoutingID(render_view_routing_id_); | 114 return RenderView::FromRoutingID(render_view_routing_id_); |
115 } | 115 } |
116 | 116 |
117 WebView* GetWebView() { | 117 WebView* GetWebView() { |
118 return GetRenderView()->GetWebView(); | 118 return GetRenderView()->GetWebView(); |
119 } | 119 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 // Serialize DOM belonging to a frame with the specified |frame_url|. | 161 // Serialize DOM belonging to a frame with the specified |frame_url|. |
162 void SerializeDomForURL(const GURL& frame_url) { | 162 void SerializeDomForURL(const GURL& frame_url) { |
163 // Find corresponding WebFrame according to frame_url. | 163 // Find corresponding WebFrame according to frame_url. |
164 WebFrame* web_frame = FindSubFrameByURL(frame_url); | 164 WebFrame* web_frame = FindSubFrameByURL(frame_url); |
165 ASSERT_TRUE(web_frame != NULL); | 165 ASSERT_TRUE(web_frame != NULL); |
166 WebString file_path = | 166 WebString file_path = |
167 base::FilePath(FILE_PATH_LITERAL("c:\\dummy.htm")).AsUTF16Unsafe(); | 167 base::FilePath(FILE_PATH_LITERAL("c:\\dummy.htm")).AsUTF16Unsafe(); |
168 std::vector<std::pair<WebURL, WebString>> url_to_local_path; | 168 std::vector<std::pair<WebURL, WebString>> url_to_local_path; |
169 url_to_local_path.push_back(std::make_pair(WebURL(frame_url), file_path)); | 169 url_to_local_path.push_back(std::make_pair(WebURL(frame_url), file_path)); |
170 // Start serializing DOM. | 170 // Start serializing DOM. |
171 bool result = WebPageSerializer::serialize( | 171 bool result = WebFrameSerializer::serialize( |
172 web_frame->toWebLocalFrame(), | 172 web_frame->toWebLocalFrame(), |
173 static_cast<WebPageSerializerClient*>(this), url_to_local_path); | 173 static_cast<WebFrameSerializerClient*>(this), url_to_local_path); |
174 ASSERT_TRUE(result); | 174 ASSERT_TRUE(result); |
175 } | 175 } |
176 | 176 |
177 void SerializeHTMLDOMWithDocTypeOnRenderer(const GURL& file_url) { | 177 void SerializeHTMLDOMWithDocTypeOnRenderer(const GURL& file_url) { |
178 // Make sure original contents have document type. | 178 // Make sure original contents have document type. |
179 WebFrame* web_frame = FindSubFrameByURL(file_url); | 179 WebFrame* web_frame = FindSubFrameByURL(file_url); |
180 ASSERT_TRUE(web_frame != NULL); | 180 ASSERT_TRUE(web_frame != NULL); |
181 WebDocument doc = web_frame->document(); | 181 WebDocument doc = web_frame->document(); |
182 ASSERT_TRUE(HasDocType(doc)); | 182 ASSERT_TRUE(HasDocType(doc)); |
183 // Do serialization. | 183 // Do serialization. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 SerializeDomForURL(xml_file_url); | 216 SerializeDomForURL(xml_file_url); |
217 // Compare the serialized contents with original contents. | 217 // Compare the serialized contents with original contents. |
218 ASSERT_TRUE(serialization_reported_end_of_data_); | 218 ASSERT_TRUE(serialization_reported_end_of_data_); |
219 ASSERT_EQ(original_contents, serialized_contents_); | 219 ASSERT_EQ(original_contents, serialized_contents_); |
220 } | 220 } |
221 | 221 |
222 void SerializeHTMLDOMWithAddingMOTWOnRenderer( | 222 void SerializeHTMLDOMWithAddingMOTWOnRenderer( |
223 const GURL& file_url, const std::string& original_contents) { | 223 const GURL& file_url, const std::string& original_contents) { |
224 // Make sure original contents does not have MOTW; | 224 // Make sure original contents does not have MOTW; |
225 std::string motw_declaration = | 225 std::string motw_declaration = |
226 WebPageSerializer::generateMarkOfTheWebDeclaration(file_url).utf8(); | 226 WebFrameSerializer::generateMarkOfTheWebDeclaration(file_url).utf8(); |
227 ASSERT_FALSE(motw_declaration.empty()); | 227 ASSERT_FALSE(motw_declaration.empty()); |
228 // The encoding of original contents is ISO-8859-1, so we convert the MOTW | 228 // The encoding of original contents is ISO-8859-1, so we convert the MOTW |
229 // declaration to ASCII and search whether original contents has it or not. | 229 // declaration to ASCII and search whether original contents has it or not. |
230 ASSERT_TRUE(std::string::npos == original_contents.find(motw_declaration)); | 230 ASSERT_TRUE(std::string::npos == original_contents.find(motw_declaration)); |
231 | 231 |
232 // Do serialization. | 232 // Do serialization. |
233 SerializeDomForURL(file_url); | 233 SerializeDomForURL(file_url); |
234 // Make sure the serialized contents have MOTW ; | 234 // Make sure the serialized contents have MOTW ; |
235 ASSERT_TRUE(serialization_reported_end_of_data_); | 235 ASSERT_TRUE(serialization_reported_end_of_data_); |
236 ASSERT_FALSE(std::string::npos == | 236 ASSERT_FALSE(std::string::npos == |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 ASSERT_TRUE(std::string(text_node.nodeValue().utf8()) == "&<>\"\'"); | 362 ASSERT_TRUE(std::string(text_node.nodeValue().utf8()) == "&<>\"\'"); |
363 // Do serialization. | 363 // Do serialization. |
364 SerializeDomForURL(file_url); | 364 SerializeDomForURL(file_url); |
365 // Compare the serialized contents with original contents. | 365 // Compare the serialized contents with original contents. |
366 ASSERT_TRUE(serialization_reported_end_of_data_); | 366 ASSERT_TRUE(serialization_reported_end_of_data_); |
367 // Compare the serialized contents with original contents to make sure | 367 // Compare the serialized contents with original contents to make sure |
368 // they are same. | 368 // they are same. |
369 // Because we add MOTW when serializing DOM, so before comparison, we also | 369 // Because we add MOTW when serializing DOM, so before comparison, we also |
370 // need to add MOTW to original_contents. | 370 // need to add MOTW to original_contents. |
371 std::string original_str = | 371 std::string original_str = |
372 WebPageSerializer::generateMarkOfTheWebDeclaration(file_url).utf8(); | 372 WebFrameSerializer::generateMarkOfTheWebDeclaration(file_url).utf8(); |
373 original_str += original_contents; | 373 original_str += original_contents; |
374 // Since WebCore now inserts a new HEAD element if there is no HEAD element | 374 // Since WebCore now inserts a new HEAD element if there is no HEAD element |
375 // when creating BODY element. (Please see | 375 // when creating BODY element. (Please see |
376 // HTMLParser::bodyCreateErrorCheck.) We need to append the HEAD content and | 376 // HTMLParser::bodyCreateErrorCheck.) We need to append the HEAD content and |
377 // corresponding META content if we find WebCore-generated HEAD element. | 377 // corresponding META content if we find WebCore-generated HEAD element. |
378 if (!doc.head().isNull()) { | 378 if (!doc.head().isNull()) { |
379 WebString encoding = web_frame->document().encoding(); | 379 WebString encoding = web_frame->document().encoding(); |
380 std::string htmlTag("<html>"); | 380 std::string htmlTag("<html>"); |
381 std::string::size_type pos = original_str.find(htmlTag); | 381 std::string::size_type pos = original_str.find(htmlTag); |
382 ASSERT_NE(std::string::npos, pos); | 382 ASSERT_NE(std::string::npos, pos); |
383 pos += htmlTag.length(); | 383 pos += htmlTag.length(); |
384 std::string head_part("<head>"); | 384 std::string head_part("<head>"); |
385 head_part += | 385 head_part += |
386 WebPageSerializer::generateMetaCharsetDeclaration(encoding).utf8(); | 386 WebFrameSerializer::generateMetaCharsetDeclaration(encoding).utf8(); |
387 head_part += "</head>"; | 387 head_part += "</head>"; |
388 original_str.insert(pos, head_part); | 388 original_str.insert(pos, head_part); |
389 } | 389 } |
390 ASSERT_EQ(original_str, serialized_contents_); | 390 ASSERT_EQ(original_str, serialized_contents_); |
391 } | 391 } |
392 | 392 |
393 void SerializeHTMLDOMWithEntitiesInAttributeValueOnRenderer() { | 393 void SerializeHTMLDOMWithEntitiesInAttributeValueOnRenderer() { |
394 base::FilePath page_file_path = GetTestFilePath( | 394 base::FilePath page_file_path = GetTestFilePath( |
395 "dom_serializer", "dom_serializer/htmlentities_in_attribute_value.htm"); | 395 "dom_serializer", "dom_serializer/htmlentities_in_attribute_value.htm"); |
396 // Get file URL. The URL is dummy URL to identify the following loading | 396 // Get file URL. The URL is dummy URL to identify the following loading |
(...skipping 14 matching lines...) Expand all Loading... |
411 ASSERT_TRUE(!body_ele.isNull()); | 411 ASSERT_TRUE(!body_ele.isNull()); |
412 WebString value = body_ele.getAttribute("title"); | 412 WebString value = body_ele.getAttribute("title"); |
413 ASSERT_TRUE(std::string(value.utf8()) == "&<>\"\'"); | 413 ASSERT_TRUE(std::string(value.utf8()) == "&<>\"\'"); |
414 // Do serialization. | 414 // Do serialization. |
415 SerializeDomForURL(file_url); | 415 SerializeDomForURL(file_url); |
416 // Compare the serialized contents with original contents. | 416 // Compare the serialized contents with original contents. |
417 ASSERT_TRUE(serialization_reported_end_of_data_); | 417 ASSERT_TRUE(serialization_reported_end_of_data_); |
418 // Compare the serialized contents with original contents to make sure | 418 // Compare the serialized contents with original contents to make sure |
419 // they are same. | 419 // they are same. |
420 std::string original_str = | 420 std::string original_str = |
421 WebPageSerializer::generateMarkOfTheWebDeclaration(file_url).utf8(); | 421 WebFrameSerializer::generateMarkOfTheWebDeclaration(file_url).utf8(); |
422 original_str += original_contents; | 422 original_str += original_contents; |
423 if (!doc.isNull()) { | 423 if (!doc.isNull()) { |
424 WebString encoding = web_frame->document().encoding(); | 424 WebString encoding = web_frame->document().encoding(); |
425 std::string htmlTag("<html>"); | 425 std::string htmlTag("<html>"); |
426 std::string::size_type pos = original_str.find(htmlTag); | 426 std::string::size_type pos = original_str.find(htmlTag); |
427 ASSERT_NE(std::string::npos, pos); | 427 ASSERT_NE(std::string::npos, pos); |
428 pos += htmlTag.length(); | 428 pos += htmlTag.length(); |
429 std::string head_part("<head>"); | 429 std::string head_part("<head>"); |
430 head_part += | 430 head_part += |
431 WebPageSerializer::generateMetaCharsetDeclaration(encoding).utf8(); | 431 WebFrameSerializer::generateMetaCharsetDeclaration(encoding).utf8(); |
432 head_part += "</head>"; | 432 head_part += "</head>"; |
433 original_str.insert(pos, head_part); | 433 original_str.insert(pos, head_part); |
434 } | 434 } |
435 ASSERT_EQ(original_str, serialized_contents_); | 435 ASSERT_EQ(original_str, serialized_contents_); |
436 } | 436 } |
437 | 437 |
438 void SerializeHTMLDOMWithNonStandardEntitiesOnRenderer(const GURL& file_url) { | 438 void SerializeHTMLDOMWithNonStandardEntitiesOnRenderer(const GURL& file_url) { |
439 // Get value of BODY's title attribute in DOM. | 439 // Get value of BODY's title attribute in DOM. |
440 WebFrame* web_frame = FindSubFrameByURL(file_url); | 440 WebFrame* web_frame = FindSubFrameByURL(file_url); |
441 WebDocument doc = web_frame->document(); | 441 WebDocument doc = web_frame->document(); |
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
854 NavigateToURL(shell(), file_url); | 854 NavigateToURL(shell(), file_url); |
855 | 855 |
856 PostTaskToInProcessRendererAndWait( | 856 PostTaskToInProcessRendererAndWait( |
857 base::Bind( | 857 base::Bind( |
858 &DomSerializerTests:: | 858 &DomSerializerTests:: |
859 SubResourceForElementsInNonHTMLNamespaceOnRenderer, | 859 SubResourceForElementsInNonHTMLNamespaceOnRenderer, |
860 base::Unretained(this), file_url)); | 860 base::Unretained(this), file_url)); |
861 } | 861 } |
862 | 862 |
863 } // namespace content | 863 } // namespace content |
OLD | NEW |