File: fake_select_file_dialog.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 (105 lines) | stat: -rw-r--r-- 3,166 bytes parent folder | download | duplicates (7)
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
// 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 "ui/shell_dialogs/fake_select_file_dialog.h"

#include <string_view>

#include "ui/shell_dialogs/select_file_policy.h"
#include "ui/shell_dialogs/selected_file_info.h"
#include "url/gurl.h"

namespace ui {

FakeSelectFileDialog::Factory::Factory() = default;
FakeSelectFileDialog::Factory::~Factory() = default;

ui::SelectFileDialog* FakeSelectFileDialog::Factory::Create(
    ui::SelectFileDialog::Listener* listener,
    std::unique_ptr<ui::SelectFilePolicy> policy) {
  FakeSelectFileDialog* dialog =
      new FakeSelectFileDialog(opened_callback_, listener, std::move(policy));
  last_dialog_ = dialog->GetWeakPtr();
  return dialog;
}

FakeSelectFileDialog* FakeSelectFileDialog::Factory::GetLastDialog() const {
  return last_dialog_.get();
}

void FakeSelectFileDialog::Factory::SetOpenCallback(
    base::RepeatingClosure callback) {
  opened_callback_ = callback;
}

// static
FakeSelectFileDialog::Factory* FakeSelectFileDialog::RegisterFactory() {
  auto factory = std::make_unique<Factory>();
  Factory* result = factory.get();
  ui::SelectFileDialog::SetFactory(std::move(factory));
  return result;
}

FakeSelectFileDialog::FakeSelectFileDialog(
    const base::RepeatingClosure& opened,
    Listener* listener,
    std::unique_ptr<ui::SelectFilePolicy> policy)
    : ui::SelectFileDialog(listener, std::move(policy)), opened_(opened) {}

FakeSelectFileDialog::~FakeSelectFileDialog() = default;

bool FakeSelectFileDialog::HasMultipleFileTypeChoicesImpl() {
  return true;
}

bool FakeSelectFileDialog::IsRunning(gfx::NativeWindow owning_window) const {
  return true;
}

void FakeSelectFileDialog::SelectFileImpl(
    Type type,
    const std::u16string& title,
    const base::FilePath& default_path,
    const FileTypeInfo* file_types,
    int file_type_index,
    const base::FilePath::StringType& default_extension,
    gfx::NativeWindow owning_window,
    const GURL* caller) {
  title_ = title;
  if (file_types)
    file_types_ = *file_types;
  default_extension_ = base::FilePath(default_extension).MaybeAsASCII();
  caller_ = caller;
  opened_.Run();
}

bool FakeSelectFileDialog::CallFileSelected(const base::FilePath& file_path,
                                            std::string_view filter_text) {
  for (size_t index = 0; index < file_types_.extensions.size(); ++index) {
    for (const base::FilePath::StringType& ext :
         file_types_.extensions[index]) {
      if (base::FilePath(ext).MaybeAsASCII() == filter_text) {
        // FileSelected accepts a 1-based index.
        listener_->FileSelected(SelectedFileInfo(file_path), index + 1);
        return true;
      }
    }
  }
  return false;
}

void FakeSelectFileDialog::CallMultiFilesSelected(
    const std::vector<base::FilePath>& files) {
  listener_->MultiFilesSelected(FilePathListToSelectedFileInfoList(files));
}

void FakeSelectFileDialog::CallFileSelectionCanceled() {
  listener_->FileSelectionCanceled();
}

void FakeSelectFileDialog::ListenerDestroyed() {
  listener_ = nullptr;
}

}  // namespace ui