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

Side by Side Diff: ppapi/native_client/src/trusted/plugin/file_downloader.cc

Issue 147083014: Introduce NaClFileInfoAutoCloser as an RAII wrapper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 10 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
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 "ppapi/native_client/src/trusted/plugin/file_downloader.h" 5 #include "ppapi/native_client/src/trusted/plugin/file_downloader.h"
6 6
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <string.h> 8 #include <string.h>
9 #include <string> 9 #include <string>
10 10
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 return posix_desc; 50 return posix_desc;
51 #else 51 #else
52 return handle; 52 return handle;
53 #endif 53 #endif
54 } 54 }
55 55
56 } // namespace 56 } // namespace
57 57
58 namespace plugin { 58 namespace plugin {
59 59
60 NaClFileInfoAutoCloser::NaClFileInfoAutoCloser() {
61 info_ = NoFileInfo();
dmichael (off chromium) 2014/01/29 18:22:28 Why not do this in the initializer list instead?
bsy 2014/01/29 20:51:28 Done.
62 }
63
64 NaClFileInfoAutoCloser::NaClFileInfoAutoCloser(NaClFileInfo pass_ownership) {
65 info_ = pass_ownership;
dmichael (off chromium) 2014/01/29 18:22:28 ditto
bsy 2014/01/29 20:51:28 Done.
66 }
67
68 void NaClFileInfoAutoCloser::FreeResources() {
69 if (-1 != info_.desc) {
70 PLUGIN_PRINTF(("NaClFileInfoAutoCloser::FreeResources close(%d)\n",
71 info_.desc));
72 close(get_desc());
dmichael (off chromium) 2014/01/29 18:22:28 tiny stupid nit: seems better to be consistent and
bsy 2014/01/29 20:51:28 not missing anything subtle, just that get_desc()
73 }
74 info_.desc = -1;
75 }
76
77 void NaClFileInfoAutoCloser::set(NaClFileInfo pass_ownership) {
dmichael (off chromium) 2014/01/29 18:22:28 nit: this should probably also be capitalized, and
bsy 2014/01/29 20:51:28 Done.
78 PLUGIN_PRINTF(("NaClFileInfoAutoCloser::set: taking ownership of %d\n",
79 pass_ownership.desc));
80 FreeResources();
dmichael (off chromium) 2014/01/29 18:22:28 nit: It might be good to check on if info_.desc ==
bsy 2014/01/29 20:51:28 Done.
81 info_ = pass_ownership;
82 }
83
84 NaClFileInfo NaClFileInfoAutoCloser::release() {
85 NaClFileInfo nvro;
dmichael (off chromium) 2014/01/29 18:22:28 Some readers won't get nvro (is named return value
bsy 2014/01/29 20:51:28 Done.
86 nvro = info_;
87 info_ = NoFileInfo();
88 return nvro;
89 }
90
60 void FileDownloader::Initialize(Plugin* instance) { 91 void FileDownloader::Initialize(Plugin* instance) {
61 PLUGIN_PRINTF(("FileDownloader::FileDownloader (this=%p)\n", 92 PLUGIN_PRINTF(("FileDownloader::FileDownloader (this=%p)\n",
62 static_cast<void*>(this))); 93 static_cast<void*>(this)));
63 CHECK(instance != NULL); 94 CHECK(instance != NULL);
64 CHECK(instance_ == NULL); // Can only initialize once. 95 CHECK(instance_ == NULL); // Can only initialize once.
65 instance_ = instance; 96 instance_ = instance;
66 callback_factory_.Initialize(this); 97 callback_factory_.Initialize(this);
67 file_io_private_interface_ = static_cast<const PPB_FileIO_Private*>( 98 file_io_private_interface_ = static_cast<const PPB_FileIO_Private*>(
68 pp::Module::Get()->GetBrowserInterface(PPB_FILEIO_PRIVATE_INTERFACE)); 99 pp::Module::Get()->GetBrowserInterface(PPB_FILEIO_PRIVATE_INTERFACE));
69 url_loader_trusted_interface_ = static_cast<const PPB_URLLoaderTrusted*>( 100 url_loader_trusted_interface_ = static_cast<const PPB_URLLoaderTrusted*>(
70 pp::Module::Get()->GetBrowserInterface(PPB_URLLOADERTRUSTED_INTERFACE)); 101 pp::Module::Get()->GetBrowserInterface(PPB_URLLOADERTRUSTED_INTERFACE));
71 temp_buffer_.resize(kTempBufferSize); 102 temp_buffer_.resize(kTempBufferSize);
72 cached_file_info_ = NoFileInfo(); 103 file_info_.FreeResources();
73 } 104 }
74 105
75 bool FileDownloader::OpenStream( 106 bool FileDownloader::OpenStream(
76 const nacl::string& url, 107 const nacl::string& url,
77 const pp::CompletionCallback& callback, 108 const pp::CompletionCallback& callback,
78 StreamCallbackSource* stream_callback_source) { 109 StreamCallbackSource* stream_callback_source) {
79 open_and_stream_ = false; 110 open_and_stream_ = false;
80 data_stream_callback_source_ = stream_callback_source; 111 data_stream_callback_source_ = stream_callback_source;
81 return Open(url, DOWNLOAD_STREAM, callback, true, NULL); 112 return Open(url, DOWNLOAD_STREAM, callback, true, NULL);
82 } 113 }
(...skipping 11 matching lines...) Expand all
94 return false; 125 return false;
95 126
96 CHECK(instance_ != NULL); 127 CHECK(instance_ != NULL);
97 open_time_ = NaClGetTimeOfDayMicroseconds(); 128 open_time_ = NaClGetTimeOfDayMicroseconds();
98 status_code_ = -1; 129 status_code_ = -1;
99 url_to_open_ = url; 130 url_to_open_ = url;
100 url_ = url; 131 url_ = url;
101 file_open_notify_callback_ = callback; 132 file_open_notify_callback_ = callback;
102 mode_ = mode; 133 mode_ = mode;
103 buffer_.clear(); 134 buffer_.clear();
104 cached_file_info_ = NoFileInfo(); 135 file_info_.FreeResources();
105 pp::URLRequestInfo url_request(instance_); 136 pp::URLRequestInfo url_request(instance_);
106 137
107 // Allow CORS. 138 // Allow CORS.
108 // Note that "SetAllowCrossOriginRequests" (currently) has the side effect of 139 // Note that "SetAllowCrossOriginRequests" (currently) has the side effect of
109 // preventing credentials from being sent on same-origin requests. We 140 // preventing credentials from being sent on same-origin requests. We
110 // therefore avoid setting this flag unless we know for sure it is a 141 // therefore avoid setting this flag unless we know for sure it is a
111 // cross-origin request, resulting in behavior similar to XMLHttpRequest. 142 // cross-origin request, resulting in behavior similar to XMLHttpRequest.
112 if (!instance_->DocumentCanRequest(url)) 143 if (!instance_->DocumentCanRequest(url))
113 url_request.SetAllowCrossOriginRequests(true); 144 url_request.SetAllowCrossOriginRequests(true);
114 145
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 pp_error)); 204 pp_error));
174 CHECK(pp_error == PP_OK_COMPLETIONPENDING); 205 CHECK(pp_error == PP_OK_COMPLETIONPENDING);
175 return true; 206 return true;
176 } 207 }
177 208
178 void FileDownloader::OpenFast(const nacl::string& url, 209 void FileDownloader::OpenFast(const nacl::string& url,
179 PP_FileHandle file_handle, 210 PP_FileHandle file_handle,
180 uint64_t file_token_lo, uint64_t file_token_hi) { 211 uint64_t file_token_lo, uint64_t file_token_hi) {
181 PLUGIN_PRINTF(("FileDownloader::OpenFast (url=%s)\n", url.c_str())); 212 PLUGIN_PRINTF(("FileDownloader::OpenFast (url=%s)\n", url.c_str()));
182 213
183 cached_file_info_ = NoFileInfo(); 214 file_info_.FreeResources();
184 CHECK(instance_ != NULL); 215 CHECK(instance_ != NULL);
185 open_time_ = NaClGetTimeOfDayMicroseconds(); 216 open_time_ = NaClGetTimeOfDayMicroseconds();
186 status_code_ = NACL_HTTP_STATUS_OK; 217 status_code_ = NACL_HTTP_STATUS_OK;
187 url_to_open_ = url; 218 url_to_open_ = url;
188 url_ = url; 219 url_ = url;
189 mode_ = DOWNLOAD_NONE; 220 mode_ = DOWNLOAD_NONE;
190 file_handle_ = file_handle; 221 if (not_streaming() && file_handle != PP_kInvalidFileHandle) {
191 file_token_.lo = file_token_lo; 222 NaClFileInfo tmp_info = NoFileInfo();
192 file_token_.hi = file_token_hi; 223 tmp_info.desc = ConvertFileDescriptor(file_handle);
224 tmp_info.file_token.lo = file_token_lo;
225 tmp_info.file_token.hi = file_token_hi;
226 file_info_.set(tmp_info);
227 }
193 } 228 }
194 229
195 struct NaClFileInfo FileDownloader::GetFileInfo() { 230 NaClFileInfo FileDownloader::GetFileInfo() {
196 PLUGIN_PRINTF(("FileDownloader::GetFileInfo\n")); 231 NaClFileInfo nrvo = NoFileInfo();
197 if (cached_file_info_.desc != -1) { 232
198 return cached_file_info_; 233 PLUGIN_PRINTF(("FileDownloader::GetFileInfo, this %p\n", this));
199 } else if (not_streaming() && file_handle_ != PP_kInvalidFileHandle) { 234 if (file_info_.get_desc() != -1) {
200 cached_file_info_.desc = ConvertFileDescriptor(file_handle_); 235 nrvo = file_info_.release();
201 if (cached_file_info_.desc != -1)
202 cached_file_info_.file_token = file_token_;
203 return cached_file_info_;
204 } 236 }
205 return NoFileInfo(); 237 PLUGIN_PRINTF(("FileDownloader::GetFileInfo -- returning %d\n", nrvo.desc));
238 return nrvo;
206 } 239 }
207 240
208 int64_t FileDownloader::TimeSinceOpenMilliseconds() const { 241 int64_t FileDownloader::TimeSinceOpenMilliseconds() const {
209 int64_t now = NaClGetTimeOfDayMicroseconds(); 242 int64_t now = NaClGetTimeOfDayMicroseconds();
210 // If Open() wasn't called or we somehow return an earlier time now, just 243 // If Open() wasn't called or we somehow return an earlier time now, just
211 // return the 0 rather than worse nonsense values. 244 // return the 0 rather than worse nonsense values.
212 if (open_time_ < 0 || now < open_time_) 245 if (open_time_ < 0 || now < open_time_)
213 return 0; 246 return 0;
214 return (now - open_time_) / NACL_MICROS_PER_MILLI; 247 return (now - open_time_) / NACL_MICROS_PER_MILLI;
215 } 248 }
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 478
446 bool FileDownloader::not_streaming() const { 479 bool FileDownloader::not_streaming() const {
447 return mode_ == DOWNLOAD_NONE; 480 return mode_ == DOWNLOAD_NONE;
448 } 481 }
449 482
450 void FileDownloader::GotFileHandleNotify(int32_t pp_error, 483 void FileDownloader::GotFileHandleNotify(int32_t pp_error,
451 PP_FileHandle handle) { 484 PP_FileHandle handle) {
452 PLUGIN_PRINTF(( 485 PLUGIN_PRINTF((
453 "FileDownloader::GotFileHandleNotify (pp_error=%" NACL_PRId32 ")\n", 486 "FileDownloader::GotFileHandleNotify (pp_error=%" NACL_PRId32 ")\n",
454 pp_error)); 487 pp_error));
455 if (pp_error == PP_OK) 488 if (pp_error == PP_OK) {
456 cached_file_info_.desc = ConvertFileDescriptor(handle); 489 NaClFileInfo tmp_info = NoFileInfo();
490 tmp_info.desc = ConvertFileDescriptor(handle);
491 file_info_.set(tmp_info);
492 }
457 493
458 stream_finish_callback_.RunAndClear(pp_error); 494 stream_finish_callback_.RunAndClear(pp_error);
459 } 495 }
460 496
461 } // namespace plugin 497 } // namespace plugin
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698