Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / toolbar / origin_chip.cc
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.
4
5 #include "chrome/browser/ui/toolbar/origin_chip.h"
6
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_service.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/safe_browsing/client_side_detection_host.h"
13 #include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/common/url_constants.h"
16 #include "content/public/browser/web_contents.h"
17 #include "extensions/browser/extension_system.h"
18 #include "extensions/common/constants.h"
19 #include "grit/component_strings.h"
20 #include "grit/generated_resources.h"
21 #include "net/base/net_util.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "url/gurl.h"
24
25 namespace {
26
27 // For selected kChromeUIScheme and kAboutScheme, return the string resource
28 // number for the title of the page. If we don't have a specialized title,
29 // returns -1.
30 int StringForChromeHost(const GURL& url) {
31   DCHECK(url.is_empty() || url.SchemeIs(content::kChromeUIScheme));
32
33   if (url.is_empty())
34     return IDS_NEW_TAB_TITLE;
35
36   // TODO(gbillock): Just get the page title and special case exceptions?
37   std::string host = url.host();
38   if (host == chrome::kChromeUIAppLauncherPageHost)
39     return IDS_APP_DEFAULT_PAGE_NAME;
40   if (host == chrome::kChromeUIBookmarksHost)
41     return IDS_BOOKMARK_MANAGER_TITLE;
42   if (host == chrome::kChromeUIComponentsHost)
43     return IDS_COMPONENTS_TITLE;
44   if (host == chrome::kChromeUICrashesHost)
45     return IDS_CRASHES_TITLE;
46 #if defined(ENABLE_MDNS)
47   if (host == chrome::kChromeUIDevicesHost)
48     return IDS_LOCAL_DISCOVERY_DEVICES_PAGE_TITLE;
49 #endif  // ENABLE_MDNS
50   if (host == chrome::kChromeUIDownloadsHost)
51     return IDS_DOWNLOAD_TITLE;
52   if (host == chrome::kChromeUIExtensionsHost)
53     return IDS_MANAGE_EXTENSIONS_SETTING_WINDOWS_TITLE;
54   if (host == chrome::kChromeUIHelpHost)
55     return IDS_ABOUT_TAB_TITLE;
56   if (host == chrome::kChromeUIHistoryHost)
57     return IDS_HISTORY_TITLE;
58   if (host == chrome::kChromeUINewTabHost)
59     return IDS_NEW_TAB_TITLE;
60   if (host == chrome::kChromeUIPluginsHost)
61     return IDS_PLUGINS_TITLE;
62   if (host == chrome::kChromeUIPolicyHost)
63     return IDS_POLICY_TITLE;
64   if (host == chrome::kChromeUIPrintHost)
65     return IDS_PRINT_PREVIEW_TITLE;
66   if (host == chrome::kChromeUISettingsHost)
67     return IDS_SETTINGS_TITLE;
68   if (host == chrome::kChromeUIVersionHost)
69     return IDS_ABOUT_VERSION_TITLE;
70
71   return -1;
72 }
73
74 }  // namespace
75
76 // static
77 bool OriginChip::IsMalware(const GURL& url, content::WebContents* tab) {
78   if (tab->GetURL() != url)
79     return false;
80
81   safe_browsing::SafeBrowsingTabObserver* sb_observer =
82       safe_browsing::SafeBrowsingTabObserver::FromWebContents(tab);
83   return sb_observer && sb_observer->detection_host() &&
84       sb_observer->detection_host()->DidPageReceiveSafeBrowsingMatch();
85 }
86
87 // static
88 base::string16 OriginChip::LabelFromURLForProfile(const GURL& provided_url,
89                                                   Profile* profile) {
90   // First, strip view-source: if it appears.  Note that GetContent removes
91   // "view-source:" but leaves the original scheme (http, https, ftp, etc).
92   GURL url(provided_url);
93   if (url.SchemeIs(content::kViewSourceScheme))
94     url = GURL(url.GetContent());
95
96   // About scheme pages. Currently all about: URLs other than about:blank
97   // redirect to chrome: URLs, so this only affects about:blank.
98   if (url.SchemeIs(chrome::kAboutScheme))
99     return base::UTF8ToUTF16(url.spec());
100
101   // Chrome built-in pages.
102   if (url.is_empty() || url.SchemeIs(content::kChromeUIScheme)) {
103     int string_ref = StringForChromeHost(url);
104     return (string_ref == -1) ?
105         base::UTF8ToUTF16("Chrome") :
106         l10n_util::GetStringUTF16(string_ref);
107   }
108
109   // For chrome-extension URLs, return the extension name.
110   if (url.SchemeIs(extensions::kExtensionScheme)) {
111     ExtensionService* service =
112         extensions::ExtensionSystem::Get(profile)->extension_service();
113     const extensions::Extension* extension =
114         service->extensions()->GetExtensionOrAppByURL(url);
115     return extension ?
116         base::UTF8ToUTF16(extension->name()) : base::UTF8ToUTF16(url.host());
117   }
118
119   if (url.SchemeIsHTTPOrHTTPS() || url.SchemeIs(content::kFtpScheme)) {
120     // See ToolbarModelImpl::GetText(). Does not pay attention to any user
121     // edits, and uses GetURL/net::FormatUrl -- We don't really care about
122     // length or the autocomplete parser.
123     // TODO(gbillock): This uses an algorithm very similar to GetText, which
124     // is probably too conservative. Try out just using a simpler mechanism of
125     // StripWWW() and IDNToUnicode().
126     std::string languages;
127     if (profile)
128       languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
129
130     // TODO(macourteau): Extract the bits we care about from FormatUrl to
131     // format |url.host()| instead of this.
132     base::string16 formatted = net::FormatUrl(url.GetOrigin(), languages,
133         net::kFormatUrlOmitAll, net::UnescapeRule::NORMAL, NULL, NULL, NULL);
134     // Remove scheme, "www.", and trailing "/".
135     if (StartsWith(formatted, base::ASCIIToUTF16("http://"), false))
136       formatted = formatted.substr(7);
137     else if (StartsWith(formatted, base::ASCIIToUTF16("https://"), false))
138       formatted = formatted.substr(8);
139     else if (StartsWith(formatted, base::ASCIIToUTF16("ftp://"), false))
140       formatted = formatted.substr(6);
141     if (StartsWith(formatted, base::ASCIIToUTF16("www."), false))
142       formatted = formatted.substr(4);
143     if (EndsWith(formatted, base::ASCIIToUTF16("/"), false))
144       formatted = formatted.substr(0, formatted.size() - 1);
145     return formatted;
146   }
147
148   // These internal-ish debugging-style schemes we don't expect users
149   // to see. In these cases, the site chip will display the first
150   // part of the full URL.
151   if (url.SchemeIs(chrome::kBlobScheme) ||
152       url.SchemeIs(chrome::kChromeNativeScheme) ||
153       url.SchemeIs(content::kChromeDevToolsScheme) ||
154       url.SchemeIs(content::kDataScheme) ||
155       url.SchemeIs(content::kFileScheme) ||
156       url.SchemeIs(content::kFileSystemScheme) ||
157       url.SchemeIs(content::kGuestScheme) ||
158       url.SchemeIs(content::kJavaScriptScheme) ||
159       url.SchemeIs(content::kMailToScheme) ||
160       url.SchemeIs(content::kMetadataScheme) ||
161       url.SchemeIs(content::kSwappedOutScheme)) {
162     std::string truncated_url;
163     base::TruncateUTF8ToByteSize(url.spec(), 1000, &truncated_url);
164     return base::UTF8ToUTF16(truncated_url);
165   }
166
167 #if defined(OS_CHROMEOS)
168   if (url.SchemeIs(chrome::kCrosScheme) ||
169       url.SchemeIs(chrome::kDriveScheme))
170     return base::UTF8ToUTF16(url.spec());
171 #endif
172
173   // If all else fails, return the hostname.
174   return base::UTF8ToUTF16(url.host());
175 }