Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / ui / passwords / manage_passwords_view_utils.cc
blobc230eaaf15febf317ed2195329bff0880748290a
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"
20 #include "url/gurl.h"
22 namespace {
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(
27 gurl1, gurl2,
28 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
31 } // namespace
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(
44 skia_image,
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;
58 int title_id =
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 replacements.push_back(url_formatter::FormatUrlForSecurityDisplay(
67 form_origin_url, std::string()));
70 if (is_smartlock_branding_enabled) {
71 // "Google Smart Lock" should be a hyperlink.
72 base::string16 title_link =
73 l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SMART_LOCK);
74 replacements.insert(replacements.begin(), title_link);
75 *title = l10n_util::GetStringFUTF16(title_id, replacements, &offsets);
76 *title_link_range =
77 gfx::Range(offsets[0], offsets[0] + title_link.length());
78 } else {
79 replacements.insert(
80 replacements.begin(),
81 l10n_util::GetStringUTF16(IDS_SAVE_PASSWORD_TITLE_BRAND));
82 *title = l10n_util::GetStringFUTF16(title_id, replacements, &offsets);
86 void GetManagePasswordsDialogTitleText(const GURL& user_visible_url,
87 const GURL& password_origin_url,
88 base::string16* title) {
89 // Check whether the registry controlled domains for user-visible URL
90 // (i.e. the one seen in the omnibox) and the managed password origin URL
91 // differ or not.
92 if (!SameDomainOrHost(user_visible_url, password_origin_url)) {
93 base::string16 formatted_url = url_formatter::FormatUrlForSecurityDisplay(
94 password_origin_url, std::string());
95 *title = l10n_util::GetStringFUTF16(
96 IDS_MANAGE_PASSWORDS_TITLE_DIFFERENT_DOMAIN, formatted_url);
97 } else {
98 *title = l10n_util::GetStringUTF16(IDS_MANAGE_PASSWORDS_TITLE);
102 void GetAccountChooserDialogTitleTextAndLinkRange(
103 bool is_smartlock_branding_enabled,
104 base::string16* title,
105 gfx::Range* title_link_range) {
106 if (is_smartlock_branding_enabled) {
107 size_t offset;
108 base::string16 title_link =
109 l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SMART_LOCK);
110 *title = l10n_util::GetStringFUTF16(
111 IDS_PASSWORD_MANAGER_ACCOUNT_CHOOSER_TITLE_SMART_LOCK, title_link,
112 &offset);
113 *title_link_range = gfx::Range(offset, offset + title_link.length());
114 } else {
115 *title =
116 l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_ACCOUNT_CHOOSER_TITLE);