| OLD | NEW | 
|---|
|  | (Empty) | 
| 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 |  | 
| 3 // found in the LICENSE file. |  | 
| 4 |  | 
| 5 #include "chrome/browser/ui/toolbar/toolbar_model.h" |  | 
| 6 |  | 
| 7 #include "base/utf_string_conversions.h" |  | 
| 8 #include "chrome/browser/autocomplete/autocomplete_input.h" |  | 
| 9 #include "chrome/browser/prefs/pref_service.h" |  | 
| 10 #include "chrome/browser/profiles/profile.h" |  | 
| 11 #include "chrome/browser/search_engines/template_url.h" |  | 
| 12 #include "chrome/browser/search_engines/template_url_service.h" |  | 
| 13 #include "chrome/browser/search_engines/template_url_service_factory.h" |  | 
| 14 #include "chrome/browser/ssl/ssl_error_info.h" |  | 
| 15 #include "chrome/browser/ui/search/search.h" |  | 
| 16 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h" |  | 
| 17 #include "chrome/common/chrome_constants.h" |  | 
| 18 #include "chrome/common/pref_names.h" |  | 
| 19 #include "chrome/common/url_constants.h" |  | 
| 20 #include "content/public/browser/cert_store.h" |  | 
| 21 #include "content/public/browser/navigation_controller.h" |  | 
| 22 #include "content/public/browser/navigation_entry.h" |  | 
| 23 #include "content/public/browser/web_contents.h" |  | 
| 24 #include "content/public/browser/web_ui.h" |  | 
| 25 #include "content/public/common/content_constants.h" |  | 
| 26 #include "content/public/common/ssl_status.h" |  | 
| 27 #include "grit/generated_resources.h" |  | 
| 28 #include "grit/theme_resources.h" |  | 
| 29 #include "net/base/cert_status_flags.h" |  | 
| 30 #include "net/base/net_util.h" |  | 
| 31 #include "ui/base/l10n/l10n_util.h" |  | 
| 32 |  | 
| 33 using content::NavigationController; |  | 
| 34 using content::NavigationEntry; |  | 
| 35 using content::SSLStatus; |  | 
| 36 using content::WebContents; |  | 
| 37 |  | 
| 38 ToolbarModel::ToolbarModel(ToolbarModelDelegate* delegate) |  | 
| 39     : delegate_(delegate), |  | 
| 40       input_in_progress_(false) { |  | 
| 41 } |  | 
| 42 |  | 
| 43 ToolbarModel::~ToolbarModel() { |  | 
| 44 } |  | 
| 45 |  | 
| 46 // ToolbarModel Implementation. |  | 
| 47 string16 ToolbarModel::GetText(bool display_search_urls_as_search_terms) const { |  | 
| 48   GURL url(GetURL()); |  | 
| 49 |  | 
| 50   if (display_search_urls_as_search_terms) { |  | 
| 51     string16 search_terms = TryToExtractSearchTermsFromURL(url); |  | 
| 52     if (!search_terms.empty()) |  | 
| 53       return search_terms; |  | 
| 54   } |  | 
| 55   std::string languages;  // Empty if we don't have a |navigation_controller|. |  | 
| 56   Profile* profile = GetProfile(); |  | 
| 57   if (profile) |  | 
| 58     languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); |  | 
| 59 |  | 
| 60   if (url.spec().length() > content::kMaxURLDisplayChars) |  | 
| 61     url = url.IsStandard() ? url.GetOrigin() : GURL(url.scheme() + ":"); |  | 
| 62   // 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   // the space. |  | 
| 65   return AutocompleteInput::FormattedStringWithEquivalentMeaning( |  | 
| 66       url, net::FormatUrl(url, languages, net::kFormatUrlOmitAll, |  | 
| 67                           net::UnescapeRule::NORMAL, NULL, NULL, NULL)); |  | 
| 68 } |  | 
| 69 |  | 
| 70 GURL ToolbarModel::GetURL() const { |  | 
| 71   const NavigationController* navigation_controller = GetNavigationController(); |  | 
| 72   if (navigation_controller) { |  | 
| 73     const NavigationEntry* entry = navigation_controller->GetVisibleEntry(); |  | 
| 74     if (entry) |  | 
| 75       return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL(); |  | 
| 76   } |  | 
| 77 |  | 
| 78   return GURL(chrome::kAboutBlankURL); |  | 
| 79 } |  | 
| 80 |  | 
| 81 bool ToolbarModel::WouldReplaceSearchURLWithSearchTerms() const { |  | 
| 82   return !TryToExtractSearchTermsFromURL(GetURL()).empty(); |  | 
| 83 } |  | 
| 84 |  | 
| 85 bool ToolbarModel::ShouldDisplayURL() const { |  | 
| 86   // Note: The order here is important. |  | 
| 87   // - 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   //   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   //   of view-source:chrome://newtab, which should display its URL despite what |  | 
| 92   //   chrome://newtab's WebUI says. |  | 
| 93   NavigationController* controller = GetNavigationController(); |  | 
| 94   NavigationEntry* entry = controller ? controller->GetVisibleEntry() : NULL; |  | 
| 95   if (entry) { |  | 
| 96     if (entry->IsViewSourceMode() || |  | 
| 97         entry->GetPageType() == content::PAGE_TYPE_INTERSTITIAL) { |  | 
| 98       return true; |  | 
| 99     } |  | 
| 100   } |  | 
| 101 |  | 
| 102   WebContents* web_contents = delegate_->GetActiveWebContents(); |  | 
| 103   if (web_contents && web_contents->GetWebUIForCurrentState()) |  | 
| 104     return !web_contents->GetWebUIForCurrentState()->ShouldHideURL(); |  | 
| 105 |  | 
| 106   if (entry && entry->GetURL().SchemeIs(chrome::kExtensionScheme)) |  | 
| 107     return false; |  | 
| 108 |  | 
| 109 #if defined(OS_CHROMEOS) |  | 
| 110   if (entry && entry->GetURL().SchemeIs(chrome::kDriveScheme)) |  | 
| 111     return false; |  | 
| 112 #endif |  | 
| 113 |  | 
| 114   return true; |  | 
| 115 } |  | 
| 116 |  | 
| 117 ToolbarModel::SecurityLevel ToolbarModel::GetSecurityLevel() const { |  | 
| 118   if (input_in_progress_)  // When editing, assume no security style. |  | 
| 119     return NONE; |  | 
| 120 |  | 
| 121   NavigationController* navigation_controller = GetNavigationController(); |  | 
| 122   if (!navigation_controller)  // We might not have a controller on init. |  | 
| 123     return NONE; |  | 
| 124 |  | 
| 125   NavigationEntry* entry = navigation_controller->GetVisibleEntry(); |  | 
| 126   if (!entry) |  | 
| 127     return NONE; |  | 
| 128 |  | 
| 129   const SSLStatus& ssl = entry->GetSSL(); |  | 
| 130   switch (ssl.security_style) { |  | 
| 131     case content::SECURITY_STYLE_UNKNOWN: |  | 
| 132     case content::SECURITY_STYLE_UNAUTHENTICATED: |  | 
| 133       return NONE; |  | 
| 134 |  | 
| 135     case content::SECURITY_STYLE_AUTHENTICATION_BROKEN: |  | 
| 136       return SECURITY_ERROR; |  | 
| 137 |  | 
| 138     case content::SECURITY_STYLE_AUTHENTICATED: |  | 
| 139       if (!!(ssl.content_status & SSLStatus::DISPLAYED_INSECURE_CONTENT)) |  | 
| 140         return SECURITY_WARNING; |  | 
| 141       if (net::IsCertStatusError(ssl.cert_status)) { |  | 
| 142         DCHECK(net::IsCertStatusMinorError(ssl.cert_status)); |  | 
| 143         return SECURITY_WARNING; |  | 
| 144       } |  | 
| 145       if ((ssl.cert_status & net::CERT_STATUS_IS_EV) && |  | 
| 146           content::CertStore::GetInstance()->RetrieveCert(ssl.cert_id, NULL)) |  | 
| 147         return EV_SECURE; |  | 
| 148       return SECURE; |  | 
| 149 |  | 
| 150     default: |  | 
| 151       NOTREACHED(); |  | 
| 152       return NONE; |  | 
| 153   } |  | 
| 154 } |  | 
| 155 |  | 
| 156 int ToolbarModel::GetIcon() const { |  | 
| 157   static int icon_ids[NUM_SECURITY_LEVELS] = { |  | 
| 158     IDR_LOCATION_BAR_HTTP, |  | 
| 159     IDR_OMNIBOX_HTTPS_VALID, |  | 
| 160     IDR_OMNIBOX_HTTPS_VALID, |  | 
| 161     IDR_OMNIBOX_HTTPS_WARNING, |  | 
| 162     IDR_OMNIBOX_HTTPS_INVALID, |  | 
| 163   }; |  | 
| 164   DCHECK(arraysize(icon_ids) == NUM_SECURITY_LEVELS); |  | 
| 165   return icon_ids[GetSecurityLevel()]; |  | 
| 166 } |  | 
| 167 |  | 
| 168 string16 ToolbarModel::GetEVCertName() const { |  | 
| 169   DCHECK_EQ(GetSecurityLevel(), EV_SECURE); |  | 
| 170   scoped_refptr<net::X509Certificate> cert; |  | 
| 171   // Note: Navigation controller and active entry are guaranteed non-NULL or |  | 
| 172   // the security level would be NONE. |  | 
| 173   content::CertStore::GetInstance()->RetrieveCert( |  | 
| 174       GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id, &cert); |  | 
| 175   return GetEVCertName(*cert); |  | 
| 176 } |  | 
| 177 |  | 
| 178 // static |  | 
| 179 string16 ToolbarModel::GetEVCertName(const net::X509Certificate& cert) { |  | 
| 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 } |  | 
| 192 |  | 
| 193 NavigationController* ToolbarModel::GetNavigationController() const { |  | 
| 194   // This |current_tab| can be NULL during the initialization of the |  | 
| 195   // toolbar during window creation (i.e. before any tabs have been added |  | 
| 196   // to the window). |  | 
| 197   WebContents* current_tab = delegate_->GetActiveWebContents(); |  | 
| 198   return current_tab ? ¤t_tab->GetController() : NULL; |  | 
| 199 } |  | 
| 200 |  | 
| 201 string16 ToolbarModel::TryToExtractSearchTermsFromURL(const GURL& url) const { |  | 
| 202   Profile* profile = GetProfile(); |  | 
| 203 |  | 
| 204   // Ensure instant extended API is enabled. |  | 
| 205   if (!profile || !chrome::search::IsInstantExtendedAPIEnabled(profile)) |  | 
| 206     return string16(); |  | 
| 207 |  | 
| 208   TemplateURLService* template_url_service = |  | 
| 209       TemplateURLServiceFactory::GetForProfile(profile); |  | 
| 210 |  | 
| 211   TemplateURL *template_url = template_url_service->GetDefaultSearchProvider(); |  | 
| 212   if (!template_url) |  | 
| 213     return string16(); |  | 
| 214 |  | 
| 215   string16 result; |  | 
| 216   template_url->ExtractSearchTermsFromURL(url, &result); |  | 
| 217   return result; |  | 
| 218 } |  | 
| 219 |  | 
| 220 Profile* ToolbarModel::GetProfile() const { |  | 
| 221   NavigationController* navigation_controller = GetNavigationController(); |  | 
| 222   return navigation_controller ? |  | 
| 223       Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) : |  | 
| 224       NULL; |  | 
| 225 } |  | 
| OLD | NEW | 
|---|