Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: chrome/browser/ui/toolbar/toolbar_model_impl.cc

Issue 11040055: Adds a FakeToolbarModel for use in testing. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: git try Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 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.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/prefs/pref_service.h" 9 #include "chrome/browser/prefs/pref_service.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/search_engines/template_url.h" 11 #include "chrome/browser/search_engines/template_url.h"
12 #include "chrome/browser/search_engines/template_url_service.h" 12 #include "chrome/browser/search_engines/template_url_service.h"
13 #include "chrome/browser/search_engines/template_url_service_factory.h" 13 #include "chrome/browser/search_engines/template_url_service_factory.h"
14 #include "chrome/browser/ssl/ssl_error_info.h" 14 #include "chrome/browser/ssl/ssl_error_info.h"
15 #include "chrome/browser/ui/search/search.h" 15 #include "chrome/browser/ui/search/search.h"
16 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h" 16 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h"
17 #include "chrome/common/chrome_constants.h" 17 #include "chrome/common/chrome_constants.h"
18 #include "chrome/common/pref_names.h" 18 #include "chrome/common/pref_names.h"
19 #include "chrome/common/url_constants.h" 19 #include "chrome/common/url_constants.h"
20 #include "content/public/browser/cert_store.h" 20 #include "content/public/browser/cert_store.h"
21 #include "content/public/browser/navigation_controller.h" 21 #include "content/public/browser/navigation_controller.h"
22 #include "content/public/browser/navigation_entry.h" 22 #include "content/public/browser/navigation_entry.h"
23 #include "content/public/browser/web_contents.h" 23 #include "content/public/browser/web_contents.h"
24 #include "content/public/browser/web_ui.h" 24 #include "content/public/browser/web_ui.h"
25 #include "content/public/common/content_constants.h" 25 #include "content/public/common/content_constants.h"
26 #include "content/public/common/ssl_status.h" 26 #include "content/public/common/ssl_status.h"
27 #include "grit/generated_resources.h" 27 #include "grit/generated_resources.h"
28 #include "grit/theme_resources.h" 28 #include "grit/theme_resources.h"
29 #include "net/base/cert_status_flags.h" 29 #include "net/base/cert_status_flags.h"
30 #include "net/base/net_util.h" 30 #include "net/base/net_util.h"
31 #include "net/base/x509_certificate.h"
31 #include "ui/base/l10n/l10n_util.h" 32 #include "ui/base/l10n/l10n_util.h"
32 33
33 using content::NavigationController; 34 using content::NavigationController;
34 using content::NavigationEntry; 35 using content::NavigationEntry;
35 using content::SSLStatus; 36 using content::SSLStatus;
36 using content::WebContents; 37 using content::WebContents;
37 38
38 ToolbarModel::ToolbarModel(ToolbarModelDelegate* delegate) 39 ToolbarModelImpl::ToolbarModelImpl(ToolbarModelDelegate* delegate)
39 : delegate_(delegate), 40 : delegate_(delegate),
40 input_in_progress_(false) { 41 input_in_progress_(false) {
41 } 42 }
42 43
43 ToolbarModel::~ToolbarModel() { 44 ToolbarModelImpl::~ToolbarModelImpl() {
44 } 45 }
45 46
46 // ToolbarModel Implementation. 47 // ToolbarModelImpl Implementation.
47 string16 ToolbarModel::GetText(bool display_search_urls_as_search_terms) const { 48 string16 ToolbarModelImpl::GetText(
49 bool display_search_urls_as_search_terms) const {
48 GURL url(GetURL()); 50 GURL url(GetURL());
49 51
50 if (display_search_urls_as_search_terms) { 52 if (display_search_urls_as_search_terms) {
51 string16 search_terms = TryToExtractSearchTermsFromURL(url); 53 string16 search_terms = TryToExtractSearchTermsFromURL(url);
52 if (!search_terms.empty()) 54 if (!search_terms.empty())
53 return search_terms; 55 return search_terms;
54 } 56 }
55 std::string languages; // Empty if we don't have a |navigation_controller|. 57 std::string languages; // Empty if we don't have a |navigation_controller|.
56 Profile* profile = GetProfile(); 58 Profile* profile = GetProfile();
57 if (profile) 59 if (profile)
58 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); 60 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
59 61
60 if (url.spec().length() > content::kMaxURLDisplayChars) 62 if (url.spec().length() > content::kMaxURLDisplayChars)
61 url = url.IsStandard() ? url.GetOrigin() : GURL(url.scheme() + ":"); 63 url = url.IsStandard() ? url.GetOrigin() : GURL(url.scheme() + ":");
62 // Note that we can't unescape spaces here, because if the user copies this 64 // Note that we can't unescape spaces here, because if the user copies this
63 // and pastes it into another program, that program may think the URL ends at 65 // and pastes it into another program, that program may think the URL ends at
64 // the space. 66 // the space.
65 return AutocompleteInput::FormattedStringWithEquivalentMeaning( 67 return AutocompleteInput::FormattedStringWithEquivalentMeaning(
66 url, net::FormatUrl(url, languages, net::kFormatUrlOmitAll, 68 url, net::FormatUrl(url, languages, net::kFormatUrlOmitAll,
67 net::UnescapeRule::NORMAL, NULL, NULL, NULL)); 69 net::UnescapeRule::NORMAL, NULL, NULL, NULL));
68 } 70 }
69 71
70 GURL ToolbarModel::GetURL() const { 72 GURL ToolbarModelImpl::GetURL() const {
71 const NavigationController* navigation_controller = GetNavigationController(); 73 const NavigationController* navigation_controller = GetNavigationController();
72 if (navigation_controller) { 74 if (navigation_controller) {
73 const NavigationEntry* entry = navigation_controller->GetVisibleEntry(); 75 const NavigationEntry* entry = navigation_controller->GetVisibleEntry();
74 if (entry) 76 if (entry)
75 return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL(); 77 return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL();
76 } 78 }
77 79
78 return GURL(chrome::kAboutBlankURL); 80 return GURL(chrome::kAboutBlankURL);
79 } 81 }
80 82
81 bool ToolbarModel::WouldReplaceSearchURLWithSearchTerms() const { 83 bool ToolbarModelImpl::WouldReplaceSearchURLWithSearchTerms() const {
82 return !TryToExtractSearchTermsFromURL(GetURL()).empty(); 84 return !TryToExtractSearchTermsFromURL(GetURL()).empty();
83 } 85 }
84 86
85 bool ToolbarModel::ShouldDisplayURL() const { 87 bool ToolbarModelImpl::ShouldDisplayURL() const {
86 // Note: The order here is important. 88 // Note: The order here is important.
87 // - The WebUI test must come before the extension scheme test because there 89 // - The WebUI test must come before the extension scheme test because there
88 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In 90 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In
89 // that case, we should prefer what the WebUI instance says. 91 // that case, we should prefer what the WebUI instance says.
90 // - The view-source test must come before the WebUI test because of the case 92 // - The view-source test must come before the WebUI test because of the case
91 // of view-source:chrome://newtab, which should display its URL despite what 93 // of view-source:chrome://newtab, which should display its URL despite what
92 // chrome://newtab's WebUI says. 94 // chrome://newtab's WebUI says.
93 NavigationController* controller = GetNavigationController(); 95 NavigationController* controller = GetNavigationController();
94 NavigationEntry* entry = controller ? controller->GetVisibleEntry() : NULL; 96 NavigationEntry* entry = controller ? controller->GetVisibleEntry() : NULL;
95 if (entry) { 97 if (entry) {
(...skipping 11 matching lines...) Expand all
107 return false; 109 return false;
108 110
109 #if defined(OS_CHROMEOS) 111 #if defined(OS_CHROMEOS)
110 if (entry && entry->GetURL().SchemeIs(chrome::kDriveScheme)) 112 if (entry && entry->GetURL().SchemeIs(chrome::kDriveScheme))
111 return false; 113 return false;
112 #endif 114 #endif
113 115
114 return true; 116 return true;
115 } 117 }
116 118
117 ToolbarModel::SecurityLevel ToolbarModel::GetSecurityLevel() const { 119 ToolbarModelImpl::SecurityLevel ToolbarModelImpl::GetSecurityLevel() const {
118 if (input_in_progress_) // When editing, assume no security style. 120 if (input_in_progress_) // When editing, assume no security style.
119 return NONE; 121 return NONE;
120 122
121 NavigationController* navigation_controller = GetNavigationController(); 123 NavigationController* navigation_controller = GetNavigationController();
122 if (!navigation_controller) // We might not have a controller on init. 124 if (!navigation_controller) // We might not have a controller on init.
123 return NONE; 125 return NONE;
124 126
125 NavigationEntry* entry = navigation_controller->GetVisibleEntry(); 127 NavigationEntry* entry = navigation_controller->GetVisibleEntry();
126 if (!entry) 128 if (!entry)
127 return NONE; 129 return NONE;
(...skipping 18 matching lines...) Expand all
146 content::CertStore::GetInstance()->RetrieveCert(ssl.cert_id, NULL)) 148 content::CertStore::GetInstance()->RetrieveCert(ssl.cert_id, NULL))
147 return EV_SECURE; 149 return EV_SECURE;
148 return SECURE; 150 return SECURE;
149 151
150 default: 152 default:
151 NOTREACHED(); 153 NOTREACHED();
152 return NONE; 154 return NONE;
153 } 155 }
154 } 156 }
155 157
156 int ToolbarModel::GetIcon() const { 158 int ToolbarModelImpl::GetIcon() const {
157 static int icon_ids[NUM_SECURITY_LEVELS] = { 159 static int icon_ids[NUM_SECURITY_LEVELS] = {
158 IDR_LOCATION_BAR_HTTP, 160 IDR_LOCATION_BAR_HTTP,
159 IDR_OMNIBOX_HTTPS_VALID, 161 IDR_OMNIBOX_HTTPS_VALID,
160 IDR_OMNIBOX_HTTPS_VALID, 162 IDR_OMNIBOX_HTTPS_VALID,
161 IDR_OMNIBOX_HTTPS_WARNING, 163 IDR_OMNIBOX_HTTPS_WARNING,
162 IDR_OMNIBOX_HTTPS_INVALID, 164 IDR_OMNIBOX_HTTPS_INVALID,
163 }; 165 };
164 DCHECK(arraysize(icon_ids) == NUM_SECURITY_LEVELS); 166 DCHECK(arraysize(icon_ids) == NUM_SECURITY_LEVELS);
165 return icon_ids[GetSecurityLevel()]; 167 return icon_ids[GetSecurityLevel()];
166 } 168 }
167 169
168 string16 ToolbarModel::GetEVCertName() const { 170 string16 ToolbarModelImpl::GetEVCertName() const {
169 DCHECK_EQ(GetSecurityLevel(), EV_SECURE); 171 DCHECK_EQ(GetSecurityLevel(), EV_SECURE);
170 scoped_refptr<net::X509Certificate> cert; 172 scoped_refptr<net::X509Certificate> cert;
171 // Note: Navigation controller and active entry are guaranteed non-NULL or 173 // Note: Navigation controller and active entry are guaranteed non-NULL or
172 // the security level would be NONE. 174 // the security level would be NONE.
173 content::CertStore::GetInstance()->RetrieveCert( 175 content::CertStore::GetInstance()->RetrieveCert(
174 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id, &cert); 176 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id, &cert);
175 return GetEVCertName(*cert); 177 return GetEVCertName(*cert);
176 } 178 }
177 179
178 // static 180 // static
179 string16 ToolbarModel::GetEVCertName(const net::X509Certificate& cert) { 181 string16 ToolbarModelImpl::GetEVCertName(const net::X509Certificate& cert) {
180 // EV are required to have an organization name and country. 182 // EV are required to have an organization name and country.
181 if (cert.subject().organization_names.empty() || 183 if (cert.subject().organization_names.empty() ||
182 cert.subject().country_name.empty()) { 184 cert.subject().country_name.empty()) {
183 NOTREACHED(); 185 NOTREACHED();
184 return string16(); 186 return string16();
185 } 187 }
186 188
187 return l10n_util::GetStringFUTF16( 189 return l10n_util::GetStringFUTF16(
188 IDS_SECURE_CONNECTION_EV, 190 IDS_SECURE_CONNECTION_EV,
189 UTF8ToUTF16(cert.subject().organization_names[0]), 191 UTF8ToUTF16(cert.subject().organization_names[0]),
190 UTF8ToUTF16(cert.subject().country_name)); 192 UTF8ToUTF16(cert.subject().country_name));
191 } 193 }
192 194
193 NavigationController* ToolbarModel::GetNavigationController() const { 195 void ToolbarModelImpl::SetInputInProgress(bool value) {
196 input_in_progress_ = value;
197 }
198
199 bool ToolbarModelImpl::GetInputInProgress() const {
200 return input_in_progress_;
201 }
202
203 NavigationController* ToolbarModelImpl::GetNavigationController() const {
194 // This |current_tab| can be NULL during the initialization of the 204 // This |current_tab| can be NULL during the initialization of the
195 // toolbar during window creation (i.e. before any tabs have been added 205 // toolbar during window creation (i.e. before any tabs have been added
196 // to the window). 206 // to the window).
197 WebContents* current_tab = delegate_->GetActiveWebContents(); 207 WebContents* current_tab = delegate_->GetActiveWebContents();
198 return current_tab ? &current_tab->GetController() : NULL; 208 return current_tab ? &current_tab->GetController() : NULL;
199 } 209 }
200 210
201 string16 ToolbarModel::TryToExtractSearchTermsFromURL(const GURL& url) const { 211 string16 ToolbarModelImpl::TryToExtractSearchTermsFromURL(
212 const GURL& url) const {
202 Profile* profile = GetProfile(); 213 Profile* profile = GetProfile();
203 214
204 // Ensure instant extended API is enabled. 215 // Ensure instant extended API is enabled.
205 if (!profile || !chrome::search::IsInstantExtendedAPIEnabled(profile)) 216 if (!profile || !chrome::search::IsInstantExtendedAPIEnabled(profile))
206 return string16(); 217 return string16();
207 218
208 TemplateURLService* template_url_service = 219 TemplateURLService* template_url_service =
209 TemplateURLServiceFactory::GetForProfile(profile); 220 TemplateURLServiceFactory::GetForProfile(profile);
210 221
211 TemplateURL *template_url = template_url_service->GetDefaultSearchProvider(); 222 TemplateURL *template_url = template_url_service->GetDefaultSearchProvider();
212 if (!template_url) 223 if (!template_url)
213 return string16(); 224 return string16();
214 225
215 string16 result; 226 string16 result;
216 template_url->ExtractSearchTermsFromURL(url, &result); 227 template_url->ExtractSearchTermsFromURL(url, &result);
217 return result; 228 return result;
218 } 229 }
219 230
220 Profile* ToolbarModel::GetProfile() const { 231 Profile* ToolbarModelImpl::GetProfile() const {
221 NavigationController* navigation_controller = GetNavigationController(); 232 NavigationController* navigation_controller = GetNavigationController();
222 return navigation_controller ? 233 return navigation_controller ?
223 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) : 234 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) :
224 NULL; 235 NULL;
225 } 236 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/toolbar/toolbar_model_impl.h ('k') | chrome/browser/ui/views/location_bar/location_bar_view.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698