Chromium Code Reviews| Index: chrome/browser/ui/passwords/manage_passwords_view_utils.cc |
| diff --git a/chrome/browser/ui/passwords/manage_passwords_view_utils.cc b/chrome/browser/ui/passwords/manage_passwords_view_utils.cc |
| index 880fada39d96842d37d119df6c7378cb41e82d5d..6973342cc8f2038190e475168f7293768d3baa87 100644 |
| --- a/chrome/browser/ui/passwords/manage_passwords_view_utils.cc |
| +++ b/chrome/browser/ui/passwords/manage_passwords_view_utils.cc |
| @@ -4,14 +4,57 @@ |
| #include "chrome/browser/ui/passwords/manage_passwords_view_utils.h" |
| +#include "base/prefs/pref_service.h" |
| #include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chrome/grit/chromium_strings.h" |
| +#include "chrome/grit/generated_resources.h" |
| #include "components/password_manager/core/browser/affiliation_utils.h" |
| +#include "content/public/browser/web_contents.h" |
| #include "net/base/net_util.h" |
| +#include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| #include "ui/gfx/geometry/rect.h" |
| #include "ui/gfx/geometry/size.h" |
| #include "ui/gfx/image/image_skia.h" |
| #include "ui/gfx/image/image_skia_operations.h" |
| +#include "ui/gfx/range/range.h" |
| #include "url/gurl.h" |
| +#include "url/url_constants.h" |
| + |
| +namespace { |
| + |
| +// Helper function to get the domain name for the |url|. |
| +base::string16 GetDomainNameForURL(const GURL& url, |
| + const std::string& languages) { |
| + if (!url.is_valid() || url.is_empty() || !url.IsStandard()) |
| + return net::FormatUrl(url, languages); |
| + |
| + base::string16 url_host, url_domain, url_subdomain; |
| + |
| + // Get Host. |
| + url_host = base::UTF8ToUTF16(url.host()); |
| + |
| + // Get domain and registry information from the URL. |
| + url_domain = |
| + base::UTF8ToUTF16(net::registry_controlled_domains::GetDomainAndRegistry( |
| + url, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)); |
| + if (url_domain.empty()) |
| + url_domain = url_host; |
| + |
| + // Get sub domain. |
| + const size_t domain_start_index = url_host.find(url_domain); |
| + base::string16 kWwwPrefix = base::UTF8ToUTF16("www."); |
| + if (domain_start_index != base::string16::npos) |
| + url_subdomain = url_host.substr(0, domain_start_index); |
| + if (url_subdomain == kWwwPrefix) |
| + url_subdomain.clear(); |
| + |
| + return (url_subdomain.empty()) ? url_domain : url_host; |
| +} |
| + |
| +} // namespace |
| const int kAvatarImageSize = 50; |
| @@ -28,3 +71,47 @@ gfx::ImageSkia ScaleImageForAccountAvatar(gfx::ImageSkia skia_image) { |
| skia::ImageOperations::RESIZE_BEST, |
| gfx::Size(kAvatarImageSize, kAvatarImageSize)); |
| } |
| + |
| +void GetSavePasswordTitleTextAndLinkRange( |
| + const content::WebContents* web_contents, |
| + const GURL& form_origin, |
| + bool is_smartlock_branding_enabled, |
| + base::string16* title, |
| + 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.
|
| + int title_id = IDS_SAVE_PASSWORD; |
| + std::vector<size_t> offsets; |
| + std::vector<base::string16> replacements; |
| + |
| + // For unit tests, |web_contents| can be null. Fallback to "this site" prompt. |
| + if (web_contents) { |
| + Profile* profile = |
| + Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| + std::string languages( |
| + profile->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
| + base::string16 visible_host( |
| + GetDomainNameForURL(web_contents->GetVisibleURL(), languages)); |
| + base::string16 form_host(GetDomainNameForURL(form_origin, languages)); |
| + title_id = (!visible_host.empty() && !form_host.empty() && |
| + visible_host != form_host) |
| + ? IDS_SAVE_PASSWORD_TITLE |
| + : IDS_SAVE_PASSWORD; |
| + |
| + if (title_id == IDS_SAVE_PASSWORD_TITLE) |
| + replacements.push_back(form_host); |
| + } |
| + |
| + if (is_smartlock_branding_enabled) { |
| + // "Google Smart Lock" should be a hyperlink. |
| + base::string16 title_link = |
| + l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SMART_LOCK); |
| + replacements.insert(replacements.begin(), title_link); |
| + *title = l10n_util::GetStringFUTF16(title_id, replacements, &offsets); |
| + *title_link_range = |
| + gfx::Range(offsets[0], offsets[0] + title_link.length()); |
| + } else { |
| + replacements.insert( |
| + replacements.begin(), |
| + l10n_util::GetStringUTF16(IDS_SAVE_PASSWORD_TITLE_BRAND)); |
| + *title = l10n_util::GetStringFUTF16(title_id, replacements, &offsets); |
| + } |
| +} |