File: file_system_chooser.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (115 lines) | stat: -rw-r--r-- 4,365 bytes parent folder | download
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_FILE_SYSTEM_ACCESS_FILE_SYSTEM_CHOOSER_H_
#define CONTENT_BROWSER_FILE_SYSTEM_ACCESS_FILE_SYSTEM_CHOOSER_H_

#include <string>

#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/functional/callback_helpers.h"
#include "base/task/task_runner.h"
#include "base/thread_annotations.h"
#include "content/common/content_export.h"
#include "content/public/browser/file_system_access_entry_factory.h"
#include "storage/browser/file_system/isolated_context.h"
#include "third_party/blink/public/mojom/file_system_access/file_system_access_manager.mojom.h"
#include "ui/shell_dialogs/select_file_dialog.h"

namespace content {

class WebContents;

// This is a ui::SelectFileDialog::Listener implementation that grants access to
// the selected files to a specific renderer process on success, and then calls
// a callback on a specific task runner. Furthermore the listener will delete
// itself when any of its listener methods are called.
// All of this class has to be called on the UI thread.
class CONTENT_EXPORT FileSystemChooser : public ui::SelectFileDialog::Listener {
 public:
  using PathType = FileSystemAccessEntryFactory::PathType;
  struct ResultEntry {
    PathType type;
    base::FilePath path;
  };

  using ResultCallback =
      base::OnceCallback<void(blink::mojom::FileSystemAccessErrorPtr,
                              std::vector<ResultEntry>)>;

  class CONTENT_EXPORT Options {
   public:
    Options(ui::SelectFileDialog::Type type,
            blink::mojom::AcceptsTypesInfoPtr accepts_types_info,
            std::u16string title,
            base::FilePath default_directory,
            base::FilePath suggested_name);
    Options(const Options&);

    ui::SelectFileDialog::Type type() const { return type_; }
    const ui::SelectFileDialog::FileTypeInfo& file_type_info() const {
      return file_types_;
    }
    const std::u16string& title() const { return title_; }
    const base::FilePath& default_path() const { return default_path_; }
    int default_file_type_index() const { return default_file_type_index_; }

   private:
    base::FilePath ResolveSuggestedNameExtension(
        base::FilePath suggested_name,
        ui::SelectFileDialog::FileTypeInfo& file_types);

    ui::SelectFileDialog::Type type_;
    ui::SelectFileDialog::FileTypeInfo file_types_;
    int default_file_type_index_ = 0;
    std::u16string title_;
    base::FilePath default_path_;
  };

  static void CreateAndShow(WebContents* web_contents,
                            const Options& options,
                            ResultCallback callback,
                            base::ScopedClosureRunner fullscreen_block);

  // Returns whether the specified extension receives special handling by the
  // Windows shell. These extensions should be sanitized before being shown in
  // the "save as" file picker.
  static bool IsShellIntegratedExtension(
      const base::FilePath::StringType& extension);

  FileSystemChooser(ui::SelectFileDialog::Type type,
                    ResultCallback callback,
                    base::ScopedClosureRunner fullscreen_block);

 private:
  ~FileSystemChooser() override;

  // ui::SelectFileDialog::Listener:
  void FileSelected(const base::FilePath& path,
                    int index,
                    void* params) override;
  void MultiFilesSelected(const std::vector<base::FilePath>& files,
                          void* params) override;
  void FileSelectedWithExtraInfo(const ui::SelectedFileInfo& file,
                                 int index,
                                 void* params) override;
  void MultiFilesSelectedWithExtraInfo(
      const std::vector<ui::SelectedFileInfo>& files,
      void* params) override;
  void FileSelectionCanceled(void* params) override;

  SEQUENCE_CHECKER(sequence_checker_);

  ResultCallback callback_ GUARDED_BY_CONTEXT(sequence_checker_);
  ui::SelectFileDialog::Type type_ GUARDED_BY_CONTEXT(sequence_checker_);
  base::ScopedClosureRunner fullscreen_block_
      GUARDED_BY_CONTEXT(sequence_checker_);

  scoped_refptr<ui::SelectFileDialog> dialog_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_FILE_SYSTEM_ACCESS_FILE_SYSTEM_CHOOSER_H_