File: private_api_sharesheet.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 (304 lines) | stat: -rw-r--r-- 11,999 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
// Copyright 2020 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/ash/extensions/file_manager/private_api_sharesheet.h"

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "chrome/browser/ash/drive/file_system_util.h"
#include "chrome/browser/ash/extensions/file_manager/private_api_util.h"
#include "chrome/browser/ash/file_manager/fileapi_util.h"
#include "chrome/browser/ash/fileapi/file_system_backend.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sharesheet/sharesheet_service.h"
#include "chrome/browser/sharesheet/sharesheet_service_factory.h"
#include "chrome/common/extensions/api/file_manager_private.h"
#include "components/drive/drive_api_util.h"
#include "components/services/app_service/public/cpp/intent_util.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/api/file_handlers/directory_util.h"
#include "extensions/browser/api/file_handlers/mime_util.h"
#include "storage/browser/file_system/file_system_context.h"
#include "storage/browser/file_system/file_system_url.h"

using storage::FileSystemURL;

namespace {

using extensions::api::file_manager_private::SharesheetLaunchSource;

sharesheet::LaunchSource GetLaunchSource(SharesheetLaunchSource launch_source) {
  switch (launch_source) {
    case (SharesheetLaunchSource::kSharesheetButton):
      return sharesheet::LaunchSource::kFilesAppShareButton;
    case (SharesheetLaunchSource::kContextMenu):
      return sharesheet::LaunchSource::kFilesAppContextMenu;
    case (SharesheetLaunchSource::kUnknown):
    case (SharesheetLaunchSource::kNone):
      return sharesheet::LaunchSource::kUnknown;
  }
}

}  // namespace

namespace extensions {

FileManagerPrivateSharesheetHasTargetsFunction::
    FileManagerPrivateSharesheetHasTargetsFunction() = default;

FileManagerPrivateSharesheetHasTargetsFunction::
    ~FileManagerPrivateSharesheetHasTargetsFunction() = default;

ExtensionFunction::ResponseAction
FileManagerPrivateSharesheetHasTargetsFunction::Run() {
  using extensions::api::file_manager_private::SharesheetHasTargets::Params;
  const std::optional<Params> params = Params::Create(args());
  EXTENSION_FUNCTION_VALIDATE(params);

  if (params->file_urls.empty()) {
    return RespondNow(Error("No URLs provided"));
  }

  profile_ = Profile::FromBrowserContext(browser_context());

  const scoped_refptr<storage::FileSystemContext> file_system_context =
      file_manager::util::GetFileSystemContextForRenderFrameHost(
          profile_, render_frame_host());

  // Collect all the URLs, convert them to GURLs, and crack all the urls into
  // file paths.
  for (const auto& url_as_string : params->file_urls) {
    const GURL url(url_as_string);
    storage::FileSystemURL file_system_url(
        file_system_context->CrackURLInFirstPartyContext(url));
    if (!ash::FileSystemBackend::CanHandleURL(file_system_url)) {
      continue;
    }
    urls_.push_back(url);
    file_system_urls_.push_back(file_system_url);
  }

  mime_type_collector_ =
      std::make_unique<app_file_handler_util::MimeTypeCollector>(profile_);
  mime_type_collector_->CollectForURLs(
      file_system_urls_,
      base::BindOnce(
          &FileManagerPrivateSharesheetHasTargetsFunction::OnMimeTypesCollected,
          this));
  return RespondLater();
}

void FileManagerPrivateSharesheetHasTargetsFunction::OnMimeTypesCollected(
    std::unique_ptr<std::vector<std::string>> mime_types) {
  sharesheet::SharesheetService* sharesheet_service =
      sharesheet::SharesheetServiceFactory::GetForProfile(profile_);

  bool result = false;

  if (!sharesheet_service) {
    LOG(ERROR) << "Couldn't get Sharesheet Service for profile";
    Respond(ArgumentList(extensions::api::file_manager_private::
                             SharesheetHasTargets::Results::Create(result)));
    return;
  }

  if (file_system_urls_.size() == 1 &&
      file_system_urls_[0].type() == storage::kFileSystemTypeDriveFs) {
    using drive::util::ConnectionStatus;
    const ConnectionStatus status = drive::util::GetDriveConnectionStatus(
        Profile::FromBrowserContext(browser_context()));

    using enum ConnectionStatus;
    if (status == kMetered || status == kConnected) {
      file_manager::util::SingleEntryPropertiesGetterForDriveFs::Start(
          file_system_urls_[0], profile_,
          base::BindOnce(&FileManagerPrivateSharesheetHasTargetsFunction::
                             OnDrivePropertyCollected,
                         this, std::move(mime_types)));
      return;
    }
  }
  result = sharesheet_service->HasShareTargets(
      apps_util::MakeShareIntent(urls_, *mime_types));
  Respond(ArgumentList(extensions::api::file_manager_private::
                           SharesheetHasTargets::Results::Create(result)));
}

void FileManagerPrivateSharesheetHasTargetsFunction::OnDrivePropertyCollected(
    std::unique_ptr<std::vector<std::string>> mime_types,
    std::unique_ptr<api::file_manager_private::EntryProperties> properties,
    base::File::Error error) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  if (error != base::File::FILE_OK) {
    LOG(ERROR) << "Error reading file properties in Drive: " << error;
    Respond(ArgumentList(extensions::api::file_manager_private::
                             SharesheetHasTargets::Results::Create(false)));
    return;
  }

  is_directory_collector_ =
      std::make_unique<app_file_handler_util::IsDirectoryCollector>(profile_);
  is_directory_collector_->CollectForEntriesPaths(
      std::vector<base::FilePath>{file_system_urls_[0].path()},
      base::BindOnce(&FileManagerPrivateSharesheetHasTargetsFunction::
                         OnIsDirectoryCollected,
                     this, std::move(mime_types), std::move(properties)));
}

void FileManagerPrivateSharesheetHasTargetsFunction::OnIsDirectoryCollected(
    std::unique_ptr<std::vector<std::string>> mime_types,
    std::unique_ptr<api::file_manager_private::EntryProperties> properties,
    std::unique_ptr<std::set<base::FilePath>> path_directory_set) {
  bool is_directory = path_directory_set->find(file_system_urls_[0].path()) !=
                      path_directory_set->end();

  sharesheet::SharesheetService* sharesheet_service =
      sharesheet::SharesheetServiceFactory::GetForProfile(profile_);
  GURL share_url =
      (properties->can_share && *properties->can_share && properties->share_url)
          ? GURL(*properties->share_url)
          : GURL();
  bool result = sharesheet_service->HasShareTargets(apps_util::MakeShareIntent(
      urls_[0], (*mime_types)[0], share_url, is_directory));
  Respond(ArgumentList(extensions::api::file_manager_private::
                           SharesheetHasTargets::Results::Create(result)));
}

FileManagerPrivateInvokeSharesheetFunction::
    FileManagerPrivateInvokeSharesheetFunction() = default;

FileManagerPrivateInvokeSharesheetFunction::
    ~FileManagerPrivateInvokeSharesheetFunction() = default;

ExtensionFunction::ResponseAction
FileManagerPrivateInvokeSharesheetFunction::Run() {
  using extensions::api::file_manager_private::InvokeSharesheet::Params;
  const std::optional<Params> params = Params::Create(args());
  EXTENSION_FUNCTION_VALIDATE(params);

  if (params->file_urls.empty()) {
    return RespondNow(Error("No URLs provided"));
  }

  if (params->dlp_source_urls.size() != params->file_urls.size()) {
    return RespondNow(Error("Mismatching URLs and DLP source URLs provided"));
  }

  profile_ = Profile::FromBrowserContext(browser_context());

  const scoped_refptr<storage::FileSystemContext> file_system_context =
      file_manager::util::GetFileSystemContextForRenderFrameHost(
          profile_, render_frame_host());

  // Collect all the URLs, convert them to GURLs, and crack all the urls into
  // file paths.
  for (const auto& url_string : params->file_urls) {
    const GURL url(url_string);
    storage::FileSystemURL file_system_url(
        file_system_context->CrackURLInFirstPartyContext(url));
    if (!ash::FileSystemBackend::CanHandleURL(file_system_url)) {
      continue;
    }
    urls_.push_back(url);
    file_system_urls_.push_back(file_system_url);
  }

  dlp_source_urls_ = std::move(params->dlp_source_urls);

  mime_type_collector_ =
      std::make_unique<app_file_handler_util::MimeTypeCollector>(profile_);
  mime_type_collector_->CollectForURLs(
      file_system_urls_,
      base::BindOnce(
          &FileManagerPrivateInvokeSharesheetFunction::OnMimeTypesCollected,
          this, GetLaunchSource(params->launch_source)));

  return RespondLater();
}

void FileManagerPrivateInvokeSharesheetFunction::OnMimeTypesCollected(
    sharesheet::LaunchSource launch_source,
    std::unique_ptr<std::vector<std::string>> mime_types) {
  // On button press show sharesheet bubble.
  sharesheet::SharesheetService* sharesheet_service =
      sharesheet::SharesheetServiceFactory::GetForProfile(profile_);
  if (!sharesheet_service) {
    Respond(Error("Cannot find sharesheet service"));
    return;
  }

  if (file_system_urls_.size() == 1 &&
      file_system_urls_[0].type() == storage::kFileSystemTypeDriveFs) {
    using drive::util::ConnectionStatus;
    const ConnectionStatus status = drive::util::GetDriveConnectionStatus(
        Profile::FromBrowserContext(browser_context()));

    using enum ConnectionStatus;
    if (status == kMetered || status == kConnected) {
      file_manager::util::SingleEntryPropertiesGetterForDriveFs::Start(
          file_system_urls_[0], profile_,
          base::BindOnce(&FileManagerPrivateInvokeSharesheetFunction::
                             OnDrivePropertyCollected,
                         this, launch_source, std::move(mime_types)));
      return;
    }
  }

  sharesheet_service->ShowBubble(
      GetSenderWebContents(),
      apps_util::MakeShareIntent(urls_, *mime_types, dlp_source_urls_),
      launch_source, base::DoNothing());
  Respond(NoArguments());
}

void FileManagerPrivateInvokeSharesheetFunction::OnDrivePropertyCollected(
    sharesheet::LaunchSource launch_source,
    std::unique_ptr<std::vector<std::string>> mime_types,
    std::unique_ptr<api::file_manager_private::EntryProperties> properties,
    base::File::Error error) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  if (error != base::File::FILE_OK) {
    Respond(Error("Drive File Error"));
    return;
  }

  is_directory_collector_ =
      std::make_unique<app_file_handler_util::IsDirectoryCollector>(profile_);
  is_directory_collector_->CollectForEntriesPaths(
      std::vector<base::FilePath>{file_system_urls_[0].path()},
      base::BindOnce(
          &FileManagerPrivateInvokeSharesheetFunction::OnIsDirectoryCollected,
          this, launch_source, std::move(mime_types), std::move(properties)));
}

void FileManagerPrivateInvokeSharesheetFunction::OnIsDirectoryCollected(
    sharesheet::LaunchSource launch_source,
    std::unique_ptr<std::vector<std::string>> mime_types,
    std::unique_ptr<api::file_manager_private::EntryProperties> properties,
    std::unique_ptr<std::set<base::FilePath>> path_directory_set) {
  bool is_directory = path_directory_set->find(file_system_urls_[0].path()) !=
                      path_directory_set->end();

  sharesheet::SharesheetService* sharesheet_service =
      sharesheet::SharesheetServiceFactory::GetForProfile(profile_);
  if (!sharesheet_service) {
    Respond(Error("Cannot find sharesheet service"));
    return;
  }

  GURL share_url =
      (properties->can_share && *properties->can_share && properties->share_url)
          ? GURL(*properties->share_url)
          : GURL();
  sharesheet_service->ShowBubble(
      GetSenderWebContents(),
      apps_util::MakeShareIntent(urls_[0], (*mime_types)[0], share_url,
                                 is_directory),
      launch_source, base::DoNothing());
  Respond(NoArguments());
}

}  // namespace extensions