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

Side by Side Diff: content/common/url_fetcher.cc

Issue 7066067: Support creating temporary files for sync file operations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address Darin's comments Created 9 years, 6 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/browser/renderer_host/redirect_to_file_resource_handler.cc ('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 (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/common/url_fetcher.h" 5 #include "content/common/url_fetcher.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/file_path.h" 10 #include "base/file_path.h"
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 public: 102 public:
103 TempFileWriter( 103 TempFileWriter(
104 URLFetcher::Core* core, 104 URLFetcher::Core* core,
105 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy); 105 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy);
106 106
107 ~TempFileWriter(); 107 ~TempFileWriter();
108 void CreateTempFile(); 108 void CreateTempFile();
109 void DidCreateTempFile(base::PlatformFileError error_code, 109 void DidCreateTempFile(base::PlatformFileError error_code,
110 base::PassPlatformFile file_handle, 110 base::PassPlatformFile file_handle,
111 FilePath file_path); 111 FilePath file_path);
112 void DidCloseTempFile(base::PlatformFileError error_code);
113 void DidReopenTempFile(base::PlatformFileError error_code,
114 base::PassPlatformFile file_handle,
115 bool created);
116 112
117 // Record |num_bytes_| response bytes in |core_->buffer_| to the file. 113 // Record |num_bytes_| response bytes in |core_->buffer_| to the file.
118 void WriteBuffer(int num_bytes); 114 void WriteBuffer(int num_bytes);
119 115
120 // Called when a write has been done. Continues writing if there are 116 // Called when a write has been done. Continues writing if there are
121 // any more bytes to write. Otherwise, initiates a read in core_. 117 // any more bytes to write. Otherwise, initiates a read in core_.
122 void ContinueWrite(base::PlatformFileError error_code, 118 void ContinueWrite(base::PlatformFileError error_code,
123 int bytes_written); 119 int bytes_written);
124 120
125 // Drop ownership of the file at path |temp_file_|. This class 121 // Drop ownership of the file at path |temp_file_|. This class
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 } 306 }
311 307
312 URLFetcher::Core::TempFileWriter::~TempFileWriter() { 308 URLFetcher::Core::TempFileWriter::~TempFileWriter() {
313 Destroy(); 309 Destroy();
314 } 310 }
315 311
316 void URLFetcher::Core::TempFileWriter::CreateTempFile() { 312 void URLFetcher::Core::TempFileWriter::CreateTempFile() {
317 CHECK(file_message_loop_proxy_.get()); 313 CHECK(file_message_loop_proxy_.get());
318 base::FileUtilProxy::CreateTemporary( 314 base::FileUtilProxy::CreateTemporary(
319 file_message_loop_proxy_, 315 file_message_loop_proxy_,
316 0, // No additional file flags.
320 callback_factory_.NewCallback( 317 callback_factory_.NewCallback(
321 &URLFetcher::Core::TempFileWriter::DidCreateTempFile)); 318 &URLFetcher::Core::TempFileWriter::DidCreateTempFile));
322 } 319 }
323 320
324 void URLFetcher::Core::TempFileWriter::DidCreateTempFile( 321 void URLFetcher::Core::TempFileWriter::DidCreateTempFile(
325 base::PlatformFileError error_code, 322 base::PlatformFileError error_code,
326 base::PassPlatformFile file_handle, 323 base::PassPlatformFile file_handle,
327 FilePath file_path) { 324 FilePath file_path) {
328 if (base::PLATFORM_FILE_OK != error_code) { 325 if (base::PLATFORM_FILE_OK != error_code) {
329 error_code_ = error_code; 326 error_code_ = error_code;
330 core_->InformDelegateFetchIsComplete(); 327 core_->InformDelegateFetchIsComplete();
331 return; 328 return;
332 } 329 }
333 330
334 temp_file_ = file_path; 331 temp_file_ = file_path;
335
336 // The file was opened with async writes enabled. FileUtilProxy::Write()
337 // treats a write that returns IO_PENDING as an error, and does not inform
338 // the caller. We need to close and reopen the file with asyncronus writes
339 // disabled.
340 // TODO(skerner): Make FileUtilProxy::Write() play nice with async IO.
341 base::FileUtilProxy::Close(
342 file_message_loop_proxy_,
343 file_handle.ReleaseValue(),
344 callback_factory_.NewCallback(
345 &URLFetcher::Core::TempFileWriter::DidCloseTempFile));
346 }
347
348 void URLFetcher::Core::TempFileWriter::DidCloseTempFile(
349 base::PlatformFileError error_code) {
350 if (base::PLATFORM_FILE_OK != error_code) {
351 error_code_ = error_code;
352 core_->InformDelegateFetchIsComplete();
353 return;
354 }
355
356 int file_flags =
357 base::PLATFORM_FILE_CREATE_ALWAYS |
358 base::PLATFORM_FILE_WRITE |
359 base::PLATFORM_FILE_TEMPORARY;
360
361 base::FileUtilProxy::CreateOrOpen(
362 file_message_loop_proxy_,
363 temp_file_,
364 file_flags,
365 callback_factory_.NewCallback(
366 &URLFetcher::Core::TempFileWriter::DidReopenTempFile));
367 }
368
369 void URLFetcher::Core::TempFileWriter::DidReopenTempFile(
370 base::PlatformFileError error_code,
371 base::PassPlatformFile file_handle,
372 bool created) {
373 if (base::PLATFORM_FILE_OK != error_code) {
374 error_code_ = error_code;
375 core_->InformDelegateFetchIsComplete();
376 return;
377 }
378
379 temp_file_handle_ = file_handle.ReleaseValue(); 332 temp_file_handle_ = file_handle.ReleaseValue();
380 total_bytes_written_ = 0; 333 total_bytes_written_ = 0;
381 334
382 core_->io_message_loop_proxy_->PostTask( 335 core_->io_message_loop_proxy_->PostTask(
383 FROM_HERE, 336 FROM_HERE,
384 NewRunnableMethod(core_, &Core::StartURLRequestWhenAppropriate)); 337 NewRunnableMethod(core_, &Core::StartURLRequestWhenAppropriate));
385 } 338 }
386 339
387 void URLFetcher::Core::TempFileWriter::WriteBuffer(int num_bytes) { 340 void URLFetcher::Core::TempFileWriter::WriteBuffer(int num_bytes) {
388 // Start writing to the temp file by setting the initial state 341 // Start writing to the temp file by setting the initial state
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after
1032 } 985 }
1033 986
1034 // static 987 // static
1035 int URLFetcher::GetNumFetcherCores() { 988 int URLFetcher::GetNumFetcherCores() {
1036 return Core::g_registry.Get().size(); 989 return Core::g_registry.Get().size();
1037 } 990 }
1038 991
1039 URLFetcher::Delegate* URLFetcher::delegate() const { 992 URLFetcher::Delegate* URLFetcher::delegate() const {
1040 return core_->delegate(); 993 return core_->delegate();
1041 } 994 }
OLDNEW
« no previous file with comments | « content/browser/renderer_host/redirect_to_file_resource_handler.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698