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

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

Powered by Google App Engine
This is Rietveld 408576698