File: share_info_file_handler.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (440 lines) | stat: -rw-r--r-- 16,617 bytes parent folder | download | duplicates (8)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Copyright 2021 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/arc/nearby_share/share_info_file_handler.h"

#include <cinttypes>
#include <string>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/numerics/checked_math.h"
#include "base/strings/stringprintf.h"
#include "base/task/sequenced_task_runner.h"
#include "chrome/browser/ash/arc/arc_util.h"
#include "chrome/browser/ash/arc/fileapi/arc_content_file_system_url_util.h"
#include "chrome/browser/ash/arc/nearby_share/arc_nearby_share_uma.h"
#include "chrome/browser/ash/file_manager/fileapi_util.h"
#include "chrome/browser/ash/fileapi/external_file_url_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chromeos/ash/experiences/arc/arc_util.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "storage/browser/file_system/file_system_context.h"
#include "storage/browser/file_system/isolated_context.h"
#include "url/gurl.h"

// Enable VLOG level 1.
#undef ENABLED_VLOG_LEVEL
#define ENABLED_VLOG_LEVEL 1

namespace arc {

namespace {
constexpr int kStreamReaderBufSizeInBytes = 32 * 1024;

// Since most shareable Android app content lives in a virtual file system,
// it will take some measurable amount of time to stream images / videos
// from ContentProviders in ARC to local files in the Chrome OS filesystem
// before Nearby Share can consume it.  Since we don't have limit on number
// of files or size of files users can share via Nearby Share, set to some
// reasonable number of minutes per GB of transfer.
constexpr base::TimeDelta kFileStreamingTimeoutPerGB = base::Minutes(2);

int64_t GetTimeoutInSecondsFromBytes(uint64_t transfer_bytes) {
  constexpr double kGBInBytes = 1 * 1024 * 1024 * 1024;
  const int64_t transfer_bytes_in_gb = base::checked_cast<int64_t>(
      std::ceil(base::checked_cast<double>(transfer_bytes) / kGBInBytes));

  // Always set timeout to at least |kFileStreamingTimeoutPerGB|.
  return (transfer_bytes_in_gb > 0)
             ? transfer_bytes_in_gb * kFileStreamingTimeoutPerGB.InSeconds()
             : kFileStreamingTimeoutPerGB.InSeconds();
}

// Returns scoped_refptr to FileSystemContext for an url.
scoped_refptr<storage::FileSystemContext> GetScopedFileSystemContext(
    Profile* const profile,
    const GURL& url) {
  content::StoragePartition* const storage =
      profile->content::BrowserContext::GetStoragePartitionForUrl(url);
  DCHECK(storage);
  return storage->GetFileSystemContext();
}

// Converts the given url to a FileSystemURL.
file_manager::util::FileSystemURLAndHandle GetFileSystemURLAndHandle(
    const storage::FileSystemContext& context,
    const GURL& url) {
  // Obtain the absolute path in the file system.
  const base::FilePath virtual_path = ash::ExternalFileURLToVirtualPath(url);
  DCHECK(!virtual_path.empty());
  // Obtain the file system URL.
  return file_manager::util::CreateIsolatedURLFromVirtualPath(
      context, url::Origin(), virtual_path);
}

std::string StripPathComponents(const std::string& file_name) {
  return base::FilePath(file_name).BaseName().AsUTF8Unsafe();
}
}  // namespace

ShareInfoFileHandler::FileShareConfig::FileShareConfig() = default;
ShareInfoFileHandler::FileShareConfig::~FileShareConfig() = default;

ShareInfoFileHandler::ShareInfoFileHandler(
    Profile* profile,
    mojom::ShareIntentInfo* share_info,
    base::FilePath directory,
    scoped_refptr<base::SequencedTaskRunner> task_runner)
    : profile_(profile), task_runner_(task_runner) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(profile_);
  DCHECK(task_runner_);
  DCHECK(share_info);

  file_config_.directory = directory;
  if (share_info->files.has_value()) {
    for (const auto& file_info : share_info->files.value()) {
      GURL externalFileUrl = ArcUrlToExternalFileUrl(file_info->content_uri);
      file_config_.external_urls.emplace_back(externalFileUrl);
      file_config_.mime_types.emplace_back(file_info->mime_type);
      file_config_.names.emplace_back(file_info->name);
      file_config_.sizes.emplace_back(file_info->size);
      if (file_info->size > 0) {
        file_config_.total_size +=
            base::checked_cast<uint64_t>(file_info->size);
      }
    }
    file_config_.num_files = file_config_.external_urls.size();
  }
}

ShareInfoFileHandler::~ShareInfoFileHandler() = default;

// static
file_manager::util::FileSystemURLAndHandle
ShareInfoFileHandler::GetFileSystemURL(content::BrowserContext* context,
                                       const GURL& url) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(context);

  Profile* const profile = Profile::FromBrowserContext(context);
  DCHECK(profile);

  scoped_refptr<storage::FileSystemContext> file_system_context =
      GetScopedFileSystemContext(profile, url);
  DCHECK(file_system_context.get());

  return GetFileSystemURLAndHandle(*file_system_context, url);
}

const std::vector<base::FilePath>& ShareInfoFileHandler::GetFilePaths() const {
  return file_config_.paths;
}

const std::vector<std::string>& ShareInfoFileHandler::GetMimeTypes() const {
  return file_config_.mime_types;
}

void ShareInfoFileHandler::StartPreparingFiles(
    StartedCallback started_callback,
    CompletedCallback completed_callback,
    ProgressBarUpdateCallback update_callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(started_callback);
  DCHECK(completed_callback);
  DCHECK(update_callback);
  DCHECK(task_runner_);

  started_callback_ = std::move(started_callback);
  completed_callback_ = std::move(completed_callback);
  update_callback_ = std::move(update_callback);
  file_sharing_started_ = true;

  if (!g_browser_process) {
    LOG(ERROR) << "Unexpected null g_browser_process.";
    UpdateNearbyShareDataHandlingFail(DataHandlingResult::kNullGBrowserProcess);
    NotifyFileSharingCompleted(base::File::FILE_ERROR_INVALID_OPERATION);
    return;
  }

  // |profile_| needs to be checked with ProfileManager::IsValidProfile
  // before using it.  Abort if profile is not created.
  if (g_browser_process->profile_manager() &&
      !g_browser_process->profile_manager()->IsValidProfile(profile_)) {
    LOG(ERROR) << "Invalid profile: " << profile_->GetProfileUserName();
    UpdateNearbyShareDataHandlingFail(DataHandlingResult::kInvalidProfile);
    NotifyFileSharingCompleted(base::File::FILE_ERROR_INVALID_OPERATION);
    return;
  }

  if (file_config_.directory.empty()) {
    LOG(ERROR) << "Base directory is empty.";
    UpdateNearbyShareDataHandlingFail(DataHandlingResult::kEmptyDirectory);
    NotifyFileSharingCompleted(base::File::FILE_ERROR_NOT_A_DIRECTORY);
    return;
  }

  VLOG(1)
      << "Creating unique directory for share and converting URLs to files.";
  task_runner_->PostTaskAndReplyWithResult(
      FROM_HERE,
      base::BindOnce(&ShareInfoFileHandler::CreateShareDirectory, this),
      base::BindOnce(&ShareInfoFileHandler::OnShareDirectoryPathCreated,
                     weak_ptr_factory_.GetWeakPtr()));
}

base::FilePath ShareInfoFileHandler::CreateShareDirectory() {
  if (!base::PathExists(file_config_.directory)) {
    LOG(ERROR) << "Base directory does not exist: " << file_config_.directory;
    UpdateNearbyShareDataHandlingFail(
        DataHandlingResult::kDirectoryDoesNotExist);
    return base::FilePath();
  }

  // Prepare a temporary share directory to store cached share files.
  base::FilePath temp_dir;
  constexpr char kShareDirPrefix[] = "share-";
  if (!base::CreateTemporaryDirInDir(file_config_.directory, kShareDirPrefix,
                                     &temp_dir) ||
      !base::PathExists(temp_dir)) {
    LOG(ERROR) << "Failed to create unique temp share directory under: "
               << file_config_.directory;
    UpdateNearbyShareDataHandlingFail(
        DataHandlingResult::kFailedToCreateDirectory);
    return base::FilePath();
  }
  return temp_dir;
}

void ShareInfoFileHandler::OnShareDirectoryPathCreated(
    base::FilePath share_dir) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(started_callback_);
  DCHECK(task_runner_);

  if (share_dir.empty()) {
    LOG(ERROR) << "Failed to prepare temp share directory.";
    UpdateNearbyShareDataHandlingFail(
        DataHandlingResult::kFailedPrepTempDirectory);
    NotifyFileSharingCompleted(base::File::FILE_ERROR_FAILED);
    return;
  }

  auto urls_size = file_config_.external_urls.size();
  if (!urls_size) {
    LOG(ERROR) << "External urls are empty.";
    UpdateNearbyShareDataHandlingFail(DataHandlingResult::kEmptyExternalURL);
    NotifyFileSharingCompleted(base::File::FILE_ERROR_INVALID_URL);
    return;
  }

  for (size_t i = 0; i < urls_size; i++) {
    const GURL& url = file_config_.external_urls[i];
    const std::string file_name = file_config_.names[i];
    int64_t file_size = file_config_.sizes[i];

    if (file_size < 0) {
      LOG(ERROR) << "Invalid size provided for file name: " << file_name;
      UpdateNearbyShareDataHandlingFail(
          DataHandlingResult::kInvalidFileNameSize);
      NotifyFileSharingCompleted(base::File::FILE_ERROR_NOT_A_FILE);
      return;
    }

    const base::FilePath dest_file_path =
        share_dir.AppendASCII(StripPathComponents(file_name));

    task_runner_->PostTaskAndReplyWithResult(
        FROM_HERE,
        base::BindOnce(&ShareInfoFileHandler::CreateFileForWrite, this,
                       dest_file_path),
        base::BindOnce(&ShareInfoFileHandler::OnFileDescriptorCreated,
                       weak_ptr_factory_.GetWeakPtr(), url, dest_file_path,
                       file_size));
  }
  std::move(started_callback_).Run();
}

base::ScopedFD ShareInfoFileHandler::CreateFileForWrite(
    const base::FilePath& file_path) {
  DCHECK(!file_path.empty());

  base::File dest_file(file_path,
                       base::File::FLAG_CREATE | base::File::FLAG_WRITE);
  if (!dest_file.IsValid() || !base::PathExists(file_path)) {
    LOG(ERROR) << "Invalid destination file at path: " << file_path;
    UpdateNearbyShareDataHandlingFail(
        DataHandlingResult::kInvalidDestinationFilePath);
    return base::ScopedFD();
  }

  return base::ScopedFD(dest_file.TakePlatformFile());
}

void ShareInfoFileHandler::OnFileDescriptorCreated(
    const GURL& url,
    const base::FilePath& dest_file_path,
    const int64_t file_size,
    base::ScopedFD dest_fd) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(url.is_valid());
  DCHECK(!dest_file_path.empty());
  DCHECK_GE(file_size, 0);

  if (!dest_fd.is_valid()) {
    LOG(ERROR) << "Invalid destination file descriptor.";
    UpdateNearbyShareDataHandlingFail(
        DataHandlingResult::kInvalidDestinationFileDescriptor);
    NotifyFileSharingCompleted(base::File::FILE_ERROR_FAILED);
    return;
  }

  contexts_.emplace_front();
  auto it_context = contexts_.begin();
  *it_context = GetScopedFileSystemContext(profile_, url);
  DCHECK(it_context->get());

  const file_manager::util::FileSystemURLAndHandle isolated_file_system =
      GetFileSystemURLAndHandle(**it_context, url);

  if (!isolated_file_system.url.is_valid()) {
    LOG(ERROR) << "Invalid FileSystemURL from handle.";
    UpdateNearbyShareDataHandlingFail(
        DataHandlingResult::kInvalidFileSystemURL);
    NotifyFileSharingCompleted(base::File::FILE_ERROR_INVALID_URL);
    return;
  }

  // Check if the obtained path providing external file URL or not.
  if (!ash::IsExternalFileURLType(isolated_file_system.url.type())) {
    LOG(ERROR) << "FileSystemURL is not of external file type.";
    UpdateNearbyShareDataHandlingFail(DataHandlingResult::kNotExternalFileType);
    NotifyFileSharingCompleted(base::File::FILE_ERROR_INVALID_URL);
    return;
  }

  file_stream_adapters_.emplace_front();
  auto it_stream_adapter = file_stream_adapters_.begin();
  *it_stream_adapter = base::MakeRefCounted<ShareInfoFileStreamAdapter>(
      *it_context, isolated_file_system.url, /*offset=*/0, file_size,
      kStreamReaderBufSizeInBytes, std::move(dest_fd),
      base::BindOnce(&ShareInfoFileHandler::OnFileStreamReadCompleted,
                     weak_ptr_factory_.GetWeakPtr(),
                     isolated_file_system.url.DebugString(), it_stream_adapter,
                     isolated_file_system.handle.id(), file_size));
  storage::IsolatedContext::GetInstance()->AddReference(
      isolated_file_system.handle.id());

  (*it_stream_adapter)->StartRunner();
  file_config_.paths.push_back(dest_file_path);

  // Used to measure time duration of file stream transfers. For reference,
  // local testing on caroline for 1.2GB takes around 1 minute.
  file_streaming_started_ = base::TimeTicks::Now();

  const int64_t timeout_seconds =
      GetTimeoutInSecondsFromBytes(GetTotalSizeOfFiles());
  const std::string timeout_message = base::StringPrintf(
      "File streaming did not complete within %" PRId64 " second(s).",
      timeout_seconds);
  if (!file_streaming_timer_.IsRunning()) {
    file_streaming_timer_.Start(
        FROM_HERE, base::Seconds(timeout_seconds),
        base::BindOnce(&ShareInfoFileHandler::OnFileStreamingTimeout,
                       weak_ptr_factory_.GetWeakPtr(), timeout_message));
  }
}

void ShareInfoFileHandler::OnFileStreamReadCompleted(
    const std::string& url_str,
    std::list<scoped_refptr<ShareInfoFileStreamAdapter>>::iterator it_adapter,
    const std::string& file_system_id,
    const int64_t bytes_read,
    bool result) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(!url_str.empty());
  DCHECK(!file_system_id.empty());
  DCHECK_GT(bytes_read, 0);

  storage::IsolatedContext::GetInstance()->RemoveReference(file_system_id);
  file_stream_adapters_.erase(it_adapter);

  if (!result) {
    LOG(ERROR) << "Failed to stream file IO data using url: " << url_str;
    UpdateNearbyShareDataHandlingFail(
        DataHandlingResult::kFailedStreamFileIOData);
    NotifyFileSharingCompleted(base::File::FILE_ERROR_IO);
    return;
  }

  num_bytes_read_ += base::checked_cast<uint64_t>(bytes_read);
  num_files_streamed_++;

  const uint64_t expected_total_bytes = GetTotalSizeOfFiles();
  const size_t expected_total_files = GetNumberOfFiles();
  DVLOG(1) << "Streamed " << num_bytes_read_ << " of " << expected_total_bytes
           << " bytes for " << num_files_streamed_ << " of "
           << expected_total_files << " files";
  if (update_callback_) {
    update_callback_.Run(base::checked_cast<double>(num_bytes_read_) /
                         expected_total_bytes);
  }

  if (num_files_streamed_ == expected_total_files &&
      num_bytes_read_ >= expected_total_bytes) {
    if (num_bytes_read_ > expected_total_bytes) {
      LOG(ERROR) << "Invalid number of bytes read: " << num_bytes_read_ << " > "
                 << expected_total_bytes;
      UpdateNearbyShareDataHandlingFail(
          DataHandlingResult::kInvalidNumberBytesRead);
      NotifyFileSharingCompleted(base::File::FILE_ERROR_INVALID_OPERATION);
      return;
    }

    // Update file streaming duration UMA metric if transfer was successful.
    const base::TimeDelta file_streaming_duration =
        base::TimeTicks::Now() - file_streaming_started_;
    UpdateNearbyShareFileStreamCompleteTime(file_streaming_duration);

    DVLOG(1) << "OnFileStreamReadCompleted: Completed streaming all files.";
    NotifyFileSharingCompleted(base::File::FILE_OK);
  }
}

void ShareInfoFileHandler::OnFileStreamingTimeout(
    const std::string& timeout_message) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  LOG(ERROR) << timeout_message;
  UpdateNearbyShareDataHandlingFail(DataHandlingResult::kTimeout);
  NotifyFileSharingCompleted(base::File::FILE_ERROR_ABORT);
}

void ShareInfoFileHandler::NotifyFileSharingCompleted(
    base::File::Error result) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  // Stop active timer and reset sharing params.
  if (file_streaming_timer_.IsRunning()) {
    file_streaming_timer_.Stop();
  }
  num_bytes_read_ = 0;
  num_files_streamed_ = 0;

  // Only call |completed_callback_| if not null and file sharing is in started
  // state to prevent calling more than once.
  if (file_sharing_started_ && completed_callback_) {
    file_sharing_started_ = false;
    std::move(completed_callback_).Run(result);
  }
}

}  // namespace arc