| 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/toolbar/toolbar_model_impl.h" | 5 #include "chrome/browser/ui/toolbar/toolbar_model_impl.h" |
| 6 | 6 |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/autocomplete/autocomplete_input.h" | 8 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 9 #include "chrome/browser/google/google_util.h" | |
| 10 #include "chrome/browser/prefs/pref_service.h" | 9 #include "chrome/browser/prefs/pref_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/search_engines/template_url.h" | |
| 13 #include "chrome/browser/search_engines/template_url_service.h" | 11 #include "chrome/browser/search_engines/template_url_service.h" |
| 14 #include "chrome/browser/search_engines/template_url_service_factory.h" | 12 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 15 #include "chrome/browser/ssl/ssl_error_info.h" | 13 #include "chrome/browser/ssl/ssl_error_info.h" |
| 16 #include "chrome/browser/ui/search/search.h" | 14 #include "chrome/browser/ui/search/search.h" |
| 17 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h" | 15 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h" |
| 18 #include "chrome/common/chrome_constants.h" | 16 #include "chrome/common/chrome_constants.h" |
| 19 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 20 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
| 21 #include "content/public/browser/cert_store.h" | 19 #include "content/public/browser/cert_store.h" |
| 22 #include "content/public/browser/navigation_controller.h" | 20 #include "content/public/browser/navigation_controller.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 43 input_in_progress_(false) { | 41 input_in_progress_(false) { |
| 44 } | 42 } |
| 45 | 43 |
| 46 ToolbarModelImpl::~ToolbarModelImpl() { | 44 ToolbarModelImpl::~ToolbarModelImpl() { |
| 47 } | 45 } |
| 48 | 46 |
| 49 // ToolbarModelImpl Implementation. | 47 // ToolbarModelImpl Implementation. |
| 50 string16 ToolbarModelImpl::GetText( | 48 string16 ToolbarModelImpl::GetText( |
| 51 bool display_search_urls_as_search_terms) const { | 49 bool display_search_urls_as_search_terms) const { |
| 52 if (display_search_urls_as_search_terms) { | 50 if (display_search_urls_as_search_terms) { |
| 53 string16 search_terms = TryToExtractSearchTermsFromURL(); | 51 const NavigationController* navigation_controller = |
| 52 GetNavigationController(); |
| 53 if (navigation_controller) { |
| 54 NavigationEntry* entry = NULL; |
| 55 entry = navigation_controller->GetActiveEntry(); |
| 56 if (entry && !entry->GetSearchTerms().empty()) { |
| 57 // Entry has existing search terms. |
| 58 return entry->GetSearchTerms(); |
| 59 } |
| 60 } |
| 61 string16 search_terms = TryToExtractSearchTermsFromCurrentURL(); |
| 54 if (!search_terms.empty()) | 62 if (!search_terms.empty()) |
| 55 return search_terms; | 63 return search_terms; |
| 56 } | 64 } |
| 57 std::string languages; // Empty if we don't have a |navigation_controller|. | 65 std::string languages; // Empty if we don't have a |navigation_controller|. |
| 58 Profile* profile = GetProfile(); | 66 Profile* profile = GetProfile(); |
| 59 if (profile) | 67 if (profile) |
| 60 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | 68 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 61 | 69 |
| 62 GURL url(GetURL()); | 70 GURL url(GetURL()); |
| 63 if (url.spec().length() > content::kMaxURLDisplayChars) | 71 if (url.spec().length() > content::kMaxURLDisplayChars) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 75 if (navigation_controller) { | 83 if (navigation_controller) { |
| 76 const NavigationEntry* entry = navigation_controller->GetVisibleEntry(); | 84 const NavigationEntry* entry = navigation_controller->GetVisibleEntry(); |
| 77 if (entry) | 85 if (entry) |
| 78 return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL(); | 86 return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL(); |
| 79 } | 87 } |
| 80 | 88 |
| 81 return GURL(chrome::kAboutBlankURL); | 89 return GURL(chrome::kAboutBlankURL); |
| 82 } | 90 } |
| 83 | 91 |
| 84 bool ToolbarModelImpl::WouldReplaceSearchURLWithSearchTerms() const { | 92 bool ToolbarModelImpl::WouldReplaceSearchURLWithSearchTerms() const { |
| 85 return !TryToExtractSearchTermsFromURL().empty(); | 93 return !TryToExtractSearchTermsFromCurrentURL().empty(); |
| 94 } |
| 95 |
| 96 string16 ToolbarModelImpl::TryToExtractSearchTermsFromCurrentURL() const { |
| 97 const GURL& url = GetURL(); |
| 98 Profile* profile = GetProfile(); |
| 99 if (!profile) |
| 100 return string16(); |
| 101 TemplateURLService* template_url_service = |
| 102 TemplateURLServiceFactory::GetForProfile(profile); |
| 103 return template_url_service->TryToExtractSearchTermsFromURL(url); |
| 86 } | 104 } |
| 87 | 105 |
| 88 bool ToolbarModelImpl::ShouldDisplayURL() const { | 106 bool ToolbarModelImpl::ShouldDisplayURL() const { |
| 89 // Note: The order here is important. | 107 // Note: The order here is important. |
| 90 // - The WebUI test must come before the extension scheme test because there | 108 // - The WebUI test must come before the extension scheme test because there |
| 91 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In | 109 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In |
| 92 // that case, we should prefer what the WebUI instance says. | 110 // that case, we should prefer what the WebUI instance says. |
| 93 // - The view-source test must come before the WebUI test because of the case | 111 // - The view-source test must come before the WebUI test because of the case |
| 94 // of view-source:chrome://newtab, which should display its URL despite what | 112 // of view-source:chrome://newtab, which should display its URL despite what |
| 95 // chrome://newtab's WebUI says. | 113 // chrome://newtab's WebUI says. |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 } | 222 } |
| 205 | 223 |
| 206 NavigationController* ToolbarModelImpl::GetNavigationController() const { | 224 NavigationController* ToolbarModelImpl::GetNavigationController() const { |
| 207 // This |current_tab| can be NULL during the initialization of the | 225 // This |current_tab| can be NULL during the initialization of the |
| 208 // toolbar during window creation (i.e. before any tabs have been added | 226 // toolbar during window creation (i.e. before any tabs have been added |
| 209 // to the window). | 227 // to the window). |
| 210 WebContents* current_tab = delegate_->GetActiveWebContents(); | 228 WebContents* current_tab = delegate_->GetActiveWebContents(); |
| 211 return current_tab ? ¤t_tab->GetController() : NULL; | 229 return current_tab ? ¤t_tab->GetController() : NULL; |
| 212 } | 230 } |
| 213 | 231 |
| 214 string16 ToolbarModelImpl::TryToExtractSearchTermsFromURL() const { | |
| 215 const GURL& url = GetURL(); | |
| 216 Profile* profile = GetProfile(); | |
| 217 | |
| 218 // Ensure instant extended API is enabled and query URL is HTTPS. | |
| 219 if (!profile || !chrome::search::IsInstantExtendedAPIEnabled(profile) || | |
| 220 !url.SchemeIs(chrome::kHttpsScheme) || | |
| 221 !google_util::IsInstantExtendedAPIGoogleSearchUrl(url.spec())) | |
| 222 return string16(); | |
| 223 | |
| 224 TemplateURLService* template_url_service = | |
| 225 TemplateURLServiceFactory::GetForProfile(profile); | |
| 226 | |
| 227 TemplateURL *template_url = template_url_service->GetDefaultSearchProvider(); | |
| 228 if (!template_url) | |
| 229 return string16(); | |
| 230 | |
| 231 string16 result; | |
| 232 template_url->ExtractSearchTermsFromURL(url, &result); | |
| 233 return result; | |
| 234 } | |
| 235 | |
| 236 Profile* ToolbarModelImpl::GetProfile() const { | 232 Profile* ToolbarModelImpl::GetProfile() const { |
| 237 NavigationController* navigation_controller = GetNavigationController(); | 233 NavigationController* navigation_controller = GetNavigationController(); |
| 238 return navigation_controller ? | 234 return navigation_controller ? |
| 239 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) : | 235 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) : |
| 240 NULL; | 236 NULL; |
| 241 } | 237 } |
| OLD | NEW |