File: data_sharing_sdk_delegate_desktop.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (283 lines) | stat: -rw-r--r-- 11,032 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
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
// 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/data_sharing/desktop/data_sharing_sdk_delegate_desktop.h"

#include "chrome/browser/data_sharing/desktop/data_sharing_conversion_utils.h"
#include "chrome/browser/ui/webui/data_sharing/data_sharing_page_handler.h"
#include "chrome/common/webui_url_constants.h"

namespace data_sharing {

namespace {
constexpr base::TimeDelta kResetWebContentsAfterLastCallDuration =
    base::Minutes(1);
}

DataSharingSDKDelegateDesktop::DataSharingSDKDelegateDesktop(
    content::BrowserContext* context)
    : context_(context),
      reset_web_contents_timer_(std::make_unique<base::OneShotTimer>()) {}

DataSharingSDKDelegateDesktop::~DataSharingSDKDelegateDesktop() = default;

void DataSharingSDKDelegateDesktop::Initialize(
    DataSharingNetworkLoader* data_sharing_network_loader) {}

void DataSharingSDKDelegateDesktop::CreateGroup(
    const data_sharing_pb::CreateGroupParams& params,
    base::OnceCallback<
        void(const base::expected<data_sharing_pb::CreateGroupResult,
                                  absl::Status>&)> callback) {
  NOTIMPLEMENTED();
}

void DataSharingSDKDelegateDesktop::ReadGroups(
    const data_sharing_pb::ReadGroupsParams& params,
    ReadGroupsCallback callback) {
  MaybeLoadWebContents(base::BindOnce(
      [](data_sharing_pb::ReadGroupsParams params, ReadGroupsCallback callback,
         DataSharingSDKDelegateDesktop* delegate,
         content::WebContents* web_contents) {
        DataSharingPageHandler* handler =
            static_cast<DataSharingUI*>(
                web_contents->GetWebUI()->GetController())
                ->page_handler();
        CHECK(handler);
        auto mojom_params = data_sharing::mojom::ReadGroupsParams::New();
        for (const auto& group_param : params.group_params()) {
          auto param = data_sharing::mojom::ReadGroupParams::New();
          param->group_id = group_param.group_id();
          param->consistency_token = group_param.consistency_token();
          mojom_params->params.push_back(std::move(param));
        }
        handler->ReadGroups(
            std::move(mojom_params),
            base::BindOnce(&DataSharingSDKDelegateDesktop::OnReadGroups,
                           base::Unretained(delegate), std::move(callback)));
      },
      params, std::move(callback), this));
}

void DataSharingSDKDelegateDesktop::AddMember(
    const data_sharing_pb::AddMemberParams& params,
    base::OnceCallback<void(const absl::Status&)> callback) {
  NOTIMPLEMENTED();
}

void DataSharingSDKDelegateDesktop::RemoveMember(
    const data_sharing_pb::RemoveMemberParams& params,
    base::OnceCallback<void(const absl::Status&)> callback) {
  NOTIMPLEMENTED();
}

void DataSharingSDKDelegateDesktop::LeaveGroup(
    const data_sharing_pb::LeaveGroupParams& params,
    base::OnceCallback<void(const absl::Status&)> callback) {
  MaybeLoadWebContents(base::BindOnce(
      [](data_sharing_pb::LeaveGroupParams params,
         base::OnceCallback<void(const absl::Status&)> callback,
         DataSharingSDKDelegateDesktop* delegate,
         content::WebContents* web_contents) {
        DataSharingPageHandler* handler =
            static_cast<DataSharingUI*>(
                web_contents->GetWebUI()->GetController())
                ->page_handler();
        CHECK(handler);
        handler->LeaveGroup(
            params.group_id(),
            base::BindOnce(&DataSharingSDKDelegateDesktop::OnLeaveGroup,
                           base::Unretained(delegate), std::move(callback)));
      },
      params, std::move(callback), this));
}

void DataSharingSDKDelegateDesktop::DeleteGroup(
    const data_sharing_pb::DeleteGroupParams& params,
    base::OnceCallback<void(const absl::Status&)> callback) {
  MaybeLoadWebContents(base::BindOnce(
      [](data_sharing_pb::DeleteGroupParams params,
         base::OnceCallback<void(const absl::Status&)> callback,
         DataSharingSDKDelegateDesktop* delegate,
         content::WebContents* web_contents) {
        DataSharingPageHandler* handler =
            static_cast<DataSharingUI*>(
                web_contents->GetWebUI()->GetController())
                ->page_handler();
        CHECK(handler);
        handler->DeleteGroup(
            params.group_id(),
            base::BindOnce(&DataSharingSDKDelegateDesktop::OnDeleteGroup,
                           base::Unretained(delegate), std::move(callback)));
      },
      params, std::move(callback), this));
}

void DataSharingSDKDelegateDesktop::LookupGaiaIdByEmail(
    const data_sharing_pb::LookupGaiaIdByEmailParams& params,
    base::OnceCallback<
        void(const base::expected<data_sharing_pb::LookupGaiaIdByEmailResult,
                                  absl::Status>&)> callback) {
  NOTIMPLEMENTED();
}

void DataSharingSDKDelegateDesktop::AddAccessToken(
    const data_sharing_pb::AddAccessTokenParams& params,
    base::OnceCallback<
        void(const base::expected<data_sharing_pb::AddAccessTokenResult,
                                  absl::Status>&)> callback) {
  NOTIMPLEMENTED();
}

void DataSharingSDKDelegateDesktop::MaybeLoadWebContents(
    LoadFinishedCallback callback) {
  // Load the WebContents if it's not loaded yet.
  if (!web_contents_) {
    GURL webui_url(chrome::kChromeUIUntrustedDataSharingAPIURL);
    content::WebContents::CreateParams create_params(context_);
    create_params.initially_hidden = true;
    create_params.site_instance =
        content::SiteInstance::CreateForURL(context_, webui_url);
    web_contents_ = content::WebContents::Create(create_params);

    web_contents_->GetController().LoadURL(webui_url, content::Referrer(),
                                           ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
                                           /*extra_headers=*/std::string());
  }

  // There are 2 cases the page handler is null.
  // 1. web_contents_ is not created yet.
  // 2. web_contents_ is created but the page handler is not created because the
  // webpage is not loaded yet. Either case we need to add the callback to the
  // queue and run it when page handler is ready.
  DataSharingUI* data_sharing_ui =
      static_cast<DataSharingUI*>(web_contents_->GetWebUI()->GetController());
  if (data_sharing_ui->page_handler()) {
    std::move(callback).Run(web_contents_.get());
  } else {
    data_sharing_ui->SetDelegate(this);
    callback_subscriptions_.emplace_back(callbacks_.Add(std::move(callback)));
  }

  // For every API call, schedule a timer to clean up the WebContents after some
  // time if there are no more calls coming after.
  ScheduleResetWebContentsTimer();
}

void DataSharingSDKDelegateDesktop::ApiInitComplete() {
  // At this point the page handler should be created.
  // Invoke the callbacks and clear the subscriptions.
  DataSharingUI* data_sharing_ui =
      static_cast<DataSharingUI*>(web_contents_->GetWebUI()->GetController());
  data_sharing_ui->SetDelegate(nullptr);
  CHECK(data_sharing_ui->page_handler());
  callbacks_.Notify(web_contents_.get());
  callback_subscriptions_.clear();
}

void DataSharingSDKDelegateDesktop::ShowErrorDialog(int status_code) {
  // No-op for this class.
}

void DataSharingSDKDelegateDesktop::OnShareLinkRequested(
    const std::string& group_id,
    const std::string& access_token,
    base::OnceCallback<void(const std::optional<GURL>&)> callback) {
  // No-op for this class.
}

void DataSharingSDKDelegateDesktop::OnGroupAction(
    data_sharing::mojom::GroupAction action,
    data_sharing::mojom::GroupActionProgress progress) {
  // No-op for this class.
}

void DataSharingSDKDelegateDesktop::Shutdown() {
  // Since WebContents depends on BrowserContext, it needs to be destroyed
  // before the BrowserContext is destroyed.
  ResetWebContents();
}

void DataSharingSDKDelegateDesktop::OnReadGroups(
    ReadGroupsCallback callback,
    data_sharing::mojom::ReadGroupsResultPtr mojom_result) {
  if (mojom_result->status_code != 0) {
    std::move(callback).Run(base::unexpected(
        absl::Status(static_cast<absl::StatusCode>(mojom_result->status_code),
                     "Read Groups failed")));
    return;
  }
  data_sharing_pb::ReadGroupsResult result;
  for (auto& group : mojom_result->groups) {
    *result.add_group_data() = ConvertGroup(group);
  }
  std::move(callback).Run(result);
}

void DataSharingSDKDelegateDesktop::OnReadGroupWithToken(
    ReadGroupWithTokenCallback callback,
    data_sharing::mojom::ReadGroupWithTokenResultPtr mojom_result) {
  if (mojom_result->status_code != 0) {
    std::move(callback).Run(base::unexpected(
        absl::Status(static_cast<absl::StatusCode>(mojom_result->status_code),
                     "Read Groups with token failed")));
    return;
  }
  data_sharing_pb::ReadGroupsResult result;
  *result.add_group_data() = ConvertGroup(mojom_result->group);
  std::move(callback).Run(result);
}

void DataSharingSDKDelegateDesktop::ReadGroupWithToken(
    const data_sharing_pb::ReadGroupWithTokenParams& params,
    base::OnceCallback<void(
        const base::expected<data_sharing_pb::ReadGroupsResult, absl::Status>&)>
        callback) {
  MaybeLoadWebContents(base::BindOnce(
      [](data_sharing_pb::ReadGroupWithTokenParams params,
         ReadGroupWithTokenCallback callback,
         DataSharingSDKDelegateDesktop* delegate,
         content::WebContents* web_contents) {
        DataSharingPageHandler* handler =
            static_cast<DataSharingUI*>(
                web_contents->GetWebUI()->GetController())
                ->page_handler();
        CHECK(handler);
        auto mojom_param = data_sharing::mojom::ReadGroupWithTokenParam::New();
        mojom_param->group_id = params.group_id();
        mojom_param->access_token = params.access_token();
        handler->ReadGroupWithToken(
            std::move(mojom_param),
            base::BindOnce(&DataSharingSDKDelegateDesktop::OnReadGroupWithToken,
                           base::Unretained(delegate), std::move(callback)));
      },
      params, std::move(callback), this));
}

void DataSharingSDKDelegateDesktop::OnLeaveGroup(
    base::OnceCallback<void(const absl::Status&)> callback,
    int status_code) {
  std::move(callback).Run(
      absl::Status(static_cast<absl::StatusCode>(status_code), "Leave Group"));
}

void DataSharingSDKDelegateDesktop::OnDeleteGroup(
    base::OnceCallback<void(const absl::Status&)> callback,
    int status_code) {
  std::move(callback).Run(
      absl::Status(static_cast<absl::StatusCode>(status_code), "Delete Group"));
}

void DataSharingSDKDelegateDesktop::ScheduleResetWebContentsTimer() {
  reset_web_contents_timer_->Start(
      FROM_HERE, kResetWebContentsAfterLastCallDuration, this,
      &DataSharingSDKDelegateDesktop::ResetWebContents);
}

void DataSharingSDKDelegateDesktop::ResetWebContents() {
  callback_subscriptions_.clear();
  web_contents_.reset();
}

}  // namespace data_sharing