This source file includes following definitions.
- IsFormSubmit
- GenerateKeywordFromNavigationEntry
- DidNavigateMainFrame
- OnMessageReceived
- OnPageHasOSDD
- GenerateKeywordIfNecessary
#include "chrome/browser/ui/search_engines/search_engine_tab_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_fetcher.h"
#include "chrome/browser/search_engines/template_url_fetcher_factory.h"
#include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/search_engines/template_url_fetcher_ui_callbacks.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/favicon_status.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/frame_navigate_params.h"
using content::NavigationController;
using content::NavigationEntry;
using content::WebContents;
DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchEngineTabHelper);
namespace {
bool IsFormSubmit(const NavigationEntry* entry) {
return (content::PageTransitionStripQualifier(entry->GetTransitionType()) ==
content::PAGE_TRANSITION_FORM_SUBMIT);
}
base::string16 GenerateKeywordFromNavigationEntry(
const NavigationEntry* entry) {
if (IsFormSubmit(entry))
return base::string16();
GURL url = entry->GetUserTypedURL();
if (!url.is_valid()) {
url = entry->GetURL();
if (!url.is_valid())
return base::string16();
}
if (!url.SchemeIs(content::kHttpScheme) || (url.path().length() > 1))
return base::string16();
return TemplateURLService::GenerateKeyword(url);
}
}
SearchEngineTabHelper::~SearchEngineTabHelper() {
}
void SearchEngineTabHelper::DidNavigateMainFrame(
const content::LoadCommittedDetails& ,
const content::FrameNavigateParams& params) {
GenerateKeywordIfNecessary(params);
}
bool SearchEngineTabHelper::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(SearchEngineTabHelper, message)
IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageHasOSDD, OnPageHasOSDD)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
SearchEngineTabHelper::SearchEngineTabHelper(WebContents* web_contents)
: content::WebContentsObserver(web_contents) {
DCHECK(web_contents);
}
void SearchEngineTabHelper::OnPageHasOSDD(
int32 page_id,
const GURL& doc_url,
const search_provider::OSDDType& msg_provider_type) {
if (!doc_url.is_valid())
return;
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
if (!web_contents()->IsActiveEntry(page_id) ||
!TemplateURLFetcherFactory::GetForProfile(profile) ||
profile->IsOffTheRecord())
return;
TemplateURLFetcher::ProviderType provider_type =
(msg_provider_type == search_provider::AUTODETECTED_PROVIDER) ?
TemplateURLFetcher::AUTODETECTED_PROVIDER :
TemplateURLFetcher::EXPLICIT_PROVIDER;
const NavigationController& controller = web_contents()->GetController();
const NavigationEntry* entry = controller.GetLastCommittedEntry();
for (int index = controller.GetLastCommittedEntryIndex();
(index > 0) && IsFormSubmit(entry);
entry = controller.GetEntryAtIndex(index))
--index;
if (IsFormSubmit(entry))
return;
base::string16 keyword;
if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) {
keyword = GenerateKeywordFromNavigationEntry(entry);
if (keyword.empty())
return;
}
TemplateURLFetcherFactory::GetForProfile(profile)->ScheduleDownload(
keyword, doc_url, entry->GetFavicon().url, web_contents(),
new TemplateURLFetcherUICallbacks(this, web_contents()), provider_type);
}
void SearchEngineTabHelper::GenerateKeywordIfNecessary(
const content::FrameNavigateParams& params) {
if (!params.searchable_form_url.is_valid())
return;
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
if (profile->IsOffTheRecord())
return;
const NavigationController& controller = web_contents()->GetController();
int last_index = controller.GetLastCommittedEntryIndex();
if (last_index <= 0)
return;
base::string16 keyword(GenerateKeywordFromNavigationEntry(
controller.GetEntryAtIndex(last_index - 1)));
if (keyword.empty())
return;
TemplateURLService* url_service =
TemplateURLServiceFactory::GetForProfile(profile);
if (!url_service)
return;
if (!url_service->loaded()) {
url_service->Load();
return;
}
TemplateURL* current_url;
GURL url = params.searchable_form_url;
if (!url_service->CanReplaceKeyword(keyword, url, ¤t_url))
return;
if (current_url) {
if (current_url->originating_url().is_valid()) {
return;
}
url_service->Remove(current_url);
}
TemplateURLData data;
data.short_name = keyword;
data.SetKeyword(keyword);
data.SetURL(url.spec());
DCHECK(controller.GetLastCommittedEntry());
const GURL& current_favicon =
controller.GetLastCommittedEntry()->GetFavicon().url;
data.favicon_url = current_favicon.is_valid() ?
current_favicon : TemplateURL::GenerateFaviconURL(params.referrer.url);
data.safe_for_autoreplace = true;
data.input_encodings.push_back(params.searchable_form_encoding);
url_service->Add(new TemplateURL(profile, data));
}