OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/history/history_tab_helper.h" |
| 6 |
| 7 #include "chrome/browser/history/history.h" |
| 8 #include "chrome/browser/history/top_sites.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/common/render_messages.h" |
| 11 #include "content/browser/tab_contents/navigation_controller.h" |
| 12 #include "content/browser/tab_contents/navigation_entry.h" |
| 13 #include "content/browser/tab_contents/tab_contents.h" |
| 14 #include "content/browser/tab_contents/tab_contents_delegate.h" |
| 15 #include "content/browser/tab_contents/title_updated_details.h" |
| 16 #include "content/common/notification_service.h" |
| 17 #include "content/common/view_messages.h" |
| 18 |
| 19 HistoryTabHelper::HistoryTabHelper(TabContents* tab_contents) |
| 20 : TabContentsObserver(tab_contents), |
| 21 received_page_title_(false) { |
| 22 registrar_.Add(this, NotificationType::TAB_CONTENTS_TITLE_UPDATED, |
| 23 Source<TabContents>(tab_contents)); |
| 24 } |
| 25 |
| 26 HistoryTabHelper::~HistoryTabHelper() { |
| 27 } |
| 28 |
| 29 void HistoryTabHelper::UpdateHistoryForNavigation( |
| 30 scoped_refptr<history::HistoryAddPageArgs> add_page_args) { |
| 31 HistoryService* hs = GetHistoryService(); |
| 32 if (hs) |
| 33 GetHistoryService()->AddPage(*add_page_args); |
| 34 } |
| 35 |
| 36 void HistoryTabHelper::UpdateHistoryPageTitle(const NavigationEntry& entry) { |
| 37 HistoryService* hs = GetHistoryService(); |
| 38 if (hs) |
| 39 hs->SetPageTitle(entry.virtual_url(), entry.title()); |
| 40 } |
| 41 |
| 42 scoped_refptr<history::HistoryAddPageArgs> |
| 43 HistoryTabHelper::CreateHistoryAddPageArgs( |
| 44 const GURL& virtual_url, |
| 45 const NavigationController::LoadCommittedDetails& details, |
| 46 const ViewHostMsg_FrameNavigate_Params& params) { |
| 47 scoped_refptr<history::HistoryAddPageArgs> add_page_args( |
| 48 new history::HistoryAddPageArgs( |
| 49 params.url, base::Time::Now(), tab_contents(), params.page_id, |
| 50 params.referrer, params.redirects, params.transition, |
| 51 history::SOURCE_BROWSED, details.did_replace_entry)); |
| 52 if (PageTransition::IsMainFrame(params.transition) && |
| 53 virtual_url != params.url) { |
| 54 // Hack on the "virtual" URL so that it will appear in history. For some |
| 55 // types of URLs, we will display a magic URL that is different from where |
| 56 // the page is actually navigated. We want the user to see in history what |
| 57 // they saw in the URL bar, so we add the virtual URL as a redirect. This |
| 58 // only applies to the main frame, as the virtual URL doesn't apply to |
| 59 // sub-frames. |
| 60 add_page_args->url = virtual_url; |
| 61 if (!add_page_args->redirects.empty()) |
| 62 add_page_args->redirects.back() = virtual_url; |
| 63 } |
| 64 return add_page_args; |
| 65 } |
| 66 |
| 67 bool HistoryTabHelper::OnMessageReceived(const IPC::Message& message) { |
| 68 bool handled = true; |
| 69 IPC_BEGIN_MESSAGE_MAP(HistoryTabHelper, message) |
| 70 IPC_MESSAGE_HANDLER(ViewHostMsg_PageContents, OnPageContents) |
| 71 IPC_MESSAGE_HANDLER(ViewHostMsg_Thumbnail, OnThumbnail) |
| 72 IPC_MESSAGE_UNHANDLED(handled = false) |
| 73 IPC_END_MESSAGE_MAP() |
| 74 |
| 75 return handled; |
| 76 } |
| 77 |
| 78 void HistoryTabHelper::DidNavigateMainFramePostCommit( |
| 79 const NavigationController::LoadCommittedDetails& details, |
| 80 const ViewHostMsg_FrameNavigate_Params& params) { |
| 81 // Allow the new page to set the title again. |
| 82 received_page_title_ = false; |
| 83 } |
| 84 |
| 85 void HistoryTabHelper::DidNavigateAnyFramePostCommit( |
| 86 const NavigationController::LoadCommittedDetails& details, |
| 87 const ViewHostMsg_FrameNavigate_Params& params) { |
| 88 // Update history. Note that this needs to happen after the entry is complete, |
| 89 // which WillNavigate[Main,Sub]Frame will do before this function is called. |
| 90 if (!params.should_update_history) |
| 91 return; |
| 92 |
| 93 // Most of the time, the displayURL matches the loaded URL, but for about: |
| 94 // URLs, we use a data: URL as the real value. We actually want to save the |
| 95 // about: URL to the history db and keep the data: URL hidden. This is what |
| 96 // the TabContents' URL getter does. |
| 97 scoped_refptr<history::HistoryAddPageArgs> add_page_args( |
| 98 CreateHistoryAddPageArgs(tab_contents()->GetURL(), details, params)); |
| 99 if (!tab_contents()->delegate() || |
| 100 !tab_contents()->delegate()->ShouldAddNavigationToHistory( |
| 101 *add_page_args, details.type)) |
| 102 return; |
| 103 |
| 104 UpdateHistoryForNavigation(add_page_args); |
| 105 } |
| 106 |
| 107 void HistoryTabHelper::Observe(NotificationType type, |
| 108 const NotificationSource& source, |
| 109 const NotificationDetails& details) { |
| 110 DCHECK(type.value == NotificationType::TAB_CONTENTS_TITLE_UPDATED); |
| 111 TitleUpdatedDetails* title = Details<TitleUpdatedDetails>(details).ptr(); |
| 112 |
| 113 if (received_page_title_) |
| 114 return; |
| 115 |
| 116 UpdateHistoryPageTitle(*title->entry()); |
| 117 |
| 118 received_page_title_ = title->explicit_set(); |
| 119 } |
| 120 |
| 121 void HistoryTabHelper::OnPageContents(const GURL& url, |
| 122 int32 page_id, |
| 123 const string16& contents) { |
| 124 // Don't index any https pages. People generally don't want their bank |
| 125 // accounts, etc. indexed on their computer, especially since some of these |
| 126 // things are not marked cachable. |
| 127 // TODO(brettw) we may want to consider more elaborate heuristics such as |
| 128 // the cachability of the page. We may also want to consider subframes (this |
| 129 // test will still index subframes if the subframe is SSL). |
| 130 // TODO(zelidrag) bug chromium-os:2808 - figure out if we want to reenable |
| 131 // content indexing for chromeos in some future releases. |
| 132 #if !defined(OS_CHROMEOS) |
| 133 if (!url.SchemeIsSecure()) { |
| 134 HistoryService* hs = GetHistoryService(); |
| 135 if (hs) |
| 136 hs->SetPageContents(url, contents); |
| 137 } |
| 138 #endif |
| 139 } |
| 140 |
| 141 void HistoryTabHelper::OnThumbnail(const GURL& url, |
| 142 const ThumbnailScore& score, |
| 143 const SkBitmap& bitmap) { |
| 144 if (tab_contents()->profile()->IsOffTheRecord()) |
| 145 return; |
| 146 |
| 147 // Tell History about this thumbnail |
| 148 history::TopSites* ts = tab_contents()->profile()->GetTopSites(); |
| 149 if (ts) |
| 150 ts->SetPageThumbnail(url, bitmap, score); |
| 151 } |
| 152 |
| 153 HistoryService* HistoryTabHelper::GetHistoryService() { |
| 154 if (tab_contents()->profile()->IsOffTheRecord()) |
| 155 return NULL; |
| 156 |
| 157 return tab_contents()->profile()->GetHistoryService(Profile::IMPLICIT_ACCESS); |
| 158 } |
OLD | NEW |