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