File: upload_extension_to_account_dialog.cc

package info (click to toggle)
chromium 140.0.7339.127-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,201,772 kB
  • sloc: cpp: 35,093,800; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,798; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,119; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (87 lines) | stat: -rw-r--r-- 4,224 bytes parent folder | download | duplicates (2)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/functional/callback_helpers.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/extensions/sync/account_extension_tracker.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/extensions/extension_dialog_utils.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/grit/generated_resources.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/base/signin_switches.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "extensions/common/extension.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/bubble/bubble_dialog_model_host.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/view.h"

namespace extensions {

void ShowUploadExtensionToAccountDialog(Browser* browser,
                                        const Extension& extension,
                                        base::OnceClosure accept_callback,
                                        base::OnceClosure cancel_callback) {
  CHECK(switches::IsExtensionsExplicitBrowserSigninEnabled());
  CHECK(AccountExtensionTracker::Get(browser->profile())
            ->CanUploadAsAccountExtension(extension));

  auto split_cancel_callback =
      base::SplitOnceCallback(std::move(cancel_callback));

  ui::DialogModel::Builder builder;
  builder.SetInternalName("UploadExtensionToAccountDialog")
      .SetTitle(
          l10n_util::GetStringUTF16(IDS_UPLOAD_MOVE_TO_ACCOUNT_DIALOG_TITLE))
      .OverrideShowCloseButton(false)
      .SetSubtitle(l10n_util::GetStringFUTF16(
          IDS_EXTENSIONS_MOVE_TO_ACCOUNT_DIALOG_SUBTITLE,
          util::GetFixupExtensionNameForUIDisplay(extension.name())))
      .AddOkButton(
          std::move(accept_callback),
          ui::DialogModel::Button::Params().SetLabel(l10n_util::GetStringUTF16(
              IDS_EXTENSIONS_MOVE_TO_ACCOUNT_DIALOG_OK_BUTTON_LABEL)))
      .AddCancelButton(std::move(split_cancel_callback.first))
      .SetCloseActionCallback(std::move(split_cancel_callback.second));

  // Show the avatar and email of the signed-in account as a custom view
  // between the dialog's subtitle and buttons.
  signin::IdentityManager* identity_manager =
      IdentityManagerFactory::GetForProfile(
          browser->profile()->GetOriginalProfile());
  AccountInfo account_info = identity_manager->FindExtendedAccountInfo(
      identity_manager->GetPrimaryAccountInfo(signin::ConsentLevel::kSignin));
  CHECK(!account_info.IsEmpty());

  auto avatar_and_email_view = std::make_unique<views::View>();
  avatar_and_email_view->AddChildView(std::make_unique<views::ImageView>(
      ui::ImageModel::FromImage(profiles::GetSizedAvatarIcon(
          account_info.account_image, 16, 16, profiles::SHAPE_CIRCLE))));
  avatar_and_email_view->AddChildView(
      std::make_unique<views::Label>(base::UTF8ToUTF16(account_info.email)));
  int horizontal_spacing = ChromeLayoutProvider::Get()->GetDistanceMetric(
      DISTANCE_ACCOUNT_INFO_ROW_AVATAR_EMAIL);
  avatar_and_email_view->SetLayoutManager(std::make_unique<views::BoxLayout>())
      ->set_between_child_spacing(horizontal_spacing);
  builder.AddCustomField(
      std::make_unique<views::BubbleDialogModelHost::CustomView>(
          std::move(avatar_and_email_view),
          views::BubbleDialogModelHost::FieldType::kControl));

  // Show a browser modal instead of one attached to the extensions icon to be
  // consistent with upload dialogs for other syncable types (e.g. bookmarks).
  chrome::ShowBrowserModal(browser, builder.Build());
}

}  // namespace extensions