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

Side by Side Diff: webkit/glue/webframe_impl.cc

Issue 150146: Add Reload and LoadData methods to WebFrame. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « webkit/glue/webframe_impl.h ('k') | webkit/glue/webframe_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com) 2 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
3 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. 3 * Copyright (C) 2006 Apple Computer, 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 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 using WebCore::IntRect; 189 using WebCore::IntRect;
190 using WebCore::KURL; 190 using WebCore::KURL;
191 using WebCore::Node; 191 using WebCore::Node;
192 using WebCore::Range; 192 using WebCore::Range;
193 using WebCore::ReloadIgnoringCacheData; 193 using WebCore::ReloadIgnoringCacheData;
194 using WebCore::RenderObject; 194 using WebCore::RenderObject;
195 using WebCore::ResourceError; 195 using WebCore::ResourceError;
196 using WebCore::ResourceHandle; 196 using WebCore::ResourceHandle;
197 using WebCore::ResourceRequest; 197 using WebCore::ResourceRequest;
198 using WebCore::VisibleSelection; 198 using WebCore::VisibleSelection;
199 using WebCore::ScriptValue;
200 using WebCore::SecurityOrigin;
199 using WebCore::SharedBuffer; 201 using WebCore::SharedBuffer;
200 using WebCore::String; 202 using WebCore::String;
201 using WebCore::SubstituteData; 203 using WebCore::SubstituteData;
202 using WebCore::TextIterator; 204 using WebCore::TextIterator;
203 using WebCore::VisiblePosition; 205 using WebCore::VisiblePosition;
204 using WebCore::XPathResult; 206 using WebCore::XPathResult;
205 207
206 using WebKit::WebCanvas; 208 using WebKit::WebCanvas;
207 using WebKit::WebConsoleMessage; 209 using WebKit::WebConsoleMessage;
210 using WebKit::WebData;
208 using WebKit::WebDataSource; 211 using WebKit::WebDataSource;
209 using WebKit::WebFindOptions; 212 using WebKit::WebFindOptions;
210 using WebKit::WebHistoryItem; 213 using WebKit::WebHistoryItem;
211 using WebKit::WebForm; 214 using WebKit::WebForm;
212 using WebKit::WebRect; 215 using WebKit::WebRect;
213 using WebKit::WebScriptSource; 216 using WebKit::WebScriptSource;
214 using WebKit::WebSize; 217 using WebKit::WebSize;
218 using WebKit::WebString;
215 using WebKit::WebURL; 219 using WebKit::WebURL;
216 using WebKit::WebURLError; 220 using WebKit::WebURLError;
217 using WebKit::WebURLRequest; 221 using WebKit::WebURLRequest;
218 using WebKit::WebURLResponse; 222 using WebKit::WebURLResponse;
219 223
220 // Key for a StatsCounter tracking how many WebFrames are active. 224 // Key for a StatsCounter tracking how many WebFrames are active.
221 static const char* const kWebFrameActiveCount = "WebFrameActiveCount"; 225 static const char* const kWebFrameActiveCount = "WebFrameActiveCount";
222 226
223 static const char* const kOSDType = "application/opensearchdescription+xml"; 227 static const char* const kOSDType = "application/opensearchdescription+xml";
224 static const char* const kOSDRel = "search"; 228 static const char* const kOSDRel = "search";
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 409
406 // Add reference on behalf of FrameLoader. See comments in 410 // Add reference on behalf of FrameLoader. See comments in
407 // WebFrameLoaderClient::frameLoaderDestroyed for more info. 411 // WebFrameLoaderClient::frameLoaderDestroyed for more info.
408 AddRef(); 412 AddRef();
409 413
410 // We must call init() after frame_ is assigned because it is referenced 414 // We must call init() after frame_ is assigned because it is referenced
411 // during init(). 415 // during init().
412 frame_->init(); 416 frame_->init();
413 } 417 }
414 418
419 void WebFrameImpl::Reload() {
420 frame_->loader()->saveDocumentAndScrollState();
421
422 StopLoading(); // Make sure existing activity stops.
423 frame_->loader()->reload();
424 }
425
415 void WebFrameImpl::LoadRequest(const WebURLRequest& request) { 426 void WebFrameImpl::LoadRequest(const WebURLRequest& request) {
416 InternalLoadRequest(request, SubstituteData(), false); 427 const ResourceRequest* resource_request =
428 webkit_glue::WebURLRequestToResourceRequest(&request);
429 DCHECK(resource_request);
430
431 if (resource_request->url().protocolIs("javascript")) {
432 LoadJavaScriptURL(resource_request->url());
433 return;
434 }
435
436 StopLoading(); // Make sure existing activity stops.
437 frame_->loader()->load(*resource_request, false);
417 } 438 }
418 439
419 void WebFrameImpl::LoadHistoryItem(const WebHistoryItem& item) { 440 void WebFrameImpl::LoadHistoryItem(const WebHistoryItem& item) {
420 RefPtr<HistoryItem> history_item = 441 RefPtr<HistoryItem> history_item =
421 webkit_glue::WebHistoryItemToHistoryItem(item); 442 webkit_glue::WebHistoryItemToHistoryItem(item);
422 DCHECK(history_item.get()); 443 DCHECK(history_item.get());
423 444
424 StopLoading(); // make sure existing activity stops 445 StopLoading(); // Make sure existing activity stops.
425 446
426 // If there is no current_item, which happens when we are navigating in 447 // If there is no current_item, which happens when we are navigating in
427 // session history after a crash, we need to manufacture one otherwise WebKit 448 // session history after a crash, we need to manufacture one otherwise WebKit
428 // hoarks. This is probably the wrong thing to do, but it seems to work. 449 // hoarks. This is probably the wrong thing to do, but it seems to work.
429 RefPtr<HistoryItem> current_item = frame_->loader()->currentHistoryItem(); 450 RefPtr<HistoryItem> current_item = frame_->loader()->currentHistoryItem();
430 if (!current_item) { 451 if (!current_item) {
431 current_item = HistoryItem::create(); 452 current_item = HistoryItem::create();
432 current_item->setLastVisitWasFailure(true); 453 current_item->setLastVisitWasFailure(true);
433 frame_->loader()->setCurrentHistoryItem(current_item); 454 frame_->loader()->setCurrentHistoryItem(current_item);
434 GetWebViewImpl()->SetCurrentHistoryItem(current_item.get()); 455 GetWebViewImpl()->SetCurrentHistoryItem(current_item.get());
435 } 456 }
436 457
437 frame_->loader()->goToItem(history_item.get(), 458 frame_->loader()->goToItem(history_item.get(),
438 WebCore::FrameLoadTypeIndexedBackForward); 459 WebCore::FrameLoadTypeIndexedBackForward);
439 } 460 }
440 461
441 void WebFrameImpl::InternalLoadRequest(const WebURLRequest& request, 462 void WebFrameImpl::LoadData(const WebData& data,
442 const SubstituteData& data, 463 const WebString& mime_type,
443 bool replace) { 464 const WebString& text_encoding,
444 const ResourceRequest* resource_request = 465 const WebURL& base_url,
445 webkit_glue::WebURLRequestToResourceRequest(&request); 466 const WebURL& unreachable_url,
446 DCHECK(resource_request); 467 bool replace) {
468 SubstituteData subst_data(
469 webkit_glue::WebDataToSharedBuffer(data),
470 webkit_glue::WebStringToString(mime_type),
471 webkit_glue::WebStringToString(text_encoding),
472 webkit_glue::WebURLToKURL(unreachable_url));
473 DCHECK(subst_data.isValid());
447 474
448 // Special-case javascript URLs. Do not interrupt the existing load when 475 StopLoading(); // Make sure existing activity stops.
449 // asked to load a javascript URL unless the script generates a result. We 476 frame_->loader()->load(ResourceRequest(webkit_glue::WebURLToKURL(base_url)),
450 // can't just use FrameLoader::executeIfJavaScriptURL because it doesn't 477 subst_data, false);
451 // handle redirects properly. 478 if (replace) {
452 const KURL& kurl = resource_request->url(); 479 // Do this to force WebKit to treat the load as replacing the currently
453 if (!data.isValid() && kurl.protocol() == "javascript") { 480 // loaded page.
454 // Don't attempt to reload javascript URLs. 481 frame_->loader()->setReplacing();
455 if (resource_request->cachePolicy() == ReloadIgnoringCacheData)
456 return;
457
458 // We can't load a javascript: URL if there is no Document!
459 if (!frame_->document())
460 return;
461
462 // TODO(darin): Is this the best API to use here? It works and seems
463 // good, but will it change out from under us?
464 String script = decodeURLEscapeSequences(
465 kurl.string().substring(sizeof("javascript:")-1));
466 WebCore::ScriptValue result = frame_->loader()->executeScript(script, true);
467 String scriptResult;
468 if (result.getString(scriptResult) &&
469 !frame_->loader()->isScheduledLocationChangePending()) {
470 // TODO(darin): We need to figure out how to represent this in session
471 // history. Hint: don't re-eval script when the user or script
472 // navigates back-n-forth (instead store the script result somewhere).
473 LoadDocumentData(kurl, scriptResult, String("text/html"), String());
474 }
475 return;
476 }
477
478 StopLoading(); // make sure existing activity stops
479
480 if (data.isValid()) {
481 DCHECK(resource_request);
482 frame_->loader()->load(*resource_request, data, false);
483 if (replace) {
484 // Do this to force WebKit to treat the load as replacing the currently
485 // loaded page.
486 frame_->loader()->setReplacing();
487 }
488 } else if (resource_request->cachePolicy() == ReloadIgnoringCacheData) {
489 frame_->loader()->reload();
490 } else {
491 frame_->loader()->load(*resource_request, false);
492 } 482 }
493 } 483 }
494 484
495 void WebFrameImpl::LoadHTMLString(const std::string& html_text, 485 void WebFrameImpl::LoadHTMLString(const WebData& data,
496 const GURL& base_url) { 486 const WebURL& base_url,
497 LoadAlternateHTMLString(WebURLRequest(base_url), html_text, GURL(), false); 487 const WebURL& unreachable_url,
498 } 488 bool replace) {
499 489 LoadData(data,
500 void WebFrameImpl::LoadAlternateHTMLString(const WebURLRequest& request, 490 WebString::fromUTF8("text/html"),
501 const std::string& html_text, 491 WebString::fromUTF8("UTF-8"),
502 const GURL& display_url, 492 base_url,
503 bool replace) { 493 unreachable_url,
504 int len = static_cast<int>(html_text.size()); 494 replace);
505 RefPtr<SharedBuffer> buf = SharedBuffer::create(html_text.data(), len);
506
507 SubstituteData subst_data(
508 buf, String("text/html"), String("UTF-8"),
509 webkit_glue::GURLToKURL(display_url));
510 DCHECK(subst_data.isValid());
511
512 InternalLoadRequest(request, subst_data, replace);
513 } 495 }
514 496
515 GURL WebFrameImpl::GetURL() const { 497 GURL WebFrameImpl::GetURL() const {
516 const WebDataSource* ds = GetDataSource(); 498 const WebDataSource* ds = GetDataSource();
517 if (!ds) 499 if (!ds)
518 return GURL(); 500 return GURL();
519 return ds->request().url(); 501 return ds->request().url();
520 } 502 }
521 503
522 GURL WebFrameImpl::GetFavIconURL() const { 504 GURL WebFrameImpl::GetFavIconURL() const {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 GetWebViewImpl()->GetPreviousHistoryItem()); 554 GetWebViewImpl()->GetPreviousHistoryItem());
573 } 555 }
574 556
575 WebHistoryItem WebFrameImpl::GetCurrentHistoryItem() const { 557 WebHistoryItem WebFrameImpl::GetCurrentHistoryItem() const {
576 frame_->loader()->saveDocumentAndScrollState(); 558 frame_->loader()->saveDocumentAndScrollState();
577 559
578 return webkit_glue::HistoryItemToWebHistoryItem( 560 return webkit_glue::HistoryItemToWebHistoryItem(
579 frame_->page()->backForwardList()->currentItem()); 561 frame_->page()->backForwardList()->currentItem());
580 } 562 }
581 563
582 void WebFrameImpl::LoadDocumentData(const KURL& base_url,
583 const String& data,
584 const String& mime_type,
585 const String& charset) {
586 // TODO(darin): This is wrong. We need to re-cast this in terms of a call to
587 // one of the FrameLoader::load(...) methods. Else, WebCore will be angry!!
588
589 // Requiring a base_url here seems like a good idea for security reasons.
590 ASSERT(!base_url.isEmpty());
591 ASSERT(!mime_type.isEmpty());
592
593 StopLoading();
594
595 // Reset any pre-existing scroll offset
596 frameview()->setScrollPosition(WebCore::IntPoint());
597
598 // Make sure the correct document type is constructed.
599 frame_->loader()->setResponseMIMEType(mime_type);
600
601 // TODO(darin): Inform the FrameLoader of the charset somehow.
602
603 frame_->loader()->begin(base_url);
604 frame_->loader()->write(data);
605 frame_->loader()->end();
606 }
607
608 static WebDataSource* DataSourceForDocLoader(DocumentLoader* loader) { 564 static WebDataSource* DataSourceForDocLoader(DocumentLoader* loader) {
609 return loader ? WebDataSourceImpl::FromLoader(loader) : NULL; 565 return loader ? WebDataSourceImpl::FromLoader(loader) : NULL;
610 } 566 }
611 567
612 WebDataSource* WebFrameImpl::GetDataSource() const { 568 WebDataSource* WebFrameImpl::GetDataSource() const {
613 return DataSourceForDocLoader(frame_->loader()->documentLoader()); 569 return DataSourceForDocLoader(frame_->loader()->documentLoader());
614 } 570 }
615 571
616 WebDataSourceImpl* WebFrameImpl::GetDataSourceImpl() const { 572 WebDataSourceImpl* WebFrameImpl::GetDataSourceImpl() const {
617 return static_cast<WebDataSourceImpl*>(GetDataSource()); 573 return static_cast<WebDataSourceImpl*>(GetDataSource());
(...skipping 975 matching lines...) Expand 10 before | Expand all | Expand 10 after
1593 delegate->DidFailLoadWithError(web_view, web_error, this); 1549 delegate->DidFailLoadWithError(web_view, web_error, this);
1594 } 1550 }
1595 } 1551 }
1596 } 1552 }
1597 1553
1598 void WebFrameImpl::LoadAlternateHTMLErrorPage(const WebURLRequest& request, 1554 void WebFrameImpl::LoadAlternateHTMLErrorPage(const WebURLRequest& request,
1599 const WebURLError& error, 1555 const WebURLError& error,
1600 const GURL& error_page_url, 1556 const GURL& error_page_url,
1601 bool replace, 1557 bool replace,
1602 const GURL& fake_url) { 1558 const GURL& fake_url) {
1603 // Load alternate HTML in place of the previous request. We create a copy of 1559 // Load alternate HTML in place of the previous request. We use a fake_url
1604 // the original request so we can replace its URL with a dummy URL. That 1560 // for the Base URL. That prevents other web content from the same origin
1605 // prevents other web content from the same origin as the failed URL to 1561 // as the failed URL to script the error page.
1606 // script the error page.
1607 WebURLRequest failed_request(request);
1608 failed_request.setURL(fake_url);
1609 1562
1610 LoadAlternateHTMLString(failed_request, std::string(), 1563 LoadHTMLString("", // Empty document
1611 error.unreachableURL, replace); 1564 fake_url,
1565 error.unreachableURL,
1566 replace);
1612 1567
1613 alt_error_page_fetcher_.reset(new AltErrorPageResourceFetcher( 1568 alt_error_page_fetcher_.reset(new AltErrorPageResourceFetcher(
1614 GetWebViewImpl(), error, this, error_page_url)); 1569 GetWebViewImpl(), error, this, error_page_url));
1615 } 1570 }
1616 1571
1617 void WebFrameImpl::ExecuteScript(const WebScriptSource& source) { 1572 void WebFrameImpl::ExecuteScript(const WebScriptSource& source) {
1618 frame_->loader()->executeScript( 1573 frame_->loader()->executeScript(
1619 WebCore::ScriptSourceCode( 1574 WebCore::ScriptSourceCode(
1620 webkit_glue::WebStringToString(source.code), 1575 webkit_glue::WebStringToString(source.code),
1621 webkit_glue::WebURLToKURL(source.url), 1576 webkit_glue::WebURLToKURL(source.url),
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
1836 return password_listeners_.get(input_element); 1791 return password_listeners_.get(input_element);
1837 } 1792 }
1838 1793
1839 void WebFrameImpl::ClearPasswordListeners() { 1794 void WebFrameImpl::ClearPasswordListeners() {
1840 for (PasswordListenerMap::iterator iter = password_listeners_.begin(); 1795 for (PasswordListenerMap::iterator iter = password_listeners_.begin();
1841 iter != password_listeners_.end(); ++iter) { 1796 iter != password_listeners_.end(); ++iter) {
1842 delete iter->second; 1797 delete iter->second;
1843 } 1798 }
1844 password_listeners_.clear(); 1799 password_listeners_.clear();
1845 } 1800 }
1801
1802 void WebFrameImpl::LoadJavaScriptURL(const KURL& url) {
1803 // This is copied from FrameLoader::executeIfJavaScriptURL. Unfortunately,
1804 // we cannot just use that method since it is private, and it also doesn't
1805 // quite behave as we require it to for bookmarklets. The key difference is
1806 // that we need to suppress loading the string result from evaluating the JS
1807 // URL if executing the JS URL resulted in a location change. We also allow
1808 // a JS URL to be loaded even if scripts on the page are otherwise disabled.
1809
1810 if (!frame_->document() || !frame_->page())
1811 return;
1812
1813 String script =
1814 decodeURLEscapeSequences(url.string().substring(strlen("javascript:")));
1815 ScriptValue result = frame_->loader()->executeScript(script, true);
1816
1817 String script_result;
1818 if (!result.getString(script_result))
1819 return;
1820
1821 SecurityOrigin* security_origin = frame_->document()->securityOrigin();
1822
1823 if (!frame_->loader()->isScheduledLocationChangePending()) {
1824 frame_->loader()->stopAllLoaders();
1825 frame_->loader()->begin(frame_->loader()->url(), true, security_origin);
1826 frame_->loader()->write(script_result);
1827 frame_->loader()->end();
1828 }
1829 }
OLDNEW
« no previous file with comments | « webkit/glue/webframe_impl.h ('k') | webkit/glue/webframe_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698