1 // Copyright (c) 2012 The Chromium Embedded Framework Authors. 2 // Portions copyright (c) 2012 The Chromium Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 6 #ifndef CEF_LIBCEF_BROWSER_FILE_DIALOG_MANAGER_H_ 7 #define CEF_LIBCEF_BROWSER_FILE_DIALOG_MANAGER_H_ 8 #pragma once 9 10 #include "include/cef_browser.h" 11 #include "libcef/browser/file_dialog_runner.h" 12 13 #include "base/compiler_specific.h" 14 #include "base/memory/weak_ptr.h" 15 #include "content/public/browser/web_contents_observer.h" 16 17 namespace content { 18 class FileSelectListener; 19 class WebContents; 20 } // namespace content 21 22 namespace net { 23 class DirectoryLister; 24 } 25 26 class AlloyBrowserHostImpl; 27 28 class CefFileDialogManager { 29 public: 30 // |runner| may be NULL if the platform doesn't implement dialogs. 31 CefFileDialogManager(AlloyBrowserHostImpl* browser, 32 std::unique_ptr<CefFileDialogRunner> runner); 33 ~CefFileDialogManager(); 34 35 // Delete the runner to free any platform constructs. 36 void Destroy(); 37 38 // Called from AlloyBrowserHostImpl::RunFileChooser. 39 // See CefBrowserHost::RunFileDialog documentation. 40 void RunFileDialog(cef_file_dialog_mode_t mode, 41 const CefString& title, 42 const CefString& default_file_path, 43 const std::vector<CefString>& accept_filters, 44 int selected_accept_filter, 45 CefRefPtr<CefRunFileDialogCallback> callback); 46 47 // Called from AlloyBrowserHostImpl::RunFileChooser. 48 // See WebContentsDelegate::RunFileChooser documentation. 49 void RunFileChooser(scoped_refptr<content::FileSelectListener> listener, 50 const blink::mojom::FileChooserParams& params); 51 52 // Run the file chooser dialog specified by |params|. Only a single dialog may 53 // be pending at any given time. |callback| will be executed asynchronously 54 // after the dialog is dismissed or if another dialog is already pending. 55 void RunFileChooser(const CefFileDialogRunner::FileChooserParams& params, 56 CefFileDialogRunner::RunFileChooserCallback callback); 57 58 private: 59 void RunFileChooserInternal( 60 const CefFileDialogRunner::FileChooserParams& params, 61 CefFileDialogRunner::RunFileChooserCallback callback); 62 63 // Used with the RunFileChooser variant where the caller specifies a callback 64 // (no associated RenderFrameHost). 65 void OnRunFileChooserCallback( 66 CefFileDialogRunner::RunFileChooserCallback callback, 67 int selected_accept_filter, 68 const std::vector<base::FilePath>& file_paths); 69 70 // Used with WebContentsDelegate::RunFileChooser when mode is 71 // blink::mojom::FileChooserParams::Mode::kUploadFolder. 72 void OnRunFileChooserUploadFolderDelegateCallback( 73 const blink::mojom::FileChooserParams::Mode mode, 74 scoped_refptr<content::FileSelectListener> listener, 75 int selected_accept_filter, 76 const std::vector<base::FilePath>& file_paths); 77 78 // Used with WebContentsDelegate::RunFileChooser to notify the 79 // RenderFrameHost. 80 void OnRunFileChooserDelegateCallback( 81 blink::mojom::FileChooserParams::Mode mode, 82 scoped_refptr<content::FileSelectListener> listener, 83 int selected_accept_filter, 84 const std::vector<base::FilePath>& file_paths); 85 86 // Clean up state associated with the last run. 87 void Cleanup(); 88 89 // AlloyBrowserHostImpl pointer is guaranteed to outlive this object. 90 AlloyBrowserHostImpl* browser_; 91 92 std::unique_ptr<CefFileDialogRunner> runner_; 93 94 // True if a file chooser is currently pending. 95 bool file_chooser_pending_; 96 97 // Used for asynchronously listing directory contents. 98 std::unique_ptr<net::DirectoryLister> lister_; 99 100 // Must be the last member. 101 base::WeakPtrFactory<CefFileDialogManager> weak_ptr_factory_; 102 103 DISALLOW_COPY_AND_ASSIGN(CefFileDialogManager); 104 }; 105 106 #endif // CEF_LIBCEF_BROWSER_JAVASCRIPT_DIALOG_MANAGER_H_ 107