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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/passwords/manage_passwords_view_utils.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/grit/chromium_strings.h"
9 #include "chrome/grit/generated_resources.h"
10 #include "components/password_manager/core/browser/affiliation_utils.h"
11 #include "components/url_formatter/elide_url.h"
12 #include "net/base/net_util.h"
13 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/geometry/size.h"
17 #include "ui/gfx/image/image_skia.h"
18 #include "ui/gfx/image/image_skia_operations.h"
19 #include "ui/gfx/range/range.h"
24 // Checks whether two URLs are from the same domain or host.
25 bool SameDomainOrHost(const GURL
& gurl1
, const GURL
& gurl2
) {
26 return net::registry_controlled_domains::SameDomainOrHost(
28 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
);
33 const int kAvatarImageSize
= 40;
35 gfx::ImageSkia
ScaleImageForAccountAvatar(gfx::ImageSkia skia_image
) {
36 gfx::Size size
= skia_image
.size();
37 if (size
.height() != size
.width()) {
38 gfx::Rect
target(size
);
39 int side
= std::min(size
.height(), size
.width());
40 target
.ClampToCenteredSize(gfx::Size(side
, side
));
41 skia_image
= gfx::ImageSkiaOperations::ExtractSubset(skia_image
, target
);
43 return gfx::ImageSkiaOperations::CreateResizedImage(
45 skia::ImageOperations::RESIZE_BEST
,
46 gfx::Size(kAvatarImageSize
, kAvatarImageSize
));
49 void GetSavePasswordDialogTitleTextAndLinkRange(
50 const GURL
& user_visible_url
,
51 const GURL
& form_origin_url
,
52 bool is_smartlock_branding_enabled
,
53 bool is_update_password_bubble
,
54 base::string16
* title
,
55 gfx::Range
* title_link_range
) {
56 std::vector
<size_t> offsets
;
57 std::vector
<base::string16
> replacements
;
59 is_update_password_bubble
? IDS_UPDATE_PASSWORD
: IDS_SAVE_PASSWORD
;
61 // Check whether the registry controlled domains for user-visible URL (i.e.
62 // the one seen in the omnibox) and the password form post-submit navigation
63 // URL differs or not.
64 if (!SameDomainOrHost(user_visible_url
, form_origin_url
)) {
65 title_id
= IDS_SAVE_PASSWORD_TITLE
;
66 // TODO(palmer): Look into passing real language prefs here, not "".
68 replacements
.push_back(url_formatter::FormatUrlForSecurityDisplay(
69 form_origin_url
, std::string()));
72 if (is_smartlock_branding_enabled
) {
73 // "Google Smart Lock" should be a hyperlink.
74 base::string16 title_link
=
75 l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SMART_LOCK
);
76 replacements
.insert(replacements
.begin(), title_link
);
77 *title
= l10n_util::GetStringFUTF16(title_id
, replacements
, &offsets
);
79 gfx::Range(offsets
[0], offsets
[0] + title_link
.length());
83 l10n_util::GetStringUTF16(IDS_SAVE_PASSWORD_TITLE_BRAND
));
84 *title
= l10n_util::GetStringFUTF16(title_id
, replacements
, &offsets
);
88 void GetManagePasswordsDialogTitleText(const GURL
& user_visible_url
,
89 const GURL
& password_origin_url
,
90 base::string16
* title
) {
91 // Check whether the registry controlled domains for user-visible URL
92 // (i.e. the one seen in the omnibox) and the managed password origin URL
94 if (!SameDomainOrHost(user_visible_url
, password_origin_url
)) {
95 // TODO(palmer): Look into passing real language prefs here, not "".
96 base::string16 formatted_url
= url_formatter::FormatUrlForSecurityDisplay(
97 password_origin_url
, std::string());
98 *title
= l10n_util::GetStringFUTF16(
99 IDS_MANAGE_PASSWORDS_TITLE_DIFFERENT_DOMAIN
, formatted_url
);
101 *title
= l10n_util::GetStringUTF16(IDS_MANAGE_PASSWORDS_TITLE
);
105 void GetAccountChooserDialogTitleTextAndLinkRange(
106 bool is_smartlock_branding_enabled
,
107 base::string16
* title
,
108 gfx::Range
* title_link_range
) {
109 if (is_smartlock_branding_enabled
) {
111 base::string16 title_link
=
112 l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SMART_LOCK
);
113 *title
= l10n_util::GetStringFUTF16(
114 IDS_PASSWORD_MANAGER_ACCOUNT_CHOOSER_TITLE_SMART_LOCK
, title_link
,
116 *title_link_range
= gfx::Range(offset
, offset
+ title_link
.length());
119 l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_ACCOUNT_CHOOSER_TITLE
);