• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 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_BROWSER_RENDERER_HOST_PEPPER_PEPPER_FILE_SYSTEM_BROWSER_HOST_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_FILE_SYSTEM_BROWSER_HOST_H_
7 
8 #include <queue>
9 #include <string>
10 #include <vector>
11 
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/weak_ptr.h"
15 #include "content/browser/renderer_host/pepper/quota_reservation.h"
16 #include "content/common/content_export.h"
17 #include "ppapi/c/pp_file_info.h"
18 #include "ppapi/c/private/ppb_isolated_file_system_private.h"
19 #include "ppapi/host/host_message_context.h"
20 #include "ppapi/host/resource_host.h"
21 #include "url/gurl.h"
22 #include "webkit/browser/fileapi/file_system_context.h"
23 
24 namespace content {
25 
26 class BrowserPpapiHost;
27 class PepperFileIOHost;
28 
29 class CONTENT_EXPORT PepperFileSystemBrowserHost
30     : public ppapi::host::ResourceHost,
31       public base::SupportsWeakPtr<PepperFileSystemBrowserHost> {
32  public:
33   // Creates a new PepperFileSystemBrowserHost for a file system of a given
34   // |type|. The host must be opened before use.
35   PepperFileSystemBrowserHost(BrowserPpapiHost* host,
36                               PP_Instance instance,
37                               PP_Resource resource,
38                               PP_FileSystemType type);
39   virtual ~PepperFileSystemBrowserHost();
40 
41   // Opens the PepperFileSystemBrowserHost to use an existing file system at the
42   // given |root_url|. The file system at |root_url| must already be opened and
43   // have the type given by GetType().
44   // Calls |callback| when complete.
45   void OpenExisting(const GURL& root_url, const base::Closure& callback);
46 
47   // ppapi::host::ResourceHost overrides.
48   virtual int32_t OnResourceMessageReceived(
49       const IPC::Message& msg,
50       ppapi::host::HostMessageContext* context) OVERRIDE;
51   virtual bool IsFileSystemHost() OVERRIDE;
52 
53   // Supports FileRefs direct access on the host side.
GetType()54   PP_FileSystemType GetType() const { return type_; }
IsOpened()55   bool IsOpened() const { return opened_; }
GetRootUrl()56   GURL GetRootUrl() const { return root_url_; }
GetFileSystemContext()57   scoped_refptr<fileapi::FileSystemContext> GetFileSystemContext() const {
58     return file_system_context_;
59   }
60 
61   // Supports FileIOs direct access on the host side.
62   // Non-NULL only for PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY}.
GetFileSystemOperationRunner()63   fileapi::FileSystemOperationRunner* GetFileSystemOperationRunner() const {
64     return file_system_operation_runner_.get();
65   }
ChecksQuota()66   bool ChecksQuota() const { return quota_reservation_ != NULL; }
67   // Opens a file for writing with quota checks. Returns the file size in the
68   // callback.
69   typedef base::Callback<void(int64_t)> OpenQuotaFileCallback;
70   void OpenQuotaFile(PepperFileIOHost* file_io_host,
71                      const fileapi::FileSystemURL& url,
72                      const OpenQuotaFileCallback& callback);
73   // Closes the file. This must be called after OpenQuotaFile and before the
74   // PepperFileIOHost is destroyed.
75   void CloseQuotaFile(PepperFileIOHost* file_io_host);
76   // Requests the given amount of quota. Returns the amount requested or
77   // PP_OK_COMPLETIONPENDING, in which case the amount granted is returned in
78   // the callback. Requests can't partially succeed so the amount granted is
79   // either 0 or the amount of the request. Requesting an amount of 0 will
80   // return immediately with a 0 result.
81   typedef base::Callback<void(int32_t)> RequestQuotaCallback;
82   int32_t RequestQuota(int32_t amount,
83                        const RequestQuotaCallback& callback);
84  private:
85   friend class PepperFileSystemBrowserHostTest;
86 
87   struct QuotaRequest {
88     QuotaRequest(int32_t amount, const RequestQuotaCallback& callback);
89     ~QuotaRequest();
90 
91     int32_t amount;
92     RequestQuotaCallback callback;
93   };
94 
95   void OpenExistingFileSystem(
96       const base::Closure& callback,
97       scoped_refptr<fileapi::FileSystemContext> file_system_context);
98   void OpenFileSystem(
99       ppapi::host::ReplyMessageContext reply_context,
100       fileapi::FileSystemType file_system_type,
101       scoped_refptr<fileapi::FileSystemContext> file_system_context);
102   void OpenFileSystemComplete(
103       ppapi::host::ReplyMessageContext reply_context,
104       const GURL& root,
105       const std::string& name,
106       base::PlatformFileError error);
107   void OpenIsolatedFileSystem(
108       ppapi::host::ReplyMessageContext reply_context,
109       const std::string& fsid,
110       PP_IsolatedFileSystemType_Private type,
111       scoped_refptr<fileapi::FileSystemContext> file_system_context);
112   void OpenPluginPrivateFileSystem(
113       ppapi::host::ReplyMessageContext reply_context,
114       const std::string& fsid,
115       scoped_refptr<fileapi::FileSystemContext> file_system_context);
116   void OpenPluginPrivateFileSystemComplete(
117       ppapi::host::ReplyMessageContext reply_context,
118       const std::string& fsid,
119       base::PlatformFileError error);
120 
121   int32_t OnHostMsgOpen(ppapi::host::HostMessageContext* context,
122                         int64_t expected_size);
123   int32_t OnHostMsgInitIsolatedFileSystem(
124       ppapi::host::HostMessageContext* context,
125       const std::string& fsid,
126       PP_IsolatedFileSystemType_Private type);
127 
128   void SendReplyForFileSystem(
129       ppapi::host::ReplyMessageContext reply_context,
130       int32_t pp_error);
131   void SendReplyForIsolatedFileSystem(
132       ppapi::host::ReplyMessageContext reply_context,
133       const std::string& fsid,
134       int32_t error);
135 
136   void SetFileSystemContext(
137       scoped_refptr<fileapi::FileSystemContext> file_system_context);
138 
139   bool ShouldCreateQuotaReservation() const;
140   void CreateQuotaReservation(const base::Closure& callback);
141   void GotQuotaReservation(
142       const base::Closure& callback,
143       scoped_refptr<QuotaReservation> quota_reservation);
144 
145   void ReserveQuota(int32_t amount);
146   void GotReservedQuota(int64_t amount,
147                         const QuotaReservation::OffsetMap& max_written_offsets);
148 
149   std::string GetPluginMimeType() const;
150 
151   // Returns plugin ID generated from plugin's MIME type.
152   std::string GeneratePluginId(const std::string& mime_type) const;
153 
154   BrowserPpapiHost* browser_ppapi_host_;
155 
156   PP_FileSystemType type_;
157   bool called_open_;  // whether open has been called.
158   bool opened_;  // whether open succeeded.
159   GURL root_url_;
160   scoped_refptr<fileapi::FileSystemContext> file_system_context_;
161 
162   scoped_ptr<fileapi::FileSystemOperationRunner> file_system_operation_runner_;
163 
164   // Used only for file systems with quota.
165   // When a PepperFileIOHost calls OpenQuotaFile, we add the id and a non-owning
166   // pointer to this map. CloseQuotaFile must be called when before the host is
167   // destroyed.
168   typedef std::map<int32_t, PepperFileIOHost*> FileMap;
169   FileMap files_;
170   std::queue<QuotaRequest> pending_quota_requests_;
171   int64_t reserved_quota_;
172   bool reserving_quota_;
173   // Access only on the FileSystemContext's default_file_task_runner().
174   scoped_refptr<QuotaReservation> quota_reservation_;
175 
176   std::string fsid_;  // used only for isolated filesystems.
177 
178   base::WeakPtrFactory<PepperFileSystemBrowserHost> weak_factory_;
179 
180   DISALLOW_COPY_AND_ASSIGN(PepperFileSystemBrowserHost);
181 };
182 
183 }  // namespace content
184 
185 #endif  // CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_FILE_SYSTEM_BROWSER_HOST_H_
186