diff options
author | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-15 21:05:12 +0000 |
---|---|---|
committer | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-15 21:05:12 +0000 |
commit | 9219d26f8ef911c5971397de04d01bee7d6369a2 (patch) | |
tree | 93e4d059268366d93a47983e74963396ab588c0f /chrome | |
parent | 6cc251f2ed23cec3a2bb138f633cad6a2266d220 (diff) | |
download | chromium_src-9219d26f8ef911c5971397de04d01bee7d6369a2.zip chromium_src-9219d26f8ef911c5971397de04d01bee7d6369a2.tar.gz chromium_src-9219d26f8ef911c5971397de04d01bee7d6369a2.tar.bz2 |
Move search engine stuff from TabContents to TabContentsWrapper.
BUG=71097
TEST=Hammer on search engines on all platforms. Namely, autocreation of search engines, and pages that have OpenSearch tags. Nothing should crash, break, or have any user-visible change.
Review URL: http://codereview.chromium.org/6515014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@75004 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
21 files changed, 391 insertions, 268 deletions
diff --git a/chrome/browser/renderer_host/render_view_host.cc b/chrome/browser/renderer_host/render_view_host.cc index be4bd2b..e3cc83e 100644 --- a/chrome/browser/renderer_host/render_view_host.cc +++ b/chrome/browser/renderer_host/render_view_host.cc @@ -777,7 +777,6 @@ bool RenderViewHost::OnMessageReceived(const IPC::Message& msg) { IPC_MESSAGE_HANDLER(ViewHostMsg_StartDragging, OnMsgStartDragging) IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateDragCursor, OnUpdateDragCursor) IPC_MESSAGE_HANDLER(ViewHostMsg_TakeFocus, OnTakeFocus) - IPC_MESSAGE_HANDLER(ViewHostMsg_PageHasOSDD, OnMsgPageHasOSDD) IPC_MESSAGE_HANDLER(ViewHostMsg_AddMessageToConsole, OnAddMessageToConsole) IPC_MESSAGE_HANDLER(ViewHostMsg_ForwardToDevToolsAgent, OnForwardToDevToolsAgent) @@ -1286,13 +1285,6 @@ void RenderViewHost::OnTakeFocus(bool reverse) { view->TakeFocus(reverse); } -void RenderViewHost::OnMsgPageHasOSDD( - int32 page_id, - const GURL& doc_url, - const ViewHostMsg_PageHasOSDD_Type& provider_type) { - delegate_->PageHasOSDD(this, page_id, doc_url, provider_type); -} - void RenderViewHost::OnAddMessageToConsole(const std::wstring& message, int32 line_no, const std::wstring& source_id) { diff --git a/chrome/browser/renderer_host/render_view_host.h b/chrome/browser/renderer_host/render_view_host.h index 88ff1df..7ab3bd7 100644 --- a/chrome/browser/renderer_host/render_view_host.h +++ b/chrome/browser/renderer_host/render_view_host.h @@ -43,7 +43,6 @@ struct ThumbnailScore; struct ViewHostMsg_AccessibilityNotification_Params; struct ViewHostMsg_CreateWindow_Params; struct ViewHostMsg_DomMessage_Params; -struct ViewHostMsg_PageHasOSDD_Type; struct ViewHostMsg_ShowPopup_Params; struct ViewMsg_Navigate_Params; struct WebApplicationInfo; @@ -606,9 +605,6 @@ class RenderViewHost : public RenderWidgetHost { const gfx::Point& image_offset); void OnUpdateDragCursor(WebKit::WebDragOperation drag_operation); void OnTakeFocus(bool reverse); - void OnMsgPageHasOSDD(int32 page_id, - const GURL& doc_url, - const ViewHostMsg_PageHasOSDD_Type& provider_type); void OnAddMessageToConsole(const std::wstring& message, int32 line_no, const std::wstring& source_id); diff --git a/chrome/browser/renderer_host/render_view_host_delegate.h b/chrome/browser/renderer_host/render_view_host_delegate.h index 0dddd30..123b2b8c 100644 --- a/chrome/browser/renderer_host/render_view_host_delegate.h +++ b/chrome/browser/renderer_host/render_view_host_delegate.h @@ -49,7 +49,6 @@ struct ThumbnailScore; struct ViewHostMsg_CreateWindow_Params; struct ViewHostMsg_DomMessage_Params; struct ViewHostMsg_FrameNavigate_Params; -struct ViewHostMsg_PageHasOSDD_Type; struct WebApplicationInfo; struct WebDropData; struct WebMenuItem; @@ -523,11 +522,6 @@ class RenderViewHostDelegate : public IPC::Channel::Listener { const std::string& json_arguments, IPC::Message* reply_msg) {} - // Notification that the page has an OpenSearch description document. - virtual void PageHasOSDD(RenderViewHost* render_view_host, - int32 page_id, const GURL& doc_url, - const ViewHostMsg_PageHasOSDD_Type& provider_type) {} - // |url| is assigned to a server that can provide alternate error pages. If // the returned URL is empty, the default error page built into WebKit will // be used. diff --git a/chrome/browser/tab_contents/tab_contents.cc b/chrome/browser/tab_contents/tab_contents.cc index 7ce24b3..264a29d 100644 --- a/chrome/browser/tab_contents/tab_contents.cc +++ b/chrome/browser/tab_contents/tab_contents.cc @@ -64,10 +64,6 @@ #include "chrome/browser/renderer_host/site_instance.h" #include "chrome/browser/renderer_host/web_cache_manager.h" #include "chrome/browser/renderer_preferences_util.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_ui_callbacks.h" -#include "chrome/browser/search_engines/template_url_model.h" #include "chrome/browser/sessions/session_types.h" #include "chrome/browser/tab_contents/infobar_delegate.h" #include "chrome/browser/tab_contents/interstitial_page.h" @@ -196,12 +192,6 @@ const char* kPrefsToObserve[] = { const int kPrefsToObserveLength = arraysize(kPrefsToObserve); -// Returns true if the entry's transition type is FORM_SUBMIT. -bool IsFormSubmit(const NavigationEntry* entry) { - return (PageTransition::StripQualifier(entry->transition_type()) == - PageTransition::FORM_SUBMIT); -} - #if defined(OS_WIN) BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) { @@ -1799,10 +1789,6 @@ void TabContents::DidNavigateMainFramePostCommit( UpdateTargetURL(details.entry->page_id(), GURL()); } - // The keyword generator uses the navigation entries, so must be called after - // the commit. - GenerateKeywordIfNecessary(params); - // Allow the new page to set the title again. received_page_title_ = false; @@ -2019,80 +2005,6 @@ void TabContents::NotifyDisconnected() { NotificationService::NoDetails()); } -void TabContents::GenerateKeywordIfNecessary( - const ViewHostMsg_FrameNavigate_Params& params) { - if (!params.searchable_form_url.is_valid()) - return; - - if (profile()->IsOffTheRecord()) - return; - - int last_index = controller_.last_committed_entry_index(); - // When there was no previous page, the last index will be 0. This is - // normally due to a form submit that opened in a new tab. - // TODO(brettw) bug 916126: we should support keywords when form submits - // happen in new tabs. - if (last_index <= 0) - return; - const NavigationEntry* previous_entry = - controller_.GetEntryAtIndex(last_index - 1); - if (IsFormSubmit(previous_entry)) { - // Only generate a keyword if the previous page wasn't itself a form - // submit. - return; - } - - GURL keyword_url = previous_entry->user_typed_url().is_valid() ? - previous_entry->user_typed_url() : previous_entry->url(); - string16 keyword = - TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected - if (keyword.empty()) - return; - - TemplateURLModel* url_model = profile()->GetTemplateURLModel(); - if (!url_model) - return; - - if (!url_model->loaded()) { - url_model->Load(); - return; - } - - const TemplateURL* current_url; - GURL url = params.searchable_form_url; - if (!url_model->CanReplaceKeyword(keyword, url, ¤t_url)) - return; - - if (current_url) { - if (current_url->originating_url().is_valid()) { - // The existing keyword was generated from an OpenSearch description - // document, don't regenerate. - return; - } - url_model->Remove(current_url); - } - TemplateURL* new_url = new TemplateURL(); - new_url->set_keyword(keyword); - new_url->set_short_name(keyword); - new_url->SetURL(url.spec(), 0, 0); - new_url->add_input_encoding(params.searchable_form_encoding); - DCHECK(controller_.GetLastCommittedEntry()); - const GURL& favicon_url = - controller_.GetLastCommittedEntry()->favicon().url(); - if (favicon_url.is_valid()) { - new_url->SetFavIconURL(favicon_url); - } else { - // The favicon url isn't valid. This means there really isn't a favicon, - // or the favicon url wasn't obtained before the load started. This assumes - // the later. - // TODO(sky): Need a way to set the favicon that doesn't involve generating - // its url. - new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer)); - } - new_url->set_safe_for_autoreplace(true); - url_model->Add(new_url); -} - void TabContents::OnGoToEntryAtOffset(int offset) { if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) { NavigationEntry* entry = controller_.GetEntryAtOffset(offset); @@ -2688,80 +2600,6 @@ void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height, } } -// Checks to see if we should generate a keyword based on the OSDD, and if -// necessary uses TemplateURLFetcher to download the OSDD and create a keyword. -void TabContents::PageHasOSDD( - RenderViewHost* render_view_host, - int32 page_id, - const GURL& url, - const ViewHostMsg_PageHasOSDD_Type& msg_provider_type) { - // Make sure page_id is the current page and other basic checks. - DCHECK(url.is_valid()); - if (!IsActiveEntry(page_id)) - return; - if (!profile()->GetTemplateURLFetcher()) - return; - if (profile()->IsOffTheRecord()) - return; - - TemplateURLFetcher::ProviderType provider_type; - switch (msg_provider_type.type) { - case ViewHostMsg_PageHasOSDD_Type::AUTODETECTED_PROVIDER: - provider_type = TemplateURLFetcher::AUTODETECTED_PROVIDER; - break; - - case ViewHostMsg_PageHasOSDD_Type::EXPLICIT_DEFAULT_PROVIDER: - provider_type = TemplateURLFetcher::EXPLICIT_DEFAULT_PROVIDER; - break; - - case ViewHostMsg_PageHasOSDD_Type::EXPLICIT_PROVIDER: - provider_type = TemplateURLFetcher::EXPLICIT_PROVIDER; - break; - - default: - NOTREACHED(); - return; - } - - const NavigationEntry* entry = controller_.GetLastCommittedEntry(); - DCHECK(entry); - - const NavigationEntry* base_entry = entry; - if (IsFormSubmit(base_entry)) { - // If the current page is a form submit, find the last page that was not - // a form submit and use its url to generate the keyword from. - int index = controller_.last_committed_entry_index() - 1; - while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index))) - index--; - if (index >= 0) - base_entry = controller_.GetEntryAtIndex(index); - else - base_entry = NULL; - } - - // We want to use the user typed URL if available since that represents what - // the user typed to get here, and fall back on the regular URL if not. - if (!base_entry) - return; - GURL keyword_url = base_entry->user_typed_url().is_valid() ? - base_entry->user_typed_url() : base_entry->url(); - if (!keyword_url.is_valid()) - return; - - string16 keyword = TemplateURLModel::GenerateKeyword( - keyword_url, - provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER); - - // Download the OpenSearch description document. If this is successful, a - // new keyword will be created when done. - profile()->GetTemplateURLFetcher()->ScheduleDownload( - keyword, - url, - base_entry->favicon().url(), - new TemplateURLFetcherUICallbacks(this), - provider_type); -} - GURL TabContents::GetAlternateErrorPageURL() const { GURL url; // Disable alternate error pages when in OffTheRecord/Incognito mode. diff --git a/chrome/browser/tab_contents/tab_contents.h b/chrome/browser/tab_contents/tab_contents.h index 36f7343..6da4023 100644 --- a/chrome/browser/tab_contents/tab_contents.h +++ b/chrome/browser/tab_contents/tab_contents.h @@ -883,10 +883,6 @@ class TabContents : public PageNavigator, void NotifyConnected(); void NotifyDisconnected(); - // If params has a searchable form, this tries to create a new keyword. - void GenerateKeywordIfNecessary( - const ViewHostMsg_FrameNavigate_Params& params); - // TabSpecificContentSettings::Delegate implementation. virtual void OnContentSettingsAccessed(bool content_was_blocked); @@ -953,10 +949,6 @@ class TabContents : public PageNavigator, virtual void ShowModalHTMLDialog(const GURL& url, int width, int height, const std::string& json_arguments, IPC::Message* reply_msg); - virtual void PageHasOSDD(RenderViewHost* render_view_host, - int32 page_id, - const GURL& url, - const ViewHostMsg_PageHasOSDD_Type& provider_type); virtual GURL GetAlternateErrorPageURL() const; virtual RendererPreferences GetRendererPrefs(Profile* profile) const; virtual WebPreferences GetWebkitPrefs(); diff --git a/chrome/browser/tab_contents/tab_contents_delegate.cc b/chrome/browser/tab_contents/tab_contents_delegate.cc index 7f3a1a0..7b0dc4b 100644 --- a/chrome/browser/tab_contents/tab_contents_delegate.cc +++ b/chrome/browser/tab_contents/tab_contents_delegate.cc @@ -1,10 +1,9 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "chrome/browser/tab_contents/tab_contents_delegate.h" -#include "chrome/browser/search_engines/template_url.h" #include "chrome/common/url_constants.h" #include "ui/gfx/rect.h" @@ -116,19 +115,6 @@ bool TabContentsDelegate::ExecuteContextMenuCommand(int command) { return false; } -void TabContentsDelegate::ConfirmSetDefaultSearchProvider( - TabContents* tab_contents, - TemplateURL* template_url, - TemplateURLModel* template_url_model) { - delete template_url; -} - -void TabContentsDelegate::ConfirmAddSearchProvider( - const TemplateURL* template_url, - Profile* profile) { - delete template_url; -} - void TabContentsDelegate::ShowPageInfo(Profile* profile, const GURL& url, const NavigationEntry::SSLStatus& ssl, diff --git a/chrome/browser/tab_contents/tab_contents_delegate.h b/chrome/browser/tab_contents/tab_contents_delegate.h index 05fe138..10891a5 100644 --- a/chrome/browser/tab_contents/tab_contents_delegate.h +++ b/chrome/browser/tab_contents/tab_contents_delegate.h @@ -36,8 +36,6 @@ struct NativeWebKeyboardEvent; class Profile; class RenderViewHost; class TabContents; -class TemplateURL; -class TemplateURLModel; struct WebApplicationInfo; // Objects implement this interface to get notified about changes in the @@ -232,18 +230,6 @@ class TabContentsDelegate : public AutomationResourceRoutingDelegate { // Returns true if the context menu command was handled virtual bool ExecuteContextMenuCommand(int command); - // Shows a confirmation dialog box for setting the default search engine - // described by |template_url|. Takes ownership of |template_url|. - virtual void ConfirmSetDefaultSearchProvider( - TabContents* tab_contents, - TemplateURL* template_url, - TemplateURLModel* template_url_model); - - // Shows a confirmation dialog box for adding a search engine described by - // |template_url|. Takes ownership of |template_url|. - virtual void ConfirmAddSearchProvider(const TemplateURL* template_url, - Profile* profile); - // Shows the page info using the specified information. // |url| is the url of the page/frame the info applies to, |ssl| is the SSL // information for that page/frame. If |show_history| is true, a section diff --git a/chrome/browser/ui/browser.cc b/chrome/browser/ui/browser.cc index 26d36c5..76dd372 100644 --- a/chrome/browser/ui/browser.cc +++ b/chrome/browser/ui/browser.cc @@ -83,6 +83,7 @@ #include "chrome/browser/ui/find_bar/find_manager.h" #include "chrome/browser/ui/omnibox/location_bar.h" #include "chrome/browser/ui/options/options_window.h" +#include "chrome/browser/ui/search_engines/search_engine_tab_helper.h" #include "chrome/browser/ui/status_bubble.h" #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" #include "chrome/browser/ui/tabs/dock_info.h" @@ -2662,8 +2663,7 @@ bool Browser::LargeIconsPermitted() const { void Browser::TabInsertedAt(TabContentsWrapper* contents, int index, bool foreground) { - contents->set_delegate(this); - contents->tab_contents()->set_delegate(this); + SetAsDelegate(contents, this); contents->controller().SetWindowID(session_id()); SyncHistoryWithTabs(index); @@ -2687,8 +2687,7 @@ void Browser::TabClosingAt(TabStripModel* tab_strip_model, NotificationService::NoDetails()); // Sever the TabContents' connection back to us. - contents->set_delegate(NULL); - contents->tab_contents()->set_delegate(NULL); + SetAsDelegate(contents, NULL); } void Browser::TabDetachedAt(TabContentsWrapper* contents, int index) { @@ -3167,18 +3166,6 @@ void Browser::OnStartDownload(DownloadItem* download, TabContents* tab) { } } -void Browser::ConfirmSetDefaultSearchProvider( - TabContents* tab_contents, - TemplateURL* template_url, - TemplateURLModel* template_url_model) { - window()->ConfirmSetDefaultSearchProvider(tab_contents, template_url, - template_url_model); -} -void Browser::ConfirmAddSearchProvider(const TemplateURL* template_url, - Profile* profile) { - window()->ConfirmAddSearchProvider(template_url, profile); -} - void Browser::ShowPageInfo(Profile* profile, const GURL& url, const NavigationEntry::SSLStatus& ssl, @@ -3281,6 +3268,19 @@ void Browser::URLStarredChanged(TabContentsWrapper* source, bool starred) { window_->SetStarredState(starred); } +void Browser::ConfirmSetDefaultSearchProvider( + TabContentsWrapper* tab_contents, + TemplateURL* template_url, + TemplateURLModel* template_url_model) { + window()->ConfirmSetDefaultSearchProvider(tab_contents, template_url, + template_url_model); +} + +void Browser::ConfirmAddSearchProvider(const TemplateURL* template_url, + Profile* profile) { + window()->ConfirmAddSearchProvider(template_url, profile); +} + /////////////////////////////////////////////////////////////////////////////// // Browser, SelectFileDialog::Listener implementation: @@ -4158,6 +4158,12 @@ Browser* Browser::GetOrCreateTabbedBrowser(Profile* profile) { return browser; } +void Browser::SetAsDelegate(TabContentsWrapper* tab, Browser* delegate) { + tab->tab_contents()->set_delegate(delegate); + tab->set_delegate(delegate); + tab->search_engine_tab_helper()->set_delegate(delegate); +} + void Browser::FindInPage(bool find_next, bool forward_direction) { ShowFindBar(); if (find_next) { @@ -4191,8 +4197,7 @@ void Browser::TabDetachedAtImpl(TabContentsWrapper* contents, int index, SyncHistoryWithTabs(0); } - contents->set_delegate(NULL); - contents->tab_contents()->set_delegate(NULL); + SetAsDelegate(contents, NULL); RemoveScheduledUpdatesFor(contents->tab_contents()); if (find_bar_controller_.get() && diff --git a/chrome/browser/ui/browser.h b/chrome/browser/ui/browser.h index 8e32d5e..6ca331c 100644 --- a/chrome/browser/ui/browser.h +++ b/chrome/browser/ui/browser.h @@ -29,6 +29,7 @@ #include "chrome/browser/tabs/tab_strip_model_observer.h" // TODO(beng): remove #include "chrome/browser/tab_contents/page_navigator.h" #include "chrome/browser/tab_contents/tab_contents_delegate.h" +#include "chrome/browser/ui/search_engines/search_engine_tab_helper_delegate.h" #include "chrome/browser/ui/shell_dialogs.h" #include "chrome/browser/ui/tab_contents/tab_contents_wrapper_delegate.h" #include "chrome/browser/ui/toolbar/toolbar_model.h" @@ -58,6 +59,7 @@ class Point; class Browser : public TabHandlerDelegate, public TabContentsDelegate, public TabContentsWrapperDelegate, + public SearchEngineTabHelperDelegate, public PageNavigator, public CommandUpdater::CommandUpdaterDelegate, public NotificationObserver, @@ -786,12 +788,6 @@ class Browser : public TabHandlerDelegate, virtual void RenderWidgetShowing(); virtual int GetExtraRenderViewHeight() const; virtual void OnStartDownload(DownloadItem* download, TabContents* tab); - virtual void ConfirmSetDefaultSearchProvider( - TabContents* tab_contents, - TemplateURL* template_url, - TemplateURLModel* template_url_model); - virtual void ConfirmAddSearchProvider(const TemplateURL* template_url, - Profile* profile); virtual void ShowPageInfo(Profile* profile, const GURL& url, const NavigationEntry::SSLStatus& ssl, @@ -815,6 +811,13 @@ class Browser : public TabHandlerDelegate, // Overridden from TabContentsWrapperDelegate: virtual void URLStarredChanged(TabContentsWrapper* source, bool starred) OVERRIDE; + // Overridden from SearchEngineTabHelperDelegate: + virtual void ConfirmSetDefaultSearchProvider( + TabContentsWrapper* tab_contents, + TemplateURL* template_url, + TemplateURLModel* template_url_model) OVERRIDE; + virtual void ConfirmAddSearchProvider(const TemplateURL* template_url, + Profile* profile) OVERRIDE; // Overridden from SelectFileDialog::Listener: virtual void FileSelected(const FilePath& path, int index, void* params); @@ -938,6 +941,10 @@ class Browser : public TabHandlerDelegate, // Assorted utility functions /////////////////////////////////////////////// + // Sets the delegate of all the parts of the |TabContentsWrapper| that + // are needed. + void SetAsDelegate(TabContentsWrapper* tab, Browser* delegate); + // Shows the Find Bar, optionally selecting the next entry that matches the // existing search string for that Tab. |forward_direction| controls the // search direction. diff --git a/chrome/browser/ui/browser_window.h b/chrome/browser/ui/browser_window.h index c297122..1097e81 100644 --- a/chrome/browser/ui/browser_window.h +++ b/chrome/browser/ui/browser_window.h @@ -188,10 +188,11 @@ class BrowserWindow { // Shows a confirmation dialog box for setting the default search engine // described by |template_url|. Takes ownership of |template_url|. virtual void ConfirmSetDefaultSearchProvider( - TabContents* tab_contents, + TabContentsWrapper* tab_contents, TemplateURL* template_url, TemplateURLModel* template_url_model) { // TODO(levin): Implement this for non-Windows platforms and make it pure. + // http://crbug.com/38475 } // Shows a confirmation dialog box for adding a search engine described by diff --git a/chrome/browser/ui/search_engines/search_engine_tab_helper.cc b/chrome/browser/ui/search_engines/search_engine_tab_helper.cc new file mode 100644 index 0000000..db1d0dd --- /dev/null +++ b/chrome/browser/ui/search_engines/search_engine_tab_helper.cc @@ -0,0 +1,202 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#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_model.h" +#include "chrome/browser/ui/search_engines/template_url_fetcher_ui_callbacks.h" +#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" +#include "chrome/common/render_messages.h" +#include "chrome/common/render_messages_params.h" + +namespace { + +// Returns true if the entry's transition type is FORM_SUBMIT. +bool IsFormSubmit(const NavigationEntry* entry) { + return (PageTransition::StripQualifier(entry->transition_type()) == + PageTransition::FORM_SUBMIT); +} + +} // namespace + +SearchEngineTabHelper::SearchEngineTabHelper(TabContentsWrapper* tab_contents) + : tab_contents_(tab_contents) { + DCHECK(tab_contents_); +} + +SearchEngineTabHelper::~SearchEngineTabHelper() { +} + +void SearchEngineTabHelper::DidNavigateMainFramePostCommit( + const NavigationController::LoadCommittedDetails& /*details*/, + const ViewHostMsg_FrameNavigate_Params& params) { + GenerateKeywordIfNecessary(params); +} + +bool SearchEngineTabHelper::OnMessageReceived(const IPC::Message& message) { + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(SearchEngineTabHelper, message) + IPC_MESSAGE_HANDLER(ViewHostMsg_PageHasOSDD, OnPageHasOSDD) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + + return handled; +} + +void SearchEngineTabHelper::OnPageHasOSDD( + int32 page_id, + const GURL& doc_url, + const ViewHostMsg_PageHasOSDD_Type& msg_provider_type) { + // Checks to see if we should generate a keyword based on the OSDD, and if + // necessary uses TemplateURLFetcher to download the OSDD and create a + // keyword. + + // Make sure page_id is the current page and other basic checks. + DCHECK(doc_url.is_valid()); + if (!tab_contents_->tab_contents()->IsActiveEntry(page_id)) + return; + if (!tab_contents_->profile()->GetTemplateURLFetcher()) + return; + if (tab_contents_->profile()->IsOffTheRecord()) + return; + + TemplateURLFetcher::ProviderType provider_type; + switch (msg_provider_type.type) { + case ViewHostMsg_PageHasOSDD_Type::AUTODETECTED_PROVIDER: + provider_type = TemplateURLFetcher::AUTODETECTED_PROVIDER; + break; + + case ViewHostMsg_PageHasOSDD_Type::EXPLICIT_DEFAULT_PROVIDER: + provider_type = TemplateURLFetcher::EXPLICIT_DEFAULT_PROVIDER; + break; + + case ViewHostMsg_PageHasOSDD_Type::EXPLICIT_PROVIDER: + provider_type = TemplateURLFetcher::EXPLICIT_PROVIDER; + break; + + default: + NOTREACHED(); + return; + } + + const NavigationController& controller = + tab_contents_->tab_contents()->controller(); + const NavigationEntry* entry = controller.GetLastCommittedEntry(); + DCHECK(entry); + + const NavigationEntry* base_entry = entry; + if (IsFormSubmit(base_entry)) { + // If the current page is a form submit, find the last page that was not + // a form submit and use its url to generate the keyword from. + int index = controller.last_committed_entry_index() - 1; + while (index >= 0 && IsFormSubmit(controller.GetEntryAtIndex(index))) + index--; + if (index >= 0) + base_entry = controller.GetEntryAtIndex(index); + else + base_entry = NULL; + } + + // We want to use the user typed URL if available since that represents what + // the user typed to get here, and fall back on the regular URL if not. + if (!base_entry) + return; + GURL keyword_url = base_entry->user_typed_url().is_valid() ? + base_entry->user_typed_url() : base_entry->url(); + if (!keyword_url.is_valid()) + return; + + string16 keyword = TemplateURLModel::GenerateKeyword( + keyword_url, + provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER); + + // Download the OpenSearch description document. If this is successful, a + // new keyword will be created when done. + tab_contents_->profile()->GetTemplateURLFetcher()->ScheduleDownload( + keyword, + doc_url, + base_entry->favicon().url(), + new TemplateURLFetcherUICallbacks(this, tab_contents_), + provider_type); +} + +void SearchEngineTabHelper::GenerateKeywordIfNecessary( + const ViewHostMsg_FrameNavigate_Params& params) { + if (!params.searchable_form_url.is_valid()) + return; + + if (tab_contents_->profile()->IsOffTheRecord()) + return; + + const NavigationController& controller = + tab_contents_->tab_contents()->controller(); + int last_index = controller.last_committed_entry_index(); + // When there was no previous page, the last index will be 0. This is + // normally due to a form submit that opened in a new tab. + // TODO(brettw) bug 916126: we should support keywords when form submits + // happen in new tabs. + if (last_index <= 0) + return; + const NavigationEntry* previous_entry = + controller.GetEntryAtIndex(last_index - 1); + if (IsFormSubmit(previous_entry)) { + // Only generate a keyword if the previous page wasn't itself a form + // submit. + return; + } + + GURL keyword_url = previous_entry->user_typed_url().is_valid() ? + previous_entry->user_typed_url() : previous_entry->url(); + string16 keyword = + TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected + if (keyword.empty()) + return; + + TemplateURLModel* url_model = + tab_contents_->profile()->GetTemplateURLModel(); + if (!url_model) + return; + + if (!url_model->loaded()) { + url_model->Load(); + return; + } + + const TemplateURL* current_url; + GURL url = params.searchable_form_url; + if (!url_model->CanReplaceKeyword(keyword, url, ¤t_url)) + return; + + if (current_url) { + if (current_url->originating_url().is_valid()) { + // The existing keyword was generated from an OpenSearch description + // document, don't regenerate. + return; + } + url_model->Remove(current_url); + } + TemplateURL* new_url = new TemplateURL(); + new_url->set_keyword(keyword); + new_url->set_short_name(keyword); + new_url->SetURL(url.spec(), 0, 0); + new_url->add_input_encoding(params.searchable_form_encoding); + DCHECK(controller.GetLastCommittedEntry()); + const GURL& favicon_url = + controller.GetLastCommittedEntry()->favicon().url(); + if (favicon_url.is_valid()) { + new_url->SetFavIconURL(favicon_url); + } else { + // The favicon url isn't valid. This means there really isn't a favicon, + // or the favicon url wasn't obtained before the load started. This assumes + // the later. + // TODO(sky): Need a way to set the favicon that doesn't involve generating + // its url. + new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer)); + } + new_url->set_safe_for_autoreplace(true); + url_model->Add(new_url); +} diff --git a/chrome/browser/ui/search_engines/search_engine_tab_helper.h b/chrome/browser/ui/search_engines/search_engine_tab_helper.h new file mode 100644 index 0000000..bcb1b0e --- /dev/null +++ b/chrome/browser/ui/search_engines/search_engine_tab_helper.h @@ -0,0 +1,51 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_UI_SEARCH_ENGINES_SEARCH_ENGINE_TAB_HELPER_H_ +#define CHROME_BROWSER_UI_SEARCH_ENGINES_SEARCH_ENGINE_TAB_HELPER_H_ +#pragma once + +#include "chrome/browser/tab_contents/tab_contents_observer.h" +#include "chrome/browser/ui/find_bar/find_bar_controller.h" +#include "chrome/browser/ui/find_bar/find_notification_details.h" + +class SearchEngineTabHelperDelegate; +class TabContentsWrapper; +struct ViewHostMsg_PageHasOSDD_Type; + +// Per-tab search engine manager. Handles dealing search engine processing +// functionality. +class SearchEngineTabHelper : public TabContentsObserver { + public: + explicit SearchEngineTabHelper(TabContentsWrapper* tab_contents); + virtual ~SearchEngineTabHelper(); + + SearchEngineTabHelperDelegate* delegate() const { return delegate_; } + void set_delegate(SearchEngineTabHelperDelegate* d) { delegate_ = d; } + + // TabContentsObserver overrides. + virtual void DidNavigateMainFramePostCommit( + const NavigationController::LoadCommittedDetails& details, + const ViewHostMsg_FrameNavigate_Params& params) OVERRIDE; + virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; + + private: + // Handles when a page specifies an OSDD (OpenSearch Description Document). + void OnPageHasOSDD(int32 page_id, + const GURL& doc_url, + const ViewHostMsg_PageHasOSDD_Type& msg_provider_type); + + // If params has a searchable form, this tries to create a new keyword. + void GenerateKeywordIfNecessary( + const ViewHostMsg_FrameNavigate_Params& params); + + TabContentsWrapper* tab_contents_; + + // Delegate for notifying our owner about stuff. Not owned by us. + SearchEngineTabHelperDelegate* delegate_; + + DISALLOW_COPY_AND_ASSIGN(SearchEngineTabHelper); +}; + +#endif // CHROME_BROWSER_UI_SEARCH_ENGINES_SEARCH_ENGINE_TAB_HELPER_H_ diff --git a/chrome/browser/ui/search_engines/search_engine_tab_helper_delegate.cc b/chrome/browser/ui/search_engines/search_engine_tab_helper_delegate.cc new file mode 100644 index 0000000..91b976c --- /dev/null +++ b/chrome/browser/ui/search_engines/search_engine_tab_helper_delegate.cc @@ -0,0 +1,11 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/ui/search_engines/search_engine_tab_helper_delegate.h" + +#include "chrome/browser/search_engines/template_url.h" + +SearchEngineTabHelperDelegate::~SearchEngineTabHelperDelegate() { +} + diff --git a/chrome/browser/ui/search_engines/search_engine_tab_helper_delegate.h b/chrome/browser/ui/search_engines/search_engine_tab_helper_delegate.h new file mode 100644 index 0000000..cc7fa88 --- /dev/null +++ b/chrome/browser/ui/search_engines/search_engine_tab_helper_delegate.h @@ -0,0 +1,34 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_UI_SEARCH_ENGINES_SEARCH_ENGINE_TAB_HELPER_DELEGATE_H_ +#define CHROME_BROWSER_UI_SEARCH_ENGINES_SEARCH_ENGINE_TAB_HELPER_DELEGATE_H_ +#pragma once + +class Profile; +class TabContentsWrapper; +class TemplateURL; +class TemplateURLModel; + +// Objects implement this interface to get notified about changes in the +// SearchEngineTabHelper and to provide necessary functionality. +class SearchEngineTabHelperDelegate { + public: + // Shows a confirmation dialog box for setting the default search engine + // described by |template_url|. Takes ownership of |template_url|. + virtual void ConfirmSetDefaultSearchProvider( + TabContentsWrapper* tab_contents, + TemplateURL* template_url, + TemplateURLModel* template_url_model) = 0; + + // Shows a confirmation dialog box for adding a search engine described by + // |template_url|. Takes ownership of |template_url|. + virtual void ConfirmAddSearchProvider(const TemplateURL* template_url, + Profile* profile) = 0; + + protected: + virtual ~SearchEngineTabHelperDelegate(); +}; + +#endif // CHROME_BROWSER_UI_SEARCH_ENGINES_SEARCH_ENGINE_TAB_HELPER_DELEGATE_H_ diff --git a/chrome/browser/search_engines/template_url_fetcher_ui_callbacks.cc b/chrome/browser/ui/search_engines/template_url_fetcher_ui_callbacks.cc index 02e1f9c..9c37f78 100644 --- a/chrome/browser/search_engines/template_url_fetcher_ui_callbacks.cc +++ b/chrome/browser/ui/search_engines/template_url_fetcher_ui_callbacks.cc @@ -1,23 +1,26 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "chrome/browser/search_engines/template_url_fetcher_ui_callbacks.h" +#include "chrome/browser/ui/search_engines/template_url_fetcher_ui_callbacks.h" #include "base/logging.h" #include "base/scoped_ptr.h" #include "chrome/common/notification_source.h" #include "chrome/common/notification_type.h" #include "chrome/browser/search_engines/template_url.h" -#include "chrome/browser/tab_contents/tab_contents.h" -#include "chrome/browser/tab_contents/tab_contents_delegate.h" +#include "chrome/browser/ui/search_engines/search_engine_tab_helper.h" +#include "chrome/browser/ui/search_engines/search_engine_tab_helper_delegate.h" +#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" TemplateURLFetcherUICallbacks::TemplateURLFetcherUICallbacks( - TabContents* source) - : source_(source) { + SearchEngineTabHelper* tab_helper, + TabContentsWrapper* tab_contents) + : source_(tab_helper), + tab_contents_(tab_contents) { registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, - Source<TabContents>(source_)); + Source<TabContents>(tab_contents_->tab_contents())); } TemplateURLFetcherUICallbacks::~TemplateURLFetcherUICallbacks() { @@ -27,14 +30,15 @@ void TemplateURLFetcherUICallbacks::ConfirmSetDefaultSearchProvider( TemplateURL* template_url, TemplateURLModel* template_url_model) { scoped_ptr<TemplateURL> owned_template_url(template_url); - if (!source_ || !source_->delegate()) + if (!source_ || !source_->delegate() || !tab_contents_) return; source_->delegate()->ConfirmSetDefaultSearchProvider( - source_, + tab_contents_, owned_template_url.release(), template_url_model); } + void TemplateURLFetcherUICallbacks::ConfirmAddSearchProvider( TemplateURL* template_url, Profile* profile) { @@ -51,6 +55,7 @@ void TemplateURLFetcherUICallbacks::Observe( const NotificationSource& source, const NotificationDetails& details) { DCHECK(type == NotificationType::TAB_CONTENTS_DESTROYED); - DCHECK(source == Source<TabContents>(source_)); + DCHECK(source == Source<TabContents>(tab_contents_->tab_contents())); source_ = NULL; + tab_contents_ = NULL; } diff --git a/chrome/browser/search_engines/template_url_fetcher_ui_callbacks.h b/chrome/browser/ui/search_engines/template_url_fetcher_ui_callbacks.h index f480ecb..c1e6d46 100644 --- a/chrome/browser/search_engines/template_url_fetcher_ui_callbacks.h +++ b/chrome/browser/ui/search_engines/template_url_fetcher_ui_callbacks.h @@ -1,9 +1,9 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_UI_CALLBACKS_H_ -#define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_UI_CALLBACKS_H_ +#ifndef CHROME_BROWSER_UI_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_UI_CALLBACKS_H_ +#define CHROME_BROWSER_UI_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_UI_CALLBACKS_H_ #pragma once #include "base/basictypes.h" @@ -11,13 +11,15 @@ #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -class TabContents; +class SearchEngineTabHelper; +class TabContentsWrapper; // Callbacks which display UI for the TemplateURLFetcher. class TemplateURLFetcherUICallbacks : public TemplateURLFetcherCallbacks, public NotificationObserver { public: - explicit TemplateURLFetcherUICallbacks(TabContents* source); + explicit TemplateURLFetcherUICallbacks(SearchEngineTabHelper* tab_helper, + TabContentsWrapper* tab_contents); virtual ~TemplateURLFetcherUICallbacks(); // TemplateURLFetcherCallback implementation. @@ -34,9 +36,12 @@ class TemplateURLFetcherUICallbacks : public TemplateURLFetcherCallbacks, const NotificationDetails& details); private: - // The TabContents where this request originated. Can be NULL if the + // The SearchEngineTabHelper where this request originated. Can be NULL if the // originating tab is closed. If NULL, the engine is not added. - TabContents* source_; + SearchEngineTabHelper* source_; + + // The TabContentsWrapper where this request originated. + TabContentsWrapper* tab_contents_; // Handles registering for our notifications. NotificationRegistrar registrar_; @@ -44,4 +49,4 @@ class TemplateURLFetcherUICallbacks : public TemplateURLFetcherCallbacks, DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcherUICallbacks); }; -#endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_UI_CALLBACKS_H_ +#endif // CHROME_BROWSER_UI_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_UI_CALLBACKS_H_ diff --git a/chrome/browser/ui/tab_contents/tab_contents_wrapper.cc b/chrome/browser/ui/tab_contents/tab_contents_wrapper.cc index dcb50c6..10d0a42 100644 --- a/chrome/browser/ui/tab_contents/tab_contents_wrapper.cc +++ b/chrome/browser/ui/tab_contents/tab_contents_wrapper.cc @@ -11,6 +11,7 @@ #include "chrome/browser/profiles/profile.h" #include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/browser/ui/find_bar/find_manager.h" +#include "chrome/browser/ui/search_engines/search_engine_tab_helper.h" #include "chrome/browser/ui/tab_contents/tab_contents_wrapper_delegate.h" #include "chrome/common/notification_service.h" @@ -32,6 +33,10 @@ TabContentsWrapper::TabContentsWrapper(TabContents* contents) // Needed so that we initialize the password manager on first navigation. tab_contents()->AddObserver(this); + // Create the tab helpers. + search_engine_tab_helper_.reset(new SearchEngineTabHelper(this)); + tab_contents()->AddObserver(search_engine_tab_helper_.get()); + // Register for notifications about URL starredness changing on any profile. registrar_.Add(this, NotificationType::URLS_STARRED, NotificationService::AllSources()); @@ -47,6 +52,9 @@ TabContentsWrapper::~TabContentsWrapper() { tab_contents()->RemoveObserver(this); if (password_manager_.get()) tab_contents()->RemoveObserver(password_manager_.get()); + if (find_manager_.get()) + tab_contents()->RemoveObserver(find_manager_.get()); + tab_contents()->RemoveObserver(search_engine_tab_helper_.get()); } PropertyAccessor<TabContentsWrapper*>* TabContentsWrapper::property_accessor() { diff --git a/chrome/browser/ui/tab_contents/tab_contents_wrapper.h b/chrome/browser/ui/tab_contents/tab_contents_wrapper.h index 742cbe2..1ba379a 100644 --- a/chrome/browser/ui/tab_contents/tab_contents_wrapper.h +++ b/chrome/browser/ui/tab_contents/tab_contents_wrapper.h @@ -18,6 +18,7 @@ class FindManager; class NavigationController; class PasswordManager; class PasswordManagerDelegate; +class SearchEngineTabHelper; class TabContentsWrapperDelegate; // Wraps TabContents and all of its supporting objects in order to control @@ -71,12 +72,14 @@ class TabContentsWrapper : public NotificationObserver, bool is_starred() const { return is_starred_; } - // Returns the PasswordManager, creating it if necessary. PasswordManager* GetPasswordManager(); - // Returns the FindManager, creating it if necessary. FindManager* GetFindManager(); + SearchEngineTabHelper* search_engine_tab_helper() { + return search_engine_tab_helper_.get(); + }; + // Overrides ----------------------------------------------------------------- // TabContentsObserver overrides: @@ -120,6 +123,8 @@ class TabContentsWrapper : public NotificationObserver, // FindManager, lazily created. scoped_ptr<FindManager> find_manager_; + scoped_ptr<SearchEngineTabHelper> search_engine_tab_helper_; + // TabContents (MUST BE LAST) ------------------------------------------------ // The supporting objects need to outlive the TabContents dtor (as they may diff --git a/chrome/browser/ui/views/frame/browser_view.cc b/chrome/browser/ui/views/frame/browser_view.cc index ae55758..eb0b90c 100644 --- a/chrome/browser/ui/views/frame/browser_view.cc +++ b/chrome/browser/ui/views/frame/browser_view.cc @@ -1046,14 +1046,15 @@ void BrowserView::DisableInactiveFrame() { } void BrowserView::ConfirmSetDefaultSearchProvider( - TabContents* tab_contents, + TabContentsWrapper* tab_contents, TemplateURL* template_url, TemplateURLModel* template_url_model) { #if defined(OS_WIN) - DefaultSearchView::Show(tab_contents, template_url, template_url_model); + DefaultSearchView::Show(tab_contents->tab_contents(), template_url, + template_url_model); #else // TODO(levin): Implement for other platforms. Right now this is behind - // a command line flag which is off. + // a command line flag which is off. http://crbug.com/38475 #endif } diff --git a/chrome/browser/ui/views/frame/browser_view.h b/chrome/browser/ui/views/frame/browser_view.h index 1610d80..aa738e4 100644 --- a/chrome/browser/ui/views/frame/browser_view.h +++ b/chrome/browser/ui/views/frame/browser_view.h @@ -280,7 +280,7 @@ class BrowserView : public BrowserBubbleHost, virtual bool IsToolbarVisible() const; virtual void DisableInactiveFrame(); virtual void ConfirmSetDefaultSearchProvider( - TabContents* tab_contents, + TabContentsWrapper* tab_contents, TemplateURL* template_url, TemplateURLModel* template_url_model); virtual void ConfirmAddSearchProvider(const TemplateURL* template_url, diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index d4fe41a6..7400114 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -2095,8 +2095,6 @@ 'browser/search_engines/template_url_fetcher.cc', 'browser/search_engines/template_url_fetcher.h', 'browser/search_engines/template_url_fetcher_callbacks.h', - 'browser/search_engines/template_url_fetcher_ui_callbacks.cc', - 'browser/search_engines/template_url_fetcher_ui_callbacks.h', 'browser/search_engines/template_url_id.h', 'browser/search_engines/template_url_model.cc', 'browser/search_engines/template_url_model.h', @@ -3142,6 +3140,12 @@ 'browser/ui/options/options_window.h', 'browser/ui/options/show_options_url.cc', 'browser/ui/options/show_options_url.h', + 'browser/ui/search_engines/search_engine_tab_helper.cc', + 'browser/ui/search_engines/search_engine_tab_helper.h', + 'browser/ui/search_engines/search_engine_tab_helper_delegate.cc', + 'browser/ui/search_engines/search_engine_tab_helper_delegate.h', + 'browser/ui/search_engines/template_url_fetcher_ui_callbacks.cc', + 'browser/ui/search_engines/template_url_fetcher_ui_callbacks.h', 'browser/ui/shell_dialogs.cc', 'browser/ui/shell_dialogs.h', 'browser/ui/status_bubble.h', |