Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(626)

Side by Side Diff: content/child/fileapi/webfilewriter_impl.cc

Issue 21041003: Implement Worker-MainThread bridge for FileWriter in Chrome (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/child/fileapi/webfilewriter_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/child/fileapi/webfilewriter_impl.h" 5 #include "content/child/fileapi/webfilewriter_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/platform_file.h"
8 #include "content/child/child_thread.h" 9 #include "content/child/child_thread.h"
9 #include "content/child/fileapi/file_system_dispatcher.h" 10 #include "content/child/fileapi/file_system_dispatcher.h"
11 #include "webkit/child/worker_task_runner.h"
12
13 using webkit_glue::WorkerTaskRunner;
10 14
11 namespace content { 15 namespace content {
12 16
13 namespace { 17 namespace {
14 18
15 inline FileSystemDispatcher* GetFileSystemDispatcher() { 19 FileSystemDispatcher* GetFileSystemDispatcher() {
16 return ChildThread::current()->file_system_dispatcher(); 20 return ChildThread::current() ?
21 ChildThread::current()->file_system_dispatcher() : NULL;
17 } 22 }
18 23
19 } // namespace 24 } // namespace
20 25
26 typedef FileSystemDispatcher::StatusCallback StatusCallback;
27 typedef FileSystemDispatcher::WriteCallback WriteCallback;
28
29 // This instance may be created outside main thread but runs mainly
30 // on main thread.
31 class WebFileWriterImpl::WriterBridge
32 : public base::RefCountedThreadSafe<WriterBridge> {
33 public:
34 WriterBridge()
35 : request_id_(0),
36 thread_id_(WorkerTaskRunner::Instance()->CurrentWorkerId()) {}
37
38 void Truncate(const GURL& path, int64 offset,
39 const StatusCallback& status_callback) {
40 status_callback_ = status_callback;
41 if (!GetFileSystemDispatcher())
42 return;
43 ChildThread::current()->file_system_dispatcher()->Truncate(
44 path, offset, &request_id_,
45 base::Bind(&WriterBridge::DidFinish, this));
46 }
47
48 void Write(const GURL& path, const GURL& blob_url, int64 offset,
49 const WriteCallback& write_callback,
50 const StatusCallback& error_callback) {
51 write_callback_ = write_callback;
52 status_callback_ = error_callback;
53 if (!GetFileSystemDispatcher())
54 return;
55 ChildThread::current()->file_system_dispatcher()->Write(
56 path, blob_url, offset, &request_id_,
57 base::Bind(&WriterBridge::DidWrite, this),
58 base::Bind(&WriterBridge::DidFinish, this));
59 }
60
61 void Cancel(const StatusCallback& status_callback) {
62 status_callback_ = status_callback;
63 if (!GetFileSystemDispatcher())
64 return;
65 ChildThread::current()->file_system_dispatcher()->Cancel(
66 request_id_,
67 base::Bind(&WriterBridge::DidFinish, this));
68 }
69
70 private:
71 friend class base::RefCountedThreadSafe<WriterBridge>;
72 virtual ~WriterBridge() {}
73
74 void DidWrite(int64 bytes, bool complete) {
75 PostTaskToWorker(base::Bind(write_callback_, bytes, complete));
76 }
77
78 void DidFinish(base::PlatformFileError status) {
79 PostTaskToWorker(base::Bind(status_callback_, status));
80 }
81
82 void PostTaskToWorker(const base::Closure& closure) {
83 if (!thread_id_)
84 closure.Run();
85 else
86 WorkerTaskRunner::Instance()->PostTask(thread_id_, closure);
87 }
88
89 StatusCallback status_callback_;
90 WriteCallback write_callback_;
91 int request_id_;
92 int thread_id_;
93 };
94
21 WebFileWriterImpl::WebFileWriterImpl( 95 WebFileWriterImpl::WebFileWriterImpl(
22 const GURL& path, WebKit::WebFileWriterClient* client) 96 const GURL& path, WebKit::WebFileWriterClient* client,
97 base::MessageLoopProxy* main_thread_loop)
23 : WebFileWriterBase(path, client), 98 : WebFileWriterBase(path, client),
24 request_id_(0) { 99 main_thread_loop_(main_thread_loop),
100 bridge_(new WriterBridge) {
25 } 101 }
26 102
27 WebFileWriterImpl::~WebFileWriterImpl() { 103 WebFileWriterImpl::~WebFileWriterImpl() {
28 } 104 }
29 105
30 void WebFileWriterImpl::DoTruncate(const GURL& path, int64 offset) { 106 void WebFileWriterImpl::DoTruncate(const GURL& path, int64 offset) {
31 // The FileSystemDispatcher takes ownership of the CallbackDispatcher. 107 RunOnMainThread(base::Bind(&WriterBridge::Truncate, bridge_,
32 GetFileSystemDispatcher()->Truncate( 108 path, offset,
33 path, offset, &request_id_, 109 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr())));
34 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr()));
35 } 110 }
36 111
37 void WebFileWriterImpl::DoWrite( 112 void WebFileWriterImpl::DoWrite(
38 const GURL& path, const GURL& blob_url, int64 offset) { 113 const GURL& path, const GURL& blob_url, int64 offset) {
39 GetFileSystemDispatcher()->Write( 114 RunOnMainThread(base::Bind(&WriterBridge::Write, bridge_,
40 path, blob_url, offset, &request_id_, 115 path, blob_url, offset,
41 base::Bind(&WebFileWriterImpl::DidWrite, AsWeakPtr()), 116 base::Bind(&WebFileWriterImpl::DidWrite, AsWeakPtr()),
42 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr())); 117 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr())));
43 } 118 }
44 119
45 void WebFileWriterImpl::DoCancel() { 120 void WebFileWriterImpl::DoCancel() {
46 GetFileSystemDispatcher()->Cancel( 121 RunOnMainThread(base::Bind(&WriterBridge::Cancel, bridge_,
47 request_id_, 122 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr())));
48 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr())); 123 }
124
125 void WebFileWriterImpl::RunOnMainThread(const base::Closure& closure) {
126 if (main_thread_loop_->RunsTasksOnCurrentThread())
127 closure.Run();
128 else
129 main_thread_loop_->PostTask(FROM_HERE, closure);
49 } 130 }
50 131
51 } // namespace content 132 } // namespace content
OLDNEW
« no previous file with comments | « content/child/fileapi/webfilewriter_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698