Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: third_party/WebKit/Source/core/loader/DocumentLoader.cpp

Issue 1823863002: Simplify parsing/loading state, attempt #2 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2011 Google Inc. All rights reserved. 3 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 return &m_frame->loader(); 121 return &m_frame->loader();
122 } 122 }
123 123
124 ResourceLoader* DocumentLoader::mainResourceLoader() const 124 ResourceLoader* DocumentLoader::mainResourceLoader() const
125 { 125 {
126 return m_mainResource ? m_mainResource->loader() : nullptr; 126 return m_mainResource ? m_mainResource->loader() : nullptr;
127 } 127 }
128 128
129 DocumentLoader::~DocumentLoader() 129 DocumentLoader::~DocumentLoader()
130 { 130 {
131 ASSERT(!m_frame || !isLoading()); 131 ASSERT(!m_frame);
132 ASSERT(!m_mainResource); 132 ASSERT(!m_mainResource);
133 ASSERT(!m_applicationCacheHost); 133 ASSERT(!m_applicationCacheHost);
134 } 134 }
135 135
136 DEFINE_TRACE(DocumentLoader) 136 DEFINE_TRACE(DocumentLoader)
137 { 137 {
138 visitor->trace(m_frame); 138 visitor->trace(m_frame);
139 visitor->trace(m_fetcher); 139 visitor->trace(m_fetcher);
140 visitor->trace(m_mainResource); 140 visitor->trace(m_mainResource);
141 visitor->trace(m_writer); 141 visitor->trace(m_writer);
142 visitor->trace(m_documentLoadTiming); 142 visitor->trace(m_documentLoadTiming);
143 visitor->trace(m_applicationCacheHost); 143 visitor->trace(m_applicationCacheHost);
144 visitor->trace(m_contentSecurityPolicy); 144 visitor->trace(m_contentSecurityPolicy);
145 } 145 }
146 146
147 unsigned long DocumentLoader::mainResourceIdentifier() const 147 unsigned long DocumentLoader::mainResourceIdentifier() const
148 { 148 {
149 return m_mainResource ? m_mainResource->identifier() : 0; 149 return m_mainResource ? m_mainResource->identifier() : 0;
150 } 150 }
151 151
152 Document* DocumentLoader::document() const
153 {
154 if (m_frame && m_frame->loader().documentLoader() == this)
155 return m_frame->document();
156 return nullptr;
157 }
158
159 const ResourceRequest& DocumentLoader::originalRequest() const 152 const ResourceRequest& DocumentLoader::originalRequest() const
160 { 153 {
161 return m_originalRequest; 154 return m_originalRequest;
162 } 155 }
163 156
164 const ResourceRequest& DocumentLoader::request() const 157 const ResourceRequest& DocumentLoader::request() const
165 { 158 {
166 return m_request; 159 return m_request;
167 } 160 }
168 161
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 if (m_isClientRedirect) 223 if (m_isClientRedirect)
231 appendRedirect(oldURL); 224 appendRedirect(oldURL);
232 appendRedirect(newURL); 225 appendRedirect(newURL);
233 } 226 }
234 227
235 const KURL& DocumentLoader::urlForHistory() const 228 const KURL& DocumentLoader::urlForHistory() const
236 { 229 {
237 return unreachableURL().isEmpty() ? url() : unreachableURL(); 230 return unreachableURL().isEmpty() ? url() : unreachableURL();
238 } 231 }
239 232
240 void DocumentLoader::mainReceivedError(const ResourceError& error)
241 {
242 ASSERT(!error.isNull());
243 ASSERT(!m_frame || !m_frame->page()->defersLoading() || InspectorInstrumenta tion::isDebuggerPaused(m_frame));
244 if (m_applicationCacheHost)
245 m_applicationCacheHost->failedLoadingMainResource();
246 if (!frameLoader())
247 return;
248 m_state = MainResourceDone;
249 frameLoader()->receivedMainResourceError(this, error);
250 clearMainResourceHandle();
251 }
252
253 // Cancels the data source's pending loads. Conceptually, a data source only lo ads
254 // one document at a time, but one document may have many related resources.
255 // stopLoading will stop all loads initiated by the data source,
256 // but not loads initiated by child frames' data sources -- that's the WebFrame' s job.
257 void DocumentLoader::stopLoading()
258 {
259 RefPtrWillBeRawPtr<LocalFrame> protectFrame(m_frame.get());
260 RefPtrWillBeRawPtr<DocumentLoader> protectLoader(this);
261
262 if (isLoading())
263 cancelMainResourceLoad(ResourceError::cancelledError(m_request.url()));
264 m_fetcher->stopFetching();
265 }
266
267 void DocumentLoader::commitIfReady() 233 void DocumentLoader::commitIfReady()
268 { 234 {
269 if (m_state < Committed) { 235 if (m_state < Committed) {
270 m_state = Committed; 236 m_state = Committed;
271 frameLoader()->commitProvisionalLoad(); 237 frameLoader()->commitProvisionalLoad();
272 } 238 }
273 } 239 }
274 240
275 bool DocumentLoader::isLoading() const
276 {
277 if (document() && document()->hasActiveParser())
278 return true;
279
280 return (m_state > NotStarted && m_state < MainResourceDone) || m_fetcher->is Fetching();
281 }
282
283 void DocumentLoader::notifyFinished(Resource* resource) 241 void DocumentLoader::notifyFinished(Resource* resource)
284 { 242 {
285 ASSERT_UNUSED(resource, m_mainResource == resource); 243 ASSERT_UNUSED(resource, m_mainResource == resource);
286 ASSERT(m_mainResource); 244 ASSERT(m_mainResource);
287 245
288 RefPtrWillBeRawPtr<DocumentLoader> protect(this); 246 RefPtrWillBeRawPtr<DocumentLoader> protect(this);
289 247
290 if (!m_mainResource->errorOccurred() && !m_mainResource->wasCanceled()) { 248 if (!m_mainResource->errorOccurred() && !m_mainResource->wasCanceled()) {
291 finishedLoading(m_mainResource->loadFinishTime()); 249 finishedLoading(m_mainResource->loadFinishTime());
292 return; 250 return;
293 } 251 }
294 252
295 mainReceivedError(m_mainResource->resourceError()); 253 if (m_applicationCacheHost)
254 m_applicationCacheHost->failedLoadingMainResource();
255 m_state = MainResourceDone;
256 frameLoader()->loadFailed(this, m_mainResource->resourceError());
257 clearMainResourceHandle();
296 } 258 }
297 259
298 void DocumentLoader::finishedLoading(double finishTime) 260 void DocumentLoader::finishedLoading(double finishTime)
299 { 261 {
300 ASSERT(!m_frame->page()->defersLoading() || InspectorInstrumentation::isDebu ggerPaused(m_frame)); 262 ASSERT(!m_frame->page()->defersLoading() || InspectorInstrumentation::isDebu ggerPaused(m_frame));
301 263
302 RefPtrWillBeRawPtr<DocumentLoader> protect(this); 264 RefPtrWillBeRawPtr<DocumentLoader> protect(this);
303 265
304 double responseEndTime = finishTime; 266 double responseEndTime = finishTime;
305 if (!responseEndTime) 267 if (!responseEndTime)
(...skipping 28 matching lines...) Expand all
334 ASSERT_UNUSED(resource, resource == m_mainResource); 296 ASSERT_UNUSED(resource, resource == m_mainResource);
335 ASSERT(!redirectResponse.isNull()); 297 ASSERT(!redirectResponse.isNull());
336 m_request = request; 298 m_request = request;
337 299
338 // If the redirecting url is not allowed to display content from the target origin, 300 // If the redirecting url is not allowed to display content from the target origin,
339 // then block the redirect. 301 // then block the redirect.
340 const KURL& requestURL = m_request.url(); 302 const KURL& requestURL = m_request.url();
341 RefPtr<SecurityOrigin> redirectingOrigin = SecurityOrigin::create(redirectRe sponse.url()); 303 RefPtr<SecurityOrigin> redirectingOrigin = SecurityOrigin::create(redirectRe sponse.url());
342 if (!redirectingOrigin->canDisplay(requestURL)) { 304 if (!redirectingOrigin->canDisplay(requestURL)) {
343 FrameLoader::reportLocalLoadFailed(m_frame, requestURL.getString()); 305 FrameLoader::reportLocalLoadFailed(m_frame, requestURL.getString());
344 cancelMainResourceLoad(ResourceError::cancelledError(requestURL)); 306 m_fetcher->stopFetching();
345 return; 307 return;
346 } 308 }
347 if (!frameLoader()->shouldContinueForNavigationPolicy(m_request, SubstituteD ata(), this, CheckContentSecurityPolicy, m_navigationType, NavigationPolicyCurre ntTab, replacesCurrentHistoryItem(), isClientRedirect())) { 309 if (!frameLoader()->shouldContinueForNavigationPolicy(m_request, SubstituteD ata(), this, CheckContentSecurityPolicy, m_navigationType, NavigationPolicyCurre ntTab, replacesCurrentHistoryItem(), isClientRedirect())) {
348 cancelMainResourceLoad(ResourceError::cancelledError(requestURL)); 310 m_fetcher->stopFetching();
349 return; 311 return;
350 } 312 }
351 313
352 ASSERT(timing().fetchStart()); 314 ASSERT(timing().fetchStart());
353 timing().addRedirect(redirectResponse.url(), requestURL); 315 timing().addRedirect(redirectResponse.url(), requestURL);
354 appendRedirect(requestURL); 316 appendRedirect(requestURL);
355 frameLoader()->receivedMainResourceRedirect(requestURL); 317 frameLoader()->receivedMainResourceRedirect(requestURL);
356 } 318 }
357 319
358 static bool canShowMIMEType(const String& mimeType, Page* page) 320 static bool canShowMIMEType(const String& mimeType, Page* page)
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 404
443 ASSERT(!m_frame->page()->defersLoading()); 405 ASSERT(!m_frame->page()->defersLoading());
444 406
445 m_response = response; 407 m_response = response;
446 408
447 if (isArchiveMIMEType(m_response.mimeType()) && m_mainResource->getDataBuffe ringPolicy() != BufferData) 409 if (isArchiveMIMEType(m_response.mimeType()) && m_mainResource->getDataBuffe ringPolicy() != BufferData)
448 m_mainResource->setDataBufferingPolicy(BufferData); 410 m_mainResource->setDataBufferingPolicy(BufferData);
449 411
450 if (!shouldContinueForResponse()) { 412 if (!shouldContinueForResponse()) {
451 InspectorInstrumentation::continueWithPolicyIgnore(m_frame, this, m_main Resource->identifier(), m_response); 413 InspectorInstrumentation::continueWithPolicyIgnore(m_frame, this, m_main Resource->identifier(), m_response);
452 cancelMainResourceLoad(ResourceError::cancelledError(m_request.url())); 414 m_fetcher->stopFetching();
453 return; 415 return;
454 } 416 }
455 417
456 if (m_response.isHTTP()) { 418 if (m_response.isHTTP()) {
457 int status = m_response.httpStatusCode(); 419 int status = m_response.httpStatusCode();
458 if ((status < 200 || status >= 300) && m_frame->owner()) 420 if ((status < 200 || status >= 300) && m_frame->owner())
459 m_frame->owner()->renderFallbackContent(); 421 m_frame->owner()->renderFallbackContent();
460 } 422 }
461 } 423 }
462 424
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 m_frame->document()->maybeHandleHttpRefresh(m_response.httpHeaderField(HTTPN ames::Refresh), Document::HttpRefreshFromHeader); 462 m_frame->document()->maybeHandleHttpRefresh(m_response.httpHeaderField(HTTPN ames::Refresh), Document::HttpRefreshFromHeader);
501 } 463 }
502 464
503 void DocumentLoader::commitData(const char* bytes, size_t length) 465 void DocumentLoader::commitData(const char* bytes, size_t length)
504 { 466 {
505 ASSERT(m_state < MainResourceDone); 467 ASSERT(m_state < MainResourceDone);
506 ensureWriter(m_response.mimeType()); 468 ensureWriter(m_response.mimeType());
507 469
508 // This can happen if document.close() is called by an event handler while 470 // This can happen if document.close() is called by an event handler while
509 // there's still pending incoming data. 471 // there's still pending incoming data.
510 if (m_frame && !m_frame->document()->parsing()) { 472 if (m_frame && !m_frame->document()->parsing())
511 cancelMainResourceLoad(ResourceError::cancelledError(m_request.url()));
512 return; 473 return;
513 }
514 474
515 if (length) 475 if (length)
516 m_state = DataReceived; 476 m_state = DataReceived;
517 477
518 m_writer->addData(bytes, length); 478 m_writer->addData(bytes, length);
519 } 479 }
520 480
521 void DocumentLoader::dataReceived(Resource* resource, const char* data, size_t l ength) 481 void DocumentLoader::dataReceived(Resource* resource, const char* data, size_t l ength)
522 { 482 {
523 ASSERT(data); 483 ASSERT(data);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
567 if (isArchiveMIMEType(response().mimeType())) 527 if (isArchiveMIMEType(response().mimeType()))
568 return; 528 return;
569 commitIfReady(); 529 commitIfReady();
570 if (!frameLoader()) 530 if (!frameLoader())
571 return; 531 return;
572 commitData(data, length); 532 commitData(data, length);
573 533
574 // If we are sending data to MediaDocument, we should stop here 534 // If we are sending data to MediaDocument, we should stop here
575 // and cancel the request. 535 // and cancel the request.
576 if (m_frame && m_frame->document()->isMediaDocument()) 536 if (m_frame && m_frame->document()->isMediaDocument())
577 cancelMainResourceLoad(ResourceError::cancelledError(m_request.url())); 537 m_fetcher->stopFetching();
578 } 538 }
579 539
580 void DocumentLoader::clearRedirectChain() 540 void DocumentLoader::clearRedirectChain()
581 { 541 {
582 m_redirectChain.clear(); 542 m_redirectChain.clear();
583 } 543 }
584 544
585 void DocumentLoader::appendRedirect(const KURL& url) 545 void DocumentLoader::appendRedirect(const KURL& url)
586 { 546 {
587 m_redirectChain.append(url); 547 m_redirectChain.append(url);
588 } 548 }
589 549
590 void DocumentLoader::detachFromFrame() 550 void DocumentLoader::detachFromFrame()
591 { 551 {
592 ASSERT(m_frame); 552 ASSERT(m_frame);
593 RefPtrWillBeRawPtr<LocalFrame> protectFrame(m_frame.get()); 553 RefPtrWillBeRawPtr<LocalFrame> protectFrame(m_frame.get());
594 RefPtrWillBeRawPtr<DocumentLoader> protectLoader(this); 554 RefPtrWillBeRawPtr<DocumentLoader> protectLoader(this);
595 555
596 // It never makes sense to have a document loader that is detached from its 556 // It never makes sense to have a document loader that is detached from its
597 // frame have any loads active, so go ahead and kill all the loads. 557 // frame have any loads active, so go ahead and kill all the loads.
598 stopLoading(); 558 m_fetcher->stopFetching();
599 559
600 // If that load cancellation triggered another detach, leave. 560 // If that load cancellation triggered another detach, leave.
601 // (fast/frames/detach-frame-nested-no-crash.html is an example of this.) 561 // (fast/frames/detach-frame-nested-no-crash.html is an example of this.)
602 if (!m_frame) 562 if (!m_frame)
603 return; 563 return;
604 564
605 m_fetcher->clearContext(); 565 m_fetcher->clearContext();
606
607 m_applicationCacheHost->detachFromDocumentLoader(); 566 m_applicationCacheHost->detachFromDocumentLoader();
608 m_applicationCacheHost.clear(); 567 m_applicationCacheHost.clear();
609 WeakIdentifierMap<DocumentLoader>::notifyObjectDestroyed(this); 568 WeakIdentifierMap<DocumentLoader>::notifyObjectDestroyed(this);
610 clearMainResourceHandle(); 569 clearMainResourceHandle();
611 m_frame = nullptr; 570 m_frame = nullptr;
612 } 571 }
613 572
614 void DocumentLoader::clearMainResourceHandle() 573 void DocumentLoader::clearMainResourceHandle()
615 { 574 {
616 if (!m_mainResource) 575 if (!m_mainResource)
(...skipping 10 matching lines...) Expand all
627 586
628 ASSERT(m_mainResource); 587 ASSERT(m_mainResource);
629 ArchiveResource* mainResource = m_fetcher->createArchive(m_mainResource.get( )); 588 ArchiveResource* mainResource = m_fetcher->createArchive(m_mainResource.get( ));
630 if (!mainResource) 589 if (!mainResource)
631 return false; 590 return false;
632 // The origin is the MHTML file, we need to set the base URL to the document encoded in the MHTML so 591 // The origin is the MHTML file, we need to set the base URL to the document encoded in the MHTML so
633 // relative URLs are resolved properly. 592 // relative URLs are resolved properly.
634 ensureWriter(mainResource->mimeType(), mainResource->url()); 593 ensureWriter(mainResource->mimeType(), mainResource->url());
635 594
636 // The Document has now been created. 595 // The Document has now been created.
637 document()->enforceSandboxFlags(SandboxAll); 596 m_frame->document()->enforceSandboxFlags(SandboxAll);
638 597
639 commitData(mainResource->data()->data(), mainResource->data()->size()); 598 commitData(mainResource->data()->data(), mainResource->data()->size());
640 return true; 599 return true;
641 } 600 }
642 601
643 const AtomicString& DocumentLoader::responseMIMEType() const 602 const AtomicString& DocumentLoader::responseMIMEType() const
644 { 603 {
645 return m_response.mimeType(); 604 return m_response.mimeType();
646 } 605 }
647 606
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 // A bunch of headers are set when the underlying ResourceLoader is created, and m_request needs to include those. 657 // A bunch of headers are set when the underlying ResourceLoader is created, and m_request needs to include those.
699 // Even when using a cached resource, we may make some modification to the r equest, e.g. adding the referer header. 658 // Even when using a cached resource, we may make some modification to the r equest, e.g. adding the referer header.
700 m_request = mainResourceLoader() ? m_mainResource->resourceRequest() : fetch Request.resourceRequest(); 659 m_request = mainResourceLoader() ? m_mainResource->resourceRequest() : fetch Request.resourceRequest();
701 // If there was a fragment identifier on m_request, the cache will have stri pped it. m_request should include 660 // If there was a fragment identifier on m_request, the cache will have stri pped it. m_request should include
702 // the fragment identifier, so add that back in. 661 // the fragment identifier, so add that back in.
703 if (equalIgnoringFragmentIdentifier(m_request.url(), fetchRequest.url())) 662 if (equalIgnoringFragmentIdentifier(m_request.url(), fetchRequest.url()))
704 m_request.setURL(fetchRequest.url()); 663 m_request.setURL(fetchRequest.url());
705 m_mainResource->addClient(this); 664 m_mainResource->addClient(this);
706 } 665 }
707 666
708 void DocumentLoader::cancelMainResourceLoad(const ResourceError& resourceError)
709 {
710 RefPtrWillBeRawPtr<DocumentLoader> protect(this);
711 ResourceError error = resourceError.isNull() ? ResourceError::cancelledError (m_request.url()) : resourceError;
712
713 if (mainResourceLoader())
714 mainResourceLoader()->cancel(error);
715
716 mainReceivedError(error);
717 }
718
719 void DocumentLoader::endWriting(DocumentWriter* writer) 667 void DocumentLoader::endWriting(DocumentWriter* writer)
720 { 668 {
721 ASSERT_UNUSED(writer, m_writer == writer); 669 ASSERT_UNUSED(writer, m_writer == writer);
722 m_writer->end(); 670 m_writer->end();
723 m_writer.clear(); 671 m_writer.clear();
724 } 672 }
725 673
726 PassRefPtrWillBeRawPtr<DocumentWriter> DocumentLoader::createWriterFor(const Doc umentInit& init, const AtomicString& mimeType, const AtomicString& encoding, boo l dispatch, ParserSynchronizationPolicy parsingPolicy) 674 PassRefPtrWillBeRawPtr<DocumentWriter> DocumentLoader::createWriterFor(const Doc umentInit& init, const AtomicString& mimeType, const AtomicString& encoding, boo l dispatch, ParserSynchronizationPolicy parsingPolicy)
727 { 675 {
728 LocalFrame* frame = init.frame(); 676 LocalFrame* frame = init.frame();
(...skipping 23 matching lines...) Expand all
752 { 700 {
753 m_writer = createWriterFor(init, mimeType(), m_writer ? m_writer->encoding() : emptyAtom, true, ForceSynchronousParsing); 701 m_writer = createWriterFor(init, mimeType(), m_writer ? m_writer->encoding() : emptyAtom, true, ForceSynchronousParsing);
754 if (!source.isNull()) 702 if (!source.isNull())
755 m_writer->appendReplacingData(source); 703 m_writer->appendReplacingData(source);
756 endWriting(m_writer.get()); 704 endWriting(m_writer.get());
757 } 705 }
758 706
759 DEFINE_WEAK_IDENTIFIER_MAP(DocumentLoader); 707 DEFINE_WEAK_IDENTIFIER_MAP(DocumentLoader);
760 708
761 } // namespace blink 709 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698