File: select_file_dialog_mac.mm

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 (192 lines) | stat: -rw-r--r-- 6,595 bytes parent folder | download | duplicates (5)
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
// Copyright 2012 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/select_file_dialog_mac.h"

#include <algorithm>

#include "base/check.h"
#include "base/functional/bind.h"
#include "base/notreached.h"
#include "base/threading/thread_restrictions.h"
#include "components/remote_cocoa/app_shim/select_file_dialog_bridge.h"
#include "components/remote_cocoa/browser/window.h"
#include "components/remote_cocoa/common/native_widget_ns_window.mojom.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "ui/shell_dialogs/select_file_policy.h"
#include "ui/shell_dialogs/selected_file_info.h"
#include "url/gurl.h"

using remote_cocoa::mojom::SelectFileDialogType;
using remote_cocoa::mojom::SelectFileTypeInfo;
using remote_cocoa::mojom::SelectFileTypeInfoPtr;

namespace ui {

using Type = SelectFileDialog::Type;
using FileTypeInfo = SelectFileDialog::FileTypeInfo;

SelectFileDialogImpl::SelectFileDialogImpl(
    Listener* listener,
    std::unique_ptr<ui::SelectFilePolicy> policy)
    : SelectFileDialog(listener, std::move(policy)), weak_factory_(this) {}

bool SelectFileDialogImpl::IsRunning(gfx::NativeWindow parent_window) const {
  for (const auto& dialog_data : dialog_data_list_) {
    if (dialog_data.parent_window == parent_window)
      return true;
  }
  return false;
}

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

void SelectFileDialogImpl::FileWasSelected(
    DialogData* dialog_data,
    bool is_multi,
    bool was_cancelled,
    const std::vector<base::FilePath>& files,
    int index,
    const std::vector<std::string>& file_tags) {
  auto it = std::ranges::find(dialog_data_list_, dialog_data,
                              [](const DialogData& d) { return &d; });
  DCHECK(it != dialog_data_list_.end());
  dialog_data_list_.erase(it);

  if (dialog_closed_callback_for_testing_)
    dialog_closed_callback_for_testing_.Run();

  if (!listener_)
    return;

  if (was_cancelled || files.empty()) {
    listener_->FileSelectionCanceled();
  } else {
    if (is_multi) {
      listener_->MultiFilesSelected(FilePathListToSelectedFileInfoList(files));
    } else {
      SelectedFileInfo file(files[0]);
      file.file_tags = file_tags;
      listener_->FileSelected(file, index);
    }
  }
}

void SelectFileDialogImpl::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 gfx_window,
    const GURL* caller) {
  DCHECK(type == SELECT_FOLDER || type == SELECT_UPLOAD_FOLDER ||
         type == SELECT_EXISTING_FOLDER || type == SELECT_OPEN_FILE ||
         type == SELECT_OPEN_MULTI_FILE || type == SELECT_SAVEAS_FILE);

  hasMultipleFileTypeChoices_ =
      file_types ? file_types->extensions.size() > 1 : true;

  // Add a new DialogData to the list. Note that it is safe to pass
  // |dialog_data| by pointer because it will only be removed from the list when
  // the callback is made or after the callback has been cancelled by
  // |weak_factory_|.
  dialog_data_list_.emplace_back(gfx_window);
  DialogData& dialog_data = dialog_data_list_.back();

  // Create a NSSavePanel for it.
  auto* mojo_window = remote_cocoa::GetWindowMojoInterface(gfx_window);
  auto receiver = dialog_data.select_file_dialog.BindNewPipeAndPassReceiver();
  if (mojo_window) {
    mojo_window->CreateSelectFileDialog(std::move(receiver));
  } else {
    NSWindow* ns_window = gfx_window.GetNativeNSWindow();
    mojo::MakeSelfOwnedReceiver(
        std::make_unique<remote_cocoa::SelectFileDialogBridge>(ns_window),
        std::move(receiver));
  }

  // Show the panel.
  SelectFileDialogType mojo_type = SelectFileDialogType::kFolder;
  switch (type) {
    case SELECT_FOLDER:
      mojo_type = SelectFileDialogType::kFolder;
      break;
    case SELECT_UPLOAD_FOLDER:
      mojo_type = SelectFileDialogType::kUploadFolder;
      break;
    case SELECT_EXISTING_FOLDER:
      mojo_type = SelectFileDialogType::kExistingFolder;
      break;
    case SELECT_OPEN_FILE:
      mojo_type = SelectFileDialogType::kOpenFile;
      break;
    case SELECT_OPEN_MULTI_FILE:
      mojo_type = SelectFileDialogType::kOpenMultiFile;
      break;
    case SELECT_SAVEAS_FILE:
      mojo_type = SelectFileDialogType::kSaveAsFile;
      break;
    default:
      NOTREACHED();
  }

  SelectFileTypeInfoPtr mojo_file_types;
  if (file_types) {
    mojo_file_types = SelectFileTypeInfo::New();
    mojo_file_types->extensions = file_types->extensions;
    mojo_file_types->extension_description_overrides =
        file_types->extension_description_overrides;
    mojo_file_types->include_all_files = file_types->include_all_files;
    mojo_file_types->keep_extension_visible =
        file_types->keep_extension_visible;
  }

  auto callback = base::BindOnce(&SelectFileDialogImpl::FileWasSelected,
                                 weak_factory_.GetWeakPtr(), &dialog_data,
                                 type == SELECT_OPEN_MULTI_FILE);

  dialog_data.select_file_dialog->Show(
      mojo_type, title, default_path, std::move(mojo_file_types),
      file_type_index, default_extension, std::move(callback));
}

SelectFileDialogImpl::DialogData::DialogData(gfx::NativeWindow parent_window_)
    : parent_window(parent_window_) {}

SelectFileDialogImpl::DialogData::~DialogData() {}

SelectFileDialogImpl::~SelectFileDialogImpl() {
  // Clear |weak_factory_| beforehand, to ensure that no callbacks will be made
  // when we cancel the NSSavePanels.
  weak_factory_.InvalidateWeakPtrs();

  // Walk through the open dialogs and issue the cancel callbacks that would
  // have been made.
  // TODO(https://crbug.com/340178601): This doesn't make sense - why would we
  // issue multiple undifferentiated FileSelectionCanceled() callbacks? Is it
  // ever possible for there to actually be more than one pending dialog?
  for (size_t i = 0; i < dialog_data_list_.size(); ++i) {
    if (listener_)
      listener_->FileSelectionCanceled();
  }

  // Cancel the NSSavePanels by destroying their bridges.
  dialog_data_list_.clear();
}

bool SelectFileDialogImpl::HasMultipleFileTypeChoicesImpl() {
  return hasMultipleFileTypeChoices_;
}

SelectFileDialog* CreateSelectFileDialog(
    SelectFileDialog::Listener* listener,
    std::unique_ptr<SelectFilePolicy> policy) {
  return new SelectFileDialogImpl(listener, std::move(policy));
}

}  // namespace ui