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