| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/containers/hash_tables.h" | 8 #include "base/containers/hash_tables.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 using blink::WebPageSerializer; | 50 using blink::WebPageSerializer; |
| 51 using blink::WebPageSerializerClient; | 51 using blink::WebPageSerializerClient; |
| 52 using blink::WebString; | 52 using blink::WebString; |
| 53 using blink::WebURL; | 53 using blink::WebURL; |
| 54 using blink::WebView; | 54 using blink::WebView; |
| 55 using blink::WebVector; | 55 using blink::WebVector; |
| 56 | 56 |
| 57 namespace content { | 57 namespace content { |
| 58 | 58 |
| 59 // Iterate recursively over sub-frames to find one with with a given url. | 59 // Iterate recursively over sub-frames to find one with with a given url. |
| 60 WebFrame* FindSubFrameByURL(WebView* web_view, const GURL& url) { | 60 WebFrame* FindSubframeByURL(WebView* web_view, const GURL& url) { |
| 61 if (!web_view->mainFrame()) | 61 if (!web_view->mainFrame()) |
| 62 return NULL; | 62 return NULL; |
| 63 | 63 |
| 64 std::vector<WebFrame*> stack; | 64 std::vector<WebFrame*> stack; |
| 65 stack.push_back(web_view->mainFrame()); | 65 stack.push_back(web_view->mainFrame()); |
| 66 | 66 |
| 67 while (!stack.empty()) { | 67 while (!stack.empty()) { |
| 68 WebFrame* current_frame = stack.back(); | 68 WebFrame* current_frame = stack.back(); |
| 69 stack.pop_back(); | 69 stack.pop_back(); |
| 70 if (GURL(current_frame->document().url()) == url) | 70 if (GURL(current_frame->document().url()) == url) |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 } | 152 } |
| 153 | 153 |
| 154 private: | 154 private: |
| 155 base::Closure quit_closure_; | 155 base::Closure quit_closure_; |
| 156 }; | 156 }; |
| 157 | 157 |
| 158 class DomSerializerTests : public ContentBrowserTest, | 158 class DomSerializerTests : public ContentBrowserTest, |
| 159 public WebPageSerializerClient { | 159 public WebPageSerializerClient { |
| 160 public: | 160 public: |
| 161 DomSerializerTests() | 161 DomSerializerTests() |
| 162 : serialized_(false), | 162 : got_end_of_frame_notification_(false), |
| 163 local_directory_name_(FILE_PATH_LITERAL("./dummy_files/")) {} | 163 local_directory_name_(FILE_PATH_LITERAL("./dummy_files/")) {} |
| 164 | 164 |
| 165 void SetUpCommandLine(base::CommandLine* command_line) override { | 165 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 166 command_line->AppendSwitch(switches::kSingleProcess); | 166 command_line->AppendSwitch(switches::kSingleProcess); |
| 167 #if defined(OS_WIN) | 167 #if defined(OS_WIN) |
| 168 // Don't want to try to create a GPU process. | 168 // Don't want to try to create a GPU process. |
| 169 command_line->AppendSwitch(switches::kDisableGpu); | 169 command_line->AppendSwitch(switches::kDisableGpu); |
| 170 #endif | 170 #endif |
| 171 } | 171 } |
| 172 | 172 |
| 173 void SetUpOnMainThread() override { | 173 void SetUpOnMainThread() override { |
| 174 render_view_routing_id_ = | 174 render_view_routing_id_ = |
| 175 shell()->web_contents()->GetRenderViewHost()->GetRoutingID(); | 175 shell()->web_contents()->GetRenderViewHost()->GetRoutingID(); |
| 176 } | 176 } |
| 177 | 177 |
| 178 // DomSerializerDelegate. | 178 void writeHtmlFragment(const WebCString& data) override { |
| 179 void didSerializeDataForFrame(const WebURL& frame_web_url, | 179 serialization_output_ += data.data(); |
| 180 const WebCString& data, | |
| 181 PageSerializationStatus status) override { | |
| 182 GURL frame_url(frame_web_url); | |
| 183 // If the all frames are finished saving, check all finish status | |
| 184 if (status == WebPageSerializerClient::AllFramesAreFinished) { | |
| 185 SerializationFinishStatusMap::iterator it = | |
| 186 serialization_finish_status_.begin(); | |
| 187 for (; it != serialization_finish_status_.end(); ++it) | |
| 188 ASSERT_TRUE(it->second); | |
| 189 serialized_ = true; | |
| 190 return; | |
| 191 } | |
| 192 | |
| 193 // Check finish status of current frame. | |
| 194 SerializationFinishStatusMap::iterator it = | |
| 195 serialization_finish_status_.find(frame_url.spec()); | |
| 196 // New frame, set initial status as false. | |
| 197 if (it == serialization_finish_status_.end()) | |
| 198 serialization_finish_status_[frame_url.spec()] = false; | |
| 199 | |
| 200 it = serialization_finish_status_.find(frame_url.spec()); | |
| 201 ASSERT_TRUE(it != serialization_finish_status_.end()); | |
| 202 // In process frame, finish status should be false. | |
| 203 ASSERT_FALSE(it->second); | |
| 204 | |
| 205 // Add data to corresponding frame's content. | |
| 206 serialized_frame_map_[frame_url.spec()] += data.data(); | |
| 207 | |
| 208 // Current frame is completed saving, change the finish status. | |
| 209 if (status == WebPageSerializerClient::CurrentFrameIsFinished) | |
| 210 it->second = true; | |
| 211 } | 180 } |
| 212 | 181 |
| 213 bool HasSerializedFrame(const GURL& frame_url) { | 182 void writeLocalPathForSubframe(const WebFrame& subframe) override { |
| 214 return serialized_frame_map_.find(frame_url.spec()) != | 183 // Tests in content/renderer/dom_serializer_browsertest.cc do not verify |
| 215 serialized_frame_map_.end(); | 184 // local paths in the serializer html (they are verified by |
| 185 // chrome/browser/download/save_page_browsertest.cc). Therefore the body of |
| 186 // writeLocalPathForSubframe can be empty below. |
| 216 } | 187 } |
| 217 | 188 |
| 218 const std::string& GetSerializedContentForFrame( | 189 void writeLocalPathForSavableResource( |
| 219 const GURL& frame_url) { | 190 const WebURL& savableResourceURL) override { |
| 220 return serialized_frame_map_[frame_url.spec()]; | 191 // Tests in content/renderer/dom_serializer_browsertest.cc do not verify |
| 192 // local paths in the serializer html (they are verified by |
| 193 // chrome/browser/download/save_page_browsertest.cc). Therefore the body of |
| 194 // writeLocalPathForSavableResource can be empty below. |
| 221 } | 195 } |
| 222 | 196 |
| 197 void endOfFrame() override { got_end_of_frame_notification_ = true; } |
| 198 |
| 199 const std::string& GetSerializedContent() { return serialization_output_; } |
| 200 |
| 223 RenderView* GetRenderView() { | 201 RenderView* GetRenderView() { |
| 224 return RenderView::FromRoutingID(render_view_routing_id_); | 202 return RenderView::FromRoutingID(render_view_routing_id_); |
| 225 } | 203 } |
| 226 | 204 |
| 227 WebView* GetWebView() { | 205 WebView* GetWebView() { |
| 228 return GetRenderView()->GetWebView(); | 206 return GetRenderView()->GetWebView(); |
| 229 } | 207 } |
| 230 | 208 |
| 231 WebFrame* GetMainFrame() { | 209 WebFrame* GetMainFrame() { |
| 232 return GetWebView()->mainFrame(); | 210 return GetWebView()->mainFrame(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 255 | 233 |
| 256 web_frame->loadData(data, "text/html", encoding_info, base_url); | 234 web_frame->loadData(data, "text/html", encoding_info, base_url); |
| 257 } | 235 } |
| 258 | 236 |
| 259 runner->Run(); | 237 runner->Run(); |
| 260 } | 238 } |
| 261 | 239 |
| 262 // Serialize DOM belonging to a frame with the specified |frame_url|. | 240 // Serialize DOM belonging to a frame with the specified |frame_url|. |
| 263 void SerializeDomForURL(const GURL& frame_url) { | 241 void SerializeDomForURL(const GURL& frame_url) { |
| 264 // Find corresponding WebFrame according to frame_url. | 242 // Find corresponding WebFrame according to frame_url. |
| 265 WebFrame* web_frame = FindSubFrameByURL(GetWebView(), frame_url); | 243 WebFrame* web_frame = FindSubframeByURL(GetWebView(), frame_url); |
| 266 ASSERT_TRUE(web_frame != NULL); | 244 ASSERT_TRUE(web_frame != NULL); |
| 267 WebVector<WebURL> links; | 245 WebVector<WebURL> links; |
| 268 links.assign(&frame_url, 1); | 246 links.assign(&frame_url, 1); |
| 269 WebString file_path = | 247 WebString file_path = |
| 270 base::FilePath(FILE_PATH_LITERAL("c:\\dummy.htm")).AsUTF16Unsafe(); | 248 base::FilePath(FILE_PATH_LITERAL("c:\\dummy.htm")).AsUTF16Unsafe(); |
| 271 WebVector<WebString> local_paths; | 249 WebVector<WebString> local_paths; |
| 272 local_paths.assign(&file_path, 1); | 250 local_paths.assign(&file_path, 1); |
| 273 // Start serializing DOM. | 251 // Start serializing DOM. |
| 274 bool result = WebPageSerializer::serialize(web_frame->toWebLocalFrame(), | 252 bool result = WebPageSerializer::serialize( |
| 275 static_cast<WebPageSerializerClient*>(this), | 253 web_frame->toWebLocalFrame(), |
| 276 links, | 254 static_cast<WebPageSerializerClient*>(this)); |
| 277 local_paths, | |
| 278 local_directory_name_.AsUTF16Unsafe()); | |
| 279 ASSERT_TRUE(result); | 255 ASSERT_TRUE(result); |
| 280 ASSERT_TRUE(serialized_); | 256 ASSERT_TRUE(got_end_of_frame_notification_); |
| 281 } | 257 } |
| 282 | 258 |
| 283 void SerializeHTMLDOMWithDocTypeOnRenderer(const GURL& file_url) { | 259 void SerializeHTMLDOMWithDocTypeOnRenderer(const GURL& file_url) { |
| 284 // Make sure original contents have document type. | 260 // Make sure original contents have document type. |
| 285 WebFrame* web_frame = FindSubFrameByURL(GetWebView(), file_url); | 261 WebFrame* web_frame = FindSubframeByURL(GetWebView(), file_url); |
| 286 ASSERT_TRUE(web_frame != NULL); | 262 ASSERT_TRUE(web_frame != NULL); |
| 287 WebDocument doc = web_frame->document(); | 263 WebDocument doc = web_frame->document(); |
| 288 ASSERT_TRUE(HasDocType(doc)); | 264 ASSERT_TRUE(HasDocType(doc)); |
| 289 // Do serialization. | 265 // Do serialization. |
| 290 SerializeDomForURL(file_url); | 266 SerializeDomForURL(file_url); |
| 291 // Load the serialized contents. | 267 // Load the serialized contents. |
| 292 ASSERT_TRUE(HasSerializedFrame(file_url)); | 268 const std::string& serialized_contents = GetSerializedContent(); |
| 293 const std::string& serialized_contents = | |
| 294 GetSerializedContentForFrame(file_url); | |
| 295 LoadContents(serialized_contents, file_url, | 269 LoadContents(serialized_contents, file_url, |
| 296 web_frame->document().encoding()); | 270 web_frame->document().encoding()); |
| 297 // Make sure serialized contents still have document type. | 271 // Make sure serialized contents still have document type. |
| 298 web_frame = GetMainFrame(); | 272 web_frame = GetMainFrame(); |
| 299 doc = web_frame->document(); | 273 doc = web_frame->document(); |
| 300 ASSERT_TRUE(HasDocType(doc)); | 274 ASSERT_TRUE(HasDocType(doc)); |
| 301 } | 275 } |
| 302 | 276 |
| 303 void SerializeHTMLDOMWithoutDocTypeOnRenderer(const GURL& file_url) { | 277 void SerializeHTMLDOMWithoutDocTypeOnRenderer(const GURL& file_url) { |
| 304 // Make sure original contents do not have document type. | 278 // Make sure original contents do not have document type. |
| 305 WebFrame* web_frame = FindSubFrameByURL(GetWebView(), file_url); | 279 WebFrame* web_frame = FindSubframeByURL(GetWebView(), file_url); |
| 306 ASSERT_TRUE(web_frame != NULL); | 280 ASSERT_TRUE(web_frame != NULL); |
| 307 WebDocument doc = web_frame->document(); | 281 WebDocument doc = web_frame->document(); |
| 308 ASSERT_TRUE(!HasDocType(doc)); | 282 ASSERT_TRUE(!HasDocType(doc)); |
| 309 // Do serialization. | 283 // Do serialization. |
| 310 SerializeDomForURL(file_url); | 284 SerializeDomForURL(file_url); |
| 311 // Load the serialized contents. | 285 // Load the serialized contents. |
| 312 ASSERT_TRUE(HasSerializedFrame(file_url)); | 286 const std::string& serialized_contents = GetSerializedContent(); |
| 313 const std::string& serialized_contents = | |
| 314 GetSerializedContentForFrame(file_url); | |
| 315 LoadContents(serialized_contents, file_url, | 287 LoadContents(serialized_contents, file_url, |
| 316 web_frame->document().encoding()); | 288 web_frame->document().encoding()); |
| 317 // Make sure serialized contents do not have document type. | 289 // Make sure serialized contents do not have document type. |
| 318 web_frame = GetMainFrame(); | 290 web_frame = GetMainFrame(); |
| 319 doc = web_frame->document(); | 291 doc = web_frame->document(); |
| 320 ASSERT_TRUE(!HasDocType(doc)); | 292 ASSERT_TRUE(!HasDocType(doc)); |
| 321 } | 293 } |
| 322 | 294 |
| 323 void SerializeXMLDocWithBuiltInEntitiesOnRenderer( | 295 void SerializeXMLDocWithBuiltInEntitiesOnRenderer( |
| 324 const GURL& xml_file_url, const std::string& original_contents) { | 296 const GURL& xml_file_url, const std::string& original_contents) { |
| 325 // Do serialization. | 297 // Do serialization. |
| 326 SerializeDomForURL(xml_file_url); | 298 SerializeDomForURL(xml_file_url); |
| 327 // Compare the serialized contents with original contents. | 299 // Compare the serialized contents with original contents. |
| 328 ASSERT_TRUE(HasSerializedFrame(xml_file_url)); | 300 const std::string& serialized_contents = GetSerializedContent(); |
| 329 const std::string& serialized_contents = | |
| 330 GetSerializedContentForFrame(xml_file_url); | |
| 331 ASSERT_EQ(original_contents, serialized_contents); | 301 ASSERT_EQ(original_contents, serialized_contents); |
| 332 } | 302 } |
| 333 | 303 |
| 334 void SerializeHTMLDOMWithAddingMOTWOnRenderer( | 304 void SerializeHTMLDOMWithAddingMOTWOnRenderer( |
| 335 const GURL& file_url, const std::string& original_contents) { | 305 const GURL& file_url, const std::string& original_contents) { |
| 336 // Make sure original contents does not have MOTW; | 306 // Make sure original contents does not have MOTW; |
| 337 std::string motw_declaration = | 307 std::string motw_declaration = |
| 338 WebPageSerializer::generateMarkOfTheWebDeclaration(file_url).utf8(); | 308 WebPageSerializer::generateMarkOfTheWebDeclaration(file_url).utf8(); |
| 339 ASSERT_FALSE(motw_declaration.empty()); | 309 ASSERT_FALSE(motw_declaration.empty()); |
| 340 // The encoding of original contents is ISO-8859-1, so we convert the MOTW | 310 // The encoding of original contents is ISO-8859-1, so we convert the MOTW |
| 341 // declaration to ASCII and search whether original contents has it or not. | 311 // declaration to ASCII and search whether original contents has it or not. |
| 342 ASSERT_TRUE(std::string::npos == original_contents.find(motw_declaration)); | 312 ASSERT_TRUE(std::string::npos == original_contents.find(motw_declaration)); |
| 343 | 313 |
| 344 // Do serialization. | 314 // Do serialization. |
| 345 SerializeDomForURL(file_url); | 315 SerializeDomForURL(file_url); |
| 346 // Make sure the serialized contents have MOTW ; | 316 // Make sure the serialized contents have MOTW ; |
| 347 ASSERT_TRUE(HasSerializedFrame(file_url)); | 317 const std::string& serialized_contents = GetSerializedContent(); |
| 348 const std::string& serialized_contents = | |
| 349 GetSerializedContentForFrame(file_url); | |
| 350 ASSERT_FALSE(std::string::npos == | 318 ASSERT_FALSE(std::string::npos == |
| 351 serialized_contents.find(motw_declaration)); | 319 serialized_contents.find(motw_declaration)); |
| 352 } | 320 } |
| 353 | 321 |
| 354 void SerializeHTMLDOMWithNoMetaCharsetInOriginalDocOnRenderer( | 322 void SerializeHTMLDOMWithNoMetaCharsetInOriginalDocOnRenderer( |
| 355 const GURL& file_url) { | 323 const GURL& file_url) { |
| 356 // Make sure there is no META charset declaration in original document. | 324 // Make sure there is no META charset declaration in original document. |
| 357 WebFrame* web_frame = FindSubFrameByURL(GetWebView(), file_url); | 325 WebFrame* web_frame = FindSubframeByURL(GetWebView(), file_url); |
| 358 ASSERT_TRUE(web_frame != NULL); | 326 ASSERT_TRUE(web_frame != NULL); |
| 359 WebDocument doc = web_frame->document(); | 327 WebDocument doc = web_frame->document(); |
| 360 ASSERT_TRUE(doc.isHTMLDocument()); | 328 ASSERT_TRUE(doc.isHTMLDocument()); |
| 361 WebElement head_element = doc.head(); | 329 WebElement head_element = doc.head(); |
| 362 ASSERT_TRUE(!head_element.isNull()); | 330 ASSERT_TRUE(!head_element.isNull()); |
| 363 // Go through all children of HEAD element. | 331 // Go through all children of HEAD element. |
| 364 for (WebNode child = head_element.firstChild(); !child.isNull(); | 332 for (WebNode child = head_element.firstChild(); !child.isNull(); |
| 365 child = child.nextSibling()) { | 333 child = child.nextSibling()) { |
| 366 std::string charset_info; | 334 std::string charset_info; |
| 367 if (IsMetaElement(child, charset_info)) | 335 if (IsMetaElement(child, charset_info)) |
| 368 ASSERT_TRUE(charset_info.empty()); | 336 ASSERT_TRUE(charset_info.empty()); |
| 369 } | 337 } |
| 370 // Do serialization. | 338 // Do serialization. |
| 371 SerializeDomForURL(file_url); | 339 SerializeDomForURL(file_url); |
| 372 | 340 |
| 373 // Load the serialized contents. | 341 // Load the serialized contents. |
| 374 ASSERT_TRUE(HasSerializedFrame(file_url)); | 342 const std::string& serialized_contents = GetSerializedContent(); |
| 375 const std::string& serialized_contents = | |
| 376 GetSerializedContentForFrame(file_url); | |
| 377 LoadContents(serialized_contents, file_url, | 343 LoadContents(serialized_contents, file_url, |
| 378 web_frame->document().encoding()); | 344 web_frame->document().encoding()); |
| 379 // Make sure the first child of HEAD element is META which has charset | 345 // Make sure the first child of HEAD element is META which has charset |
| 380 // declaration in serialized contents. | 346 // declaration in serialized contents. |
| 381 web_frame = GetMainFrame(); | 347 web_frame = GetMainFrame(); |
| 382 ASSERT_TRUE(web_frame != NULL); | 348 ASSERT_TRUE(web_frame != NULL); |
| 383 doc = web_frame->document(); | 349 doc = web_frame->document(); |
| 384 ASSERT_TRUE(doc.isHTMLDocument()); | 350 ASSERT_TRUE(doc.isHTMLDocument()); |
| 385 head_element = doc.head(); | 351 head_element = doc.head(); |
| 386 ASSERT_TRUE(!head_element.isNull()); | 352 ASSERT_TRUE(!head_element.isNull()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 399 std::string charset_info; | 365 std::string charset_info; |
| 400 if (IsMetaElement(child, charset_info)) | 366 if (IsMetaElement(child, charset_info)) |
| 401 ASSERT_TRUE(charset_info.empty()); | 367 ASSERT_TRUE(charset_info.empty()); |
| 402 } | 368 } |
| 403 } | 369 } |
| 404 | 370 |
| 405 void SerializeHTMLDOMWithMultipleMetaCharsetInOriginalDocOnRenderer( | 371 void SerializeHTMLDOMWithMultipleMetaCharsetInOriginalDocOnRenderer( |
| 406 const GURL& file_url) { | 372 const GURL& file_url) { |
| 407 // Make sure there are multiple META charset declarations in original | 373 // Make sure there are multiple META charset declarations in original |
| 408 // document. | 374 // document. |
| 409 WebFrame* web_frame = FindSubFrameByURL(GetWebView(), file_url); | 375 WebFrame* web_frame = FindSubframeByURL(GetWebView(), file_url); |
| 410 ASSERT_TRUE(web_frame != NULL); | 376 ASSERT_TRUE(web_frame != NULL); |
| 411 WebDocument doc = web_frame->document(); | 377 WebDocument doc = web_frame->document(); |
| 412 ASSERT_TRUE(doc.isHTMLDocument()); | 378 ASSERT_TRUE(doc.isHTMLDocument()); |
| 413 WebElement head_ele = doc.head(); | 379 WebElement head_ele = doc.head(); |
| 414 ASSERT_TRUE(!head_ele.isNull()); | 380 ASSERT_TRUE(!head_ele.isNull()); |
| 415 // Go through all children of HEAD element. | 381 // Go through all children of HEAD element. |
| 416 int charset_declaration_count = 0; | 382 int charset_declaration_count = 0; |
| 417 for (WebNode child = head_ele.firstChild(); !child.isNull(); | 383 for (WebNode child = head_ele.firstChild(); !child.isNull(); |
| 418 child = child.nextSibling()) { | 384 child = child.nextSibling()) { |
| 419 std::string charset_info; | 385 std::string charset_info; |
| 420 if (IsMetaElement(child, charset_info) && !charset_info.empty()) | 386 if (IsMetaElement(child, charset_info) && !charset_info.empty()) |
| 421 charset_declaration_count++; | 387 charset_declaration_count++; |
| 422 } | 388 } |
| 423 // The original doc has more than META tags which have charset declaration. | 389 // The original doc has more than META tags which have charset declaration. |
| 424 ASSERT_TRUE(charset_declaration_count > 1); | 390 ASSERT_TRUE(charset_declaration_count > 1); |
| 425 | 391 |
| 426 // Do serialization. | 392 // Do serialization. |
| 427 SerializeDomForURL(file_url); | 393 SerializeDomForURL(file_url); |
| 428 | 394 |
| 429 // Load the serialized contents. | 395 // Load the serialized contents. |
| 430 ASSERT_TRUE(HasSerializedFrame(file_url)); | 396 const std::string& serialized_contents = GetSerializedContent(); |
| 431 const std::string& serialized_contents = | |
| 432 GetSerializedContentForFrame(file_url); | |
| 433 LoadContents(serialized_contents, file_url, | 397 LoadContents(serialized_contents, file_url, |
| 434 web_frame->document().encoding()); | 398 web_frame->document().encoding()); |
| 435 // Make sure only first child of HEAD element is META which has charset | 399 // Make sure only first child of HEAD element is META which has charset |
| 436 // declaration in serialized contents. | 400 // declaration in serialized contents. |
| 437 web_frame = GetMainFrame(); | 401 web_frame = GetMainFrame(); |
| 438 ASSERT_TRUE(web_frame != NULL); | 402 ASSERT_TRUE(web_frame != NULL); |
| 439 doc = web_frame->document(); | 403 doc = web_frame->document(); |
| 440 ASSERT_TRUE(doc.isHTMLDocument()); | 404 ASSERT_TRUE(doc.isHTMLDocument()); |
| 441 head_ele = doc.head(); | 405 head_ele = doc.head(); |
| 442 ASSERT_TRUE(!head_ele.isNull()); | 406 ASSERT_TRUE(!head_ele.isNull()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 465 // actions. The test content is in constant:original_contents. | 429 // actions. The test content is in constant:original_contents. |
| 466 GURL file_url = net::FilePathToFileURL(page_file_path); | 430 GURL file_url = net::FilePathToFileURL(page_file_path); |
| 467 ASSERT_TRUE(file_url.SchemeIsFile()); | 431 ASSERT_TRUE(file_url.SchemeIsFile()); |
| 468 // Test contents. | 432 // Test contents. |
| 469 static const char* const original_contents = | 433 static const char* const original_contents = |
| 470 "<html><body>&<>\"\'</body></html>"; | 434 "<html><body>&<>\"\'</body></html>"; |
| 471 // Load the test contents. | 435 // Load the test contents. |
| 472 LoadContents(original_contents, file_url, WebString()); | 436 LoadContents(original_contents, file_url, WebString()); |
| 473 | 437 |
| 474 // Get BODY's text content in DOM. | 438 // Get BODY's text content in DOM. |
| 475 WebFrame* web_frame = FindSubFrameByURL(GetWebView(), file_url); | 439 WebFrame* web_frame = FindSubframeByURL(GetWebView(), file_url); |
| 476 ASSERT_TRUE(web_frame != NULL); | 440 ASSERT_TRUE(web_frame != NULL); |
| 477 WebDocument doc = web_frame->document(); | 441 WebDocument doc = web_frame->document(); |
| 478 ASSERT_TRUE(doc.isHTMLDocument()); | 442 ASSERT_TRUE(doc.isHTMLDocument()); |
| 479 WebElement body_ele = doc.body(); | 443 WebElement body_ele = doc.body(); |
| 480 ASSERT_TRUE(!body_ele.isNull()); | 444 ASSERT_TRUE(!body_ele.isNull()); |
| 481 WebNode text_node = body_ele.firstChild(); | 445 WebNode text_node = body_ele.firstChild(); |
| 482 ASSERT_TRUE(text_node.isTextNode()); | 446 ASSERT_TRUE(text_node.isTextNode()); |
| 483 ASSERT_TRUE(std::string(text_node.nodeValue().utf8()) == "&<>\"\'"); | 447 ASSERT_TRUE(std::string(text_node.nodeValue().utf8()) == "&<>\"\'"); |
| 484 // Do serialization. | 448 // Do serialization. |
| 485 SerializeDomForURL(file_url); | 449 SerializeDomForURL(file_url); |
| 486 // Compare the serialized contents with original contents. | 450 // Compare the serialized contents with original contents. |
| 487 ASSERT_TRUE(HasSerializedFrame(file_url)); | 451 const std::string& serialized_contents = GetSerializedContent(); |
| 488 const std::string& serialized_contents = | |
| 489 GetSerializedContentForFrame(file_url); | |
| 490 // Compare the serialized contents with original contents to make sure | 452 // Compare the serialized contents with original contents to make sure |
| 491 // they are same. | 453 // they are same. |
| 492 // Because we add MOTW when serializing DOM, so before comparison, we also | 454 // Because we add MOTW when serializing DOM, so before comparison, we also |
| 493 // need to add MOTW to original_contents. | 455 // need to add MOTW to original_contents. |
| 494 std::string original_str = | 456 std::string original_str = |
| 495 WebPageSerializer::generateMarkOfTheWebDeclaration(file_url).utf8(); | 457 WebPageSerializer::generateMarkOfTheWebDeclaration(file_url).utf8(); |
| 496 original_str += original_contents; | 458 original_str += original_contents; |
| 497 // Since WebCore now inserts a new HEAD element if there is no HEAD element | 459 // Since WebCore now inserts a new HEAD element if there is no HEAD element |
| 498 // when creating BODY element. (Please see | 460 // when creating BODY element. (Please see |
| 499 // HTMLParser::bodyCreateErrorCheck.) We need to append the HEAD content and | 461 // HTMLParser::bodyCreateErrorCheck.) We need to append the HEAD content and |
| (...skipping 19 matching lines...) Expand all Loading... |
| 519 // Get file URL. The URL is dummy URL to identify the following loading | 481 // Get file URL. The URL is dummy URL to identify the following loading |
| 520 // actions. The test content is in constant:original_contents. | 482 // actions. The test content is in constant:original_contents. |
| 521 GURL file_url = net::FilePathToFileURL(page_file_path); | 483 GURL file_url = net::FilePathToFileURL(page_file_path); |
| 522 ASSERT_TRUE(file_url.SchemeIsFile()); | 484 ASSERT_TRUE(file_url.SchemeIsFile()); |
| 523 // Test contents. | 485 // Test contents. |
| 524 static const char* const original_contents = | 486 static const char* const original_contents = |
| 525 "<html><body title=\"&<>"'\"></body></html>"; | 487 "<html><body title=\"&<>"'\"></body></html>"; |
| 526 // Load the test contents. | 488 // Load the test contents. |
| 527 LoadContents(original_contents, file_url, WebString()); | 489 LoadContents(original_contents, file_url, WebString()); |
| 528 // Get value of BODY's title attribute in DOM. | 490 // Get value of BODY's title attribute in DOM. |
| 529 WebFrame* web_frame = FindSubFrameByURL(GetWebView(), file_url); | 491 WebFrame* web_frame = FindSubframeByURL(GetWebView(), file_url); |
| 530 ASSERT_TRUE(web_frame != NULL); | 492 ASSERT_TRUE(web_frame != NULL); |
| 531 WebDocument doc = web_frame->document(); | 493 WebDocument doc = web_frame->document(); |
| 532 ASSERT_TRUE(doc.isHTMLDocument()); | 494 ASSERT_TRUE(doc.isHTMLDocument()); |
| 533 WebElement body_ele = doc.body(); | 495 WebElement body_ele = doc.body(); |
| 534 ASSERT_TRUE(!body_ele.isNull()); | 496 ASSERT_TRUE(!body_ele.isNull()); |
| 535 WebString value = body_ele.getAttribute("title"); | 497 WebString value = body_ele.getAttribute("title"); |
| 536 ASSERT_TRUE(std::string(value.utf8()) == "&<>\"\'"); | 498 ASSERT_TRUE(std::string(value.utf8()) == "&<>\"\'"); |
| 537 // Do serialization. | 499 // Do serialization. |
| 538 SerializeDomForURL(file_url); | 500 SerializeDomForURL(file_url); |
| 539 // Compare the serialized contents with original contents. | 501 // Compare the serialized contents with original contents. |
| 540 ASSERT_TRUE(HasSerializedFrame(file_url)); | 502 const std::string& serialized_contents = GetSerializedContent(); |
| 541 const std::string& serialized_contents = | |
| 542 GetSerializedContentForFrame(file_url); | |
| 543 // Compare the serialized contents with original contents to make sure | 503 // Compare the serialized contents with original contents to make sure |
| 544 // they are same. | 504 // they are same. |
| 545 std::string original_str = | 505 std::string original_str = |
| 546 WebPageSerializer::generateMarkOfTheWebDeclaration(file_url).utf8(); | 506 WebPageSerializer::generateMarkOfTheWebDeclaration(file_url).utf8(); |
| 547 original_str += original_contents; | 507 original_str += original_contents; |
| 548 if (!doc.isNull()) { | 508 if (!doc.isNull()) { |
| 549 WebString encoding = web_frame->document().encoding(); | 509 WebString encoding = web_frame->document().encoding(); |
| 550 std::string htmlTag("<html>"); | 510 std::string htmlTag("<html>"); |
| 551 std::string::size_type pos = original_str.find(htmlTag); | 511 std::string::size_type pos = original_str.find(htmlTag); |
| 552 ASSERT_NE(std::string::npos, pos); | 512 ASSERT_NE(std::string::npos, pos); |
| 553 pos += htmlTag.length(); | 513 pos += htmlTag.length(); |
| 554 std::string head_part("<head>"); | 514 std::string head_part("<head>"); |
| 555 head_part += | 515 head_part += |
| 556 WebPageSerializer::generateMetaCharsetDeclaration(encoding).utf8(); | 516 WebPageSerializer::generateMetaCharsetDeclaration(encoding).utf8(); |
| 557 head_part += "</head>"; | 517 head_part += "</head>"; |
| 558 original_str.insert(pos, head_part); | 518 original_str.insert(pos, head_part); |
| 559 } | 519 } |
| 560 ASSERT_EQ(original_str, serialized_contents); | 520 ASSERT_EQ(original_str, serialized_contents); |
| 561 } | 521 } |
| 562 | 522 |
| 563 void SerializeHTMLDOMWithNonStandardEntitiesOnRenderer(const GURL& file_url) { | 523 void SerializeHTMLDOMWithNonStandardEntitiesOnRenderer(const GURL& file_url) { |
| 564 // Get value of BODY's title attribute in DOM. | 524 // Get value of BODY's title attribute in DOM. |
| 565 WebFrame* web_frame = FindSubFrameByURL(GetWebView(), file_url); | 525 WebFrame* web_frame = FindSubframeByURL(GetWebView(), file_url); |
| 566 WebDocument doc = web_frame->document(); | 526 WebDocument doc = web_frame->document(); |
| 567 ASSERT_TRUE(doc.isHTMLDocument()); | 527 ASSERT_TRUE(doc.isHTMLDocument()); |
| 568 WebElement body_element = doc.body(); | 528 WebElement body_element = doc.body(); |
| 569 // Unescaped string for "%⊅¹'". | 529 // Unescaped string for "%⊅¹'". |
| 570 static const wchar_t parsed_value[] = { | 530 static const wchar_t parsed_value[] = { |
| 571 '%', 0x2285, 0x00b9, '\'', 0 | 531 '%', 0x2285, 0x00b9, '\'', 0 |
| 572 }; | 532 }; |
| 573 WebString value = body_element.getAttribute("title"); | 533 WebString value = body_element.getAttribute("title"); |
| 574 WebString content = doc.contentAsTextForTesting(); | 534 WebString content = doc.contentAsTextForTesting(); |
| 575 ASSERT_TRUE(base::UTF16ToWide(value) == parsed_value); | 535 ASSERT_TRUE(base::UTF16ToWide(value) == parsed_value); |
| 576 ASSERT_TRUE(base::UTF16ToWide(content) == parsed_value); | 536 ASSERT_TRUE(base::UTF16ToWide(content) == parsed_value); |
| 577 | 537 |
| 578 // Do serialization. | 538 // Do serialization. |
| 579 SerializeDomForURL(file_url); | 539 SerializeDomForURL(file_url); |
| 580 // Check the serialized string. | 540 // Check the serialized string. |
| 581 ASSERT_TRUE(HasSerializedFrame(file_url)); | 541 const std::string& serialized_contents = GetSerializedContent(); |
| 582 const std::string& serialized_contents = | |
| 583 GetSerializedContentForFrame(file_url); | |
| 584 // Confirm that the serialized string has no non-standard HTML entities. | 542 // Confirm that the serialized string has no non-standard HTML entities. |
| 585 ASSERT_EQ(std::string::npos, serialized_contents.find("%")); | 543 ASSERT_EQ(std::string::npos, serialized_contents.find("%")); |
| 586 ASSERT_EQ(std::string::npos, serialized_contents.find("⊅")); | 544 ASSERT_EQ(std::string::npos, serialized_contents.find("⊅")); |
| 587 ASSERT_EQ(std::string::npos, serialized_contents.find("¹")); | 545 ASSERT_EQ(std::string::npos, serialized_contents.find("¹")); |
| 588 ASSERT_EQ(std::string::npos, serialized_contents.find("'")); | 546 ASSERT_EQ(std::string::npos, serialized_contents.find("'")); |
| 589 } | 547 } |
| 590 | 548 |
| 591 void SerializeHTMLDOMWithBaseTagOnRenderer(const GURL& file_url, | 549 void SerializeHTMLDOMWithBaseTagOnRenderer(const GURL& file_url, |
| 592 const GURL& path_dir_url) { | 550 const GURL& path_dir_url) { |
| 593 // There are total 2 available base tags in this test file. | 551 // There are total 2 available base tags in this test file. |
| 594 const int kTotalBaseTagCountInTestFile = 2; | 552 const int kTotalBaseTagCountInTestFile = 2; |
| 595 | 553 |
| 596 // Since for this test, we assume there is no savable sub-resource links for | 554 // Since for this test, we assume there is no savable sub-resource links for |
| 597 // this test file, also all links are relative URLs in this test file, so we | 555 // this test file, also all links are relative URLs in this test file, so we |
| 598 // need to check those relative URLs and make sure document has BASE tag. | 556 // need to check those relative URLs and make sure document has BASE tag. |
| 599 WebFrame* web_frame = FindSubFrameByURL(GetWebView(), file_url); | 557 WebFrame* web_frame = FindSubframeByURL(GetWebView(), file_url); |
| 600 ASSERT_TRUE(web_frame != NULL); | 558 ASSERT_TRUE(web_frame != NULL); |
| 601 WebDocument doc = web_frame->document(); | 559 WebDocument doc = web_frame->document(); |
| 602 ASSERT_TRUE(doc.isHTMLDocument()); | 560 ASSERT_TRUE(doc.isHTMLDocument()); |
| 603 // Go through all descent nodes. | 561 // Go through all descent nodes. |
| 604 WebElementCollection all = doc.all(); | 562 WebElementCollection all = doc.all(); |
| 605 int original_base_tag_count = 0; | 563 int original_base_tag_count = 0; |
| 606 for (WebElement element = all.firstItem(); !element.isNull(); | 564 for (WebElement element = all.firstItem(); !element.isNull(); |
| 607 element = all.nextItem()) { | 565 element = all.nextItem()) { |
| 608 if (element.hasHTMLTagName("base")) { | 566 if (element.hasHTMLTagName("base")) { |
| 609 original_base_tag_count++; | 567 original_base_tag_count++; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 625 ASSERT_EQ(original_base_tag_count, kTotalBaseTagCountInTestFile); | 583 ASSERT_EQ(original_base_tag_count, kTotalBaseTagCountInTestFile); |
| 626 // Make sure in original document, the base URL is not equal with the | 584 // Make sure in original document, the base URL is not equal with the |
| 627 // |path_dir_url|. | 585 // |path_dir_url|. |
| 628 GURL original_base_url(doc.baseURL()); | 586 GURL original_base_url(doc.baseURL()); |
| 629 ASSERT_NE(original_base_url, path_dir_url); | 587 ASSERT_NE(original_base_url, path_dir_url); |
| 630 | 588 |
| 631 // Do serialization. | 589 // Do serialization. |
| 632 SerializeDomForURL(file_url); | 590 SerializeDomForURL(file_url); |
| 633 | 591 |
| 634 // Load the serialized contents. | 592 // Load the serialized contents. |
| 635 ASSERT_TRUE(HasSerializedFrame(file_url)); | 593 const std::string& serialized_contents = GetSerializedContent(); |
| 636 const std::string& serialized_contents = | |
| 637 GetSerializedContentForFrame(file_url); | |
| 638 LoadContents(serialized_contents, file_url, | 594 LoadContents(serialized_contents, file_url, |
| 639 web_frame->document().encoding()); | 595 web_frame->document().encoding()); |
| 640 | 596 |
| 641 // Make sure all links are absolute URLs and doc there are some number of | 597 // Make sure all links are absolute URLs and doc there are some number of |
| 642 // BASE tags in serialized HTML data. Each of those BASE tags have same base | 598 // BASE tags in serialized HTML data. Each of those BASE tags have same base |
| 643 // URL which is as same as URL of current test file. | 599 // URL which is as same as URL of current test file. |
| 644 web_frame = GetMainFrame(); | 600 web_frame = GetMainFrame(); |
| 645 ASSERT_TRUE(web_frame != NULL); | 601 ASSERT_TRUE(web_frame != NULL); |
| 646 doc = web_frame->document(); | 602 doc = web_frame->document(); |
| 647 ASSERT_TRUE(doc.isHTMLDocument()); | 603 ASSERT_TRUE(doc.isHTMLDocument()); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 WebDocument doc = web_frame->document(); | 650 WebDocument doc = web_frame->document(); |
| 695 ASSERT_TRUE(doc.isHTMLDocument()); | 651 ASSERT_TRUE(doc.isHTMLDocument()); |
| 696 WebElement head_element = doc.head(); | 652 WebElement head_element = doc.head(); |
| 697 ASSERT_TRUE(!head_element.isNull()); | 653 ASSERT_TRUE(!head_element.isNull()); |
| 698 ASSERT_TRUE(!head_element.hasChildNodes()); | 654 ASSERT_TRUE(!head_element.hasChildNodes()); |
| 699 ASSERT_TRUE(head_element.childNodes().length() == 0); | 655 ASSERT_TRUE(head_element.childNodes().length() == 0); |
| 700 | 656 |
| 701 // Do serialization. | 657 // Do serialization. |
| 702 SerializeDomForURL(file_url); | 658 SerializeDomForURL(file_url); |
| 703 // Make sure the serialized contents have META ; | 659 // Make sure the serialized contents have META ; |
| 704 ASSERT_TRUE(HasSerializedFrame(file_url)); | 660 const std::string& serialized_contents = GetSerializedContent(); |
| 705 const std::string& serialized_contents = | |
| 706 GetSerializedContentForFrame(file_url); | |
| 707 | 661 |
| 708 // Reload serialized contents and make sure there is only one META tag. | 662 // Reload serialized contents and make sure there is only one META tag. |
| 709 LoadContents(serialized_contents, file_url, | 663 LoadContents(serialized_contents, file_url, |
| 710 web_frame->document().encoding()); | 664 web_frame->document().encoding()); |
| 711 web_frame = GetMainFrame(); | 665 web_frame = GetMainFrame(); |
| 712 ASSERT_TRUE(web_frame != NULL); | 666 ASSERT_TRUE(web_frame != NULL); |
| 713 doc = web_frame->document(); | 667 doc = web_frame->document(); |
| 714 ASSERT_TRUE(doc.isHTMLDocument()); | 668 ASSERT_TRUE(doc.isHTMLDocument()); |
| 715 head_element = doc.head(); | 669 head_element = doc.head(); |
| 716 ASSERT_TRUE(!head_element.isNull()); | 670 ASSERT_TRUE(!head_element.isNull()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 730 WebElement body_element = doc.body(); | 684 WebElement body_element = doc.body(); |
| 731 ASSERT_TRUE(!body_element.isNull()); | 685 ASSERT_TRUE(!body_element.isNull()); |
| 732 WebNode text_node = body_element.firstChild(); | 686 WebNode text_node = body_element.firstChild(); |
| 733 ASSERT_TRUE(text_node.isTextNode()); | 687 ASSERT_TRUE(text_node.isTextNode()); |
| 734 WebString text_node_contents = text_node.nodeValue(); | 688 WebString text_node_contents = text_node.nodeValue(); |
| 735 ASSERT_TRUE(std::string(text_node_contents.utf8()) == "hello world"); | 689 ASSERT_TRUE(std::string(text_node_contents.utf8()) == "hello world"); |
| 736 } | 690 } |
| 737 | 691 |
| 738 void SubResourceForElementsInNonHTMLNamespaceOnRenderer( | 692 void SubResourceForElementsInNonHTMLNamespaceOnRenderer( |
| 739 const GURL& file_url) { | 693 const GURL& file_url) { |
| 740 WebFrame* web_frame = FindSubFrameByURL(GetWebView(), file_url); | 694 WebFrame* web_frame = FindSubframeByURL(GetWebView(), file_url); |
| 741 ASSERT_TRUE(web_frame != NULL); | 695 ASSERT_TRUE(web_frame != NULL); |
| 742 WebDocument doc = web_frame->document(); | 696 WebDocument doc = web_frame->document(); |
| 743 WebNode lastNodeInBody = doc.body().lastChild(); | 697 WebNode lastNodeInBody = doc.body().lastChild(); |
| 744 ASSERT_TRUE(lastNodeInBody.isElementNode()); | 698 ASSERT_TRUE(lastNodeInBody.isElementNode()); |
| 745 WebString uri = GetSubResourceLinkFromElement( | 699 WebString uri = GetSubResourceLinkFromElement( |
| 746 lastNodeInBody.to<WebElement>()); | 700 lastNodeInBody.to<WebElement>()); |
| 747 EXPECT_TRUE(uri.isNull()); | 701 EXPECT_TRUE(uri.isNull()); |
| 748 } | 702 } |
| 749 | 703 |
| 750 private: | 704 private: |
| 751 int32 render_view_routing_id_; | 705 int32 render_view_routing_id_; |
| 752 // Map frame_url to corresponding serialized_content. | 706 std::string serialization_output_; |
| 753 typedef base::hash_map<std::string, std::string> SerializedFrameContentMap; | |
| 754 SerializedFrameContentMap serialized_frame_map_; | |
| 755 // Map frame_url to corresponding status of serialization finish. | 707 // Map frame_url to corresponding status of serialization finish. |
| 756 typedef base::hash_map<std::string, bool> SerializationFinishStatusMap; | 708 typedef base::hash_map<std::string, bool> SerializationFinishStatusMap; |
| 757 SerializationFinishStatusMap serialization_finish_status_; | 709 SerializationFinishStatusMap serialization_finish_status_; |
| 758 // Flag indicates whether the process of serializing DOM is finished or not. | 710 // Flag indicates whether the process of serializing DOM is finished or not. |
| 759 bool serialized_; | 711 bool got_end_of_frame_notification_; |
| 760 // The local_directory_name_ is dummy relative path of directory which | 712 // The local_directory_name_ is dummy relative path of directory which |
| 761 // contain all saved auxiliary files included all sub frames and resources. | 713 // contain all saved auxiliary files included all sub frames and resources. |
| 762 const base::FilePath local_directory_name_; | 714 const base::FilePath local_directory_name_; |
| 763 }; | 715 }; |
| 764 | 716 |
| 765 // If original contents have document type, the serialized contents also have | 717 // If original contents have document type, the serialized contents also have |
| 766 // document type. | 718 // document type. |
| 767 // Disabled by ellyjones@ on 2015-05-18, see https://crbug.com/488495. | 719 // Disabled by ellyjones@ on 2015-05-18, see https://crbug.com/488495. |
| 768 #if defined(OS_MACOSX) | 720 #if defined(OS_MACOSX) |
| 769 #define MAYBE_SerializeHTMLDOMWithDocType DISABLED_SerializeHTMLDOMWithDocType | 721 #define MAYBE_SerializeHTMLDOMWithDocType DISABLED_SerializeHTMLDOMWithDocType |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1001 NavigateToURL(shell(), file_url); | 953 NavigateToURL(shell(), file_url); |
| 1002 | 954 |
| 1003 PostTaskToInProcessRendererAndWait( | 955 PostTaskToInProcessRendererAndWait( |
| 1004 base::Bind( | 956 base::Bind( |
| 1005 &DomSerializerTests:: | 957 &DomSerializerTests:: |
| 1006 SubResourceForElementsInNonHTMLNamespaceOnRenderer, | 958 SubResourceForElementsInNonHTMLNamespaceOnRenderer, |
| 1007 base::Unretained(this), file_url)); | 959 base::Unretained(this), file_url)); |
| 1008 } | 960 } |
| 1009 | 961 |
| 1010 } // namespace content | 962 } // namespace content |
| OLD | NEW |