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

Side by Side Diff: webkit/plugins/ppapi/quota_file_io.cc

Issue 11615036: Reduce copy on QuotaFileIO::Write (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: crash fix on deletion Created 7 years, 11 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 | « no previous file | 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "webkit/plugins/ppapi/quota_file_io.h" 5 #include "webkit/plugins/ppapi/quota_file_io.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop_proxy.h" 12 #include "base/message_loop_proxy.h"
13 #include "base/stl_util.h" 13 #include "base/stl_util.h"
14 #include "base/task_runner_util.h"
14 #include "webkit/plugins/ppapi/host_globals.h" 15 #include "webkit/plugins/ppapi/host_globals.h"
15 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" 16 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
16 #include "webkit/plugins/ppapi/resource_helper.h" 17 #include "webkit/plugins/ppapi/resource_helper.h"
17 18
18 using base::PlatformFile; 19 using base::PlatformFile;
19 using base::PlatformFileError; 20 using base::PlatformFileError;
20 using quota::StorageType; 21 using quota::StorageType;
21 22
22 namespace webkit { 23 namespace webkit {
23 namespace ppapi { 24 namespace ppapi {
24 25
25 namespace { 26 namespace {
26 StorageType PPFileSystemTypeToQuotaStorageType(PP_FileSystemType type) { 27 StorageType PPFileSystemTypeToQuotaStorageType(PP_FileSystemType type) {
27 switch (type) { 28 switch (type) {
28 case PP_FILESYSTEMTYPE_LOCALPERSISTENT: 29 case PP_FILESYSTEMTYPE_LOCALPERSISTENT:
29 return quota::kStorageTypePersistent; 30 return quota::kStorageTypePersistent;
30 case PP_FILESYSTEMTYPE_LOCALTEMPORARY: 31 case PP_FILESYSTEMTYPE_LOCALTEMPORARY:
31 return quota::kStorageTypeTemporary; 32 return quota::kStorageTypeTemporary;
32 default: 33 default:
33 return quota::kStorageTypeUnknown; 34 return quota::kStorageTypeUnknown;
34 } 35 }
35 NOTREACHED(); 36 NOTREACHED();
36 return quota::kStorageTypeUnknown; 37 return quota::kStorageTypeUnknown;
37 } 38 }
39
40 int WriteAdapter(PlatformFile file, int64 offset,
41 scoped_ptr<char[]> data, int size) {
42 return base::WritePlatformFile(file, offset, data.get(), size);
43 }
44
38 } // namespace 45 } // namespace
39 46
40 class QuotaFileIO::PendingOperationBase { 47 class QuotaFileIO::PendingOperationBase {
41 public: 48 public:
42 virtual ~PendingOperationBase() {} 49 virtual ~PendingOperationBase() {}
43 50
44 // Either one of Run() or DidFail() is called (the latter is called when 51 // Either one of Run() or DidFail() is called (the latter is called when
45 // there was more than one error during quota queries). 52 // there was more than one error during quota queries).
46 virtual void Run() = 0; 53 virtual void Run() = 0;
47 virtual void DidFail(PlatformFileError error) = 0; 54 virtual void DidFail(PlatformFileError error) = 0;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 return; 100 return;
94 } 101 }
95 DCHECK(buffer_.get()); 102 DCHECK(buffer_.get());
96 103
97 PluginDelegate* plugin_delegate = quota_io_->GetPluginDelegate(); 104 PluginDelegate* plugin_delegate = quota_io_->GetPluginDelegate();
98 if (!plugin_delegate) { 105 if (!plugin_delegate) {
99 DidFail(base::PLATFORM_FILE_ERROR_FAILED); 106 DidFail(base::PLATFORM_FILE_ERROR_FAILED);
100 return; 107 return;
101 } 108 }
102 109
103 if (!base::FileUtilProxy::Write( 110 if (!base::PostTaskAndReplyWithResult(
104 plugin_delegate->GetFileThreadMessageLoopProxy(), 111 plugin_delegate->GetFileThreadMessageLoopProxy(), FROM_HERE,
105 quota_io_->file_, offset_, buffer_.get(), bytes_to_write_, 112 base::Bind(&WriteAdapter,
106 base::Bind(&WriteOperation::DidFinish, 113 quota_io_->file_, offset_,
114 base::Passed(&buffer_), bytes_to_write_),
115 base::Bind(&WriteOperation::DidWrite,
107 weak_factory_.GetWeakPtr()))) { 116 weak_factory_.GetWeakPtr()))) {
108 DidFail(base::PLATFORM_FILE_ERROR_FAILED); 117 DidFail(base::PLATFORM_FILE_ERROR_FAILED);
109 return; 118 return;
110 } 119 }
111 } 120 }
112 121
113 virtual void DidFail(PlatformFileError error) OVERRIDE { 122 virtual void DidFail(PlatformFileError error) OVERRIDE {
114 DidFinish(error, 0); 123 DidFinish(error, 0);
115 } 124 }
116 125
117 bool finished() const { return finished_; } 126 bool finished() const { return finished_; }
118 127
119 virtual void WillRunCallback() { 128 virtual void WillRunCallback() {
120 base::MessageLoopProxy::current()->PostTask( 129 base::MessageLoopProxy::current()->PostTask(
121 FROM_HERE, 130 FROM_HERE,
122 base::Bind(&WriteOperation::RunCallback, weak_factory_.GetWeakPtr())); 131 base::Bind(&WriteOperation::RunCallback, weak_factory_.GetWeakPtr()));
123 } 132 }
124 133
125 private: 134 private:
135 void DidWrite(int bytes_written) {
136 base::PlatformFileError error = bytes_written > 0 ?
137 base::PLATFORM_FILE_OK : base::PLATFORM_FILE_ERROR_FAILED;
138 DidFinish(error, bytes_written);
139 }
140
126 void DidFinish(PlatformFileError status, int bytes_written) { 141 void DidFinish(PlatformFileError status, int bytes_written) {
127 finished_ = true; 142 finished_ = true;
128 status_ = status; 143 status_ = status;
129 bytes_written_ = bytes_written; 144 bytes_written_ = bytes_written;
130 int64_t max_offset = 145 int64_t max_offset =
131 (status != base::PLATFORM_FILE_OK) ? 0 : offset_ + bytes_written; 146 (status != base::PLATFORM_FILE_OK) ? 0 : offset_ + bytes_written;
132 // This may delete itself by calling RunCallback. 147 // This may delete itself by calling RunCallback.
133 quota_io_->DidWrite(this, max_offset); 148 quota_io_->DidWrite(this, max_offset);
134 } 149 }
135 150
136 virtual void RunCallback() { 151 virtual void RunCallback() {
137 DCHECK_EQ(false, callback_.is_null()); 152 DCHECK_EQ(false, callback_.is_null());
138 callback_.Run(status_, bytes_written_); 153 callback_.Run(status_, bytes_written_);
139 delete this; 154 delete this;
140 } 155 }
141 156
142 const int64_t offset_; 157 const int64_t offset_;
143 scoped_array<char> buffer_; 158 scoped_ptr<char[]> buffer_;
144 const int32_t bytes_to_write_; 159 const int32_t bytes_to_write_;
145 WriteCallback callback_; 160 WriteCallback callback_;
146 bool finished_; 161 bool finished_;
147 PlatformFileError status_; 162 PlatformFileError status_;
148 int64_t bytes_written_; 163 int64_t bytes_written_;
149 base::WeakPtrFactory<WriteOperation> weak_factory_; 164 base::WeakPtrFactory<WriteOperation> weak_factory_;
150 }; 165 };
151 166
152 class QuotaFileIO::SetLengthOperation : public PendingOperationBase { 167 class QuotaFileIO::SetLengthOperation : public PendingOperationBase {
153 public: 168 public:
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 410
396 411
397 PluginDelegate* plugin_delegate = GetPluginDelegate(); 412 PluginDelegate* plugin_delegate = GetPluginDelegate();
398 if (plugin_delegate) 413 if (plugin_delegate)
399 plugin_delegate->DidUpdateFile(file_url_, delta); 414 plugin_delegate->DidUpdateFile(file_url_, delta);
400 inflight_operations_ = 0; 415 inflight_operations_ = 0;
401 } 416 }
402 417
403 } // namespace ppapi 418 } // namespace ppapi
404 } // namespace webkit 419 } // namespace webkit
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698