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.
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_
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"
22 #include "webkit/browser/fileapi/file_system_context.h"
26 class BrowserPpapiHost
;
27 class PepperFileIOHost
;
29 class CONTENT_EXPORT PepperFileSystemBrowserHost
30 : public ppapi::host::ResourceHost
,
31 public base::SupportsWeakPtr
<PepperFileSystemBrowserHost
> {
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
,
38 PP_FileSystemType type
);
39 virtual ~PepperFileSystemBrowserHost();
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
);
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
;
53 // Supports FileRefs direct access on the host side.
54 PP_FileSystemType
GetType() const { return type_
; }
55 bool IsOpened() const { return opened_
; }
56 GURL
GetRootUrl() const { return root_url_
; }
57 scoped_refptr
<fileapi::FileSystemContext
> GetFileSystemContext() const {
58 return file_system_context_
;
61 // Supports FileIOs direct access on the host side.
62 // Non-NULL only for PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY}.
63 fileapi::FileSystemOperationRunner
* GetFileSystemOperationRunner() const {
64 return file_system_operation_runner_
.get();
66 bool ChecksQuota() const { return quota_reservation_
!= NULL
; }
67 // Opens a file for writing with quota checks. Returns the file size in the
69 typedef base::Callback
<void(int64_t)> OpenQuotaFileCallback
;
70 void OpenQuotaFile(PepperFileIOHost
* file_io_host
,
71 const base::FilePath
& file_path
,
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
);
85 friend class PepperFileSystemBrowserHostTest
;
88 QuotaRequest(int32_t amount
, const RequestQuotaCallback
& callback
);
92 RequestQuotaCallback callback
;
95 void OpenExistingFileSystem(
96 const base::Closure
& callback
,
97 scoped_refptr
<fileapi::FileSystemContext
> file_system_context
);
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
,
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
);
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
);
128 void SendReplyForFileSystem(
129 ppapi::host::ReplyMessageContext reply_context
,
131 void SendReplyForIsolatedFileSystem(
132 ppapi::host::ReplyMessageContext reply_context
,
133 const std::string
& fsid
,
136 void SetFileSystemContext(
137 scoped_refptr
<fileapi::FileSystemContext
> file_system_context
);
139 int32_t CreateQuotaReservation(const base::Closure
& callback
);
140 void GotQuotaReservation(
141 const base::Closure
& callback
,
142 scoped_refptr
<QuotaReservation
> quota_reservation
);
144 void ReserveQuota(int32_t amount
);
145 void GotReservedQuota(int64_t amount
,
146 const QuotaReservation::OffsetMap
& max_written_offsets
);
148 std::string
GetPluginMimeType() const;
150 // Returns plugin ID generated from plugin's MIME type.
151 std::string
GeneratePluginId(const std::string
& mime_type
) const;
153 BrowserPpapiHost
* browser_ppapi_host_
;
155 PP_FileSystemType type_
;
156 bool called_open_
; // whether open has been called.
157 bool opened_
; // whether open succeeded.
159 scoped_refptr
<fileapi::FileSystemContext
> file_system_context_
;
161 scoped_ptr
<fileapi::FileSystemOperationRunner
> file_system_operation_runner_
;
163 // Used only for file systems with quota.
164 // When a PepperFileIOHost calls OpenQuotaFile, we add the id and a non-owning
165 // pointer to this map. CloseQuotaFile must be called when before the host is
167 typedef std::map
<int32_t, PepperFileIOHost
*> FileMap
;
169 std::queue
<QuotaRequest
> pending_quota_requests_
;
170 int64_t reserved_quota_
;
171 bool reserving_quota_
;
172 // Access only on the FileSystemContext's default_file_task_runner().
173 scoped_refptr
<QuotaReservation
> quota_reservation_
;
175 std::string fsid_
; // used only for isolated filesystems.
177 base::WeakPtrFactory
<PepperFileSystemBrowserHost
> weak_factory_
;
179 DISALLOW_COPY_AND_ASSIGN(PepperFileSystemBrowserHost
);
182 } // namespace content
184 #endif // CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_FILE_SYSTEM_BROWSER_HOST_H_