| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/toolbar/toolbar_model_impl.h" | |
| 6 | |
| 7 #include "base/strings/utf_string_conversions.h" | |
| 8 #include "base/time/time.h" | |
| 9 #include "build/build_config.h" | |
| 10 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h" | |
| 11 #include "components/google/core/browser/google_util.h" | |
| 12 #include "components/prefs/pref_service.h" | |
| 13 #include "components/security_state/security_state_model.h" | |
| 14 #include "components/url_formatter/elide_url.h" | |
| 15 #include "components/url_formatter/url_formatter.h" | |
| 16 #include "grit/components_scaled_resources.h" | |
| 17 #include "grit/components_strings.h" | |
| 18 #include "net/cert/cert_status_flags.h" | |
| 19 #include "net/cert/x509_certificate.h" | |
| 20 #include "net/ssl/ssl_connection_status_flags.h" | |
| 21 #include "ui/base/l10n/l10n_util.h" | |
| 22 #include "ui/gfx/text_elider.h" | |
| 23 #include "ui/gfx/vector_icons_public.h" | |
| 24 | |
| 25 using security_state::SecurityStateModel; | |
| 26 | |
| 27 ToolbarModelImpl::ToolbarModelImpl(ToolbarModelDelegate* delegate, | |
| 28 size_t max_url_display_chars) | |
| 29 : delegate_(delegate), max_url_display_chars_(max_url_display_chars) {} | |
| 30 | |
| 31 ToolbarModelImpl::~ToolbarModelImpl() { | |
| 32 } | |
| 33 | |
| 34 // ToolbarModelImpl Implementation. | |
| 35 base::string16 ToolbarModelImpl::GetText() const { | |
| 36 base::string16 search_terms(GetSearchTerms(false)); | |
| 37 if (!search_terms.empty()) | |
| 38 return search_terms; | |
| 39 | |
| 40 return GetFormattedURL(NULL); | |
| 41 } | |
| 42 | |
| 43 base::string16 ToolbarModelImpl::GetFormattedURL(size_t* prefix_end) const { | |
| 44 // May be empty during initialization. | |
| 45 std::string languages = delegate_->GetAcceptLanguages(); | |
| 46 | |
| 47 GURL url(GetURL()); | |
| 48 // Note that we can't unescape spaces here, because if the user copies this | |
| 49 // and pastes it into another program, that program may think the URL ends at | |
| 50 // the space. | |
| 51 const base::string16 formatted_text = | |
| 52 delegate_->FormattedStringWithEquivalentMeaning( | |
| 53 url, url_formatter::FormatUrl( | |
| 54 url, languages, url_formatter::kFormatUrlOmitAll, | |
| 55 net::UnescapeRule::NORMAL, nullptr, prefix_end, nullptr)); | |
| 56 if (formatted_text.length() <= max_url_display_chars_) | |
| 57 return formatted_text; | |
| 58 | |
| 59 // Truncating the URL breaks editing and then pressing enter, but hopefully | |
| 60 // people won't try to do much with such enormous URLs anyway. If this becomes | |
| 61 // a real problem, we could perhaps try to keep some sort of different "elided | |
| 62 // visible URL" where editing affects and reloads the "real underlying URL", | |
| 63 // but this seems very tricky for little gain. | |
| 64 return gfx::TruncateString(formatted_text, max_url_display_chars_ - 1, | |
| 65 gfx::CHARACTER_BREAK) + | |
| 66 gfx::kEllipsisUTF16; | |
| 67 } | |
| 68 | |
| 69 base::string16 ToolbarModelImpl::GetCorpusNameForMobile() const { | |
| 70 if (!WouldPerformSearchTermReplacement(false)) | |
| 71 return base::string16(); | |
| 72 GURL url(GetURL()); | |
| 73 // If there is a query in the url fragment look for the corpus name there, | |
| 74 // otherwise look for the corpus name in the query parameters. | |
| 75 const std::string& query_str(google_util::HasGoogleSearchQueryParam( | |
| 76 url.ref_piece()) ? url.ref() : url.query()); | |
| 77 url::Component query(0, query_str.length()), key, value; | |
| 78 const char kChipKey[] = "sboxchip"; | |
| 79 while (url::ExtractQueryKeyValue(query_str.c_str(), &query, &key, &value)) { | |
| 80 if (key.is_nonempty() && query_str.substr(key.begin, key.len) == kChipKey) { | |
| 81 return net::UnescapeAndDecodeUTF8URLComponent( | |
| 82 query_str.substr(value.begin, value.len), | |
| 83 net::UnescapeRule::NORMAL); | |
| 84 } | |
| 85 } | |
| 86 return base::string16(); | |
| 87 } | |
| 88 | |
| 89 GURL ToolbarModelImpl::GetURL() const { | |
| 90 GURL url; | |
| 91 return delegate_->GetURL(&url) ? url : GURL(url::kAboutBlankURL); | |
| 92 } | |
| 93 | |
| 94 bool ToolbarModelImpl::WouldPerformSearchTermReplacement( | |
| 95 bool ignore_editing) const { | |
| 96 return !GetSearchTerms(ignore_editing).empty(); | |
| 97 } | |
| 98 | |
| 99 SecurityStateModel::SecurityLevel ToolbarModelImpl::GetSecurityLevel( | |
| 100 bool ignore_editing) const { | |
| 101 // When editing, assume no security style. | |
| 102 return (input_in_progress() && !ignore_editing) | |
| 103 ? SecurityStateModel::NONE | |
| 104 : delegate_->GetSecurityLevel(); | |
| 105 } | |
| 106 | |
| 107 int ToolbarModelImpl::GetIcon() const { | |
| 108 switch (GetSecurityLevel(false)) { | |
| 109 case SecurityStateModel::NONE: | |
| 110 return IDR_LOCATION_BAR_HTTP; | |
| 111 case SecurityStateModel::EV_SECURE: | |
| 112 case SecurityStateModel::SECURE: | |
| 113 return IDR_OMNIBOX_HTTPS_VALID; | |
| 114 case SecurityStateModel::SECURITY_WARNING: | |
| 115 // Surface Dubious as Neutral. | |
| 116 return IDR_LOCATION_BAR_HTTP; | |
| 117 case SecurityStateModel::SECURITY_POLICY_WARNING: | |
| 118 return IDR_OMNIBOX_HTTPS_POLICY_WARNING; | |
| 119 case SecurityStateModel::SECURITY_ERROR: | |
| 120 return IDR_OMNIBOX_HTTPS_INVALID; | |
| 121 } | |
| 122 | |
| 123 NOTREACHED(); | |
| 124 return IDR_LOCATION_BAR_HTTP; | |
| 125 } | |
| 126 | |
| 127 gfx::VectorIconId ToolbarModelImpl::GetVectorIcon() const { | |
| 128 #if !defined(OS_ANDROID) && !defined(OS_MACOSX) && !defined(OS_IOS) | |
| 129 switch (GetSecurityLevel(false)) { | |
| 130 case SecurityStateModel::NONE: | |
| 131 return gfx::VectorIconId::LOCATION_BAR_HTTP; | |
| 132 case SecurityStateModel::EV_SECURE: | |
| 133 case SecurityStateModel::SECURE: | |
| 134 return gfx::VectorIconId::LOCATION_BAR_HTTPS_VALID; | |
| 135 case SecurityStateModel::SECURITY_WARNING: | |
| 136 // Surface Dubious as Neutral. | |
| 137 return gfx::VectorIconId::LOCATION_BAR_HTTP; | |
| 138 case SecurityStateModel::SECURITY_POLICY_WARNING: | |
| 139 return gfx::VectorIconId::BUSINESS; | |
| 140 case SecurityStateModel::SECURITY_ERROR: | |
| 141 return gfx::VectorIconId::LOCATION_BAR_HTTPS_INVALID; | |
| 142 } | |
| 143 #endif | |
| 144 | |
| 145 NOTREACHED(); | |
| 146 return gfx::VectorIconId::VECTOR_ICON_NONE; | |
| 147 } | |
| 148 | |
| 149 base::string16 ToolbarModelImpl::GetEVCertName() const { | |
| 150 if (GetSecurityLevel(false) != SecurityStateModel::EV_SECURE) | |
| 151 return base::string16(); | |
| 152 | |
| 153 // Note: cert is guaranteed non-NULL or the security level would be NONE. | |
| 154 scoped_refptr<net::X509Certificate> cert = delegate_->GetCertificate(); | |
| 155 DCHECK(cert.get()); | |
| 156 | |
| 157 // EV are required to have an organization name and country. | |
| 158 DCHECK(!cert->subject().organization_names.empty()); | |
| 159 DCHECK(!cert->subject().country_name.empty()); | |
| 160 return l10n_util::GetStringFUTF16( | |
| 161 IDS_SECURE_CONNECTION_EV, | |
| 162 base::UTF8ToUTF16(cert->subject().organization_names[0]), | |
| 163 base::UTF8ToUTF16(cert->subject().country_name)); | |
| 164 } | |
| 165 | |
| 166 bool ToolbarModelImpl::ShouldDisplayURL() const { | |
| 167 return delegate_->ShouldDisplayURL(); | |
| 168 } | |
| 169 | |
| 170 base::string16 ToolbarModelImpl::GetSearchTerms(bool ignore_editing) const { | |
| 171 if (!url_replacement_enabled() || (input_in_progress() && !ignore_editing)) | |
| 172 return base::string16(); | |
| 173 | |
| 174 return delegate_->GetSearchTerms(GetSecurityLevel(ignore_editing)); | |
| 175 } | |
| OLD | NEW |