This source file includes following definitions.
- received_page_title_
- UpdateHistoryForNavigation
- UpdateHistoryPageTitle
- CreateHistoryAddPageArgs
- DidNavigateMainFrame
- DidNavigateAnyFrame
- TitleWasSet
- GetHistoryService
- WebContentsDestroyed
#include "chrome/browser/history/history_tab_helper.h"
#include <utility>
#include "chrome/browser/history/history_service.h"
#include "chrome/browser/history/history_service_factory.h"
#if !defined(OS_ANDROID)
#include "chrome/browser/network_time/navigation_time_helper.h"
#endif
#include "chrome/browser/prerender/prerender_contents.h"
#include "chrome/browser/prerender/prerender_manager.h"
#include "chrome/browser/prerender/prerender_manager_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/render_messages.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/common/frame_navigate_params.h"
#if !defined(OS_ANDROID)
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#endif
using content::NavigationEntry;
using content::WebContents;
DEFINE_WEB_CONTENTS_USER_DATA_KEY(HistoryTabHelper);
HistoryTabHelper::HistoryTabHelper(WebContents* web_contents)
: content::WebContentsObserver(web_contents),
received_page_title_(false) {
}
HistoryTabHelper::~HistoryTabHelper() {
}
void HistoryTabHelper::UpdateHistoryForNavigation(
const history::HistoryAddPageArgs& add_page_args) {
HistoryService* hs = GetHistoryService();
if (hs)
GetHistoryService()->AddPage(add_page_args);
}
void HistoryTabHelper::UpdateHistoryPageTitle(const NavigationEntry& entry) {
HistoryService* hs = GetHistoryService();
if (hs)
hs->SetPageTitle(entry.GetVirtualURL(),
entry.GetTitleForDisplay(std::string()));
}
history::HistoryAddPageArgs
HistoryTabHelper::CreateHistoryAddPageArgs(
const GURL& virtual_url,
base::Time timestamp,
bool did_replace_entry,
const content::FrameNavigateParams& params) {
history::HistoryAddPageArgs add_page_args(
params.url, timestamp, web_contents(), params.page_id,
params.referrer.url, params.redirects, params.transition,
history::SOURCE_BROWSED, did_replace_entry);
if (content::PageTransitionIsMainFrame(params.transition) &&
virtual_url != params.url) {
add_page_args.url = virtual_url;
if (!add_page_args.redirects.empty())
add_page_args.redirects.back() = virtual_url;
}
return add_page_args;
}
void HistoryTabHelper::DidNavigateMainFrame(
const content::LoadCommittedDetails& details,
const content::FrameNavigateParams& params) {
received_page_title_ = false;
}
void HistoryTabHelper::DidNavigateAnyFrame(
const content::LoadCommittedDetails& details,
const content::FrameNavigateParams& params) {
if (!params.should_update_history)
return;
#if !defined(OS_ANDROID)
base::Time navigation_time =
NavigationTimeHelper::FromWebContents(web_contents())->GetNavigationTime(
details.entry);
#else
base::Time navigation_time = details.entry->GetTimestamp();
#endif
const history::HistoryAddPageArgs& add_page_args =
CreateHistoryAddPageArgs(
web_contents()->GetURL(), navigation_time,
details.did_replace_entry, params);
prerender::PrerenderManager* prerender_manager =
prerender::PrerenderManagerFactory::GetForProfile(
Profile::FromBrowserContext(web_contents()->GetBrowserContext()));
if (prerender_manager) {
prerender::PrerenderContents* prerender_contents =
prerender_manager->GetPrerenderContents(web_contents());
if (prerender_contents) {
prerender_contents->DidNavigate(add_page_args);
return;
}
}
#if !defined(OS_ANDROID)
Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
if (!browser || browser->is_app())
return;
#endif
UpdateHistoryForNavigation(add_page_args);
}
void HistoryTabHelper::TitleWasSet(NavigationEntry* entry, bool explicit_set) {
if (received_page_title_)
return;
if (entry) {
UpdateHistoryPageTitle(*entry);
received_page_title_ = explicit_set;
}
}
HistoryService* HistoryTabHelper::GetHistoryService() {
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
if (profile->IsOffTheRecord())
return NULL;
return HistoryServiceFactory::GetForProfile(profile,
Profile::IMPLICIT_ACCESS);
}
void HistoryTabHelper::WebContentsDestroyed(WebContents* tab) {
Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext());
if (profile->IsOffTheRecord())
return;
HistoryService* hs =
HistoryServiceFactory::GetForProfile(profile, Profile::IMPLICIT_ACCESS);
if (hs) {
NavigationEntry* entry = tab->GetController().GetLastCommittedEntry();
if (entry) {
hs->UpdateWithPageEndTime(tab, entry->GetPageID(), tab->GetURL(),
base::Time::Now());
}
}
}