| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chrome/browser/ui/search/search_tab_helper.h" | 5 #include "chrome/browser/ui/search/search_tab_helper.h" |
| 6 | 6 |
| 7 #include "chrome/browser/google/google_util.h" | 7 #include "chrome/browser/google/google_util.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/search_engines/template_url.h" | 9 #include "chrome/browser/search_engines/template_url.h" |
| 10 #include "chrome/browser/search_engines/template_url_service.h" | 10 #include "chrome/browser/search_engines/template_url_service.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 return template_url->HasSearchTermsReplacementKey(url) && | 51 return template_url->HasSearchTermsReplacementKey(url) && |
| 52 template_url->ExtractSearchTermsFromURL(url, &result) && !result.empty(); | 52 template_url->ExtractSearchTermsFromURL(url, &result) && !result.empty(); |
| 53 } | 53 } |
| 54 | 54 |
| 55 } // namespace | 55 } // namespace |
| 56 | 56 |
| 57 namespace chrome { | 57 namespace chrome { |
| 58 namespace search { | 58 namespace search { |
| 59 | 59 |
| 60 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) | 60 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) |
| 61 : WebContentsObserver(web_contents), | 61 : is_search_enabled_(IsSearchEnabled(ProfileFromWebContents(web_contents))), |
| 62 is_search_enabled_(IsSearchEnabled(ProfileFromWebContents(web_contents))), | |
| 63 user_input_in_progress_(false), | 62 user_input_in_progress_(false), |
| 64 model_(web_contents) { | 63 model_(web_contents) { |
| 65 if (!is_search_enabled_) | 64 if (!is_search_enabled_) |
| 66 return; | 65 return; |
| 67 | 66 |
| 68 registrar_.Add( | 67 registrar_.Add( |
| 69 this, | 68 this, |
| 70 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 69 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 71 content::Source<content::NavigationController>( | 70 content::Source<content::NavigationController>( |
| 72 &web_contents->GetController())); | 71 &web_contents->GetController())); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 86 | 85 |
| 87 UpdateModelBasedOnURL(web_contents()->GetURL()); | 86 UpdateModelBasedOnURL(web_contents()->GetURL()); |
| 88 } | 87 } |
| 89 | 88 |
| 90 void SearchTabHelper::NavigationEntryUpdated() { | 89 void SearchTabHelper::NavigationEntryUpdated() { |
| 91 if (!is_search_enabled_) | 90 if (!is_search_enabled_) |
| 92 return; | 91 return; |
| 93 UpdateModelBasedOnURL(web_contents()->GetURL()); | 92 UpdateModelBasedOnURL(web_contents()->GetURL()); |
| 94 } | 93 } |
| 95 | 94 |
| 96 void SearchTabHelper::NavigateToPendingEntry( | |
| 97 const GURL& url, | |
| 98 content::NavigationController::ReloadType reload_type) { | |
| 99 if (!is_search_enabled_) | |
| 100 return; | |
| 101 | |
| 102 // NTP mode changes are initiated at "pending", all others are initiated | |
| 103 // when "committed". This is because NTP is rendered natively so is faster | |
| 104 // to render than the web contents and we need to coordinate the animations. | |
| 105 if (IsNTP(url)) | |
| 106 UpdateModelBasedOnURL(url); | |
| 107 } | |
| 108 | |
| 109 void SearchTabHelper::Observe( | 95 void SearchTabHelper::Observe( |
| 110 int type, | 96 int type, |
| 111 const content::NotificationSource& source, | 97 const content::NotificationSource& source, |
| 112 const content::NotificationDetails& details) { | 98 const content::NotificationDetails& details) { |
| 113 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); | 99 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); |
| 114 content::LoadCommittedDetails* committed_details = | 100 content::LoadCommittedDetails* committed_details = |
| 115 content::Details<content::LoadCommittedDetails>(details).ptr(); | 101 content::Details<content::LoadCommittedDetails>(details).ptr(); |
| 116 // See comment in |NavigateToPendingEntry()| about why |!IsNTP()| is used. | 102 UpdateModelBasedOnURL(committed_details->entry->GetVirtualURL()); |
| 117 if (!IsNTP(committed_details->entry->GetURL())) { | |
| 118 UpdateModelBasedOnURL(committed_details->entry->GetURL()); | |
| 119 } | |
| 120 } | 103 } |
| 121 | 104 |
| 122 void SearchTabHelper::UpdateModelBasedOnURL(const GURL& url) { | 105 void SearchTabHelper::UpdateModelBasedOnURL(const GURL& url) { |
| 123 Mode::Type type = Mode::MODE_DEFAULT; | 106 Mode::Type type = Mode::MODE_DEFAULT; |
| 124 Mode::Origin origin = Mode::ORIGIN_DEFAULT; | 107 Mode::Origin origin = Mode::ORIGIN_DEFAULT; |
| 125 if (IsNTP(url)) { | 108 if (IsNTP(url)) { |
| 126 type = Mode::MODE_NTP; | 109 type = Mode::MODE_NTP; |
| 127 origin = Mode::ORIGIN_NTP; | 110 origin = Mode::ORIGIN_NTP; |
| 128 } else if (IsSearchResults(url, ProfileFromWebContents(web_contents()))) { | 111 } else if (IsSearchResults(url, ProfileFromWebContents(web_contents()))) { |
| 129 type = Mode::MODE_SEARCH_RESULTS; | 112 type = Mode::MODE_SEARCH_RESULTS; |
| 130 origin = Mode::ORIGIN_SEARCH; | 113 origin = Mode::ORIGIN_SEARCH; |
| 131 } | 114 } |
| 132 if (user_input_in_progress_) | 115 if (user_input_in_progress_) |
| 133 type = Mode::MODE_SEARCH_SUGGESTIONS; | 116 type = Mode::MODE_SEARCH_SUGGESTIONS; |
| 134 model_.SetMode(Mode(type, origin)); | 117 model_.SetMode(Mode(type, origin)); |
| 135 } | 118 } |
| 136 | 119 |
| 137 const content::WebContents* SearchTabHelper::web_contents() const { | 120 const content::WebContents* SearchTabHelper::web_contents() const { |
| 138 return model_.web_contents(); | 121 return model_.web_contents(); |
| 139 } | 122 } |
| 140 | 123 |
| 141 } // namespace search | 124 } // namespace search |
| 142 } // namespace chrome | 125 } // namespace chrome |
| OLD | NEW |