IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / renderer_host / pepper / pepper_file_system_browser_host.h
blob6a90faeb86b786a3a2bb35a0b4e8439218d50f2b
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_
8 #include <queue>
9 #include <string>
10 #include <vector>
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"
24 namespace content {
26 class BrowserPpapiHost;
27 class PepperFileIOHost;
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();
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
68 // callback.
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 int64_t max_written_offset);
78 private:
79 friend class PepperFileSystemBrowserHostTest;
81 void OpenExistingFileSystem(
82 const base::Closure& callback,
83 scoped_refptr<fileapi::FileSystemContext> file_system_context);
84 void OpenFileSystem(
85 ppapi::host::ReplyMessageContext reply_context,
86 fileapi::FileSystemType file_system_type,
87 scoped_refptr<fileapi::FileSystemContext> file_system_context);
88 void OpenFileSystemComplete(
89 ppapi::host::ReplyMessageContext reply_context,
90 const GURL& root,
91 const std::string& name,
92 base::PlatformFileError error);
93 void OpenIsolatedFileSystem(
94 ppapi::host::ReplyMessageContext reply_context,
95 const std::string& fsid,
96 PP_IsolatedFileSystemType_Private type,
97 scoped_refptr<fileapi::FileSystemContext> file_system_context);
98 void OpenPluginPrivateFileSystem(
99 ppapi::host::ReplyMessageContext reply_context,
100 const std::string& fsid,
101 scoped_refptr<fileapi::FileSystemContext> file_system_context);
102 void OpenPluginPrivateFileSystemComplete(
103 ppapi::host::ReplyMessageContext reply_context,
104 const std::string& fsid,
105 base::PlatformFileError error);
107 int32_t OnHostMsgOpen(
108 ppapi::host::HostMessageContext* context,
109 int64_t expected_size);
110 int32_t OnHostMsgInitIsolatedFileSystem(
111 ppapi::host::HostMessageContext* context,
112 const std::string& fsid,
113 PP_IsolatedFileSystemType_Private type);
114 int32_t OnHostMsgReserveQuota(
115 ppapi::host::HostMessageContext* context,
116 int64_t amount,
117 const std::map<int32_t, int64_t>& max_written_offsets);
119 void SendReplyForFileSystem(
120 ppapi::host::ReplyMessageContext reply_context,
121 int32_t pp_error);
122 void SendReplyForIsolatedFileSystem(
123 ppapi::host::ReplyMessageContext reply_context,
124 const std::string& fsid,
125 int32_t error);
127 void SetFileSystemContext(
128 scoped_refptr<fileapi::FileSystemContext> file_system_context);
130 bool ShouldCreateQuotaReservation() const;
131 void CreateQuotaReservation(const base::Closure& callback);
132 void GotQuotaReservation(
133 const base::Closure& callback,
134 scoped_refptr<QuotaReservation> quota_reservation);
136 void GotReservedQuota(
137 ppapi::host::ReplyMessageContext reply_context,
138 int64_t amount,
139 const std::map<int32_t, int64_t>& max_written_offsets);
140 void DidOpenQuotaFile(
141 PP_Resource file_io_resource,
142 const OpenQuotaFileCallback& callback,
143 int64_t max_written_offset);
145 std::string GetPluginMimeType() const;
147 // Returns plugin ID generated from plugin's MIME type.
148 std::string GeneratePluginId(const std::string& mime_type) const;
150 BrowserPpapiHost* browser_ppapi_host_;
152 PP_FileSystemType type_;
153 bool called_open_; // whether open has been called.
154 bool opened_; // whether open succeeded.
155 GURL root_url_;
156 scoped_refptr<fileapi::FileSystemContext> file_system_context_;
158 scoped_ptr<fileapi::FileSystemOperationRunner> file_system_operation_runner_;
160 // Used only for file systems with quota.
161 // When a PepperFileIOHost calls OpenQuotaFile, we add the id and a non-owning
162 // pointer to this map. CloseQuotaFile must be called when before the host is
163 // destroyed.
164 typedef std::map<int32_t, PepperFileIOHost*> FileMap;
165 FileMap files_;
166 int64_t reserved_quota_;
167 bool reserving_quota_;
168 // Access only on the FileSystemContext's default_file_task_runner().
169 scoped_refptr<QuotaReservation> quota_reservation_;
171 std::string fsid_; // used only for isolated filesystems.
173 base::WeakPtrFactory<PepperFileSystemBrowserHost> weak_factory_;
175 DISALLOW_COPY_AND_ASSIGN(PepperFileSystemBrowserHost);
178 } // namespace content
180 #endif // CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_FILE_SYSTEM_BROWSER_HOST_H_