1 // Copyright (c) 2010 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.h"
9 #include "chrome/browser/autocomplete/autocomplete_edit.h"
10 #include "chrome/browser/prefs/pref_service.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ssl/ssl_error_info.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/common/chrome_constants.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/common/url_constants.h"
17 #include "content/browser/cert_store.h"
18 #include "content/browser/tab_contents/navigation_controller.h"
19 #include "content/browser/tab_contents/navigation_entry.h"
20 #include "content/browser/tab_contents/tab_contents.h"
21 #include "content/common/content_constants.h"
22 #include "grit/generated_resources.h"
23 #include "grit/theme_resources.h"
24 #include "net/base/cert_status_flags.h"
25 #include "net/base/net_util.h"
26
ToolbarModel(Browser * browser)27 ToolbarModel::ToolbarModel(Browser* browser)
28 : browser_(browser),
29 input_in_progress_(false) {
30 }
31
~ToolbarModel()32 ToolbarModel::~ToolbarModel() {
33 }
34
35 // ToolbarModel Implementation.
GetText() const36 std::wstring ToolbarModel::GetText() const {
37 GURL url(chrome::kAboutBlankURL);
38 std::string languages; // Empty if we don't have a |navigation_controller|.
39
40 NavigationController* navigation_controller = GetNavigationController();
41 if (navigation_controller) {
42 languages = navigation_controller->profile()->GetPrefs()->GetString(
43 prefs::kAcceptLanguages);
44 NavigationEntry* entry = navigation_controller->GetActiveEntry();
45 if (!navigation_controller->tab_contents()->ShouldDisplayURL()) {
46 // Explicitly hide the URL for this tab.
47 url = GURL();
48 } else if (entry) {
49 url = entry->virtual_url();
50 }
51 }
52 if (url.spec().length() > content::kMaxURLDisplayChars)
53 url = url.IsStandard() ? url.GetOrigin() : GURL(url.scheme() + ":");
54 // Note that we can't unescape spaces here, because if the user copies this
55 // and pastes it into another program, that program may think the URL ends at
56 // the space.
57 return UTF16ToWideHack(
58 AutocompleteInput::FormattedStringWithEquivalentMeaning(
59 url,
60 net::FormatUrl(url, languages, net::kFormatUrlOmitAll,
61 UnescapeRule::NORMAL, NULL, NULL, NULL)));
62 }
63
GetSecurityLevel() const64 ToolbarModel::SecurityLevel ToolbarModel::GetSecurityLevel() const {
65 if (input_in_progress_) // When editing, assume no security style.
66 return NONE;
67
68 NavigationController* navigation_controller = GetNavigationController();
69 if (!navigation_controller) // We might not have a controller on init.
70 return NONE;
71
72 NavigationEntry* entry = navigation_controller->GetActiveEntry();
73 if (!entry)
74 return NONE;
75
76 const NavigationEntry::SSLStatus& ssl = entry->ssl();
77 switch (ssl.security_style()) {
78 case SECURITY_STYLE_UNKNOWN:
79 case SECURITY_STYLE_UNAUTHENTICATED:
80 return NONE;
81
82 case SECURITY_STYLE_AUTHENTICATION_BROKEN:
83 return SECURITY_ERROR;
84
85 case SECURITY_STYLE_AUTHENTICATED:
86 if (ssl.displayed_insecure_content())
87 return SECURITY_WARNING;
88 if (net::IsCertStatusError(ssl.cert_status())) {
89 DCHECK_EQ(ssl.cert_status() & net::CERT_STATUS_ALL_ERRORS,
90 net::CERT_STATUS_UNABLE_TO_CHECK_REVOCATION);
91 return SECURITY_WARNING;
92 }
93 if ((ssl.cert_status() & net::CERT_STATUS_IS_EV) &&
94 CertStore::GetInstance()->RetrieveCert(ssl.cert_id(), NULL))
95 return EV_SECURE;
96 return SECURE;
97
98 default:
99 NOTREACHED();
100 return NONE;
101 }
102 }
103
GetIcon() const104 int ToolbarModel::GetIcon() const {
105 static int icon_ids[NUM_SECURITY_LEVELS] = {
106 IDR_OMNIBOX_HTTP,
107 IDR_OMNIBOX_HTTPS_VALID,
108 IDR_OMNIBOX_HTTPS_VALID,
109 IDR_OMNIBOX_HTTPS_WARNING,
110 IDR_OMNIBOX_HTTPS_INVALID,
111 };
112 DCHECK(arraysize(icon_ids) == NUM_SECURITY_LEVELS);
113 return icon_ids[GetSecurityLevel()];
114 }
115
GetEVCertName() const116 std::wstring ToolbarModel::GetEVCertName() const {
117 DCHECK_EQ(GetSecurityLevel(), EV_SECURE);
118 scoped_refptr<net::X509Certificate> cert;
119 // Note: Navigation controller and active entry are guaranteed non-NULL or
120 // the security level would be NONE.
121 CertStore::GetInstance()->RetrieveCert(
122 GetNavigationController()->GetActiveEntry()->ssl().cert_id(), &cert);
123 return UTF16ToWideHack(SSLManager::GetEVCertName(*cert));
124 }
125
GetNavigationController() const126 NavigationController* ToolbarModel::GetNavigationController() const {
127 // This |current_tab| can be NULL during the initialization of the
128 // toolbar during window creation (i.e. before any tabs have been added
129 // to the window).
130 TabContents* current_tab = browser_->GetSelectedTabContents();
131 return current_tab ? ¤t_tab->controller() : NULL;
132 }
133