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.
4
5 #include "content/child/fileapi/webfilewriter_impl.h"
6
7 #include "base/bind.h"
8 #include "base/synchronization/waitable_event.h"
9 #include "content/child/child_thread.h"
10 #include "content/child/fileapi/file_system_dispatcher.h"
11 #include "content/child/worker_task_runner.h"
12
13 namespace content {
14
15 namespace {
16
GetFileSystemDispatcher()17 FileSystemDispatcher* GetFileSystemDispatcher() {
18 return ChildThread::current() ?
19 ChildThread::current()->file_system_dispatcher() : NULL;
20 }
21
22 } // namespace
23
24 typedef FileSystemDispatcher::StatusCallback StatusCallback;
25 typedef FileSystemDispatcher::WriteCallback WriteCallback;
26
27 // This instance may be created outside main thread but runs mainly
28 // on main thread.
29 class WebFileWriterImpl::WriterBridge
30 : public base::RefCountedThreadSafe<WriterBridge> {
31 public:
WriterBridge(WebFileWriterImpl::Type type)32 WriterBridge(WebFileWriterImpl::Type type)
33 : request_id_(0),
34 thread_id_(WorkerTaskRunner::Instance()->CurrentWorkerId()),
35 written_bytes_(0) {
36 if (type == WebFileWriterImpl::TYPE_SYNC)
37 waitable_event_.reset(new base::WaitableEvent(false, false));
38 }
39
Truncate(const GURL & path,int64 offset,const StatusCallback & status_callback)40 void Truncate(const GURL& path, int64 offset,
41 const StatusCallback& status_callback) {
42 status_callback_ = status_callback;
43 if (!GetFileSystemDispatcher())
44 return;
45 ChildThread::current()->file_system_dispatcher()->Truncate(
46 path, offset, &request_id_,
47 base::Bind(&WriterBridge::DidFinish, this));
48 }
49
Write(const GURL & path,const std::string & id,int64 offset,const WriteCallback & write_callback,const StatusCallback & error_callback)50 void Write(const GURL& path, const std::string& id, int64 offset,
51 const WriteCallback& write_callback,
52 const StatusCallback& error_callback) {
53 write_callback_ = write_callback;
54 status_callback_ = error_callback;
55 if (!GetFileSystemDispatcher())
56 return;
57 ChildThread::current()->file_system_dispatcher()->Write(
58 path, id, offset, &request_id_,
59 base::Bind(&WriterBridge::DidWrite, this),
60 base::Bind(&WriterBridge::DidFinish, this));
61 }
62
Cancel(const StatusCallback & status_callback)63 void Cancel(const StatusCallback& status_callback) {
64 status_callback_ = status_callback;
65 if (!GetFileSystemDispatcher())
66 return;
67 ChildThread::current()->file_system_dispatcher()->Cancel(
68 request_id_,
69 base::Bind(&WriterBridge::DidFinish, this));
70 }
71
waitable_event()72 base::WaitableEvent* waitable_event() {
73 return waitable_event_.get();
74 }
75
WaitAndRun()76 void WaitAndRun() {
77 waitable_event_->Wait();
78 DCHECK(!results_closure_.is_null());
79 results_closure_.Run();
80 }
81
82 private:
83 friend class base::RefCountedThreadSafe<WriterBridge>;
~WriterBridge()84 virtual ~WriterBridge() {}
85
DidWrite(int64 bytes,bool complete)86 void DidWrite(int64 bytes, bool complete) {
87 written_bytes_ += bytes;
88 if (waitable_event_ && !complete)
89 return;
90 PostTaskToWorker(base::Bind(write_callback_, written_bytes_, complete));
91 }
92
DidFinish(base::File::Error status)93 void DidFinish(base::File::Error status) {
94 PostTaskToWorker(base::Bind(status_callback_, status));
95 }
96
PostTaskToWorker(const base::Closure & closure)97 void PostTaskToWorker(const base::Closure& closure) {
98 written_bytes_ = 0;
99 if (!thread_id_) {
100 DCHECK(!waitable_event_);
101 closure.Run();
102 return;
103 }
104 if (waitable_event_) {
105 results_closure_ = closure;
106 waitable_event_->Signal();
107 return;
108 }
109 WorkerTaskRunner::Instance()->PostTask(thread_id_, closure);
110 }
111
112 StatusCallback status_callback_;
113 WriteCallback write_callback_;
114 int request_id_;
115 int thread_id_;
116 int written_bytes_;
117 scoped_ptr<base::WaitableEvent> waitable_event_;
118 base::Closure results_closure_;
119 };
120
WebFileWriterImpl(const GURL & path,blink::WebFileWriterClient * client,Type type,base::MessageLoopProxy * main_thread_loop)121 WebFileWriterImpl::WebFileWriterImpl(
122 const GURL& path, blink::WebFileWriterClient* client,
123 Type type,
124 base::MessageLoopProxy* main_thread_loop)
125 : WebFileWriterBase(path, client),
126 main_thread_loop_(main_thread_loop),
127 bridge_(new WriterBridge(type)) {
128 }
129
~WebFileWriterImpl()130 WebFileWriterImpl::~WebFileWriterImpl() {
131 }
132
DoTruncate(const GURL & path,int64 offset)133 void WebFileWriterImpl::DoTruncate(const GURL& path, int64 offset) {
134 RunOnMainThread(base::Bind(&WriterBridge::Truncate, bridge_,
135 path, offset,
136 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr())));
137 }
138
DoWrite(const GURL & path,const std::string & blob_id,int64 offset)139 void WebFileWriterImpl::DoWrite(
140 const GURL& path, const std::string& blob_id, int64 offset) {
141 RunOnMainThread(base::Bind(&WriterBridge::Write, bridge_,
142 path, blob_id, offset,
143 base::Bind(&WebFileWriterImpl::DidWrite, AsWeakPtr()),
144 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr())));
145 }
146
DoCancel()147 void WebFileWriterImpl::DoCancel() {
148 RunOnMainThread(base::Bind(&WriterBridge::Cancel, bridge_,
149 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr())));
150 }
151
RunOnMainThread(const base::Closure & closure)152 void WebFileWriterImpl::RunOnMainThread(const base::Closure& closure) {
153 if (main_thread_loop_->RunsTasksOnCurrentThread()) {
154 DCHECK(!bridge_->waitable_event());
155 closure.Run();
156 return;
157 }
158 main_thread_loop_->PostTask(FROM_HERE, closure);
159 if (bridge_->waitable_event())
160 bridge_->WaitAndRun();
161 }
162
163 } // namespace content
164