Refactor views app list services to allow more code sharing
[chromium-blink-merge.git] / chrome / browser / ui / toolbar / origin_chip_info.cc
blob3026af87c156fb6c0b6b6ed3a682f1513d9f54b0
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/component_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_);
114 if (displayed_url_.SchemeIs(extensions::kExtensionScheme)) {
115 icon_ = IDR_EXTENSIONS_FAVICON;
117 const extensions::Extension* extension =
118 extensions::ExtensionSystem::Get(profile_)->extension_service()->
119 extensions()->GetExtensionOrAppByURL(displayed_url_);
120 extension_icon_image_.reset(
121 new extensions::IconImage(profile_,
122 extension,
123 extensions::IconsInfo::GetIcons(extension),
124 extension_misc::EXTENSION_ICON_BITTY,
125 extensions::util::GetDefaultAppIcon(),
126 owner_));
128 // Forces load of the image.
129 extension_icon_image_->image_skia().GetRepresentation(1.0f);
130 if (!extension_icon_image_->image_skia().image_reps().empty())
131 owner_->OnExtensionIconImageChanged(extension_icon_image_.get());
132 } else {
133 if (extension_icon_image_) {
134 extension_icon_image_.reset();
135 owner_->OnExtensionIconImageChanged(NULL);
138 icon_ = (displayed_url_.is_empty() ||
139 displayed_url_.SchemeIs(content::kChromeUIScheme)) ?
140 IDR_PRODUCT_LOGO_16 :
141 toolbar_model->GetIconForSecurityLevel(security_level_);
144 return true;
147 // static
148 bool OriginChip::IsMalware(const GURL& url, const content::WebContents* tab) {
149 DCHECK(tab);
151 if (tab->GetURL() != url)
152 return false;
154 const safe_browsing::SafeBrowsingTabObserver* sb_observer =
155 safe_browsing::SafeBrowsingTabObserver::FromWebContents(tab);
156 return sb_observer && sb_observer->detection_host() &&
157 sb_observer->detection_host()->DidPageReceiveSafeBrowsingMatch();
160 // static
161 base::string16 OriginChip::LabelFromURLForProfile(const GURL& provided_url,
162 Profile* profile) {
163 // First, strip view-source: if it appears. Note that GetContent removes
164 // "view-source:" but leaves the original scheme (http, https, ftp, etc).
165 GURL url(provided_url);
166 if (url.SchemeIs(content::kViewSourceScheme))
167 url = GURL(url.GetContent());
169 // About scheme pages. Currently all about: URLs other than about:blank
170 // redirect to chrome: URLs, so this only affects about:blank.
171 if (url.SchemeIs(content::kAboutScheme))
172 return base::UTF8ToUTF16(url.spec());
174 // Chrome built-in pages.
175 if (url.is_empty() || url.SchemeIs(content::kChromeUIScheme)) {
176 int string_ref = StringForChromeHost(url);
177 return l10n_util::GetStringUTF16(
178 (string_ref == -1) ? IDS_SHORT_PRODUCT_NAME : string_ref);
181 // For chrome-extension URLs, return the extension name.
182 if (url.SchemeIs(extensions::kExtensionScheme)) {
183 ExtensionService* service =
184 extensions::ExtensionSystem::Get(profile)->extension_service();
185 const extensions::Extension* extension =
186 service->extensions()->GetExtensionOrAppByURL(url);
187 return extension ?
188 base::UTF8ToUTF16(extension->name()) : base::UTF8ToUTF16(url.host());
191 if (url.SchemeIsHTTPOrHTTPS() || url.SchemeIs(content::kFtpScheme)) {
192 // See ToolbarModelImpl::GetText(). Does not pay attention to any user
193 // edits, and uses GetURL/net::FormatUrl -- We don't really care about
194 // length or the autocomplete parser.
195 // TODO(gbillock): This uses an algorithm very similar to GetText, which
196 // is probably too conservative. Try out just using a simpler mechanism of
197 // StripWWW() and IDNToUnicode().
198 std::string languages;
199 if (profile)
200 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
202 // TODO(macourteau): Extract the bits we care about from FormatUrl to
203 // format |url.host()| instead of this.
204 base::string16 formatted = net::FormatUrl(url.GetOrigin(), languages,
205 net::kFormatUrlOmitAll, net::UnescapeRule::NORMAL, NULL, NULL, NULL);
206 // Remove scheme, "www.", and trailing "/".
207 if (StartsWith(formatted, base::ASCIIToUTF16("http://"), false))
208 formatted = formatted.substr(7);
209 else if (StartsWith(formatted, base::ASCIIToUTF16("https://"), false))
210 formatted = formatted.substr(8);
211 else if (StartsWith(formatted, base::ASCIIToUTF16("ftp://"), false))
212 formatted = formatted.substr(6);
213 if (StartsWith(formatted, base::ASCIIToUTF16("www."), false))
214 formatted = formatted.substr(4);
215 if (EndsWith(formatted, base::ASCIIToUTF16("/"), false))
216 formatted = formatted.substr(0, formatted.size() - 1);
217 return formatted;
220 // These internal-ish debugging-style schemes we don't expect users
221 // to see. In these cases, the site chip will display the first
222 // part of the full URL.
223 if (url.SchemeIs(chrome::kChromeNativeScheme) ||
224 url.SchemeIs(content::kBlobScheme) ||
225 url.SchemeIs(content::kChromeDevToolsScheme) ||
226 url.SchemeIs(content::kDataScheme) ||
227 url.SchemeIs(content::kFileScheme) ||
228 url.SchemeIs(content::kFileSystemScheme) ||
229 url.SchemeIs(content::kGuestScheme) ||
230 url.SchemeIs(content::kJavaScriptScheme) ||
231 url.SchemeIs(content::kMailToScheme) ||
232 url.SchemeIs(content::kMetadataScheme) ||
233 url.SchemeIs(content::kSwappedOutScheme)) {
234 std::string truncated_url;
235 base::TruncateUTF8ToByteSize(url.spec(), 1000, &truncated_url);
236 return base::UTF8ToUTF16(truncated_url);
239 #if defined(OS_CHROMEOS)
240 if (url.SchemeIs(chrome::kCrosScheme) ||
241 url.SchemeIs(chrome::kDriveScheme))
242 return base::UTF8ToUTF16(url.spec());
243 #endif
245 // If all else fails, return the hostname.
246 return base::UTF8ToUTF16(url.host());