| OLD | NEW |
| 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" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 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 "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
| 32 | 32 |
| 33 using content::NavigationController; | 33 using content::NavigationController; |
| 34 using content::NavigationEntry; | 34 using content::NavigationEntry; |
| 35 using content::SSLStatus; | 35 using content::SSLStatus; |
| 36 using content::WebContents; | 36 using content::WebContents; |
| 37 | 37 |
| 38 ToolbarModel::ToolbarModel(ToolbarModelDelegate* delegate) | 38 ToolbarModelImpl::ToolbarModelImpl(ToolbarModelDelegate* delegate) |
| 39 : delegate_(delegate), | 39 : delegate_(delegate), |
| 40 input_in_progress_(false) { | 40 input_in_progress_(false) { |
| 41 } | 41 } |
| 42 | 42 |
| 43 ToolbarModel::~ToolbarModel() { | 43 ToolbarModelImpl::~ToolbarModelImpl() { |
| 44 } | 44 } |
| 45 | 45 |
| 46 // ToolbarModel Implementation. | 46 // ToolbarModelImpl Implementation. |
| 47 string16 ToolbarModel::GetText(bool display_search_urls_as_search_terms) const { | 47 string16 ToolbarModelImpl::GetText( |
| 48 bool display_search_urls_as_search_terms) const { |
| 48 GURL url(GetURL()); | 49 GURL url(GetURL()); |
| 49 | 50 |
| 50 if (display_search_urls_as_search_terms) { | 51 if (display_search_urls_as_search_terms) { |
| 51 string16 search_terms = TryToExtractSearchTermsFromURL(url); | 52 string16 search_terms = TryToExtractSearchTermsFromURL(url); |
| 52 if (!search_terms.empty()) | 53 if (!search_terms.empty()) |
| 53 return search_terms; | 54 return search_terms; |
| 54 } | 55 } |
| 55 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|. |
| 56 Profile* profile = GetProfile(); | 57 Profile* profile = GetProfile(); |
| 57 if (profile) | 58 if (profile) |
| 58 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | 59 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 59 | 60 |
| 60 if (url.spec().length() > content::kMaxURLDisplayChars) | 61 if (url.spec().length() > content::kMaxURLDisplayChars) |
| 61 url = url.IsStandard() ? url.GetOrigin() : GURL(url.scheme() + ":"); | 62 url = url.IsStandard() ? url.GetOrigin() : GURL(url.scheme() + ":"); |
| 62 // Note that we can't unescape spaces here, because if the user copies this | 63 // 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 | 64 // and pastes it into another program, that program may think the URL ends at |
| 64 // the space. | 65 // the space. |
| 65 return AutocompleteInput::FormattedStringWithEquivalentMeaning( | 66 return AutocompleteInput::FormattedStringWithEquivalentMeaning( |
| 66 url, net::FormatUrl(url, languages, net::kFormatUrlOmitAll, | 67 url, net::FormatUrl(url, languages, net::kFormatUrlOmitAll, |
| 67 net::UnescapeRule::NORMAL, NULL, NULL, NULL)); | 68 net::UnescapeRule::NORMAL, NULL, NULL, NULL)); |
| 68 } | 69 } |
| 69 | 70 |
| 70 GURL ToolbarModel::GetURL() const { | 71 GURL ToolbarModelImpl::GetURL() const { |
| 71 const NavigationController* navigation_controller = GetNavigationController(); | 72 const NavigationController* navigation_controller = GetNavigationController(); |
| 72 if (navigation_controller) { | 73 if (navigation_controller) { |
| 73 const NavigationEntry* entry = navigation_controller->GetVisibleEntry(); | 74 const NavigationEntry* entry = navigation_controller->GetVisibleEntry(); |
| 74 if (entry) | 75 if (entry) |
| 75 return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL(); | 76 return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL(); |
| 76 } | 77 } |
| 77 | 78 |
| 78 return GURL(chrome::kAboutBlankURL); | 79 return GURL(chrome::kAboutBlankURL); |
| 79 } | 80 } |
| 80 | 81 |
| 81 bool ToolbarModel::WouldReplaceSearchURLWithSearchTerms() const { | 82 bool ToolbarModelImpl::WouldReplaceSearchURLWithSearchTerms() const { |
| 82 return !TryToExtractSearchTermsFromURL(GetURL()).empty(); | 83 return !TryToExtractSearchTermsFromURL(GetURL()).empty(); |
| 83 } | 84 } |
| 84 | 85 |
| 85 bool ToolbarModel::ShouldDisplayURL() const { | 86 bool ToolbarModelImpl::ShouldDisplayURL() const { |
| 86 // Note: The order here is important. | 87 // Note: The order here is important. |
| 87 // - The WebUI test must come before the extension scheme test because there | 88 // - 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 | 89 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In |
| 89 // that case, we should prefer what the WebUI instance says. | 90 // 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 | 91 // - 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 | 92 // of view-source:chrome://newtab, which should display its URL despite what |
| 92 // chrome://newtab's WebUI says. | 93 // chrome://newtab's WebUI says. |
| 93 NavigationController* controller = GetNavigationController(); | 94 NavigationController* controller = GetNavigationController(); |
| 94 NavigationEntry* entry = controller ? controller->GetVisibleEntry() : NULL; | 95 NavigationEntry* entry = controller ? controller->GetVisibleEntry() : NULL; |
| 95 if (entry) { | 96 if (entry) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 107 return false; | 108 return false; |
| 108 | 109 |
| 109 #if defined(OS_CHROMEOS) | 110 #if defined(OS_CHROMEOS) |
| 110 if (entry && entry->GetURL().SchemeIs(chrome::kDriveScheme)) | 111 if (entry && entry->GetURL().SchemeIs(chrome::kDriveScheme)) |
| 111 return false; | 112 return false; |
| 112 #endif | 113 #endif |
| 113 | 114 |
| 114 return true; | 115 return true; |
| 115 } | 116 } |
| 116 | 117 |
| 117 ToolbarModel::SecurityLevel ToolbarModel::GetSecurityLevel() const { | 118 ToolbarModelImpl::SecurityLevel ToolbarModelImpl::GetSecurityLevel() const { |
| 118 if (input_in_progress_) // When editing, assume no security style. | 119 if (input_in_progress_) // When editing, assume no security style. |
| 119 return NONE; | 120 return NONE; |
| 120 | 121 |
| 121 NavigationController* navigation_controller = GetNavigationController(); | 122 NavigationController* navigation_controller = GetNavigationController(); |
| 122 if (!navigation_controller) // We might not have a controller on init. | 123 if (!navigation_controller) // We might not have a controller on init. |
| 123 return NONE; | 124 return NONE; |
| 124 | 125 |
| 125 NavigationEntry* entry = navigation_controller->GetVisibleEntry(); | 126 NavigationEntry* entry = navigation_controller->GetVisibleEntry(); |
| 126 if (!entry) | 127 if (!entry) |
| 127 return NONE; | 128 return NONE; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 146 content::CertStore::GetInstance()->RetrieveCert(ssl.cert_id, NULL)) | 147 content::CertStore::GetInstance()->RetrieveCert(ssl.cert_id, NULL)) |
| 147 return EV_SECURE; | 148 return EV_SECURE; |
| 148 return SECURE; | 149 return SECURE; |
| 149 | 150 |
| 150 default: | 151 default: |
| 151 NOTREACHED(); | 152 NOTREACHED(); |
| 152 return NONE; | 153 return NONE; |
| 153 } | 154 } |
| 154 } | 155 } |
| 155 | 156 |
| 156 int ToolbarModel::GetIcon() const { | 157 int ToolbarModelImpl::GetIcon() const { |
| 157 static int icon_ids[NUM_SECURITY_LEVELS] = { | 158 static int icon_ids[NUM_SECURITY_LEVELS] = { |
| 158 IDR_LOCATION_BAR_HTTP, | 159 IDR_LOCATION_BAR_HTTP, |
| 159 IDR_OMNIBOX_HTTPS_VALID, | 160 IDR_OMNIBOX_HTTPS_VALID, |
| 160 IDR_OMNIBOX_HTTPS_VALID, | 161 IDR_OMNIBOX_HTTPS_VALID, |
| 161 IDR_OMNIBOX_HTTPS_WARNING, | 162 IDR_OMNIBOX_HTTPS_WARNING, |
| 162 IDR_OMNIBOX_HTTPS_INVALID, | 163 IDR_OMNIBOX_HTTPS_INVALID, |
| 163 }; | 164 }; |
| 164 DCHECK(arraysize(icon_ids) == NUM_SECURITY_LEVELS); | 165 DCHECK(arraysize(icon_ids) == NUM_SECURITY_LEVELS); |
| 165 return icon_ids[GetSecurityLevel()]; | 166 return icon_ids[GetSecurityLevel()]; |
| 166 } | 167 } |
| 167 | 168 |
| 168 string16 ToolbarModel::GetEVCertName() const { | 169 string16 ToolbarModelImpl::GetEVCertName() const { |
| 169 DCHECK_EQ(GetSecurityLevel(), EV_SECURE); | 170 DCHECK_EQ(GetSecurityLevel(), EV_SECURE); |
| 170 scoped_refptr<net::X509Certificate> cert; | 171 scoped_refptr<net::X509Certificate> cert; |
| 171 // Note: Navigation controller and active entry are guaranteed non-NULL or | 172 // Note: Navigation controller and active entry are guaranteed non-NULL or |
| 172 // the security level would be NONE. | 173 // the security level would be NONE. |
| 173 content::CertStore::GetInstance()->RetrieveCert( | 174 content::CertStore::GetInstance()->RetrieveCert( |
| 174 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id, &cert); | 175 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id, &cert); |
| 175 return GetEVCertName(*cert); | 176 return ToolbarModel::GetEVCertName(*cert); |
| 176 } | 177 } |
| 177 | 178 |
| 178 // static | 179 void ToolbarModelImpl::SetInputInProgress(bool value) { |
| 179 string16 ToolbarModel::GetEVCertName(const net::X509Certificate& cert) { | 180 input_in_progress_ = value; |
| 180 // EV are required to have an organization name and country. | |
| 181 if (cert.subject().organization_names.empty() || | |
| 182 cert.subject().country_name.empty()) { | |
| 183 NOTREACHED(); | |
| 184 return string16(); | |
| 185 } | |
| 186 | |
| 187 return l10n_util::GetStringFUTF16( | |
| 188 IDS_SECURE_CONNECTION_EV, | |
| 189 UTF8ToUTF16(cert.subject().organization_names[0]), | |
| 190 UTF8ToUTF16(cert.subject().country_name)); | |
| 191 } | 181 } |
| 192 | 182 |
| 193 NavigationController* ToolbarModel::GetNavigationController() const { | 183 bool ToolbarModelImpl::GetInputInProgress() const { |
| 184 return input_in_progress_; |
| 185 } |
| 186 |
| 187 NavigationController* ToolbarModelImpl::GetNavigationController() const { |
| 194 // This |current_tab| can be NULL during the initialization of the | 188 // This |current_tab| can be NULL during the initialization of the |
| 195 // toolbar during window creation (i.e. before any tabs have been added | 189 // toolbar during window creation (i.e. before any tabs have been added |
| 196 // to the window). | 190 // to the window). |
| 197 WebContents* current_tab = delegate_->GetActiveWebContents(); | 191 WebContents* current_tab = delegate_->GetActiveWebContents(); |
| 198 return current_tab ? ¤t_tab->GetController() : NULL; | 192 return current_tab ? ¤t_tab->GetController() : NULL; |
| 199 } | 193 } |
| 200 | 194 |
| 201 string16 ToolbarModel::TryToExtractSearchTermsFromURL(const GURL& url) const { | 195 string16 ToolbarModelImpl::TryToExtractSearchTermsFromURL( |
| 196 const GURL& url) const { |
| 202 Profile* profile = GetProfile(); | 197 Profile* profile = GetProfile(); |
| 203 | 198 |
| 204 // Ensure instant extended API is enabled. | 199 // Ensure instant extended API is enabled. |
| 205 if (!profile || !chrome::search::IsInstantExtendedAPIEnabled(profile)) | 200 if (!profile || !chrome::search::IsInstantExtendedAPIEnabled(profile)) |
| 206 return string16(); | 201 return string16(); |
| 207 | 202 |
| 208 TemplateURLService* template_url_service = | 203 TemplateURLService* template_url_service = |
| 209 TemplateURLServiceFactory::GetForProfile(profile); | 204 TemplateURLServiceFactory::GetForProfile(profile); |
| 210 | 205 |
| 211 TemplateURL *template_url = template_url_service->GetDefaultSearchProvider(); | 206 TemplateURL *template_url = template_url_service->GetDefaultSearchProvider(); |
| 212 if (!template_url) | 207 if (!template_url) |
| 213 return string16(); | 208 return string16(); |
| 214 | 209 |
| 215 string16 result; | 210 string16 result; |
| 216 template_url->ExtractSearchTermsFromURL(url, &result); | 211 template_url->ExtractSearchTermsFromURL(url, &result); |
| 217 return result; | 212 return result; |
| 218 } | 213 } |
| 219 | 214 |
| 220 Profile* ToolbarModel::GetProfile() const { | 215 Profile* ToolbarModelImpl::GetProfile() const { |
| 221 NavigationController* navigation_controller = GetNavigationController(); | 216 NavigationController* navigation_controller = GetNavigationController(); |
| 222 return navigation_controller ? | 217 return navigation_controller ? |
| 223 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) : | 218 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) : |
| 224 NULL; | 219 NULL; |
| 225 } | 220 } |
| OLD | NEW |