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

Side by Side Diff: webkit/browser/fileapi/local_file_stream_writer.cc

Issue 206083004: Revert of Revert of Add a parameter to FileStreamWriter::CreateForLocalFile to allow creating new (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 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 "webkit/browser/fileapi/local_file_stream_writer.h" 5 #include "webkit/browser/fileapi/local_file_stream_writer.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "net/base/file_stream.h" 9 #include "net/base/file_stream.h"
10 #include "net/base/io_buffer.h" 10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h" 11 #include "net/base/net_errors.h"
12 12
13 namespace fileapi { 13 namespace fileapi {
14 14
15 namespace { 15 namespace {
16 16
17 const int kOpenFlagsForWrite = base::PLATFORM_FILE_OPEN | 17 const int kOpenFlagsForWrite = base::PLATFORM_FILE_OPEN |
18 base::PLATFORM_FILE_WRITE | 18 base::PLATFORM_FILE_WRITE |
19 base::PLATFORM_FILE_ASYNC; 19 base::PLATFORM_FILE_ASYNC;
20 const int kCreateFlagsForWrite = base::PLATFORM_FILE_CREATE |
21 base::PLATFORM_FILE_WRITE |
22 base::PLATFORM_FILE_ASYNC;
20 23
21 } // namespace 24 } // namespace
22 25
23 FileStreamWriter* FileStreamWriter::CreateForLocalFile( 26 FileStreamWriter* FileStreamWriter::CreateForLocalFile(
24 base::TaskRunner* task_runner, 27 base::TaskRunner* task_runner,
25 const base::FilePath& file_path, 28 const base::FilePath& file_path,
26 int64 initial_offset) { 29 int64 initial_offset,
27 return new LocalFileStreamWriter(task_runner, file_path, initial_offset); 30 OpenOrCreate open_or_create) {
31 return new LocalFileStreamWriter(
32 task_runner, file_path, initial_offset, open_or_create);
28 } 33 }
29 34
30 LocalFileStreamWriter::~LocalFileStreamWriter() { 35 LocalFileStreamWriter::~LocalFileStreamWriter() {
31 // Invalidate weak pointers so that we won't receive any callbacks from 36 // Invalidate weak pointers so that we won't receive any callbacks from
32 // in-flight stream operations, which might be triggered during the file close 37 // in-flight stream operations, which might be triggered during the file close
33 // in the FileStream destructor. 38 // in the FileStream destructor.
34 weak_factory_.InvalidateWeakPtrs(); 39 weak_factory_.InvalidateWeakPtrs();
35 40
36 // FileStream's destructor closes the file safely, since we opened the file 41 // FileStream's destructor closes the file safely, since we opened the file
37 // by its Open() method. 42 // by its Open() method.
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 79
75 has_pending_operation_ = true; 80 has_pending_operation_ = true;
76 int result = InitiateFlush(callback); 81 int result = InitiateFlush(callback);
77 if (result != net::ERR_IO_PENDING) 82 if (result != net::ERR_IO_PENDING)
78 has_pending_operation_ = false; 83 has_pending_operation_ = false;
79 return result; 84 return result;
80 } 85 }
81 86
82 LocalFileStreamWriter::LocalFileStreamWriter(base::TaskRunner* task_runner, 87 LocalFileStreamWriter::LocalFileStreamWriter(base::TaskRunner* task_runner,
83 const base::FilePath& file_path, 88 const base::FilePath& file_path,
84 int64 initial_offset) 89 int64 initial_offset,
90 OpenOrCreate open_or_create)
85 : file_path_(file_path), 91 : file_path_(file_path),
92 open_or_create_(open_or_create),
86 initial_offset_(initial_offset), 93 initial_offset_(initial_offset),
87 task_runner_(task_runner), 94 task_runner_(task_runner),
88 has_pending_operation_(false), 95 has_pending_operation_(false),
89 weak_factory_(this) {} 96 weak_factory_(this) {}
90 97
91 int LocalFileStreamWriter::InitiateOpen( 98 int LocalFileStreamWriter::InitiateOpen(
92 const net::CompletionCallback& error_callback, 99 const net::CompletionCallback& error_callback,
93 const base::Closure& main_operation) { 100 const base::Closure& main_operation) {
94 DCHECK(has_pending_operation_); 101 DCHECK(has_pending_operation_);
95 DCHECK(!stream_impl_.get()); 102 DCHECK(!stream_impl_.get());
96 103
97 stream_impl_.reset(new net::FileStream(NULL, task_runner_)); 104 stream_impl_.reset(new net::FileStream(NULL, task_runner_));
98 105
106 int open_flags = 0;
107 switch (open_or_create_) {
108 case OPEN_EXISTING_FILE:
109 open_flags = kOpenFlagsForWrite;
110 break;
111 case CREATE_NEW_FILE:
112 open_flags = kCreateFlagsForWrite;
113 break;
114 }
115
99 return stream_impl_->Open(file_path_, 116 return stream_impl_->Open(file_path_,
100 kOpenFlagsForWrite, 117 open_flags,
101 base::Bind(&LocalFileStreamWriter::DidOpen, 118 base::Bind(&LocalFileStreamWriter::DidOpen,
102 weak_factory_.GetWeakPtr(), 119 weak_factory_.GetWeakPtr(),
103 error_callback, 120 error_callback,
104 main_operation)); 121 main_operation));
105 } 122 }
106 123
107 void LocalFileStreamWriter::DidOpen( 124 void LocalFileStreamWriter::DidOpen(
108 const net::CompletionCallback& error_callback, 125 const net::CompletionCallback& error_callback,
109 const base::Closure& main_operation, 126 const base::Closure& main_operation,
110 int result) { 127 int result) {
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 return false; 248 return false;
232 249
233 net::CompletionCallback pending_cancel = cancel_callback_; 250 net::CompletionCallback pending_cancel = cancel_callback_;
234 has_pending_operation_ = false; 251 has_pending_operation_ = false;
235 cancel_callback_.Reset(); 252 cancel_callback_.Reset();
236 pending_cancel.Run(net::OK); 253 pending_cancel.Run(net::OK);
237 return true; 254 return true;
238 } 255 }
239 256
240 } // namespace fileapi 257 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/browser/fileapi/local_file_stream_writer.h ('k') | webkit/browser/fileapi/local_file_stream_writer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698