summaryrefslogtreecommitdiffstats
path: root/content/child/fileapi/webfilewriter_impl.cc
blob: bd5fbe32932080dbfeeb2882e4e1de74082e2ba4 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// 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.

#include "content/child/fileapi/webfilewriter_impl.h"

#include "base/bind.h"
#include "base/platform_file.h"
#include "base/synchronization/waitable_event.h"
#include "content/child/child_thread.h"
#include "content/child/fileapi/file_system_dispatcher.h"
#include "webkit/child/worker_task_runner.h"

using webkit_glue::WorkerTaskRunner;

namespace content {

namespace {

FileSystemDispatcher* GetFileSystemDispatcher() {
  return ChildThread::current() ?
      ChildThread::current()->file_system_dispatcher() : NULL;
}

}  // namespace

typedef FileSystemDispatcher::StatusCallback StatusCallback;
typedef FileSystemDispatcher::WriteCallback WriteCallback;

// This instance may be created outside main thread but runs mainly
// on main thread.
class WebFileWriterImpl::WriterBridge
    : public base::RefCountedThreadSafe<WriterBridge> {
 public:
  WriterBridge(WebFileWriterImpl::Type type)
      : request_id_(0),
        thread_id_(WorkerTaskRunner::Instance()->CurrentWorkerId()),
        written_bytes_(0) {
    if (type == WebFileWriterImpl::TYPE_SYNC)
      waitable_event_.reset(new base::WaitableEvent(false, false));
  }

  void Truncate(const GURL& path, int64 offset,
                const StatusCallback& status_callback) {
    status_callback_ = status_callback;
    if (!GetFileSystemDispatcher())
      return;
    ChildThread::current()->file_system_dispatcher()->Truncate(
        path, offset, &request_id_,
        base::Bind(&WriterBridge::DidFinish, this));
  }

  void Write(const GURL& path, const std::string& id, int64 offset,
             const WriteCallback& write_callback,
             const StatusCallback& error_callback) {
    write_callback_ = write_callback;
    status_callback_ = error_callback;
    if (!GetFileSystemDispatcher())
      return;
    ChildThread::current()->file_system_dispatcher()->Write(
        path, id, offset, &request_id_,
        base::Bind(&WriterBridge::DidWrite, this),
        base::Bind(&WriterBridge::DidFinish, this));
  }

  void Cancel(const StatusCallback& status_callback) {
    status_callback_ = status_callback;
    if (!GetFileSystemDispatcher())
      return;
    ChildThread::current()->file_system_dispatcher()->Cancel(
        request_id_,
        base::Bind(&WriterBridge::DidFinish, this));
  }

  base::WaitableEvent* waitable_event() {
    return waitable_event_.get();
  }

  void WaitAndRun() {
    waitable_event_->Wait();
    DCHECK(!results_closure_.is_null());
    results_closure_.Run();
  }

 private:
  friend class base::RefCountedThreadSafe<WriterBridge>;
  virtual ~WriterBridge() {}

  void DidWrite(int64 bytes, bool complete) {
    written_bytes_ += bytes;
    if (waitable_event_ && !complete)
      return;
    PostTaskToWorker(base::Bind(write_callback_, written_bytes_, complete));
  }

  void DidFinish(base::PlatformFileError status) {
    PostTaskToWorker(base::Bind(status_callback_, status));
  }

  void PostTaskToWorker(const base::Closure& closure) {
    written_bytes_ = 0;
    if (!thread_id_) {
      DCHECK(!waitable_event_);
      closure.Run();
      return;
    }
    if (waitable_event_) {
      results_closure_ = closure;
      waitable_event_->Signal();
      return;
    }
    WorkerTaskRunner::Instance()->PostTask(thread_id_, closure);
  }

  StatusCallback status_callback_;
  WriteCallback write_callback_;
  int request_id_;
  int thread_id_;
  int written_bytes_;
  scoped_ptr<base::WaitableEvent> waitable_event_;
  base::Closure results_closure_;
};

WebFileWriterImpl::WebFileWriterImpl(
     const GURL& path, WebKit::WebFileWriterClient* client,
     Type type,
     base::MessageLoopProxy* main_thread_loop)
  : WebFileWriterBase(path, client),
    main_thread_loop_(main_thread_loop),
    bridge_(new WriterBridge(type)) {
}

WebFileWriterImpl::~WebFileWriterImpl() {
}

void WebFileWriterImpl::DoTruncate(const GURL& path, int64 offset) {
  RunOnMainThread(base::Bind(&WriterBridge::Truncate, bridge_,
      path, offset,
      base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr())));
}

void WebFileWriterImpl::DoWrite(
    const GURL& path, const std::string& blob_id, int64 offset) {
  RunOnMainThread(base::Bind(&WriterBridge::Write, bridge_,
      path, blob_id, offset,
      base::Bind(&WebFileWriterImpl::DidWrite, AsWeakPtr()),
      base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr())));
}

void WebFileWriterImpl::DoCancel() {
  RunOnMainThread(base::Bind(&WriterBridge::Cancel, bridge_,
      base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr())));
}

void WebFileWriterImpl::RunOnMainThread(const base::Closure& closure) {
  if (main_thread_loop_->RunsTasksOnCurrentThread()) {
    DCHECK(!bridge_->waitable_event());
    closure.Run();
    return;
  }
  main_thread_loop_->PostTask(FROM_HERE, closure);
  if (bridge_->waitable_event())
    bridge_->WaitAndRun();
}

}  // namespace content