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/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/autocomplete/autocomplete_input.h" | 10 #include "chrome/browser/autocomplete/autocomplete_input.h" |
11 #include "chrome/browser/google/google_util.h" | |
12 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/search_engines/template_url.h" | |
14 #include "chrome/browser/search_engines/template_url_service.h" | |
15 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
16 #include "chrome/browser/ssl/ssl_error_info.h" | 12 #include "chrome/browser/ssl/ssl_error_info.h" |
17 #include "chrome/browser/ui/search/search.h" | 13 #include "chrome/browser/ui/search/search.h" |
18 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h" | 14 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h" |
19 #include "chrome/common/chrome_constants.h" | 15 #include "chrome/common/chrome_constants.h" |
20 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
21 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
22 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
23 #include "content/public/browser/cert_store.h" | 19 #include "content/public/browser/cert_store.h" |
24 #include "content/public/browser/navigation_controller.h" | 20 #include "content/public/browser/navigation_controller.h" |
25 #include "content/public/browser/navigation_entry.h" | 21 #include "content/public/browser/navigation_entry.h" |
26 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
27 #include "content/public/browser/web_ui.h" | 23 #include "content/public/browser/web_ui.h" |
28 #include "content/public/common/content_constants.h" | 24 #include "content/public/common/content_constants.h" |
29 #include "content/public/common/ssl_status.h" | 25 #include "content/public/common/ssl_status.h" |
30 #include "extensions/common/constants.h" | 26 #include "extensions/common/constants.h" |
31 #include "grit/generated_resources.h" | 27 #include "grit/generated_resources.h" |
32 #include "grit/theme_resources.h" | 28 #include "grit/theme_resources.h" |
33 #include "net/base/cert_status_flags.h" | 29 #include "net/base/cert_status_flags.h" |
34 #include "net/base/net_util.h" | 30 #include "net/base/net_util.h" |
35 #include "net/base/x509_certificate.h" | 31 #include "net/base/x509_certificate.h" |
36 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
37 | 33 |
38 using content::NavigationController; | 34 using content::NavigationController; |
39 using content::NavigationEntry; | 35 using content::NavigationEntry; |
40 using content::SSLStatus; | 36 using content::SSLStatus; |
41 using content::WebContents; | 37 using content::WebContents; |
42 | 38 |
43 namespace { | |
44 | |
45 // Coerces an instant URL to look like a regular search URL so we can extract | |
46 // query terms from the URL. | |
47 GURL ConvertInstantURLToSearchURL(const GURL& instant_url, | |
48 const TemplateURL& template_url) { | |
49 GURL search_url(template_url.url_ref().ReplaceSearchTerms( | |
50 TemplateURLRef::SearchTermsArgs(string16()))); | |
51 const std::string& scheme = search_url.scheme(); | |
52 const std::string& host = search_url.host(); | |
53 const std::string& port = search_url.port(); | |
54 GURL::Replacements replacements; | |
55 replacements.SetSchemeStr(scheme); | |
56 replacements.SetHostStr(host); | |
57 replacements.SetPortStr(port); | |
58 return instant_url.ReplaceComponents(replacements); | |
59 } | |
60 | |
61 } // namespace | |
62 | |
63 ToolbarModelImpl::ToolbarModelImpl(ToolbarModelDelegate* delegate) | 39 ToolbarModelImpl::ToolbarModelImpl(ToolbarModelDelegate* delegate) |
64 : delegate_(delegate), | 40 : delegate_(delegate), |
65 input_in_progress_(false) { | 41 input_in_progress_(false) { |
66 } | 42 } |
67 | 43 |
68 ToolbarModelImpl::~ToolbarModelImpl() { | 44 ToolbarModelImpl::~ToolbarModelImpl() { |
69 } | 45 } |
70 | 46 |
71 // ToolbarModelImpl Implementation. | 47 // ToolbarModelImpl Implementation. |
72 string16 ToolbarModelImpl::GetText( | 48 string16 ToolbarModelImpl::GetText( |
73 bool display_search_urls_as_search_terms) const { | 49 bool display_search_urls_as_search_terms) const { |
74 if (display_search_urls_as_search_terms) { | 50 if (display_search_urls_as_search_terms) { |
75 string16 search_terms = TryToExtractSearchTermsFromURL(); | 51 string16 search_terms = |
| 52 chrome::search::GetSearchTerms(delegate_->GetActiveWebContents()); |
76 if (!search_terms.empty()) | 53 if (!search_terms.empty()) |
77 return search_terms; | 54 return search_terms; |
78 } | 55 } |
79 std::string languages; // Empty if we don't have a |navigation_controller|. | 56 std::string languages; // Empty if we don't have a |navigation_controller|. |
80 Profile* profile = GetProfile(); | 57 Profile* profile = GetProfile(); |
81 if (profile) | 58 if (profile) |
82 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | 59 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); |
83 | 60 |
84 GURL url(GetURL()); | 61 GURL url(GetURL()); |
85 if (url.spec().length() > content::kMaxURLDisplayChars) | 62 if (url.spec().length() > content::kMaxURLDisplayChars) |
(...skipping 11 matching lines...) Expand all Loading... |
97 if (navigation_controller) { | 74 if (navigation_controller) { |
98 const NavigationEntry* entry = navigation_controller->GetVisibleEntry(); | 75 const NavigationEntry* entry = navigation_controller->GetVisibleEntry(); |
99 if (entry) | 76 if (entry) |
100 return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL(); | 77 return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL(); |
101 } | 78 } |
102 | 79 |
103 return GURL(chrome::kAboutBlankURL); | 80 return GURL(chrome::kAboutBlankURL); |
104 } | 81 } |
105 | 82 |
106 bool ToolbarModelImpl::WouldReplaceSearchURLWithSearchTerms() const { | 83 bool ToolbarModelImpl::WouldReplaceSearchURLWithSearchTerms() const { |
107 return !TryToExtractSearchTermsFromURL().empty(); | 84 const content::WebContents* contents = delegate_->GetActiveWebContents(); |
| 85 return !chrome::search::GetSearchTerms(contents).empty(); |
108 } | 86 } |
109 | 87 |
110 bool ToolbarModelImpl::ShouldDisplayURL() const { | 88 bool ToolbarModelImpl::ShouldDisplayURL() const { |
111 // Note: The order here is important. | 89 // Note: The order here is important. |
112 // - The WebUI test must come before the extension scheme test because there | 90 // - The WebUI test must come before the extension scheme test because there |
113 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In | 91 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In |
114 // that case, we should prefer what the WebUI instance says. | 92 // that case, we should prefer what the WebUI instance says. |
115 // - The view-source test must come before the WebUI test because of the case | 93 // - The view-source test must come before the WebUI test because of the case |
116 // of view-source:chrome://newtab, which should display its URL despite what | 94 // of view-source:chrome://newtab, which should display its URL despite what |
117 // chrome://newtab's WebUI says. | 95 // chrome://newtab's WebUI says. |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 } | 207 } |
230 | 208 |
231 NavigationController* ToolbarModelImpl::GetNavigationController() const { | 209 NavigationController* ToolbarModelImpl::GetNavigationController() const { |
232 // This |current_tab| can be NULL during the initialization of the | 210 // This |current_tab| can be NULL during the initialization of the |
233 // toolbar during window creation (i.e. before any tabs have been added | 211 // toolbar during window creation (i.e. before any tabs have been added |
234 // to the window). | 212 // to the window). |
235 WebContents* current_tab = delegate_->GetActiveWebContents(); | 213 WebContents* current_tab = delegate_->GetActiveWebContents(); |
236 return current_tab ? ¤t_tab->GetController() : NULL; | 214 return current_tab ? ¤t_tab->GetController() : NULL; |
237 } | 215 } |
238 | 216 |
239 string16 ToolbarModelImpl::TryToExtractSearchTermsFromURL() const { | |
240 GURL url = GetURL(); | |
241 Profile* profile = GetProfile(); | |
242 | |
243 // Ensure query extraction is enabled and query URL is HTTPS. | |
244 if (!profile || !chrome::search::IsQueryExtractionEnabled(profile) || | |
245 !url.SchemeIs(chrome::kHttpsScheme)) | |
246 return string16(); | |
247 | |
248 TemplateURLService* template_url_service = | |
249 TemplateURLServiceFactory::GetForProfile(profile); | |
250 | |
251 TemplateURL* template_url = template_url_service->GetDefaultSearchProvider(); | |
252 if (!template_url) | |
253 return string16(); | |
254 | |
255 // Coerce URLs set via --instant-url to look like a regular search URL so we | |
256 // can extract search terms from them. | |
257 if (chrome::search::IsForcedInstantURL(url)) | |
258 url = ConvertInstantURLToSearchURL(url, *template_url); | |
259 | |
260 if (!template_url->HasSearchTermsReplacementKey(url)) | |
261 return string16(); | |
262 | |
263 string16 result; | |
264 template_url->ExtractSearchTermsFromURL(url, &result); | |
265 return result; | |
266 } | |
267 | |
268 Profile* ToolbarModelImpl::GetProfile() const { | 217 Profile* ToolbarModelImpl::GetProfile() const { |
269 NavigationController* navigation_controller = GetNavigationController(); | 218 NavigationController* navigation_controller = GetNavigationController(); |
270 return navigation_controller ? | 219 return navigation_controller ? |
271 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) : | 220 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) : |
272 NULL; | 221 NULL; |
273 } | 222 } |
OLD | NEW |