Temporarily re-enabling SizeAfterPrefChange test with traces (this time for Linux...
[chromium-blink-merge.git] / chrome / browser / ui / toolbar / origin_chip_info.cc
blobbc5e601139f7923f25e08716788048e1f03184da
1 // Copyright 2014 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.
5 #include "chrome/browser/ui/toolbar/origin_chip_info.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/extensions/extension_icon_image.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_util.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/safe_browsing/client_side_detection_host.h"
15 #include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h"
16 #include "chrome/browser/ui/toolbar/toolbar_model.h"
17 #include "chrome/common/extensions/extension_constants.h"
18 #include "chrome/common/pref_names.h"
19 #include "chrome/common/url_constants.h"
20 #include "content/public/browser/web_contents.h"
21 #include "extensions/browser/extension_system.h"
22 #include "extensions/common/constants.h"
23 #include "extensions/common/manifest_handlers/icons_handler.h"
24 #include "grit/chromium_strings.h"
25 #include "grit/components_strings.h"
26 #include "grit/generated_resources.h"
27 #include "grit/theme_resources.h"
28 #include "net/base/net_util.h"
29 #include "ui/base/l10n/l10n_util.h"
30 #include "url/gurl.h"
32 namespace {
34 // For selected kChromeUIScheme and kAboutScheme, return the string resource
35 // number for the title of the page. If we don't have a specialized title,
36 // returns -1.
37 int StringForChromeHost(const GURL& url) {
38 DCHECK(url.is_empty() || url.SchemeIs(content::kChromeUIScheme));
40 if (url.is_empty())
41 return IDS_NEW_TAB_TITLE;
43 // TODO(gbillock): Just get the page title and special case exceptions?
44 std::string host = url.host();
45 if (host == chrome::kChromeUIAppLauncherPageHost)
46 return IDS_APP_DEFAULT_PAGE_NAME;
47 if (host == chrome::kChromeUIBookmarksHost)
48 return IDS_BOOKMARK_MANAGER_TITLE;
49 if (host == chrome::kChromeUIComponentsHost)
50 return IDS_COMPONENTS_TITLE;
51 if (host == chrome::kChromeUICrashesHost)
52 return IDS_CRASHES_TITLE;
53 #if defined(ENABLE_SERVICE_DISCOVERY)
54 if (host == chrome::kChromeUIDevicesHost)
55 return IDS_LOCAL_DISCOVERY_DEVICES_PAGE_TITLE;
56 #endif // ENABLE_SERVICE_DISCOVERY
57 if (host == chrome::kChromeUIDownloadsHost)
58 return IDS_DOWNLOAD_TITLE;
59 if (host == chrome::kChromeUIExtensionsHost)
60 return IDS_MANAGE_EXTENSIONS_SETTING_WINDOWS_TITLE;
61 if (host == chrome::kChromeUIHelpHost)
62 return IDS_ABOUT_TAB_TITLE;
63 if (host == chrome::kChromeUIHistoryHost)
64 return IDS_HISTORY_TITLE;
65 if (host == chrome::kChromeUINewTabHost)
66 return IDS_NEW_TAB_TITLE;
67 if (host == chrome::kChromeUIPluginsHost)
68 return IDS_PLUGINS_TITLE;
69 if (host == chrome::kChromeUIPolicyHost)
70 return IDS_POLICY_TITLE;
71 if (host == chrome::kChromeUIPrintHost)
72 return IDS_PRINT_PREVIEW_TITLE;
73 if (host == chrome::kChromeUISettingsHost)
74 return IDS_SETTINGS_TITLE;
75 if (host == chrome::kChromeUIVersionHost)
76 return IDS_ABOUT_VERSION_TITLE;
78 return -1;
81 } // namespace
83 OriginChipInfo::OriginChipInfo(
84 extensions::IconImage::Observer* owner,
85 Profile* profile)
86 : owner_(owner),
87 profile_(profile) {}
89 OriginChipInfo::~OriginChipInfo() {}
91 bool OriginChipInfo::Update(const content::WebContents* web_contents,
92 const ToolbarModel* toolbar_model) {
93 GURL displayed_url = toolbar_model->GetURL();
94 ToolbarModel::SecurityLevel security_level =
95 toolbar_model->GetSecurityLevel(true);
96 bool is_url_malware = OriginChip::IsMalware(displayed_url, web_contents);
98 if ((displayed_url_ == displayed_url) &&
99 (security_level_ == security_level) &&
100 (is_url_malware_ == is_url_malware))
101 return false;
103 displayed_url_ = displayed_url;
104 security_level_ = security_level;
105 is_url_malware_ = is_url_malware;
107 label_ = OriginChip::LabelFromURLForProfile(displayed_url, profile_);
108 if (security_level_ == ToolbarModel::EV_SECURE) {
109 label_ = l10n_util::GetStringFUTF16(IDS_SITE_CHIP_EV_SSL_LABEL,
110 toolbar_model->GetEVCertName(),
111 label_);
115 if (displayed_url_.SchemeIs(extensions::kExtensionScheme)) {
116 const extensions::Extension* extension =
117 extensions::ExtensionSystem::Get(profile_)->extension_service()->
118 extensions()->GetExtensionOrAppByURL(displayed_url_);
120 if (extension) {
121 icon_ = IDR_EXTENSIONS_FAVICON;
122 extension_icon_image_.reset(
123 new extensions::IconImage(profile_,
124 extension,
125 extensions::IconsInfo::GetIcons(extension),
126 extension_misc::EXTENSION_ICON_BITTY,
127 extensions::util::GetDefaultAppIcon(),
128 owner_));
130 // Forces load of the image.
131 extension_icon_image_->image_skia().GetRepresentation(1.0f);
132 if (!extension_icon_image_->image_skia().image_reps().empty())
133 owner_->OnExtensionIconImageChanged(extension_icon_image_.get());
135 return true;
139 if (extension_icon_image_) {
140 extension_icon_image_.reset();
141 owner_->OnExtensionIconImageChanged(NULL);
144 icon_ = (displayed_url_.is_empty() ||
145 displayed_url_.SchemeIs(content::kChromeUIScheme)) ?
146 IDR_PRODUCT_LOGO_16 :
147 toolbar_model->GetIconForSecurityLevel(security_level_);
149 return true;
152 base::string16 OriginChipInfo::Tooltip() const {
153 return base::UTF8ToUTF16(displayed_url_.spec());
156 // static
157 bool OriginChip::IsMalware(const GURL& url, const content::WebContents* tab) {
158 DCHECK(tab);
160 if (tab->GetURL() != url)
161 return false;
163 const safe_browsing::SafeBrowsingTabObserver* sb_observer =
164 safe_browsing::SafeBrowsingTabObserver::FromWebContents(tab);
165 return sb_observer && sb_observer->detection_host() &&
166 sb_observer->detection_host()->DidPageReceiveSafeBrowsingMatch();
169 // static
170 base::string16 OriginChip::LabelFromURLForProfile(const GURL& provided_url,
171 Profile* profile) {
172 // First, strip view-source: if it appears. Note that GetContent removes
173 // "view-source:" but leaves the original scheme (http, https, ftp, etc).
174 GURL url(provided_url);
175 if (url.SchemeIs(content::kViewSourceScheme))
176 url = GURL(url.GetContent());
178 // About scheme pages. Currently all about: URLs other than about:blank
179 // redirect to chrome: URLs, so this only affects about:blank.
180 if (url.SchemeIs(content::kAboutScheme))
181 return base::UTF8ToUTF16(url.spec());
183 // Chrome built-in pages.
184 if (url.is_empty() || url.SchemeIs(content::kChromeUIScheme)) {
185 int string_ref = StringForChromeHost(url);
186 return l10n_util::GetStringUTF16(
187 (string_ref == -1) ? IDS_SHORT_PRODUCT_NAME : string_ref);
190 // For chrome-extension URLs, return the extension name.
191 if (url.SchemeIs(extensions::kExtensionScheme)) {
192 ExtensionService* service =
193 extensions::ExtensionSystem::Get(profile)->extension_service();
194 const extensions::Extension* extension =
195 service->extensions()->GetExtensionOrAppByURL(url);
196 return extension ?
197 base::UTF8ToUTF16(extension->name()) : base::UTF8ToUTF16(url.host());
200 if (url.SchemeIsHTTPOrHTTPS() || url.SchemeIs(url::kFtpScheme)) {
201 // See ToolbarModelImpl::GetText(). Does not pay attention to any user
202 // edits, and uses GetURL/net::FormatUrl -- We don't really care about
203 // length or the autocomplete parser.
204 // TODO(gbillock): This uses an algorithm very similar to GetText, which
205 // is probably too conservative. Try out just using a simpler mechanism of
206 // StripWWW() and IDNToUnicode().
207 std::string languages;
208 if (profile)
209 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
211 // TODO(macourteau): Extract the bits we care about from FormatUrl to
212 // format |url.host()| instead of this.
213 base::string16 formatted = net::FormatUrl(url.GetOrigin(), languages,
214 net::kFormatUrlOmitAll, net::UnescapeRule::NORMAL, NULL, NULL, NULL);
215 // Remove scheme, "www.", and trailing "/".
216 if (StartsWith(formatted, base::ASCIIToUTF16("http://"), false))
217 formatted = formatted.substr(7);
218 else if (StartsWith(formatted, base::ASCIIToUTF16("https://"), false))
219 formatted = formatted.substr(8);
220 else if (StartsWith(formatted, base::ASCIIToUTF16("ftp://"), false))
221 formatted = formatted.substr(6);
222 if (StartsWith(formatted, base::ASCIIToUTF16("www."), false))
223 formatted = formatted.substr(4);
224 if (EndsWith(formatted, base::ASCIIToUTF16("/"), false))
225 formatted = formatted.substr(0, formatted.size() - 1);
226 return formatted;
229 // These internal-ish debugging-style schemes we don't expect users
230 // to see. In these cases, the site chip will display the first
231 // part of the full URL.
232 if (url.SchemeIs(chrome::kChromeNativeScheme) ||
233 url.SchemeIs(url::kBlobScheme) ||
234 url.SchemeIs(content::kChromeDevToolsScheme) ||
235 url.SchemeIs(url::kDataScheme) ||
236 url.SchemeIs(url::kFileScheme) ||
237 url.SchemeIs(url::kFileSystemScheme) ||
238 url.SchemeIs(content::kGuestScheme) ||
239 url.SchemeIs(url::kJavaScriptScheme) ||
240 url.SchemeIs(url::kMailToScheme) ||
241 url.SchemeIs(content::kMetadataScheme) ||
242 url.SchemeIs(content::kSwappedOutScheme)) {
243 std::string truncated_url;
244 base::TruncateUTF8ToByteSize(url.spec(), 1000, &truncated_url);
245 return base::UTF8ToUTF16(truncated_url);
248 #if defined(OS_CHROMEOS)
249 if (url.SchemeIs(chrome::kCrosScheme) ||
250 url.SchemeIs(chrome::kDriveScheme))
251 return base::UTF8ToUTF16(url.spec());
252 #endif
254 // If all else fails, return the hostname.
255 return base::UTF8ToUTF16(url.host());