1 // Copyright (c) 2013 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 CONTENT_RENDERER_PEPPER_PEPPER_FILE_SYSTEM_HOST_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_FILE_SYSTEM_HOST_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 
12 #include "base/files/file.h"
13 #include "base/macros.h"
14 #include "base/memory/weak_ptr.h"
15 #include "mojo/public/cpp/bindings/remote.h"
16 #include "ppapi/c/pp_file_info.h"
17 #include "ppapi/c/private/ppb_isolated_file_system_private.h"
18 #include "ppapi/host/host_message_context.h"
19 #include "ppapi/host/resource_host.h"
20 #include "third_party/blink/public/mojom/filesystem/file_system.mojom.h"
21 #include "url/gurl.h"
22 
23 namespace content {
24 
25 class RendererPpapiHost;
26 
27 class PepperFileSystemHost
28     : public ppapi::host::ResourceHost,
29       public base::SupportsWeakPtr<PepperFileSystemHost> {
30  public:
31   // Creates a new PepperFileSystemHost for a file system of a given |type|. The
32   // host will not be connected to any specific file system, and will need to
33   // subsequently be opened before use.
34   PepperFileSystemHost(RendererPpapiHost* host,
35                        PP_Instance instance,
36                        PP_Resource resource,
37                        PP_FileSystemType type);
38   // Creates a new PepperFileSystemHost with an existing file system at the
39   // given |root_url| and of the given |type|. The file system must already be
40   // opened. Once created, the PepperFileSystemHost is already opened for use.
41   PepperFileSystemHost(RendererPpapiHost* host,
42                        PP_Instance instance,
43                        PP_Resource resource,
44                        const GURL& root_url,
45                        PP_FileSystemType type);
46   ~PepperFileSystemHost() override;
47 
48   // ppapi::host::ResourceHost override.
49   int32_t OnResourceMessageReceived(
50       const IPC::Message& msg,
51       ppapi::host::HostMessageContext* context) override;
52   bool IsFileSystemHost() override;
53 
54   // Supports FileRefs direct access on the host side.
GetType()55   PP_FileSystemType GetType() const { return type_; }
IsOpened()56   bool IsOpened() const { return opened_; }
GetRootUrl()57   GURL GetRootUrl() const { return root_url_; }
58 
59  private:
60   // Callback for OpenFileSystem.
61   void DidOpenFileSystem(const std::string& name_unused,
62                          const GURL& root,
63                          base::File::Error error);
64   void DidFailOpenFileSystem(base::File::Error error);
65 
66   int32_t OnHostMsgOpen(ppapi::host::HostMessageContext* context,
67                         int64_t expected_size);
68   int32_t OnHostMsgInitIsolatedFileSystem(
69       ppapi::host::HostMessageContext* context,
70       const std::string& fsid,
71       PP_IsolatedFileSystemType_Private type);
72 
73   blink::mojom::FileSystemManager& GetFileSystemManager();
74 
75   RendererPpapiHost* renderer_ppapi_host_;
76   ppapi::host::ReplyMessageContext reply_context_;
77 
78   PP_FileSystemType type_;
79   bool opened_;  // whether open is successful.
80   GURL root_url_;
81   bool called_open_;  // whether open has been called.
82   mojo::Remote<blink::mojom::FileSystemManager> file_system_manager_;
83 
84   DISALLOW_COPY_AND_ASSIGN(PepperFileSystemHost);
85 };
86 
87 }  // namespace content
88 
89 #endif  // CONTENT_RENDERER_PEPPER_PEPPER_FILE_SYSTEM_HOST_H_
90