File: batch_upload_dialog_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 (318 lines) | stat: -rw-r--r-- 12,265 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// 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 "chrome/browser/ui/views/profiles/batch_upload_dialog_view.h"

#include "base/functional/callback.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/strcat.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/profiles/batch_upload_ui_delegate.h"
#include "chrome/browser/ui/webui/signin/batch_upload_ui.h"
#include "chrome/common/webui_url_constants.h"
#include "components/constrained_window/constrained_window_views.h"
#include "components/input/native_web_keyboard_event.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/widget/widget.h"

namespace {

constexpr int kBatchUploadDialogFixedWidth = 512;
constexpr int kBatchUploadDialogMaxHeight = 628;

constexpr char kDataTypeInformationHistogramBase[] =
    "Sync.BatchUpload.DataType";

BatchUploadUI* GetBatchUploadUI(views::WebView* web_view) {
  return web_view->GetWebContents()
      ->GetWebUI()
      ->GetController()
      ->GetAs<BatchUploadUI>();
}

// Account is expected not to be in error.
AccountInfo GetBatchUploadPrimaryAccountInfo(
    signin::IdentityManager& identity_manager) {
  AccountInfo primary_account = identity_manager.FindExtendedAccountInfo(
      identity_manager.GetPrimaryAccountInfo(signin::ConsentLevel::kSignin));
  CHECK(!primary_account.email.empty());
  CHECK(!identity_manager.HasAccountWithRefreshTokenInPersistentErrorState(
      primary_account.account_id));
  return primary_account;
}

// Records the availability of all data types.
void RecordAvailableDataTypes(
    const std::map<syncer::DataType, int>& data_item_count) {
  for (const auto& [type, count] : data_item_count) {
    CHECK_NE(count, 0);
    base::UmaHistogramEnumeration(
        base::StrCat({kDataTypeInformationHistogramBase, "Available"}),
        DataTypeHistogramValue(type));
  }
}

// Records whether at least one element was selected per data type. Will not
// record for a specific data type if it is not in the map or has no items
// selected. Also records the percentage of selected items vs available items.
// Returns whether any data was selected.
bool RecordSelectedDataTypes(
    const std::map<syncer::DataType,
                   std::vector<syncer::LocalDataItemModel::DataId>>&
        selected_types,
    const std::map<syncer::DataType, int>& data_item_count_map) {
  bool has_selected_data = false;
  for (const auto& [type, selected_items] : selected_types) {
    int selected_count = selected_items.size();
    if (selected_count != 0) {
      has_selected_data = true;
      base::UmaHistogramEnumeration(
          base::StrCat({kDataTypeInformationHistogramBase, "Selected"}),
          DataTypeHistogramValue(type));

      int available_count = data_item_count_map.at(type);
      CHECK_LE(selected_count, available_count);
      base::UmaHistogramPercentage(
          base::StrCat(
              {kDataTypeInformationHistogramBase, "SelectedItemPercentage"}),
          selected_count * 100 / available_count);
    }
  }
  return has_selected_data;
}

}  // namespace

BatchUploadDialogView::~BatchUploadDialogView() {
  // Makes sure that everything is cleaned up if it was not done before.
  OnClose();

  // Records at view destruction to make sure it is recorded once only per
  // dialog.
  base::UmaHistogramEnumeration("Sync.BatchUpload.DialogCloseReason",
                                close_reason_);
}

// static
BatchUploadDialogView* BatchUploadDialogView::CreateBatchUploadDialogView(
    Browser& browser,
    std::vector<syncer::LocalDataDescription> local_data_description_list,
    BatchUploadService::EntryPoint entry_point,
    BatchUploadSelectedDataTypeItemsCallback complete_callback) {
  std::unique_ptr<BatchUploadDialogView> dialog_view = base::WrapUnique(
      new BatchUploadDialogView(browser, std::move(local_data_description_list),
                                entry_point, std::move(complete_callback)));
  BatchUploadDialogView* dialog_view_ptr = dialog_view.get();

  gfx::NativeWindow window = browser.tab_strip_model()
                                 ->GetActiveWebContents()
                                 ->GetTopLevelNativeWindow();

  constrained_window::CreateBrowserModalDialogViews(std::move(dialog_view),
                                                    window);
  return dialog_view_ptr;
}

BatchUploadDialogView::BatchUploadDialogView(
    Browser& browser,
    std::vector<syncer::LocalDataDescription> local_data_description_list,
    BatchUploadService::EntryPoint entry_point,
    BatchUploadSelectedDataTypeItemsCallback complete_callback)
    : complete_callback_(std::move(complete_callback)),
      entry_point_(entry_point) {
  SetModalType(ui::mojom::ModalType::kWindow);
  // No native buttons.
  SetButtons(static_cast<int>(ui::mojom::DialogButton::kNone));
  // No close (x) top right button.
  SetShowCloseButton(false);
  set_margins(gfx::Insets());

  // Setting a close callback to make sure every time the view is being closed,
  // that all necessary data are cleared. The view and underlying child views
  // may be destroyed asynchronosly.
  SetCloseCallback(
      base::BindOnce(&BatchUploadDialogView::OnClose, base::Unretained(this)));

  // Create the web view in the native bubble.
  std::unique_ptr<views::WebView> web_view =
      std::make_unique<views::WebView>(browser.profile());
  web_view->LoadInitialURL(GURL(chrome::kChromeUIBatchUploadURL));
  web_view_ = web_view.get();
  web_view_->GetWebContents()->SetDelegate(this);
  SetInitiallyFocusedView(web_view_);
  // Set initial height to max height in order not to have an empty window.
  web_view_->SetPreferredSize(
      gfx::Size(kBatchUploadDialogFixedWidth, kBatchUploadDialogMaxHeight));

  signin::IdentityManager* identity_manager =
      IdentityManagerFactory::GetForProfile(browser.profile());
  CHECK(identity_manager);
  primary_account_info_ = GetBatchUploadPrimaryAccountInfo(*identity_manager);

  for (const syncer::LocalDataDescription& local_data_description :
       local_data_description_list) {
    const int item_count = local_data_description.local_data_models.size();
    CHECK_NE(item_count, 0);
    data_item_count_map_.insert_or_assign(local_data_description.type,
                                          item_count);
  }

  BatchUploadUI* web_ui = GetBatchUploadUI(web_view_);
  CHECK(web_ui);
  // Initializes the UI that will initialize the handler when ready.
  web_ui->Initialize(
      primary_account_info_, &browser, std::move(local_data_description_list),
      base::BindRepeating(&BatchUploadDialogView::SetHeightAndShowWidget,
                          base::Unretained(this)),
      base::BindRepeating(&BatchUploadDialogView::AllowWebViewInput,
                          base::Unretained(this)),
      base::BindOnce(&BatchUploadDialogView::OnDialogSelectionMade,
                     base::Unretained(this)));

  AddChildView(std::move(web_view));

  SetLayoutManager(std::make_unique<views::FillLayout>());

  // Observe Identity Manager for sign in state changes.
  scoped_identity_manager_observation_.Observe(identity_manager);
}

void BatchUploadDialogView::OnClose() {
  // `complete_callback_` will destroy data owned by the service/controller
  // passed to the UI and handler. We need to make sure those are cleared if the
  // UI is still alive, before clearing the service/controller.
  BatchUploadUI* web_ui = GetBatchUploadUI(web_view_);
  if (web_ui) {
    web_ui->Clear();
  }

  // If the view was closed without a user action, run the callback as if it was
  // cancelled (empty result).
  if (complete_callback_) {
    std::move(complete_callback_).Run({});
  }
}

void BatchUploadDialogView::OnDialogSelectionMade(
    const std::map<syncer::DataType,
                   std::vector<syncer::LocalDataItemModel::DataId>>&
        selected_map) {
  bool has_selected_data =
      RecordSelectedDataTypes(selected_map, data_item_count_map_);

  std::move(complete_callback_).Run(selected_map);
  CloseWithReason(has_selected_data
                      ? BatchUploadDialogCloseReason::kSaveClicked
                      : BatchUploadDialogCloseReason::kCancelClicked);
}

void BatchUploadDialogView::SetHeightAndShowWidget(int height) {
  views::Widget* widget = GetWidget();
  // This might happen if an update was requested after a close event happens
  // externally such as signing out or signin pending that triggers the closing
  // of the widget. The widget does not get destroyed right away but is in
  // closing mode.
  if (widget->IsClosed()) {
    return;
  }

  // Beyond `kBatchUploadDialogMaxHeight`, the dialog will show a scrollbar.
  web_view_->SetPreferredSize(
      gfx::Size(kBatchUploadDialogFixedWidth,
                std::min(height, kBatchUploadDialogMaxHeight)));
  widget->SetSize(widget->non_client_view()->GetPreferredSize());

  // `SetHeightAndShowWidget()` may be called multiple times, only show the view
  // and set the static values once.
  if (!widget->IsVisible()) {
    // Enforce the web view round corners to match the native view. Since we set
    // the view margin to 0 in the constructor, it leads to the webview
    // overlapping on the native view in the corners.
    web_view_->holder()->SetCornerRadii(
        gfx::RoundedCornersF(GetCornerRadius()));

    widget->Show();

    RecordAvailableDataTypes(data_item_count_map_);
    base::UmaHistogramEnumeration("Sync.BatchUpload.Opened", entry_point_);
  }
}

void BatchUploadDialogView::AllowWebViewInput(bool allow) {
  if (allow) {
    scoped_ignore_events_.reset();
    return;
  }

  scoped_ignore_events_ =
      web_view_->GetWebContents()->IgnoreInputEvents(std::nullopt);
}

void BatchUploadDialogView::OnPrimaryAccountChanged(
    const signin::PrimaryAccountChangeEvent& event_details) {
  switch (event_details.GetEventTypeFor(signin::ConsentLevel::kSignin)) {
    case signin::PrimaryAccountChangeEvent::Type::kCleared:
      CloseWithReason(BatchUploadDialogCloseReason::kSignout);
      return;
    case signin::PrimaryAccountChangeEvent::Type::kNone:
    case signin::PrimaryAccountChangeEvent::Type::kSet:
      break;
  }
}

void BatchUploadDialogView::OnErrorStateOfRefreshTokenUpdatedForAccount(
    const CoreAccountInfo& account_info,
    const GoogleServiceAuthError& error,
    signin_metrics::SourceForRefreshTokenOperation token_operation_source) {
  if (account_info == primary_account_info_ && error.IsPersistentError()) {
    CloseWithReason(BatchUploadDialogCloseReason::kSiginPending);
  }
}

bool BatchUploadDialogView::HandleKeyboardEvent(
    content::WebContents* source,
    const input::NativeWebKeyboardEvent& event) {
  // TODO(crbug.com/373297250): Look into using
  // `views::UnhandledKeyboardEventHandler` to properly handle all
  // shortcut/unhandled keyboard events.
  if (event.dom_key == ui::DomKey::ESCAPE) {
    CloseWithReason(BatchUploadDialogCloseReason::kDismissed);
    return true;
  }

  return false;
}

void BatchUploadDialogView::CloseWithReason(
    BatchUploadDialogCloseReason reason) {
  close_reason_ = reason;
  GetWidget()->Close();
}

views::WebView* BatchUploadDialogView::GetWebViewForTesting() {
  return web_view_;
}

// BatchUploadUIDelegate -------------------------------------------------------

void BatchUploadUIDelegate::ShowBatchUploadDialogInternal(
    Browser& browser,
    std::vector<syncer::LocalDataDescription> local_data_description_list,
    BatchUploadService::EntryPoint entry_point,
    BatchUploadSelectedDataTypeItemsCallback complete_callback) {
  BatchUploadDialogView::CreateBatchUploadDialogView(
      browser, std::move(local_data_description_list), entry_point,
      std::move(complete_callback));
}

BEGIN_METADATA(BatchUploadDialogView)
END_METADATA