summaryrefslogtreecommitdiffstats
path: root/content/child/fileapi/webfilesystem_impl.h
blob: 7546c8ed899e94af7aaa845dea61e1bd7a16a77e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_CHILD_FILEAPI_WEBFILESYSTEM_IMPL_H_
#define CONTENT_CHILD_FILEAPI_WEBFILESYSTEM_IMPL_H_

#include <map>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/threading/non_thread_safe.h"
#include "content/public/child/worker_thread.h"
#include "third_party/WebKit/public/platform/WebFileSystem.h"

namespace base {
class WaitableEvent;
class SingleThreadTaskRunner;
}

namespace blink {
class WebURL;
class WebFileWriter;
class WebFileWriterClient;
}

namespace content {

class WebFileSystemImpl : public blink::WebFileSystem,
                          public WorkerThread::Observer,
                          public base::NonThreadSafe {
 public:
  class WaitableCallbackResults;

  // Returns thread-specific instance.  If non-null |main_thread_loop|
  // is given and no thread-specific instance has been created it may
  // create a new instance.
  static WebFileSystemImpl* ThreadSpecificInstance(
      const scoped_refptr<base::SingleThreadTaskRunner>&
          main_thread_task_runner);

  // Deletes thread-specific instance (if exists). For workers it deletes
  // itself in WillStopCurrentWorkerThread(), but for an instance created on the
  // main thread this method must be called.
  static void DeleteThreadSpecificInstance();

  explicit WebFileSystemImpl(
      const scoped_refptr<base::SingleThreadTaskRunner>&
          main_thread_task_runner);
  ~WebFileSystemImpl() override;

  // WorkerThread::Observer implementation.
  void WillStopCurrentWorkerThread() override;

  // WebFileSystem implementation.
  void openFileSystem(const blink::WebURL& storage_partition,
                      const blink::WebFileSystemType type,
                      blink::WebFileSystemCallbacks) override;
  void resolveURL(const blink::WebURL& filesystem_url,
                  blink::WebFileSystemCallbacks) override;
  void deleteFileSystem(const blink::WebURL& storage_partition,
                        const blink::WebFileSystemType type,
                        blink::WebFileSystemCallbacks) override;
  void move(const blink::WebURL& src_path,
            const blink::WebURL& dest_path,
            blink::WebFileSystemCallbacks) override;
  void copy(const blink::WebURL& src_path,
            const blink::WebURL& dest_path,
            blink::WebFileSystemCallbacks) override;
  void remove(const blink::WebURL& path,
              blink::WebFileSystemCallbacks) override;
  void removeRecursively(const blink::WebURL& path,
                         blink::WebFileSystemCallbacks) override;
  void readMetadata(const blink::WebURL& path,
                    blink::WebFileSystemCallbacks) override;
  void createFile(const blink::WebURL& path,
                  bool exclusive,
                  blink::WebFileSystemCallbacks) override;
  void createDirectory(const blink::WebURL& path,
                       bool exclusive,
                       blink::WebFileSystemCallbacks) override;
  void fileExists(const blink::WebURL& path,
                  blink::WebFileSystemCallbacks) override;
  void directoryExists(const blink::WebURL& path,
                       blink::WebFileSystemCallbacks) override;
  int readDirectory(const blink::WebURL& path,
                    blink::WebFileSystemCallbacks) override;
  void createFileWriter(const blink::WebURL& path,
                        blink::WebFileWriterClient*,
                        blink::WebFileSystemCallbacks) override;
  void createSnapshotFileAndReadMetadata(
      const blink::WebURL& path,
      blink::WebFileSystemCallbacks) override;
  bool waitForAdditionalResult(int callbacksId) override;

  int RegisterCallbacks(const blink::WebFileSystemCallbacks& callbacks);
  blink::WebFileSystemCallbacks GetCallbacks(int callbacks_id);
  void UnregisterCallbacks(int callbacks_id);

 private:
  typedef std::map<int, blink::WebFileSystemCallbacks> CallbacksMap;
  typedef std::map<int, scoped_refptr<WaitableCallbackResults> >
      WaitableCallbackResultsMap;

  WaitableCallbackResults* MaybeCreateWaitableResults(
      const blink::WebFileSystemCallbacks& callbacks, int callbacks_id);

  scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;

  CallbacksMap callbacks_;
  int next_callbacks_id_;
  WaitableCallbackResultsMap waitable_results_;

  DISALLOW_COPY_AND_ASSIGN(WebFileSystemImpl);
};

}  // namespace content

#endif  // CONTENT_CHILD_FILEAPI_WEBFILESYSTEM_IMPL_H_