File: upload_extension_to_account_dialog.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (83 lines) | stat: -rw-r--r-- 4,060 bytes parent folder | download | duplicates (7)
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
// 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/account_extension_tracker.h"
#include "chrome/browser/extensions/extension_util.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/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/extensions/extensions_dialogs_utils.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/layout/layout_provider.h"

namespace extensions {

void ShowUploadExtensionToAccountDialog(Browser* browser,
                                        const Extension& extension,
                                        base::OnceClosure accept_callback,
                                        base::OnceClosure cancel_callback) {
  CHECK(base::FeatureList::IsEnabled(
      switches::kEnableExtensionsExplicitBrowserSignin));
  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