File: create_application_shortcut_view.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; 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 (275 lines) | stat: -rw-r--r-- 11,112 bytes parent folder | download | duplicates (6)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/create_application_shortcut_view.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/web_applications/extensions/web_app_extension_shortcut.h"
#include "chrome/browser/web_applications/os_integration/os_integration_manager.h"
#include "chrome/browser/web_applications/os_integration/os_integration_sub_manager.h"
#include "chrome/browser/web_applications/os_integration/web_app_shortcut.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "components/constrained_window/constrained_window_views.h"
#include "components/prefs/pref_service.h"
#include "extensions/common/extension.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"

#if BUILDFLAG(IS_WIN)
#include "base/win/shortcut.h"
#include "chrome/installer/util/taskbar_util.h"
#endif  // BUILDFLAG(IS_WIN)

namespace chrome {

void ShowCreateChromeAppShortcutsDialog(
    gfx::NativeWindow parent_window,
    Profile* profile,
    const extensions::Extension* app,
    base::OnceCallback<void(bool)> close_callback) {
  constrained_window::CreateBrowserModalDialogViews(
      new CreateChromeApplicationShortcutView(profile, app,
                                              std::move(close_callback)),
      parent_window)
      ->Show();
}

void ShowCreateChromeAppShortcutsDialog(
    gfx::NativeWindow parent_window,
    Profile* profile,
    const std::string& web_app_id,
    base::OnceCallback<void(bool)> close_callback) {
  constrained_window::CreateBrowserModalDialogViews(
      new CreateChromeApplicationShortcutView(profile, web_app_id,
                                              std::move(close_callback)),
      parent_window)
      ->Show();
}

}  // namespace chrome

CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView(
    Profile* profile,
    const extensions::Extension* app,
    base::OnceCallback<void(bool)> close_callback)
    : CreateChromeApplicationShortcutView(profile,
                                          /*is_extension=*/true,
                                          std::move(close_callback)) {
  // Get shortcut and icon information; needed for creating the shortcut.
  web_app::GetShortcutInfoForApp(
      app, profile,
      base::BindRepeating(&CreateChromeApplicationShortcutView::OnAppInfoLoaded,
                          weak_ptr_factory_.GetWeakPtr()));
}

CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView(
    Profile* profile,
    const std::string& web_app_id,
    base::OnceCallback<void(bool)> close_callback)
    : CreateChromeApplicationShortcutView(profile,
                                          /*is_extension=*/false,
                                          std::move(close_callback)) {
  web_app::WebAppProvider* provider =
      web_app::WebAppProvider::GetForWebApps(profile);
  provider->os_integration_manager().GetShortcutInfoForAppFromRegistrar(
      web_app_id,
      base::BindRepeating(&CreateChromeApplicationShortcutView::OnAppInfoLoaded,
                          weak_ptr_factory_.GetWeakPtr()));
}

CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView(
    Profile* profile,
    bool is_extension,
    base::OnceCallback<void(bool)> close_callback)
    : profile_(profile),
      prefs_(profile->GetPrefs()),
      is_extension_(is_extension),
      close_callback_(std::move(close_callback)) {
  SetModalType(ui::mojom::ModalType::kWindow);
  SetButtonLabel(ui::mojom::DialogButton::kOk,
                 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_COMMIT));
  set_margins(ChromeLayoutProvider::Get()->GetDialogInsetsForContentType(
      views::DialogContentType::kText, views::DialogContentType::kText));
  SetAcceptCallback(
      base::BindOnce(&CreateChromeApplicationShortcutView::OnDialogAccepted,
                     base::Unretained(this)));
  auto canceled = [](CreateChromeApplicationShortcutView* dialog) {
    if (!dialog->close_callback_.is_null()) {
      std::move(dialog->close_callback_).Run(false);
    }
  };
  SetCancelCallback(base::BindOnce(canceled, base::Unretained(this)));
  SetCloseCallback(base::BindOnce(canceled, base::Unretained(this)));
  InitControls();
}

CreateChromeApplicationShortcutView::~CreateChromeApplicationShortcutView() =
    default;

void CreateChromeApplicationShortcutView::InitControls() {
  auto create_shortcuts_label = std::make_unique<views::Label>(
      l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_LABEL));
  create_shortcuts_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  create_shortcuts_label->SetMultiLine(true);

  std::unique_ptr<views::Checkbox> desktop_check_box = AddCheckbox(
      l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_DESKTOP_CHKBOX),
      prefs::kWebAppCreateOnDesktop);

  std::unique_ptr<views::Checkbox> menu_check_box;
  std::unique_ptr<views::Checkbox> pin_to_taskbar_checkbox;

#if BUILDFLAG(IS_WIN)
  menu_check_box = AddCheckbox(
      l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_START_MENU_CHKBOX),
      prefs::kWebAppCreateInAppsMenu);

  // Only include the pin-to-taskbar option when running on versions of Windows
  // that support pinning.
  if (CanPinShortcutToTaskbar()) {
    pin_to_taskbar_checkbox =
        AddCheckbox(l10n_util::GetStringUTF16(IDS_PIN_TO_TASKBAR_CHKBOX),
                    prefs::kWebAppCreateInQuickLaunchBar);
  }
#elif BUILDFLAG(IS_POSIX)
  menu_check_box =
      AddCheckbox(l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_MENU_CHKBOX),
                  prefs::kWebAppCreateInAppsMenu);
#endif

  ChromeLayoutProvider* provider = ChromeLayoutProvider::Get();

  SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical, gfx::Insets(),
      provider->GetDistanceMetric(views::DISTANCE_RELATED_CONTROL_VERTICAL)));
  AddChildView(std::move(create_shortcuts_label));
  desktop_check_box_ = AddChildView(std::move(desktop_check_box));
  if (menu_check_box) {
    menu_check_box_ = AddChildView(std::move(menu_check_box));
  }
  if (pin_to_taskbar_checkbox) {
    quick_launch_check_box_ = AddChildView(std::move(pin_to_taskbar_checkbox));
  }
}

gfx::Size CreateChromeApplicationShortcutView::CalculatePreferredSize(
    const views::SizeBounds& available_size) const {
  static const int kDialogWidth = 360;
  int height =
      GetLayoutManager()->GetPreferredHeightForWidth(this, kDialogWidth);
  return gfx::Size(kDialogWidth, height);
}

bool CreateChromeApplicationShortcutView::IsDialogButtonEnabled(
    ui::mojom::DialogButton button) const {
  if (button != ui::mojom::DialogButton::kOk) {
    return true;  // It's always possible to cancel out of creating a shortcut.
  }

  if (!shortcut_info_) {
    return false;  // Dialog's not ready because app info hasn't been loaded.
  }

  // One of the three location checkboxes must be checked:
  return desktop_check_box_->GetChecked() ||
         (menu_check_box_ && menu_check_box_->GetChecked()) ||
         (quick_launch_check_box_ && quick_launch_check_box_->GetChecked());
}

std::u16string CreateChromeApplicationShortcutView::GetWindowTitle() const {
  return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_TITLE);
}

void CreateChromeApplicationShortcutView::OnDialogAccepted() {
  DCHECK(IsDialogButtonEnabled(ui::mojom::DialogButton::kOk));

  if (!close_callback_.is_null()) {
    std::move(close_callback_).Run(/*success=*/shortcut_info_ != nullptr);
  }

  // Shortcut can't be created because app info hasn't been loaded.
  if (!shortcut_info_) {
    return;
  }

  web_app::ShortcutLocations creation_locations;
  creation_locations.on_desktop = desktop_check_box_->GetChecked();
  if (menu_check_box_ && menu_check_box_->GetChecked()) {
    creation_locations.applications_menu_location =
        web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS;
  }

#if BUILDFLAG(IS_WIN)
  creation_locations.in_quick_launch_bar =
      quick_launch_check_box_ && quick_launch_check_box_->GetChecked();
#elif BUILDFLAG(IS_POSIX)
  // Create shortcut in Mac dock or as Linux (gnome/kde) application launcher
  // are not implemented yet.
  creation_locations.in_quick_launch_bar = false;
#endif

  // If the dialog has been triggered from a web_app, then we need to perform OS
  // integration using sub managers so that shortcuts can be properly added,
  // updated or deleted. Otherwise, shortcuts created need not be tracked as
  // they will not be tied to an app_id.
  if (!shortcut_info_->app_id.empty() && !is_extension_) {
    auto* provider = web_app::WebAppProvider::GetForWebApps(profile_);
    CHECK(provider);
    provider->scheduler().SynchronizeOsIntegration(
        shortcut_info_->app_id, base::DoNothing(),
        web_app::ConvertShortcutLocationsToSynchronizeOptions(
            creation_locations, web_app::SHORTCUT_CREATION_BY_USER),
        /*upgrade_to_fully_installed_if_installed=*/true);
  } else {
    web_app::CreateShortcutsWithInfo(web_app::SHORTCUT_CREATION_BY_USER,
                                     creation_locations, base::DoNothing(),
                                     std::move(shortcut_info_));
  }
}

std::unique_ptr<views::Checkbox>
CreateChromeApplicationShortcutView::AddCheckbox(const std::u16string& text,
                                                 const std::string& pref_path) {
  auto checkbox =
      std::make_unique<views::Checkbox>(text, views::Button::PressedCallback());
  checkbox->SetCallback(base::BindRepeating(
      &CreateChromeApplicationShortcutView::CheckboxPressed,
      base::Unretained(this), pref_path, base::Unretained(checkbox.get())));
  checkbox->SetChecked(prefs_->GetBoolean(pref_path));
  return checkbox;
}

void CreateChromeApplicationShortcutView::CheckboxPressed(
    std::string pref_path,
    views::Checkbox* checkbox) {
  prefs_->SetBoolean(pref_path, checkbox->GetChecked());
  DialogModelChanged();
}

void CreateChromeApplicationShortcutView::OnAppInfoLoaded(
    std::unique_ptr<web_app::ShortcutInfo> shortcut_info) {
  // GetShortcutInfoForApp request may return nullptr |shortcut_info| to this
  // callback if web app was uninstalled during that asynchronous request.
  shortcut_info_ = std::move(shortcut_info);
  // This may cause there to be shortcut info when there was none before, so
  // make sure the accept button gets enabled.
  DialogModelChanged();
}

BEGIN_METADATA(CreateChromeApplicationShortcutView)
END_METADATA