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