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 PPAPI_PROXY_FILE_SYSTEM_RESOURCE_H_
6#define PPAPI_PROXY_FILE_SYSTEM_RESOURCE_H_
7
8#include <map>
9#include <queue>
10#include <string>
11
12#include "base/memory/ref_counted.h"
13#include "ppapi/c/pp_file_info.h"
14#include "ppapi/c/pp_resource.h"
15#include "ppapi/c/private/ppb_isolated_file_system_private.h"
16#include "ppapi/proxy/connection.h"
17#include "ppapi/proxy/plugin_resource.h"
18#include "ppapi/proxy/ppapi_proxy_export.h"
19#include "ppapi/proxy/resource_message_params.h"
20#include "ppapi/thunk/ppb_file_system_api.h"
21
22namespace ppapi {
23
24class TrackedCallback;
25
26namespace proxy {
27
28class PPAPI_PROXY_EXPORT FileSystemResource
29    : public PluginResource,
30      public NON_EXPORTED_BASE(thunk::PPB_FileSystem_API) {
31 public:
32  // Creates a new FileSystemResource. The resource must be subsequently opened
33  // via Open() before use.
34  FileSystemResource(Connection connection,
35                     PP_Instance instance,
36                     PP_FileSystemType type);
37  // Creates a FileSystemResource, attached to an existing pending host
38  // resource. The |pending_renderer_id| and |pending_browser_id| must be
39  // already-opened file systems.
40  FileSystemResource(Connection connection,
41                     PP_Instance instance,
42                     int pending_renderer_id,
43                     int pending_browser_id,
44                     PP_FileSystemType type);
45  virtual ~FileSystemResource();
46
47  // Resource overrides.
48  virtual thunk::PPB_FileSystem_API* AsPPB_FileSystem_API() OVERRIDE;
49
50  // PPB_FileSystem_API implementation.
51  virtual int32_t Open(int64_t expected_size,
52                       scoped_refptr<TrackedCallback> callback) OVERRIDE;
53  virtual PP_FileSystemType GetType() OVERRIDE;
54  virtual void OpenQuotaFile(PP_Resource file_io) OVERRIDE;
55  virtual void CloseQuotaFile(PP_Resource file_io) OVERRIDE;
56  typedef base::Callback<void(int64_t)> RequestQuotaCallback;
57  virtual int64_t RequestQuota(int64_t amount,
58                               const RequestQuotaCallback& callback) OVERRIDE;
59
60  int32_t InitIsolatedFileSystem(const std::string& fsid,
61                                 PP_IsolatedFileSystemType_Private type,
62                                 const base::Callback<void(int32_t)>& callback);
63 private:
64  struct QuotaRequest {
65    QuotaRequest(int64_t amount,
66                 const RequestQuotaCallback& callback);
67    ~QuotaRequest();
68
69    int64_t amount;
70    RequestQuotaCallback callback;
71  };
72
73  // Called when the host has responded to our open request.
74  void OpenComplete(scoped_refptr<TrackedCallback> callback,
75                    const ResourceMessageReplyParams& params);
76
77  // Called when the host has responded to our InitIsolatedFileSystem request.
78  void InitIsolatedFileSystemComplete(
79      const base::Callback<void(int32_t)>& callback,
80      const ResourceMessageReplyParams& params);
81
82  void ReserveQuota(int64_t amount);
83  typedef std::map<int32_t, int64_t> OffsetMap;
84  void ReserveQuotaComplete(const ResourceMessageReplyParams& params,
85                            int64_t amount,
86                            const OffsetMap& max_written_offsets);
87
88  PP_FileSystemType type_;
89  bool called_open_;
90  uint32_t callback_count_;
91  int32_t callback_result_;
92
93  std::set<PP_Resource> files_;
94  std::queue<QuotaRequest> pending_quota_requests_;
95  int64_t reserved_quota_;
96  bool reserving_quota_;
97
98  DISALLOW_COPY_AND_ASSIGN(FileSystemResource);
99};
100
101}  // namespace proxy
102}  // namespace ppapi
103
104#endif  // PPAPI_PROXY_FILE_SYSTEM_RESOURCE_H_
105