File: select_file_dialog_extension.cc

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 (625 lines) | stat: -rw-r--r-- 23,252 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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// 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 "chrome/browser/ui/views/select_file_dialog_extension.h"

#include <map>
#include <memory>
#include <string>
#include <utility>

#include "ash/public/cpp/shell_window_ids.h"
#include "ash/public/cpp/tablet_mode.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/no_destructor.h"
#include "base/strings/stringprintf.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/app_mode/app_mode_utils.h"
#include "chrome/browser/apps/platform_apps/app_window_registry_util.h"
#include "chrome/browser/ash/extensions/file_manager/select_file_dialog_extension_user_data.h"
#include "chrome/browser/ash/file_manager/app_id.h"
#include "chrome/browser/ash/file_manager/fileapi_util.h"
#include "chrome/browser/ash/file_manager/select_file_dialog_util.h"
#include "chrome/browser/ash/file_manager/url_util.h"
#include "chrome/browser/ash/login/ui/login_display_host.h"
#include "chrome/browser/ash/login/ui/login_web_dialog.h"
#include "chrome/browser/ash/login/ui/webui_login_view.h"
#include "chrome/browser/ash/policy/dlp/dlp_files_controller_ash.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_file_destination.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_view_host.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/webui/ash/system_web_dialog_delegate.h"
#include "chromeos/ui/base/window_properties.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/app_window/native_app_window.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "ui/aura/window.h"
#include "ui/base/base_window.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/color_palette.h"
#include "ui/shell_dialogs/select_file_policy.h"
#include "ui/shell_dialogs/selected_file_info.h"
#include "ui/views/widget/widget.h"

using extensions::AppWindow;

namespace {

const int kFileManagerWidth = 972;  // pixels
const int kFileManagerHeight = 640;  // pixels
const int kFileManagerMinimumWidth = 640;  // pixels
const int kFileManagerMinimumHeight = 240;  // pixels

constexpr char kFakeEntryURLScheme[] = "fake-entry://";
constexpr char kFakeEntryPath[] = "/.fake-entry";

// Holds references to file manager dialogs that have callbacks pending
// to their listeners.
class PendingDialog {
 public:
  static PendingDialog* GetInstance();
  void Add(SelectFileDialogExtension::RoutingID id,
           scoped_refptr<SelectFileDialogExtension> dialog);
  void Remove(SelectFileDialogExtension::RoutingID id);
  scoped_refptr<SelectFileDialogExtension> Find(
      SelectFileDialogExtension::RoutingID id);

 private:
  friend struct base::DefaultSingletonTraits<PendingDialog>;
  using Map = std::map<SelectFileDialogExtension::RoutingID,
                       scoped_refptr<SelectFileDialogExtension>>;
  Map map_;
};

// static
PendingDialog* PendingDialog::GetInstance() {
  static base::NoDestructor<PendingDialog> instance;
  return instance.get();
}

void PendingDialog::Add(SelectFileDialogExtension::RoutingID id,
                        scoped_refptr<SelectFileDialogExtension> dialog) {
  DCHECK(dialog.get());
  if (map_.find(id) == map_.end())
    map_.insert(std::make_pair(id, dialog));
  else
    DLOG(WARNING) << "Duplicate pending dialog " << id;
}

void PendingDialog::Remove(SelectFileDialogExtension::RoutingID id) {
  map_.erase(id);
}

scoped_refptr<SelectFileDialogExtension> PendingDialog::Find(
    SelectFileDialogExtension::RoutingID id) {
  Map::const_iterator it = map_.find(id);
  if (it == map_.end())
    return nullptr;
  return it->second;
}

// Return the Chrome OS WebUI login WebContents, if applicable.
content::WebContents* GetLoginWebContents() {
  auto* host = ash::LoginDisplayHost::default_host();
  return host ? host->GetOobeWebContents() : nullptr;
}

// Given |owner_window| finds corresponding |base_window|, it's associated
// |web_contents| and |profile|.
void FindRuntimeContext(gfx::NativeWindow owner_window,
                        ui::BaseWindow** base_window,
                        content::WebContents** web_contents) {
  *base_window = nullptr;
  *web_contents = nullptr;
  // To get the base_window and web contents, either a Browser or AppWindow is
  // needed.
  Browser* owner_browser = nullptr;
  AppWindow* app_window = nullptr;

  // If owner_window is supplied, use that to find a browser or a app window.
  if (owner_window) {
    owner_browser = chrome::FindBrowserWithWindow(owner_window);
    if (!owner_browser) {
      // If an owner_window was supplied but we couldn't find a browser, this
      // could be for a app window.
      app_window =
          AppWindowRegistryUtil::GetAppWindowForNativeWindowAnyProfile(
              owner_window);
    }
  }

  if (app_window) {
    *base_window = app_window->GetBaseWindow();
    *web_contents = app_window->web_contents();
  } else {
    // If the owning window is still unknown, this could be a background page or
    // and extension popup. Use the last active browser.
    if (!owner_browser)
      owner_browser = chrome::FindLastActive();
    if (owner_browser) {
      *base_window = owner_browser->window();
      *web_contents = owner_browser->tab_strip_model()->GetActiveWebContents();
    }
  }

  // In ChromeOS kiosk launch mode, we can still show file picker for
  // certificate manager dialog. There are no browser or webapp window
  // instances present in this case.
  if (chrome::IsRunningInForcedAppMode() && !(*web_contents))
    *web_contents = ash::LoginWebDialog::GetCurrentWebContents();

  // Check for a WebContents used for the Chrome OS WebUI login flow.
  if (!*web_contents)
    *web_contents = GetLoginWebContents();
}

SelectFileDialogExtension::RoutingID GetRoutingID(
    content::WebContents* web_contents,
    const SelectFileDialogExtension::Owner& owner) {
  if (owner.android_task_id.has_value())
    return base::StringPrintf("android.%d", *owner.android_task_id);

  // Lacros ids are already prefixed with "lacros".
  if (owner.lacros_window_id.has_value())
    return *owner.lacros_window_id;

  if (web_contents) {
    return base::StringPrintf(
        "web.%d", web_contents->GetPrimaryMainFrame()->GetFrameTreeNodeId());
  }
  LOG(ERROR) << "Unable to generate a RoutingID";
  return "";
}

}  // namespace

// A customization of SystemWebDialogDelegate that provides notifications
// to SelectFileDialogExtension about web dialog closing events. Must be outside
// anonymous namespace for the friend declaration to work.
class SystemFilesAppDialogDelegate : public ash::SystemWebDialogDelegate {
 public:
  SystemFilesAppDialogDelegate(base::WeakPtr<SelectFileDialogExtension> parent,
                               const std::string& id,
                               GURL url,
                               std::u16string title)
      : ash::SystemWebDialogDelegate(url, title),
        id_(id),
        parent_(std::move(parent)) {}
  ~SystemFilesAppDialogDelegate() override = default;

  void SetModal(bool modal) {
    set_modal_type(modal ? ui::MODAL_TYPE_WINDOW : ui::MODAL_TYPE_NONE);
  }

  FrameKind GetWebDialogFrameKind() const override {
    // The default is kDialog, however it doesn't allow to customize the title
    // color and to make the dialog movable and re-sizable.
    return FrameKind::kNonClient;
  }

  void GetMinimumDialogSize(gfx::Size* size) const override {
    size->set_width(kFileManagerMinimumWidth);
    size->set_height(kFileManagerMinimumHeight);
  }

  void GetDialogSize(gfx::Size* size) const override {
    *size = SystemWebDialogDelegate::ComputeDialogSizeForInternalScreen(
        {kFileManagerWidth, kFileManagerHeight});
  }

  void OnDialogShown(content::WebUI* webui) override {
    if (parent_) {
      parent_->OnSystemDialogShown(webui->GetWebContents(), id_);
    }
    ash::SystemWebDialogDelegate::OnDialogShown(webui);
  }

  void OnDialogWillClose() override {
    if (parent_) {
      parent_->OnSystemDialogWillClose();
    }
  }

 private:
  // The routing ID. We store it so that we can call back into the
  // SelectFileDialog to inform it about contents::WebContents and
  // the ID associated with it.
  const std::string id_;

  // The parent of this delegate.
  base::WeakPtr<SelectFileDialogExtension> parent_;
};

/////////////////////////////////////////////////////////////////////////////

SelectFileDialogExtension::Owner::Owner() = default;
SelectFileDialogExtension::Owner::~Owner() = default;
SelectFileDialogExtension::Owner::Owner(
    const SelectFileDialogExtension::Owner&) = default;
SelectFileDialogExtension::Owner& SelectFileDialogExtension::Owner::operator=(
    const SelectFileDialogExtension::Owner&) = default;
SelectFileDialogExtension::Owner::Owner(SelectFileDialogExtension::Owner&&) =
    default;
SelectFileDialogExtension::Owner& SelectFileDialogExtension::Owner::operator=(
    SelectFileDialogExtension::Owner&&) = default;

// static
SelectFileDialogExtension* SelectFileDialogExtension::Create(
    Listener* listener,
    std::unique_ptr<ui::SelectFilePolicy> policy) {
  return new SelectFileDialogExtension(listener, std::move(policy));
}

SelectFileDialogExtension::SelectFileDialogExtension(
    Listener* listener,
    std::unique_ptr<ui::SelectFilePolicy> policy)
    : SelectFileDialog(listener, std::move(policy)),
      system_files_app_web_contents_(nullptr) {}

SelectFileDialogExtension::~SelectFileDialogExtension() = default;

bool SelectFileDialogExtension::IsRunning(
    gfx::NativeWindow owner_window) const {
  return owner_.window == owner_window;
}

void SelectFileDialogExtension::ListenerDestroyed() {
  listener_ = nullptr;
  params_ = nullptr;
  PendingDialog::GetInstance()->Remove(routing_id_);
}

void SelectFileDialogExtension::OnSystemDialogShown(
    content::WebContents* web_contents,
    const std::string& id) {
  system_files_app_web_contents_ = web_contents;
  SelectFileDialogExtensionUserData::SetDialogDataForWebContents(
      web_contents, id, type_, owner_.dialog_caller);
}

void SelectFileDialogExtension::OnSystemDialogWillClose() {
  profile_ = nullptr;
  auto dialog_caller = owner_.dialog_caller;
  owner_ = {};
  system_files_app_web_contents_ = nullptr;
  PendingDialog::GetInstance()->Remove(routing_id_);
  // Actually invoke the appropriate callback on our listener.
  ApplyPolicyAndNotifyListener(std::move(dialog_caller));
}

// static
void SelectFileDialogExtension::OnFileSelected(
    RoutingID routing_id,
    const ui::SelectedFileInfo& file,
    int index) {
  scoped_refptr<SelectFileDialogExtension> dialog =
      PendingDialog::GetInstance()->Find(routing_id);
  if (!dialog.get())
    return;
  dialog->selection_type_ = SINGLE_FILE;
  dialog->selection_files_.clear();
  dialog->selection_files_.push_back(file);
  dialog->selection_index_ = index;
}

// static
void SelectFileDialogExtension::OnMultiFilesSelected(
    RoutingID routing_id,
    const std::vector<ui::SelectedFileInfo>& files) {
  scoped_refptr<SelectFileDialogExtension> dialog =
      PendingDialog::GetInstance()->Find(routing_id);
  if (!dialog.get())
    return;
  dialog->selection_type_ = MULTIPLE_FILES;
  dialog->selection_files_ = files;
  dialog->selection_index_ = 0;
}

// static
void SelectFileDialogExtension::OnFileSelectionCanceled(RoutingID routing_id) {
  scoped_refptr<SelectFileDialogExtension> dialog =
      PendingDialog::GetInstance()->Find(routing_id);
  if (!dialog.get())
    return;
  dialog->selection_type_ = CANCEL;
  dialog->selection_files_.clear();
  dialog->selection_index_ = 0;
}

content::RenderFrameHost* SelectFileDialogExtension::GetPrimaryMainFrame() {
  if (system_files_app_web_contents_)
    return system_files_app_web_contents_->GetPrimaryMainFrame();
  return nullptr;
}

GURL SelectFileDialogExtension::MakeDialogURL(
    Type type,
    const std::u16string& title,
    const base::FilePath& default_path,
    const FileTypeInfo* file_types,
    int file_type_index,
    const std::string& search_query,
    bool show_android_picker_apps,
    std::vector<std::string> volume_filter,
    Profile* profile) {
  base::FilePath relative_path;
  if (base::FilePath(kFakeEntryPath)
          .AppendRelativePath(default_path, &relative_path)) {
    GURL selection_url = GURL(kFakeEntryURLScheme + relative_path.value());
    GURL current_directory_url =
        GURL(kFakeEntryURLScheme + relative_path.DirName().value());
    return file_manager::util::GetFileManagerMainPageUrlWithParams(
        type, title, current_directory_url, selection_url, /*target_name=*/"",
        file_types, file_type_index, search_query, show_android_picker_apps,
        std::move(volume_filter));
  }

  base::FilePath download_default_path(
      DownloadPrefs::FromBrowserContext(profile)->DownloadPath());
  base::FilePath selection_path =
      default_path.IsAbsolute()
          ? default_path
          : download_default_path.Append(default_path.BaseName());
  base::FilePath fallback_path = profile->last_selected_directory().empty()
                                     ? download_default_path
                                     : profile->last_selected_directory();

  // Convert the above absolute paths to file system URLs.
  GURL selection_url;
  if (!file_manager::util::ConvertAbsoluteFilePathToFileSystemUrl(
          profile, selection_path, file_manager::util::GetFileManagerURL(),
          &selection_url)) {
    // Due to the current design, an invalid temporal cache file path may passed
    // as |default_path| (crbug.com/178013 #9-#11). In such a case, we use the
    // last selected directory as a workaround. Real fix is tracked at
    // crbug.com/110119.
    base::FilePath base_name = default_path.BaseName();
    if (!file_manager::util::ConvertAbsoluteFilePathToFileSystemUrl(
            profile,
            // If the base_name is absolute (happens for default_path '/') it's
            // not usable in Append.
            base_name.IsAbsolute() ? fallback_path
                                   : fallback_path.Append(base_name),
            file_manager::util::GetFileManagerURL(), &selection_url)) {
      DVLOG(1) << "Unable to resolve the selection URL.";
    }
  }

  GURL current_directory_url;
  base::FilePath current_directory_path = selection_path.DirName();
  if (!file_manager::util::ConvertAbsoluteFilePathToFileSystemUrl(
          profile, current_directory_path,
          file_manager::util::GetFileManagerURL(), &current_directory_url)) {
    // Fallback if necessary, see the comment above.
    if (!file_manager::util::ConvertAbsoluteFilePathToFileSystemUrl(
            profile, fallback_path, file_manager::util::GetFileManagerURL(),
            &current_directory_url)) {
      DVLOG(1) << "Unable to resolve the current directory URL for: "
               << fallback_path.value();
    }
  }

  return file_manager::util::GetFileManagerMainPageUrlWithParams(
      type, title, current_directory_url, selection_url,
      default_path.BaseName().value(), file_types, file_type_index,
      search_query, show_android_picker_apps, std::move(volume_filter));
}

void SelectFileDialogExtension::SelectFileWithFileManagerParams(
    Type type,
    const std::u16string& title,
    const base::FilePath& default_path,
    const FileTypeInfo* file_types,
    int file_type_index,
    void* params,
    const Owner& owner,
    const std::string& search_query,
    bool show_android_picker_apps,
    bool use_media_store_filter) {
  if (owner_.window) {
    LOG(ERROR) << "File dialog already in use!";
    return;
  }

  // The base window to associate the dialog with.
  ui::BaseWindow* base_window = nullptr;

  // The web contents to associate the dialog with.
  content::WebContents* web_contents = nullptr;

  // The folder selection dialog created for capture mode should never be
  // parented to a browser window (if one exists). https://crbug.com/1258842.
  const bool is_for_capture_mode =
      owner.window &&
      owner.window->GetId() ==
          ash::kShellWindowId_CaptureModeFolderSelectionDialogOwner;

  const bool skip_finding_browser = is_for_capture_mode ||
                                    owner.android_task_id.has_value() ||
                                    owner.lacros_window_id.has_value();

  can_resize_ = !ash::TabletMode::IsInTabletMode() && !is_for_capture_mode;

  // Obtain BaseWindow and WebContents if the owner window is browser.
  if (!skip_finding_browser)
    FindRuntimeContext(owner.window, &base_window, &web_contents);

  if (web_contents)
    profile_ = Profile::FromBrowserContext(web_contents->GetBrowserContext());

  // Handle the cases where |web_contents| is not available or |web_contents| is
  // associated with Default profile.
  if (!web_contents || ash::ProfileHelper::IsSigninProfile(profile_))
    profile_ = ProfileManager::GetActiveUserProfile();

  DCHECK(profile_);

  // Check if we have another dialog opened for the contents. It's unlikely, but
  // possible. In such situation, discard this request.
  RoutingID routing_id = GetRoutingID(web_contents, owner);
  if (PendingExists(routing_id))
    return;

  std::vector<std::string> volume_filter;
  if (owner.is_lacros) {
    // SelectFileAsh (Lacros) is opening the dialog: only show fusebox volumes
    // in File Manager UI to return real file descriptors to SelectFileAsh.
    volume_filter.push_back("fusebox-only");
  } else if (use_media_store_filter) {
    // ArcSelectFile is opening the dialog: add 'media-store-files-only' filter
    // to only show volumes in File Manager UI that are indexed by the Android
    // MediaStore.
    volume_filter.push_back("media-store-files-only");
  }

  GURL file_manager_url = SelectFileDialogExtension::MakeDialogURL(
      type, title, default_path, file_types, file_type_index, search_query,
      show_android_picker_apps, std::move(volume_filter), profile_);

  has_multiple_file_type_choices_ =
      !file_types || (file_types->extensions.size() > 1);

  std::u16string dialog_title =
      !title.empty() ? title
                     : file_manager::util::GetSelectFileDialogTitle(type);
  gfx::NativeWindow parent_window =
      base_window ? base_window->GetNativeWindow() : owner.window.get();

  owner_ = owner;
  type_ = type;

  auto* dialog_delegate = new SystemFilesAppDialogDelegate(
      weak_factory_.GetWeakPtr(), routing_id, file_manager_url, dialog_title);
  dialog_delegate->SetModal(owner.window != nullptr);
  dialog_delegate->set_can_resize(can_resize_);
  dialog_delegate->ShowSystemDialogForBrowserContext(profile_, parent_window);

  // Connect our listener to FileDialogFunction's per-tab callbacks.
  AddPending(routing_id);

  params_ = params;
  routing_id_ = routing_id;
}

void SelectFileDialogExtension::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 owner_window,
    void* params,
    const GURL* caller) {
  // |default_extension| is ignored.
  Owner owner;
  owner.window = owner_window;
  if (caller && caller->is_valid()) {
    owner.dialog_caller.emplace(*caller);
  }
  SelectFileWithFileManagerParams(type, title, default_path, file_types,
                                  file_type_index, params, owner,
                                  /*search_query=*/"",
                                  /*show_android_picker_apps=*/false);
}

bool SelectFileDialogExtension::HasMultipleFileTypeChoicesImpl() {
  return has_multiple_file_type_choices_;
}

bool SelectFileDialogExtension::IsResizeable() const {
  return can_resize_;
}

void SelectFileDialogExtension::ApplyPolicyAndNotifyListener(
    absl::optional<policy::DlpFileDestination> dialog_caller) {
  if (!listener_)
    return;

  // The selected files are passed by reference to the listener. Ensure they
  // outlive the dialog if it is immediately deleted by the listener.
  std::vector<ui::SelectedFileInfo> selection_files =
      std::move(selection_files_);
  selection_files_.clear();

  if (!dialog_caller.has_value() || selection_files.empty()) {
    NotifyListener(std::move(selection_files));
    return;
  }

  if (auto* files_controller =
          policy::DlpFilesControllerAsh::GetForPrimaryProfile();
      files_controller && type_ == Type::SELECT_SAVEAS_FILE) {
    files_controller->CheckIfDownloadAllowed(
        dialog_caller.value(),
        // TODO(crbug.com/1385687): Handle selection_files.size() > 1.
        selection_files[0].local_path.empty() ? selection_files[0].file_path
                                              : selection_files[0].local_path,
        base::BindOnce(
            [](base::WeakPtr<SelectFileDialogExtension> weak_ptr,
               std::vector<ui::SelectedFileInfo> selection_files,
               bool is_allowed) {
              if (!is_allowed)
                weak_ptr->selection_type_ = SelectionType::CANCEL;
              weak_ptr->NotifyListener(std::move(selection_files));
            },
            weak_factory_.GetWeakPtr(), std::move(selection_files)));
    return;
  } else if (files_controller) {
    files_controller->FilterDisallowedUploads(
        std::move(selection_files), dialog_caller.value(),
        base::BindOnce(
            [](base::WeakPtr<SelectFileDialogExtension> weak_ptr,
               std::vector<ui::SelectedFileInfo> allowed_files) {
              if (allowed_files.empty())
                weak_ptr->selection_type_ = SelectionType::CANCEL;
              weak_ptr->NotifyListener(std::move(allowed_files));
            },
            weak_factory_.GetWeakPtr()));
    return;
  }
  NotifyListener(std::move(selection_files));
}

void SelectFileDialogExtension::NotifyListener(
    std::vector<ui::SelectedFileInfo> selection_files) {
  if (!listener_)
    return;
  switch (selection_type_) {
    case CANCEL:
      listener_->FileSelectionCanceled(params_);
      break;
    case SINGLE_FILE:
      listener_->FileSelectedWithExtraInfo(selection_files[0], selection_index_,
                                           params_);
      break;
    case MULTIPLE_FILES:
      listener_->MultiFilesSelectedWithExtraInfo(selection_files, params_);
      break;
    default:
      NOTREACHED_NORETURN();
  }
}

void SelectFileDialogExtension::AddPending(RoutingID routing_id) {
  PendingDialog::GetInstance()->Add(routing_id, this);
}

// static
bool SelectFileDialogExtension::PendingExists(RoutingID routing_id) {
  return PendingDialog::GetInstance()->Find(routing_id).get() != nullptr;
}