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

Side by Side Diff: content/browser/web_contents/navigation_controller_impl.cc

Issue 11635059: navigation: Retain a screenshot of the page before it unloads (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 12 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 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 "content/browser/web_contents/navigation_controller_impl.h" 5 #include "content/browser/web_contents/navigation_controller_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/string_number_conversions.h" // Temporary 10 #include "base/string_number_conversions.h" // Temporary
(...skipping 10 matching lines...) Expand all
21 #include "content/browser/web_contents/interstitial_page_impl.h" 21 #include "content/browser/web_contents/interstitial_page_impl.h"
22 #include "content/browser/web_contents/navigation_entry_impl.h" 22 #include "content/browser/web_contents/navigation_entry_impl.h"
23 #include "content/browser/web_contents/web_contents_impl.h" 23 #include "content/browser/web_contents/web_contents_impl.h"
24 #include "content/common/view_messages.h" 24 #include "content/common/view_messages.h"
25 #include "content/public/browser/browser_context.h" 25 #include "content/public/browser/browser_context.h"
26 #include "content/public/browser/content_browser_client.h" 26 #include "content/public/browser/content_browser_client.h"
27 #include "content/public/browser/invalidate_type.h" 27 #include "content/public/browser/invalidate_type.h"
28 #include "content/public/browser/navigation_details.h" 28 #include "content/public/browser/navigation_details.h"
29 #include "content/public/browser/notification_service.h" 29 #include "content/public/browser/notification_service.h"
30 #include "content/public/browser/notification_types.h" 30 #include "content/public/browser/notification_types.h"
31 #include "content/public/browser/render_widget_host.h"
32 #include "content/public/browser/render_widget_host_view.h"
31 #include "content/public/browser/storage_partition.h" 33 #include "content/public/browser/storage_partition.h"
32 #include "content/public/browser/user_metrics.h" 34 #include "content/public/browser/user_metrics.h"
33 #include "content/public/browser/web_contents_delegate.h" 35 #include "content/public/browser/web_contents_delegate.h"
34 #include "content/public/common/content_client.h" 36 #include "content/public/common/content_client.h"
35 #include "content/public/common/content_constants.h" 37 #include "content/public/common/content_constants.h"
36 #include "content/public/common/url_constants.h" 38 #include "content/public/common/url_constants.h"
37 #include "net/base/escape.h" 39 #include "net/base/escape.h"
38 #include "net/base/mime_util.h" 40 #include "net/base/mime_util.h"
39 #include "net/base/net_util.h" 41 #include "net/base/net_util.h"
42 #include "skia/ext/platform_canvas.h"
43 #include "ui/gfx/codec/png_codec.h"
40 #include "webkit/glue/glue_serialize.h" 44 #include "webkit/glue/glue_serialize.h"
41 45
42 namespace content { 46 namespace content {
43 namespace { 47 namespace {
44 48
45 const int kInvalidateAll = 0xFFFFFFFF; 49 const int kInvalidateAll = 0xFFFFFFFF;
46 50
47 // Invoked when entries have been pruned, or removed. For example, if the 51 // Invoked when entries have been pruned, or removed. For example, if the
48 // current entries are [google, digg, yahoo], with the current entry google, 52 // current entries are [google, digg, yahoo], with the current entry google,
49 // and the user types in cnet, then digg and yahoo are pruned. 53 // and the user types in cnet, then digg and yahoo are pruned.
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 if (index < 0 || index >= GetEntryCount()) 467 if (index < 0 || index >= GetEntryCount())
464 return NULL; 468 return NULL;
465 469
466 return entries_[index].get(); 470 return entries_[index].get();
467 } 471 }
468 472
469 int NavigationControllerImpl::GetIndexForOffset(int offset) const { 473 int NavigationControllerImpl::GetIndexForOffset(int offset) const {
470 return GetCurrentEntryIndex() + offset; 474 return GetCurrentEntryIndex() + offset;
471 } 475 }
472 476
477 void NavigationControllerImpl::TakeScreenshot() {
478 CHECK(web_contents_);
479
480 NavigationEntryImpl* active_entry =
481 NavigationEntryImpl::FromNavigationEntry(GetActiveEntry());
482 if (!active_entry)
483 return;
484
485 RenderViewHost* render_view_host = web_contents_->GetRenderViewHost();
486 content::RenderWidgetHostView* view = render_view_host->GetView();
487 if (!view)
488 return;
489
490 skia::PlatformBitmap* temp_bitmap = new skia::PlatformBitmap;
491 render_view_host->CopyFromBackingStore(gfx::Rect(),
492 view->GetViewBounds().size(),
493 base::Bind(&NavigationControllerImpl::TakingScreenshotComplete,
494 base::Unretained(this),
495 active_entry,
496 base::Owned(temp_bitmap)),
497 temp_bitmap);
498 }
499
500 void NavigationControllerImpl::TakingScreenshotComplete(
501 NavigationEntryImpl* entry,
502 skia::PlatformBitmap* bitmap,
503 bool success) {
504 int index = GetIndexOfEntry(entry);
505 if (index < 0) {
506 LOG(ERROR) << "Invalid entry";
507 return;
508 }
509
510 if (!success) {
511 LOG(ERROR) << "Taking snapshot was unsuccessful for "
512 << entry->GetURL().spec();
513 return;
514 }
515
516 std::vector<unsigned char> data;
517 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap->GetBitmap(), true, &data))
518 entry->SetScreenshotPNGData(data);
519 }
520
473 bool NavigationControllerImpl::CanGoBack() const { 521 bool NavigationControllerImpl::CanGoBack() const {
474 return entries_.size() > 1 && GetCurrentEntryIndex() > 0; 522 return entries_.size() > 1 && GetCurrentEntryIndex() > 0;
475 } 523 }
476 524
477 bool NavigationControllerImpl::CanGoForward() const { 525 bool NavigationControllerImpl::CanGoForward() const {
478 int index = GetCurrentEntryIndex(); 526 int index = GetCurrentEntryIndex();
479 return index >= 0 && index < (static_cast<int>(entries_.size()) - 1); 527 return index >= 0 && index < (static_cast<int>(entries_.size()) - 1);
480 } 528 }
481 529
482 bool NavigationControllerImpl::CanGoToOffset(int offset) const { 530 bool NavigationControllerImpl::CanGoToOffset(int offset) const {
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 LoadEntry(entry); 731 LoadEntry(entry);
684 } 732 }
685 733
686 void NavigationControllerImpl::DocumentLoadedInFrame() { 734 void NavigationControllerImpl::DocumentLoadedInFrame() {
687 is_initial_navigation_ = false; 735 is_initial_navigation_ = false;
688 } 736 }
689 737
690 bool NavigationControllerImpl::RendererDidNavigate( 738 bool NavigationControllerImpl::RendererDidNavigate(
691 const ViewHostMsg_FrameNavigate_Params& params, 739 const ViewHostMsg_FrameNavigate_Params& params,
692 LoadCommittedDetails* details) { 740 LoadCommittedDetails* details) {
741 if (details->is_main_frame)
742 TakeScreenshot();
693 743
694 // Save the previous state before we clobber it. 744 // Save the previous state before we clobber it.
695 if (GetLastCommittedEntry()) { 745 if (GetLastCommittedEntry()) {
696 details->previous_url = GetLastCommittedEntry()->GetURL(); 746 details->previous_url = GetLastCommittedEntry()->GetURL();
697 details->previous_entry_index = GetLastCommittedEntryIndex(); 747 details->previous_entry_index = GetLastCommittedEntryIndex();
698 } else { 748 } else {
699 details->previous_url = GURL(); 749 details->previous_url = GURL();
700 details->previous_entry_index = -1; 750 details->previous_entry_index = -1;
701 } 751 }
702 752
(...skipping 731 matching lines...) Expand 10 before | Expand all | Expand 10 after
1434 DCHECK_EQ(max_entry_count(), entries_.size()); 1484 DCHECK_EQ(max_entry_count(), entries_.size());
1435 DCHECK(last_committed_entry_index_ > 0); 1485 DCHECK(last_committed_entry_index_ > 0);
1436 RemoveEntryAtIndex(0); 1486 RemoveEntryAtIndex(0);
1437 NotifyPrunedEntries(this, true, 1); 1487 NotifyPrunedEntries(this, true, 1);
1438 } 1488 }
1439 } 1489 }
1440 1490
1441 void NavigationControllerImpl::NavigateToPendingEntry(ReloadType reload_type) { 1491 void NavigationControllerImpl::NavigateToPendingEntry(ReloadType reload_type) {
1442 needs_reload_ = false; 1492 needs_reload_ = false;
1443 1493
1494 TakeScreenshot();
1495
1444 // If we were navigating to a slow-to-commit page, and the user performs 1496 // If we were navigating to a slow-to-commit page, and the user performs
1445 // a session history navigation to the last committed page, RenderViewHost 1497 // a session history navigation to the last committed page, RenderViewHost
1446 // will force the throbber to start, but WebKit will essentially ignore the 1498 // will force the throbber to start, but WebKit will essentially ignore the
1447 // navigation, and won't send a message to stop the throbber. To prevent this 1499 // navigation, and won't send a message to stop the throbber. To prevent this
1448 // from happening, we drop the navigation here and stop the slow-to-commit 1500 // from happening, we drop the navigation here and stop the slow-to-commit
1449 // page from loading (which would normally happen during the navigation). 1501 // page from loading (which would normally happen during the navigation).
1450 if (pending_entry_index_ != -1 && 1502 if (pending_entry_index_ != -1 &&
1451 pending_entry_index_ == last_committed_entry_index_ && 1503 pending_entry_index_ == last_committed_entry_index_ &&
1452 (entries_[pending_entry_index_]->restore_type() == 1504 (entries_[pending_entry_index_]->restore_type() ==
1453 NavigationEntryImpl::RESTORE_NONE) && 1505 NavigationEntryImpl::RESTORE_NONE) &&
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
1609 } 1661 }
1610 } 1662 }
1611 } 1663 }
1612 1664
1613 void NavigationControllerImpl::SetGetTimestampCallbackForTest( 1665 void NavigationControllerImpl::SetGetTimestampCallbackForTest(
1614 const base::Callback<base::Time()>& get_timestamp_callback) { 1666 const base::Callback<base::Time()>& get_timestamp_callback) {
1615 get_timestamp_callback_ = get_timestamp_callback; 1667 get_timestamp_callback_ = get_timestamp_callback;
1616 } 1668 }
1617 1669
1618 } // namespace content 1670 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698