OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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.h" | 5 #include "chrome/browser/ui/toolbar/toolbar_model.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" |
9 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/ssl/ssl_error_info.h" | 12 #include "chrome/browser/ssl/ssl_error_info.h" |
| 13 #include "chrome/browser/ui/search/search.h" |
12 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h" | 14 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h" |
13 #include "chrome/common/chrome_constants.h" | 15 #include "chrome/common/chrome_constants.h" |
14 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
15 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
16 #include "content/public/browser/cert_store.h" | 18 #include "content/public/browser/cert_store.h" |
17 #include "content/public/browser/navigation_controller.h" | 19 #include "content/public/browser/navigation_controller.h" |
18 #include "content/public/browser/navigation_entry.h" | 20 #include "content/public/browser/navigation_entry.h" |
19 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
20 #include "content/public/browser/web_ui.h" | 22 #include "content/public/browser/web_ui.h" |
21 #include "content/public/common/content_constants.h" | 23 #include "content/public/common/content_constants.h" |
(...skipping 11 matching lines...) Expand all Loading... |
33 | 35 |
34 ToolbarModel::ToolbarModel(ToolbarModelDelegate* delegate) | 36 ToolbarModel::ToolbarModel(ToolbarModelDelegate* delegate) |
35 : delegate_(delegate), | 37 : delegate_(delegate), |
36 input_in_progress_(false) { | 38 input_in_progress_(false) { |
37 } | 39 } |
38 | 40 |
39 ToolbarModel::~ToolbarModel() { | 41 ToolbarModel::~ToolbarModel() { |
40 } | 42 } |
41 | 43 |
42 // ToolbarModel Implementation. | 44 // ToolbarModel Implementation. |
43 string16 ToolbarModel::GetText() const { | 45 string16 ToolbarModel::GetText(bool display_search_urls_as_search_terms) const { |
44 GURL url(chrome::kAboutBlankURL); | 46 GURL url(GetURL()); |
| 47 |
| 48 if (display_search_urls_as_search_terms) { |
| 49 string16 search_terms = TryToExtractSearchTermsFromURL(url); |
| 50 if (!search_terms.empty()) |
| 51 return search_terms; |
| 52 } |
45 std::string languages; // Empty if we don't have a |navigation_controller|. | 53 std::string languages; // Empty if we don't have a |navigation_controller|. |
| 54 Profile* profile = GetProfile(); |
| 55 if (profile) |
| 56 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); |
46 | 57 |
47 NavigationController* navigation_controller = GetNavigationController(); | |
48 if (navigation_controller) { | |
49 Profile* profile = | |
50 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()); | |
51 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | |
52 NavigationEntry* entry = navigation_controller->GetVisibleEntry(); | |
53 if (!ShouldDisplayURL()) { | |
54 url = GURL(); | |
55 } else if (entry) { | |
56 url = entry->GetVirtualURL(); | |
57 } | |
58 } | |
59 if (url.spec().length() > content::kMaxURLDisplayChars) | 58 if (url.spec().length() > content::kMaxURLDisplayChars) |
60 url = url.IsStandard() ? url.GetOrigin() : GURL(url.scheme() + ":"); | 59 url = url.IsStandard() ? url.GetOrigin() : GURL(url.scheme() + ":"); |
61 // Note that we can't unescape spaces here, because if the user copies this | 60 // Note that we can't unescape spaces here, because if the user copies this |
62 // and pastes it into another program, that program may think the URL ends at | 61 // and pastes it into another program, that program may think the URL ends at |
63 // the space. | 62 // the space. |
64 return AutocompleteInput::FormattedStringWithEquivalentMeaning( | 63 return AutocompleteInput::FormattedStringWithEquivalentMeaning( |
65 url, net::FormatUrl(url, languages, net::kFormatUrlOmitAll, | 64 url, net::FormatUrl(url, languages, net::kFormatUrlOmitAll, |
66 net::UnescapeRule::NORMAL, NULL, NULL, NULL)); | 65 net::UnescapeRule::NORMAL, NULL, NULL, NULL)); |
67 } | 66 } |
68 | 67 |
| 68 GURL ToolbarModel::GetURL() const { |
| 69 const NavigationController* navigation_controller = GetNavigationController(); |
| 70 if (navigation_controller) { |
| 71 const NavigationEntry* entry = navigation_controller->GetVisibleEntry(); |
| 72 if (entry) |
| 73 return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL(); |
| 74 } |
| 75 |
| 76 return GURL(chrome::kAboutBlankURL); |
| 77 } |
| 78 |
| 79 bool ToolbarModel::WouldReplaceSearchURLWithSearchTerms() const { |
| 80 return !TryToExtractSearchTermsFromURL(GetURL()).empty(); |
| 81 } |
| 82 |
69 bool ToolbarModel::ShouldDisplayURL() const { | 83 bool ToolbarModel::ShouldDisplayURL() const { |
70 // Note: The order here is important. | 84 // Note: The order here is important. |
71 // - The WebUI test must come before the extension scheme test because there | 85 // - The WebUI test must come before the extension scheme test because there |
72 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In | 86 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In |
73 // that case, we should prefer what the WebUI instance says. | 87 // that case, we should prefer what the WebUI instance says. |
74 // - The view-source test must come before the WebUI test because of the case | 88 // - The view-source test must come before the WebUI test because of the case |
75 // of view-source:chrome://newtab, which should display its URL despite what | 89 // of view-source:chrome://newtab, which should display its URL despite what |
76 // chrome://newtab's WebUI says. | 90 // chrome://newtab's WebUI says. |
77 NavigationController* controller = GetNavigationController(); | 91 NavigationController* controller = GetNavigationController(); |
78 NavigationEntry* entry = controller ? controller->GetVisibleEntry() : NULL; | 92 NavigationEntry* entry = controller ? controller->GetVisibleEntry() : NULL; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 UTF8ToUTF16(cert.subject().country_name)); | 188 UTF8ToUTF16(cert.subject().country_name)); |
175 } | 189 } |
176 | 190 |
177 NavigationController* ToolbarModel::GetNavigationController() const { | 191 NavigationController* ToolbarModel::GetNavigationController() const { |
178 // This |current_tab| can be NULL during the initialization of the | 192 // This |current_tab| can be NULL during the initialization of the |
179 // toolbar during window creation (i.e. before any tabs have been added | 193 // toolbar during window creation (i.e. before any tabs have been added |
180 // to the window). | 194 // to the window). |
181 WebContents* current_tab = delegate_->GetActiveWebContents(); | 195 WebContents* current_tab = delegate_->GetActiveWebContents(); |
182 return current_tab ? ¤t_tab->GetController() : NULL; | 196 return current_tab ? ¤t_tab->GetController() : NULL; |
183 } | 197 } |
| 198 |
| 199 string16 ToolbarModel::TryToExtractSearchTermsFromURL(const GURL& url) const { |
| 200 Profile* profile = GetProfile(); |
| 201 if (profile && |
| 202 chrome::search::IsInstantExtendedAPIEnabled(profile) && |
| 203 google_util::IsInstantExtendedAPIGoogleSearchUrl(url.spec())) { |
| 204 // TODO(dominich): http://crbug.com/135106 - Replace this with whatever the |
| 205 // final solution is as per http://crbug.com/139176. |
| 206 return google_util::GetSearchTermsFromGoogleSearchURL(url.spec()); |
| 207 } |
| 208 return string16(); |
| 209 } |
| 210 |
| 211 Profile* ToolbarModel::GetProfile() const { |
| 212 NavigationController* navigation_controller = GetNavigationController(); |
| 213 return navigation_controller ? |
| 214 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) : |
| 215 NULL; |
| 216 } |
OLD | NEW |