File: file_chooser_chromeos.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (124 lines) | stat: -rw-r--r-- 4,227 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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/file_transfer/file_chooser_chromeos.h"

#include <memory>
#include <string>

#include "base/files/file_path.h"
#include "base/location.h"
#include "base/memory/scoped_refptr.h"
#include "base/notreached.h"
#include "base/task/bind_post_task.h"
#include "base/task/sequenced_task_runner.h"
#include "base/threading/sequence_bound.h"
#include "remoting/base/string_resources.h"
#include "remoting/host/chromeos/ash_proxy.h"
#include "remoting/host/file_transfer/file_chooser.h"
#include "remoting/protocol/file_transfer_helpers.h"
#include "ui/aura/window.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/shell_dialogs/select_file_dialog.h"
#include "ui/shell_dialogs/select_file_policy.h"
#include "ui/shell_dialogs/selected_file_info.h"

namespace remoting {

class FileChooserChromeOs::Core : public ui::SelectFileDialog::Listener {
 public:
  explicit Core(ResultCallback callback);

  Core(const Core&) = delete;
  Core& operator=(const Core&) = delete;

  ~Core() override;

  void Show();

  // ui::SelectFileDialog::Listener implementation.
  void FileSelected(const ui::SelectedFileInfo& file, int index) override;
  void FileSelectionCanceled() override;

 private:
  void RunCallback(const FileChooser::Result& result);

  void Cleanup();

  scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
  const raw_ref<AshProxy, LeakedDanglingUntriaged> ash_;
  FileChooser::ResultCallback callback_;
};

FileChooserChromeOs::FileChooserChromeOs(
    scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
    ResultCallback callback) {
  auto callback_on_current_thread =
      base::BindPostTaskToCurrentDefault<void, Result>(std::move(callback));
  file_chooser_core_ = base::SequenceBound<FileChooserChromeOs::Core>(
      std::move(ui_task_runner), std::move(callback_on_current_thread));
}

FileChooserChromeOs::~FileChooserChromeOs() = default;

void FileChooserChromeOs::Show() {
  file_chooser_core_.AsyncCall(&FileChooserChromeOs::Core::Show);
}

////////////////////////////////////////////////////////////////////////////////
//   FileChooserChromeOs::Core
////////////////////////////////////////////////////////////////////////////////

FileChooserChromeOs::Core::Core(ResultCallback callback)
    : select_file_dialog_(ui::SelectFileDialog::Create(
          /*listener=*/this,
          /*policy=*/nullptr)),
      ash_(AshProxy::Get()),
      callback_(std::move(callback)) {}

FileChooserChromeOs::Core::~Core() {
  select_file_dialog_->ListenerDestroyed();
}

void FileChooserChromeOs::Core::FileSelected(const ui::SelectedFileInfo& file,
                                             int index) {
  RunCallback(file.file_path);
}

void FileChooserChromeOs::Core::FileSelectionCanceled() {
  RunCallback(protocol::MakeFileTransferError(
      FROM_HERE, protocol::FileTransfer_Error_Type_CANCELED));
}

void FileChooserChromeOs::Core::RunCallback(const FileChooser::Result& result) {
  std::move(callback_).Run(result);
}

void FileChooserChromeOs::Core::Show() {
  // Restrict file access to the native volumes on the device and USB drives.
  // This will prevent the remote user from selecting files from mapped network
  // drives and other url paths (for eg. Google Drive). See b/297183388 for more
  // information.
  ui::SelectFileDialog::FileTypeInfo file_type_info;
  file_type_info.allowed_paths =
      ui::SelectFileDialog::FileTypeInfo::AllowedPaths::NATIVE_PATH;

  select_file_dialog_->SelectFile(
      ui::SelectFileDialog::SELECT_OPEN_FILE,
      /*title=*/std::u16string(),
      /*default_path=*/base::FilePath(),
      /*file_types=*/&file_type_info,
      /*file_type_index=*/0,
      /*default_extension=*/base::FilePath::StringType(),
      /*owning_window=*/ash_->GetSelectFileContainer());
}

std::unique_ptr<FileChooser> FileChooser::Create(
    scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
    ResultCallback callback) {
  return std::make_unique<FileChooserChromeOs>(std::move(ui_task_runner),
                                               std::move(callback));
}

}  // namespace remoting