File: origin_chip_info.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (265 lines) | stat: -rw-r--r-- 9,981 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/location_bar/origin_chip_info.h"

#include <string>

#include "base/prefs/pref_service.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/safe_browsing/client_side_detection_host.h"
#include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h"
#include "chrome/browser/ui/toolbar/toolbar_model.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_icon_image.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/constants.h"
#include "extensions/common/manifest_handlers/icons_handler.h"
#include "grit/components_strings.h"
#include "grit/theme_resources.h"
#include "net/base/net_util.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"

namespace {

// For selected kChromeUIScheme and url::kAboutScheme, return the string
// resource
// number for the title of the page. If we don't have a specialized title,
// returns -1.
int StringForChromeHost(const GURL& url) {
  DCHECK(url.is_empty() || url.SchemeIs(content::kChromeUIScheme));

  if (url.is_empty())
    return IDS_NEW_TAB_TITLE;

  // TODO(gbillock): Just get the page title and special case exceptions?
  std::string host = url.host();
  if (host == chrome::kChromeUIAppLauncherPageHost)
    return IDS_APP_DEFAULT_PAGE_NAME;
  if (host == chrome::kChromeUIBookmarksHost)
    return IDS_BOOKMARK_MANAGER_TITLE;
  if (host == chrome::kChromeUIComponentsHost)
    return IDS_COMPONENTS_TITLE;
  if (host == chrome::kChromeUICrashesHost)
    return IDS_CRASHES_TITLE;
#if defined(ENABLE_SERVICE_DISCOVERY)
  if (host == chrome::kChromeUIDevicesHost)
    return IDS_LOCAL_DISCOVERY_DEVICES_PAGE_TITLE;
#endif  // ENABLE_SERVICE_DISCOVERY
  if (host == chrome::kChromeUIDownloadsHost)
    return IDS_DOWNLOAD_TITLE;
  if (host == chrome::kChromeUIExtensionsHost)
    return IDS_MANAGE_EXTENSIONS_SETTING_WINDOWS_TITLE;
  if (host == chrome::kChromeUIHelpHost)
    return IDS_ABOUT_TITLE;
  if (host == chrome::kChromeUIHistoryHost)
    return IDS_HISTORY_TITLE;
  if (host == chrome::kChromeUINewTabHost)
    return IDS_NEW_TAB_TITLE;
  if (host == chrome::kChromeUIPluginsHost)
    return IDS_PLUGINS_TITLE;
  if (host == chrome::kChromeUIPolicyHost)
    return IDS_POLICY_TITLE;
#if defined(ENABLE_PRINT_PREVIEW)
  if (host == chrome::kChromeUIPrintHost)
    return IDS_PRINT_PREVIEW_TITLE;
#endif  // ENABLE_PRINT_PREVIEW
  if (host == chrome::kChromeUISettingsHost)
    return IDS_SETTINGS_TITLE;
  if (host == chrome::kChromeUIVersionHost)
    return IDS_ABOUT_VERSION_TITLE;

  return -1;
}

}  // namespace

OriginChipInfo::OriginChipInfo(extensions::IconImage::Observer* owner,
                               Profile* profile)
    : owner_(owner),
      profile_(profile),
      security_level_(ToolbarModel::NONE),
      is_url_malware_(false),
      icon_(IDR_PRODUCT_LOGO_16) {
}

OriginChipInfo::~OriginChipInfo() {
}

bool OriginChipInfo::Update(const content::WebContents* web_contents,
                            const ToolbarModel* toolbar_model) {
  GURL displayed_url = toolbar_model->GetURL();
  ToolbarModel::SecurityLevel security_level =
      toolbar_model->GetSecurityLevel(true);
  bool is_url_malware = OriginChip::IsMalware(displayed_url, web_contents);

  if ((displayed_url_ == displayed_url) &&
      (security_level_ == security_level) &&
      (is_url_malware_ == is_url_malware))
    return false;

  displayed_url_ = displayed_url;
  security_level_ = security_level;
  is_url_malware_ = is_url_malware;

  label_ = OriginChip::LabelFromURLForProfile(displayed_url, profile_);
  if (security_level_ == ToolbarModel::EV_SECURE) {
    label_ = l10n_util::GetStringFUTF16(IDS_SITE_CHIP_EV_SSL_LABEL,
                                        toolbar_model->GetEVCertName(),
                                        label_);
  }


  if (displayed_url_.SchemeIs(extensions::kExtensionScheme)) {
    const extensions::Extension* extension =
        extensions::ExtensionRegistry::Get(profile_)
            ->enabled_extensions()
            .GetByID(displayed_url_.host());

    if (extension) {
      icon_ = IDR_EXTENSIONS_FAVICON;
      extension_icon_image_.reset(
          new extensions::IconImage(profile_,
                                    extension,
                                    extensions::IconsInfo::GetIcons(extension),
                                    extension_misc::EXTENSION_ICON_BITTY,
                                    extensions::util::GetDefaultAppIcon(),
                                    owner_));

      // Forces load of the image.
      extension_icon_image_->image_skia().GetRepresentation(1.0f);
      if (!extension_icon_image_->image_skia().image_reps().empty())
        owner_->OnExtensionIconImageChanged(extension_icon_image_.get());

      return true;
    }
  }

  if (extension_icon_image_) {
    extension_icon_image_.reset();
    owner_->OnExtensionIconImageChanged(NULL);
  }

  icon_ = (displayed_url_.is_empty() ||
           displayed_url_.SchemeIs(content::kChromeUIScheme)) ?
      IDR_PRODUCT_LOGO_16 :
      toolbar_model->GetIconForSecurityLevel(security_level_);

  return true;
}

base::string16 OriginChipInfo::Tooltip() const {
  return base::UTF8ToUTF16(displayed_url_.spec());
}

// static
bool OriginChip::IsMalware(const GURL& url, const content::WebContents* tab) {
  DCHECK(tab);

  if (tab->GetURL() != url)
    return false;

  const safe_browsing::SafeBrowsingTabObserver* sb_observer =
      safe_browsing::SafeBrowsingTabObserver::FromWebContents(tab);
  return sb_observer && sb_observer->DidPageReceiveSafeBrowsingMatch();
}

// static
base::string16 OriginChip::LabelFromURLForProfile(const GURL& provided_url,
                                                  Profile* profile) {
  // First, strip view-source: if it appears.  Note that GetContent removes
  // "view-source:" but leaves the original scheme (http, https, ftp, etc).
  GURL url(provided_url);
  if (url.SchemeIs(content::kViewSourceScheme))
    url = GURL(url.GetContent());

  // About scheme pages. Currently all about: URLs other than about:blank
  // redirect to chrome: URLs, so this only affects about:blank.
  if (url.SchemeIs(url::kAboutScheme))
    return base::UTF8ToUTF16(url.spec());

  // Chrome built-in pages.
  if (url.is_empty() || url.SchemeIs(content::kChromeUIScheme)) {
    int string_ref = StringForChromeHost(url);
    return l10n_util::GetStringUTF16(
        (string_ref == -1) ? IDS_SHORT_PRODUCT_NAME : string_ref);
  }

  // For chrome-extension URLs, return the extension name.
  if (url.SchemeIs(extensions::kExtensionScheme)) {
    const extensions::Extension* extension =
        extensions::ExtensionRegistry::Get(profile)
            ->enabled_extensions()
            .GetByID(url.host());
    return extension ?
        base::UTF8ToUTF16(extension->name()) : base::UTF8ToUTF16(url.host());
  }

  if (url.SchemeIsHTTPOrHTTPS() || url.SchemeIs(url::kFtpScheme)) {
    // See ToolbarModelImpl::GetText(). Does not pay attention to any user
    // edits, and uses GetURL/net::FormatUrl -- We don't really care about
    // length or the autocomplete parser.
    // TODO(gbillock): This uses an algorithm very similar to GetText, which
    // is probably too conservative. Try out just using a simpler mechanism of
    // StripWWW() and IDNToUnicode().
    std::string languages;
    if (profile)
      languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages);

    // TODO(macourteau): Extract the bits we care about from FormatUrl to
    // format |url.host()| instead of this.
    base::string16 formatted = net::FormatUrl(url.GetOrigin(), languages,
        net::kFormatUrlOmitAll, net::UnescapeRule::NORMAL, NULL, NULL, NULL);
    // Remove scheme, "www.", and trailing "/".
    if (StartsWith(formatted, base::ASCIIToUTF16("http://"), false))
      formatted = formatted.substr(7);
    else if (StartsWith(formatted, base::ASCIIToUTF16("https://"), false))
      formatted = formatted.substr(8);
    else if (StartsWith(formatted, base::ASCIIToUTF16("ftp://"), false))
      formatted = formatted.substr(6);
    if (StartsWith(formatted, base::ASCIIToUTF16("www."), false))
      formatted = formatted.substr(4);
    if (EndsWith(formatted, base::ASCIIToUTF16("/"), false))
      formatted = formatted.substr(0, formatted.size() - 1);
    return formatted;
  }

  // These internal-ish debugging-style schemes we don't expect users
  // to see. In these cases, the site chip will display the first
  // part of the full URL.
  if (url.SchemeIs(chrome::kChromeNativeScheme) ||
      url.SchemeIs(url::kBlobScheme) ||
      url.SchemeIs(content::kChromeDevToolsScheme) ||
      url.SchemeIs(url::kDataScheme) ||
      url.SchemeIs(url::kFileScheme) ||
      url.SchemeIs(url::kFileSystemScheme) ||
      url.SchemeIs(content::kGuestScheme) ||
      url.SchemeIs(url::kJavaScriptScheme) ||
      url.SchemeIs(url::kMailToScheme) ||
      url.SchemeIs(content::kMetadataScheme) ||
      url.SchemeIs(content::kSwappedOutScheme)) {
    std::string truncated_url;
    base::TruncateUTF8ToByteSize(url.spec(), 1000, &truncated_url);
    return base::UTF8ToUTF16(truncated_url);
  }

#if defined(OS_CHROMEOS)
  if (url.SchemeIs(chrome::kCrosScheme) ||
      url.SchemeIs(content::kExternalFileScheme)) {
    return base::UTF8ToUTF16(url.spec());
  }
#endif

  // If all else fails, return the hostname.
  return base::UTF8ToUTF16(url.host());
}