Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/passwords/manage_passwords_view_utils.h" | 5 #include "chrome/browser/ui/passwords/manage_passwords_view_utils.h" |
| 6 | 6 |
| 7 #include "base/prefs/pref_service.h" | |
| 7 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/common/pref_names.h" | |
| 11 #include "chrome/grit/chromium_strings.h" | |
| 12 #include "chrome/grit/generated_resources.h" | |
| 8 #include "components/password_manager/core/browser/affiliation_utils.h" | 13 #include "components/password_manager/core/browser/affiliation_utils.h" |
| 14 #include "content/public/browser/web_contents.h" | |
| 9 #include "net/base/net_util.h" | 15 #include "net/base/net_util.h" |
| 16 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | |
| 17 #include "ui/base/l10n/l10n_util.h" | |
| 10 #include "ui/gfx/geometry/rect.h" | 18 #include "ui/gfx/geometry/rect.h" |
| 11 #include "ui/gfx/geometry/size.h" | 19 #include "ui/gfx/geometry/size.h" |
| 12 #include "ui/gfx/image/image_skia.h" | 20 #include "ui/gfx/image/image_skia.h" |
| 13 #include "ui/gfx/image/image_skia_operations.h" | 21 #include "ui/gfx/image/image_skia_operations.h" |
| 22 #include "ui/gfx/range/range.h" | |
| 14 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 24 #include "url/url_constants.h" | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 // Helper function to get the domain name for the |url|. | |
| 29 base::string16 GetDomainNameForURL(const GURL& url, | |
| 30 const std::string& languages) { | |
| 31 if (!url.is_valid() || url.is_empty() || !url.IsStandard()) | |
| 32 return net::FormatUrl(url, languages); | |
| 33 | |
| 34 base::string16 url_host, url_domain, url_subdomain; | |
| 35 | |
| 36 // Get Host. | |
| 37 url_host = base::UTF8ToUTF16(url.host()); | |
| 38 | |
| 39 // Get domain and registry information from the URL. | |
| 40 url_domain = | |
| 41 base::UTF8ToUTF16(net::registry_controlled_domains::GetDomainAndRegistry( | |
| 42 url, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)); | |
| 43 if (url_domain.empty()) | |
| 44 url_domain = url_host; | |
| 45 | |
| 46 // Get sub domain. | |
| 47 const size_t domain_start_index = url_host.find(url_domain); | |
| 48 base::string16 kWwwPrefix = base::UTF8ToUTF16("www."); | |
| 49 if (domain_start_index != base::string16::npos) | |
| 50 url_subdomain = url_host.substr(0, domain_start_index); | |
| 51 if (url_subdomain == kWwwPrefix) | |
| 52 url_subdomain.clear(); | |
| 53 | |
| 54 return (url_subdomain.empty()) ? url_domain : url_host; | |
| 55 } | |
| 56 | |
| 57 } // namespace | |
| 15 | 58 |
| 16 const int kAvatarImageSize = 50; | 59 const int kAvatarImageSize = 50; |
| 17 | 60 |
| 18 gfx::ImageSkia ScaleImageForAccountAvatar(gfx::ImageSkia skia_image) { | 61 gfx::ImageSkia ScaleImageForAccountAvatar(gfx::ImageSkia skia_image) { |
| 19 gfx::Size size = skia_image.size(); | 62 gfx::Size size = skia_image.size(); |
| 20 if (size.height() != size.width()) { | 63 if (size.height() != size.width()) { |
| 21 gfx::Rect target(size); | 64 gfx::Rect target(size); |
| 22 int side = std::min(size.height(), size.width()); | 65 int side = std::min(size.height(), size.width()); |
| 23 target.ClampToCenteredSize(gfx::Size(side, side)); | 66 target.ClampToCenteredSize(gfx::Size(side, side)); |
| 24 skia_image = gfx::ImageSkiaOperations::ExtractSubset(skia_image, target); | 67 skia_image = gfx::ImageSkiaOperations::ExtractSubset(skia_image, target); |
| 25 } | 68 } |
| 26 return gfx::ImageSkiaOperations::CreateResizedImage( | 69 return gfx::ImageSkiaOperations::CreateResizedImage( |
| 27 skia_image, | 70 skia_image, |
| 28 skia::ImageOperations::RESIZE_BEST, | 71 skia::ImageOperations::RESIZE_BEST, |
| 29 gfx::Size(kAvatarImageSize, kAvatarImageSize)); | 72 gfx::Size(kAvatarImageSize, kAvatarImageSize)); |
| 30 } | 73 } |
| 74 | |
| 75 void GetSavePasswordTitleTextAndLinkRange( | |
| 76 const content::WebContents* web_contents, | |
| 77 const GURL& form_origin, | |
| 78 bool is_smartlock_branding_enabled, | |
| 79 base::string16* title, | |
| 80 gfx::Range* title_link_range) { | |
|
vivekg
2015/06/23 14:38:01
Should we have CHECK(web_contents) here?
Pritam Nikam
2015/06/25 08:59:14
Ditto.
| |
| 81 int title_id = IDS_SAVE_PASSWORD; | |
| 82 std::vector<size_t> offsets; | |
| 83 std::vector<base::string16> replacements; | |
| 84 | |
| 85 // For unit tests, |web_contents| can be null. Fallback to "this site" prompt. | |
| 86 if (web_contents) { | |
| 87 Profile* profile = | |
| 88 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 89 std::string languages( | |
| 90 profile->GetPrefs()->GetString(prefs::kAcceptLanguages)); | |
| 91 base::string16 visible_host( | |
| 92 GetDomainNameForURL(web_contents->GetVisibleURL(), languages)); | |
| 93 base::string16 form_host(GetDomainNameForURL(form_origin, languages)); | |
| 94 title_id = (!visible_host.empty() && !form_host.empty() && | |
| 95 visible_host != form_host) | |
| 96 ? IDS_SAVE_PASSWORD_TITLE | |
| 97 : IDS_SAVE_PASSWORD; | |
| 98 | |
| 99 if (title_id == IDS_SAVE_PASSWORD_TITLE) | |
| 100 replacements.push_back(form_host); | |
| 101 } | |
| 102 | |
| 103 if (is_smartlock_branding_enabled) { | |
| 104 // "Google Smart Lock" should be a hyperlink. | |
| 105 base::string16 title_link = | |
| 106 l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SMART_LOCK); | |
| 107 replacements.insert(replacements.begin(), title_link); | |
| 108 *title = l10n_util::GetStringFUTF16(title_id, replacements, &offsets); | |
| 109 *title_link_range = | |
| 110 gfx::Range(offsets[0], offsets[0] + title_link.length()); | |
| 111 } else { | |
| 112 replacements.insert( | |
| 113 replacements.begin(), | |
| 114 l10n_util::GetStringUTF16(IDS_SAVE_PASSWORD_TITLE_BRAND)); | |
| 115 *title = l10n_util::GetStringFUTF16(title_id, replacements, &offsets); | |
| 116 } | |
| 117 } | |
| OLD | NEW |