File: batch_upload_service.h

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 (160 lines) | stat: -rw-r--r-- 6,348 bytes parent folder | download | duplicates (3)
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
// 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.

#ifndef CHROME_BROWSER_PROFILES_BATCH_UPLOAD_BATCH_UPLOAD_SERVICE_H_
#define CHROME_BROWSER_PROFILES_BATCH_UPLOAD_BATCH_UPLOAD_SERVICE_H_

#include <memory>
#include <vector>

#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/timer/timer.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/sync/service/local_data_description.h"

class Browser;
class BatchUploadDelegate;

namespace signin {
class IdentityManager;
}  // namespace signin

namespace syncer {
class SyncService;
}  // namespace syncer

// Service that allows the management of the Batch Upload Dialog. Used to open
// the dialog and manages its lifetime.
// It communicates with the `syncer::SyncService` to get information of the
// current local data for eligible types.
class BatchUploadService : public KeyedService {
 public:
  BatchUploadService(signin::IdentityManager* identity_manager,
                     syncer::SyncService* sync_service,
                     std::unique_ptr<BatchUploadDelegate> delegate);
  BatchUploadService(const BatchUploadService&) = delete;
  BatchUploadService& operator=(const BatchUploadService&) = delete;
  ~BatchUploadService() override;

  // Lists the different entry points to the Batch Upload Dialog.
  // TODO(crbug.com/416219929): Currently all existing entry points are tied to
  // a data type. In the future, neutral entry points may be added.
  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  //
  // LINT.IfChange(EntryPoint)
  enum class EntryPoint {
    kPasswordManagerSettings = 0,
    kPasswordPromoCard = 1,
    kBookmarksManagerPromoCard = 2,
    kProfileMenu = 3,

    kMaxValue = kProfileMenu,
  };
  // LINT.ThenChange(/tools/metrics/histograms/metadata/sync/enums.xml:BatchUploadEntryPoint)

  // Attempts to open the Batch Upload modal dialog that allows uploading the
  // local profile data. The dialog will only be opened if there are some local
  // data (of any type) to show and the dialog is not shown already in the
  // profile. `dialog_shown_callback` returns whether the dialog was shown or
  // not.
  void OpenBatchUpload(
      Browser* browser,
      EntryPoint entry_point,
      base::OnceCallback<void(bool)> dialog_shown_callback = base::DoNothing());

  // Returns whether the dialog is currently showing on a browser.
  bool IsDialogOpened() const;

  // Gets all the local data for the available types.
  // Available types are the sync types that the batch upload supports.
  // This function is asynchronous and the results are returned in
  // `result_callback`.
  void GetLocalDataDescriptionsForAvailableTypes(
      base::OnceCallback<
          void(std::map<syncer::DataType, syncer::LocalDataDescription>)>
          result_callback);

  // Gets the ordered list of all available types in BatchUpload.
  static std::vector<syncer::DataType> AvailableTypesOrder();

 private:
  // Callback that returns a map of `syncer::LocalDataDescription` for the data
  // types that can be shown in the Batch Upload dialog.
  void OnGetLocalDataDescriptionsReady(
      std::map<syncer::DataType, syncer::LocalDataDescription> local_data_map);

  // Callback of the dialog view closing, contains the IDs of the selected items
  // per data type. Selected items will be processed to be moved to the account
  // storage. Empty map means the dialog was closed explicitly not to move any
  // data.
  void OnBatchUploadDialogResult(
      const std::map<syncer::DataType,
                     std::vector<syncer::LocalDataItemModel::DataId>>&
          item_ids_to_move);

  // Whether the profile is in the proper sign in state to see the dialog.
  bool IsUserEligibleToOpenDialog() const;

  // Changes the avatar button text to saving data and starts a timer that will
  // revert the button text on timeout.
  void TriggerAvatarButtonSavingDataText(Browser* browser);

  // Callback to clear the overridden avatar text on timeout.
  void OnAvatarOverrideTextTimeout();

  // Resets part of the state related to the dialog lifetime.
  void ResetDialogState();

  // This state is divided into two parts:
  // - `DialogState`: that is active from triggering the request of opening the
  // dialog until the dialog is closed or resulted in not opening.
  // - `SavingBrowserState`: that is active after the dialog was accepted.
  // Accepting the dialog closes it and resets `DialogState`. However
  // post-accepting (saving), the avatar button of the browser that was showing
  // the dialog expands and shows a saving text for few seconds. This state
  // holds needed information for that modification. The dialog is still allowed
  // to be opened while this state is active.
  struct ResettableState {
    // Fields related to the dialog currently showing.
    struct DialogState {
      // Browser that is showing the dialog.
      raw_ptr<Browser> browser_;
      // Entry point of the dialog.
      EntryPoint entry_point_ = EntryPoint::kPasswordManagerSettings;
      // Called when the decision about showing the dialog is made.
      // Returns whether it was shown or not.
      base::OnceCallback<void(bool)> dialog_shown_callback_;

      DialogState();
      ~DialogState();
    };

    // Fields related to the effect on the browser post accepting the dialog.
    struct SavingBrowserState {
      // Callback that will clear the modified text on the avatar button.
      base::ScopedClosureRunner avatar_override_clear_callback_;
      // Timer to clear the avatar override text.
      base::OneShotTimer avatar_override_timer_;
    };

    std::unique_ptr<DialogState> dialog_state_;
    std::unique_ptr<SavingBrowserState> saving_browser_state_;

    ResettableState();
    ~ResettableState();
  };

  raw_ref<signin::IdentityManager> identity_manager_;
  raw_ref<syncer::SyncService> sync_service_;
  std::unique_ptr<BatchUploadDelegate> delegate_;

  // Full state of the flow from requesting opening the dialog to saving
  // data/canceling the flow.
  ResettableState state_;
};

#endif  // CHROME_BROWSER_PROFILES_BATCH_UPLOAD_BATCH_UPLOAD_SERVICE_H_