1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_BROWSER_WEB_LAUNCH_WEB_LAUNCH_FILES_HELPER_H_
6 #define CHROME_BROWSER_WEB_LAUNCH_WEB_LAUNCH_FILES_HELPER_H_
7 
8 #include <vector>
9 
10 #include "base/files/file_path.h"
11 #include "base/macros.h"
12 #include "base/memory/scoped_refptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "content/public/browser/native_file_system_entry_factory.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "content/public/browser/web_contents_user_data.h"
17 #include "third_party/blink/public/mojom/file_system_access/native_file_system_directory_handle.mojom-forward.h"
18 #include "third_party/blink/public/mojom/web_launch/web_launch.mojom.h"
19 #include "url/gurl.h"
20 
21 namespace content {
22 
23 class WebContents;
24 class NavigationHandle;
25 
26 }  // namespace content
27 
28 namespace web_launch {
29 
30 // A helper for sending launch paths to the renderer process.
31 // Launch files cannot be sent immediately because the data is stored on a
32 // document for |launch_url_|, which is not created until |launch_url_| is
33 // committed.
34 //
35 // Note: The lifetime of this class is tied to the WebContents it is attached
36 // to. However, in general it will be destroyed before the WebContents, when the
37 // helper sends the NativeFileSystemEntries to the renderer.
38 class WebLaunchFilesHelper
39     : public content::WebContentsObserver,
40       public content::WebContentsUserData<WebLaunchFilesHelper> {
41  public:
42   WEB_CONTENTS_USER_DATA_KEY_DECL();
43   static void SetLaunchPaths(content::WebContents* web_contents,
44                              const GURL& launch_url,
45                              std::vector<base::FilePath> launch_paths);
46 
47   // System Web Apps Only. |launch_dir| is prepended to |launch_entries_| and
48   // sent to the JavaScript side.
49   static void SetLaunchDirectoryAndLaunchPaths(
50       content::WebContents* web_contents,
51       const GURL& launch_url,
52       base::FilePath launch_dir,
53       std::vector<base::FilePath> launch_paths);
54 
55   WebLaunchFilesHelper(content::WebContents* web_contents,
56                        const GURL& launch_url,
57                        std::vector<base::FilePath> launch_paths);
58 
59   // System Web Apps Only.
60   WebLaunchFilesHelper(content::WebContents* web_contents,
61                        const GURL& launch_url,
62                        base::FilePath launch_dir,
63                        std::vector<base::FilePath> launch_paths);
64 
65   ~WebLaunchFilesHelper() override;
66 
67   // content::WebContentsObserver:
68   void DidFinishNavigation(content::NavigationHandle* handle) override;
69 
70  private:
71   // Sends the launch entries to the renderer if they have been created and the
72   // renderer is ready to receive them.
73   void MaybeSendLaunchEntries();
74 
75   // The entries causing the launch (may be empty).
76   std::vector<blink::mojom::NativeFileSystemEntryPtr> launch_entries_;
77 
78   // The url the launch entries are for.
79   GURL launch_url_;
80 
81   base::WeakPtrFactory<WebLaunchFilesHelper> weak_ptr_factory_{this};
82   DISALLOW_COPY_AND_ASSIGN(WebLaunchFilesHelper);
83 };
84 
85 }  // namespace web_launch
86 
87 #endif  // CHROME_BROWSER_WEB_LAUNCH_WEB_LAUNCH_FILES_HELPER_H_
88