| OLD | NEW |
| 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 "net/disk_cache/entry_impl.h" | 5 #include "net/disk_cache/entry_impl.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 // This class implements FileIOCallback to buffer the callback from a file IO | 29 // This class implements FileIOCallback to buffer the callback from a file IO |
| 30 // operation from the actual net class. | 30 // operation from the actual net class. |
| 31 class SyncCallback: public disk_cache::FileIOCallback { | 31 class SyncCallback: public disk_cache::FileIOCallback { |
| 32 public: | 32 public: |
| 33 // |end_event_type| is the event type to log on completion. Logs nothing on | 33 // |end_event_type| is the event type to log on completion. Logs nothing on |
| 34 // discard, or when the NetLog is not set to log all events. | 34 // discard, or when the NetLog is not set to log all events. |
| 35 SyncCallback(disk_cache::EntryImpl* entry, net::IOBuffer* buffer, | 35 SyncCallback(disk_cache::EntryImpl* entry, net::IOBuffer* buffer, |
| 36 net::OldCompletionCallback* callback, | 36 net::OldCompletionCallback* callback, |
| 37 net::NetLog::EventType end_event_type) | 37 net::NetLog::EventType end_event_type) |
| 38 : entry_(entry), old_callback_(callback), buf_(buffer), | |
| 39 start_(TimeTicks::Now()), end_event_type_(end_event_type) { | |
| 40 entry->AddRef(); | |
| 41 entry->IncrementIoCount(); | |
| 42 } | |
| 43 SyncCallback(disk_cache::EntryImpl* entry, net::IOBuffer* buffer, | |
| 44 const net::CompletionCallback& callback, | |
| 45 net::NetLog::EventType end_event_type) | |
| 46 : entry_(entry), callback_(callback), buf_(buffer), | 38 : entry_(entry), callback_(callback), buf_(buffer), |
| 47 start_(TimeTicks::Now()), end_event_type_(end_event_type) { | 39 start_(TimeTicks::Now()), end_event_type_(end_event_type) { |
| 48 entry->AddRef(); | 40 entry->AddRef(); |
| 49 entry->IncrementIoCount(); | 41 entry->IncrementIoCount(); |
| 50 } | 42 } |
| 51 ~SyncCallback() {} | 43 ~SyncCallback() {} |
| 52 | 44 |
| 53 virtual void OnFileIOComplete(int bytes_copied); | 45 virtual void OnFileIOComplete(int bytes_copied); |
| 54 void Discard(); | 46 void Discard(); |
| 55 | 47 |
| 56 private: | 48 private: |
| 57 disk_cache::EntryImpl* entry_; | 49 disk_cache::EntryImpl* entry_; |
| 58 net::OldCompletionCallback* old_callback_; | 50 net::OldCompletionCallback* callback_; |
| 59 net::CompletionCallback callback_; | |
| 60 scoped_refptr<net::IOBuffer> buf_; | 51 scoped_refptr<net::IOBuffer> buf_; |
| 61 TimeTicks start_; | 52 TimeTicks start_; |
| 62 const net::NetLog::EventType end_event_type_; | 53 const net::NetLog::EventType end_event_type_; |
| 63 | 54 |
| 64 DISALLOW_COPY_AND_ASSIGN(SyncCallback); | 55 DISALLOW_COPY_AND_ASSIGN(SyncCallback); |
| 65 }; | 56 }; |
| 66 | 57 |
| 67 void SyncCallback::OnFileIOComplete(int bytes_copied) { | 58 void SyncCallback::OnFileIOComplete(int bytes_copied) { |
| 68 entry_->DecrementIoCount(); | 59 entry_->DecrementIoCount(); |
| 69 if (old_callback_ || !callback_.is_null()) { | 60 if (callback_) { |
| 70 if (entry_->net_log().IsLoggingAllEvents()) { | 61 if (entry_->net_log().IsLoggingAllEvents()) { |
| 71 entry_->net_log().EndEvent( | 62 entry_->net_log().EndEvent( |
| 72 end_event_type_, | 63 end_event_type_, |
| 73 make_scoped_refptr( | 64 make_scoped_refptr( |
| 74 new disk_cache::ReadWriteCompleteParameters(bytes_copied))); | 65 new disk_cache::ReadWriteCompleteParameters(bytes_copied))); |
| 75 } | 66 } |
| 76 entry_->ReportIOTime(disk_cache::EntryImpl::kAsyncIO, start_); | 67 entry_->ReportIOTime(disk_cache::EntryImpl::kAsyncIO, start_); |
| 77 | 68 callback_->Run(bytes_copied); |
| 78 if (old_callback_) | |
| 79 old_callback_->Run(bytes_copied); | |
| 80 else | |
| 81 callback_.Run(bytes_copied); | |
| 82 } | 69 } |
| 83 entry_->Release(); | 70 entry_->Release(); |
| 84 delete this; | 71 delete this; |
| 85 } | 72 } |
| 86 | 73 |
| 87 void SyncCallback::Discard() { | 74 void SyncCallback::Discard() { |
| 88 old_callback_ = NULL; | 75 callback_ = NULL; |
| 89 callback_.Reset(); | |
| 90 buf_ = NULL; | 76 buf_ = NULL; |
| 91 OnFileIOComplete(0); | 77 OnFileIOComplete(0); |
| 92 } | 78 } |
| 93 | 79 |
| 94 const int kMaxBufferSize = 1024 * 1024; // 1 MB. | 80 const int kMaxBufferSize = 1024 * 1024; // 1 MB. |
| 95 | 81 |
| 96 } // namespace | 82 } // namespace |
| 97 | 83 |
| 98 namespace disk_cache { | 84 namespace disk_cache { |
| 99 | 85 |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 int result = InternalReadData(index, offset, buf, buf_len, callback); | 320 int result = InternalReadData(index, offset, buf, buf_len, callback); |
| 335 | 321 |
| 336 if (result != net::ERR_IO_PENDING && net_log_.IsLoggingAllEvents()) { | 322 if (result != net::ERR_IO_PENDING && net_log_.IsLoggingAllEvents()) { |
| 337 net_log_.EndEvent( | 323 net_log_.EndEvent( |
| 338 net::NetLog::TYPE_ENTRY_READ_DATA, | 324 net::NetLog::TYPE_ENTRY_READ_DATA, |
| 339 make_scoped_refptr(new ReadWriteCompleteParameters(result))); | 325 make_scoped_refptr(new ReadWriteCompleteParameters(result))); |
| 340 } | 326 } |
| 341 return result; | 327 return result; |
| 342 } | 328 } |
| 343 | 329 |
| 344 int EntryImpl::ReadDataImpl( | |
| 345 int index, int offset, net::IOBuffer* buf, int buf_len, | |
| 346 const net::CompletionCallback& callback) { | |
| 347 if (net_log_.IsLoggingAllEvents()) { | |
| 348 net_log_.BeginEvent( | |
| 349 net::NetLog::TYPE_ENTRY_READ_DATA, | |
| 350 make_scoped_refptr( | |
| 351 new ReadWriteDataParameters(index, offset, buf_len, false))); | |
| 352 } | |
| 353 | |
| 354 int result = InternalReadData(index, offset, buf, buf_len, callback); | |
| 355 | |
| 356 if (result != net::ERR_IO_PENDING && net_log_.IsLoggingAllEvents()) { | |
| 357 net_log_.EndEvent( | |
| 358 net::NetLog::TYPE_ENTRY_READ_DATA, | |
| 359 make_scoped_refptr(new ReadWriteCompleteParameters(result))); | |
| 360 } | |
| 361 return result; | |
| 362 } | |
| 363 | |
| 364 int EntryImpl::WriteDataImpl(int index, int offset, net::IOBuffer* buf, | 330 int EntryImpl::WriteDataImpl(int index, int offset, net::IOBuffer* buf, |
| 365 int buf_len, OldCompletionCallback* callback, | 331 int buf_len, OldCompletionCallback* callback, |
| 366 bool truncate) { | 332 bool truncate) { |
| 367 if (net_log_.IsLoggingAllEvents()) { | 333 if (net_log_.IsLoggingAllEvents()) { |
| 368 net_log_.BeginEvent( | 334 net_log_.BeginEvent( |
| 369 net::NetLog::TYPE_ENTRY_WRITE_DATA, | 335 net::NetLog::TYPE_ENTRY_WRITE_DATA, |
| 370 make_scoped_refptr( | 336 make_scoped_refptr( |
| 371 new ReadWriteDataParameters(index, offset, buf_len, truncate))); | 337 new ReadWriteDataParameters(index, offset, buf_len, truncate))); |
| 372 } | 338 } |
| 373 | 339 |
| 374 int result = InternalWriteData(index, offset, buf, buf_len, callback, | 340 int result = InternalWriteData(index, offset, buf, buf_len, callback, |
| 375 truncate); | 341 truncate); |
| 376 | 342 |
| 377 if (result != net::ERR_IO_PENDING && net_log_.IsLoggingAllEvents()) { | 343 if (result != net::ERR_IO_PENDING && net_log_.IsLoggingAllEvents()) { |
| 378 net_log_.EndEvent( | 344 net_log_.EndEvent( |
| 379 net::NetLog::TYPE_ENTRY_WRITE_DATA, | 345 net::NetLog::TYPE_ENTRY_WRITE_DATA, |
| 380 make_scoped_refptr(new ReadWriteCompleteParameters(result))); | 346 make_scoped_refptr(new ReadWriteCompleteParameters(result))); |
| 381 } | 347 } |
| 382 return result; | 348 return result; |
| 383 } | 349 } |
| 384 | 350 |
| 385 int EntryImpl::WriteDataImpl( | |
| 386 int index, int offset, net::IOBuffer* buf, int buf_len, | |
| 387 const net::CompletionCallback& callback, bool truncate) { | |
| 388 if (net_log_.IsLoggingAllEvents()) { | |
| 389 net_log_.BeginEvent( | |
| 390 net::NetLog::TYPE_ENTRY_WRITE_DATA, | |
| 391 make_scoped_refptr( | |
| 392 new ReadWriteDataParameters(index, offset, buf_len, truncate))); | |
| 393 } | |
| 394 | |
| 395 int result = InternalWriteData(index, offset, buf, buf_len, callback, | |
| 396 truncate); | |
| 397 | |
| 398 if (result != net::ERR_IO_PENDING && net_log_.IsLoggingAllEvents()) { | |
| 399 net_log_.EndEvent( | |
| 400 net::NetLog::TYPE_ENTRY_WRITE_DATA, | |
| 401 make_scoped_refptr(new ReadWriteCompleteParameters(result))); | |
| 402 } | |
| 403 return result; | |
| 404 } | |
| 405 | |
| 406 int EntryImpl::ReadSparseDataImpl(int64 offset, net::IOBuffer* buf, int buf_len, | 351 int EntryImpl::ReadSparseDataImpl(int64 offset, net::IOBuffer* buf, int buf_len, |
| 407 OldCompletionCallback* callback) { | 352 OldCompletionCallback* callback) { |
| 408 DCHECK(node_.Data()->dirty || read_only_); | 353 DCHECK(node_.Data()->dirty || read_only_); |
| 409 int result = InitSparseData(); | 354 int result = InitSparseData(); |
| 410 if (net::OK != result) | 355 if (net::OK != result) |
| 411 return result; | 356 return result; |
| 412 | 357 |
| 413 TimeTicks start = TimeTicks::Now(); | 358 TimeTicks start = TimeTicks::Now(); |
| 414 result = sparse_->StartIO(SparseControl::kReadOperation, offset, buf, buf_len, | 359 result = sparse_->StartIO(SparseControl::kReadOperation, offset, buf, buf_len, |
| 415 callback); | 360 callback); |
| (...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 866 return 0; | 811 return 0; |
| 867 | 812 |
| 868 if (buf_len < 0) | 813 if (buf_len < 0) |
| 869 return net::ERR_INVALID_ARGUMENT; | 814 return net::ERR_INVALID_ARGUMENT; |
| 870 | 815 |
| 871 backend_->background_queue()->ReadData(this, index, offset, buf, buf_len, | 816 backend_->background_queue()->ReadData(this, index, offset, buf, buf_len, |
| 872 callback); | 817 callback); |
| 873 return net::ERR_IO_PENDING; | 818 return net::ERR_IO_PENDING; |
| 874 } | 819 } |
| 875 | 820 |
| 876 int EntryImpl::ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, | |
| 877 const net::CompletionCallback& callback) { | |
| 878 if (callback.is_null()) | |
| 879 return ReadDataImpl(index, offset, buf, buf_len, callback); | |
| 880 | |
| 881 DCHECK(node_.Data()->dirty || read_only_); | |
| 882 if (index < 0 || index >= kNumStreams) | |
| 883 return net::ERR_INVALID_ARGUMENT; | |
| 884 | |
| 885 int entry_size = entry_.Data()->data_size[index]; | |
| 886 if (offset >= entry_size || offset < 0 || !buf_len) | |
| 887 return 0; | |
| 888 | |
| 889 if (buf_len < 0) | |
| 890 return net::ERR_INVALID_ARGUMENT; | |
| 891 | |
| 892 backend_->background_queue()->ReadData(this, index, offset, buf, buf_len, | |
| 893 callback); | |
| 894 return net::ERR_IO_PENDING; | |
| 895 } | |
| 896 | |
| 897 int EntryImpl::WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, | 821 int EntryImpl::WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, |
| 898 OldCompletionCallback* callback, bool truncate) { | 822 OldCompletionCallback* callback, bool truncate) { |
| 899 if (!callback) | 823 if (!callback) |
| 900 return WriteDataImpl(index, offset, buf, buf_len, callback, truncate); | 824 return WriteDataImpl(index, offset, buf, buf_len, callback, truncate); |
| 901 | 825 |
| 902 DCHECK(node_.Data()->dirty || read_only_); | 826 DCHECK(node_.Data()->dirty || read_only_); |
| 903 if (index < 0 || index >= kNumStreams) | 827 if (index < 0 || index >= kNumStreams) |
| 904 return net::ERR_INVALID_ARGUMENT; | 828 return net::ERR_INVALID_ARGUMENT; |
| 905 | 829 |
| 906 if (offset < 0 || buf_len < 0) | 830 if (offset < 0 || buf_len < 0) |
| 907 return net::ERR_INVALID_ARGUMENT; | 831 return net::ERR_INVALID_ARGUMENT; |
| 908 | 832 |
| 909 backend_->background_queue()->WriteData(this, index, offset, buf, buf_len, | 833 backend_->background_queue()->WriteData(this, index, offset, buf, buf_len, |
| 910 truncate, callback); | 834 truncate, callback); |
| 911 return net::ERR_IO_PENDING; | 835 return net::ERR_IO_PENDING; |
| 912 } | 836 } |
| 913 | 837 |
| 914 int EntryImpl::WriteData( | |
| 915 int index, int offset, net::IOBuffer* buf, int buf_len, | |
| 916 const net::CompletionCallback& callback, bool truncate) { | |
| 917 if (callback.is_null()) | |
| 918 return WriteDataImpl(index, offset, buf, buf_len, callback, truncate); | |
| 919 | |
| 920 DCHECK(node_.Data()->dirty || read_only_); | |
| 921 if (index < 0 || index >= kNumStreams) | |
| 922 return net::ERR_INVALID_ARGUMENT; | |
| 923 | |
| 924 if (offset < 0 || buf_len < 0) | |
| 925 return net::ERR_INVALID_ARGUMENT; | |
| 926 | |
| 927 backend_->background_queue()->WriteData(this, index, offset, buf, buf_len, | |
| 928 truncate, callback); | |
| 929 return net::ERR_IO_PENDING; | |
| 930 } | |
| 931 | |
| 932 int EntryImpl::ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, | 838 int EntryImpl::ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, |
| 933 net::OldCompletionCallback* callback) { | 839 net::OldCompletionCallback* callback) { |
| 934 if (!callback) | 840 if (!callback) |
| 935 return ReadSparseDataImpl(offset, buf, buf_len, callback); | 841 return ReadSparseDataImpl(offset, buf, buf_len, callback); |
| 936 | 842 |
| 937 backend_->background_queue()->ReadSparseData(this, offset, buf, buf_len, | 843 backend_->background_queue()->ReadSparseData(this, offset, buf, buf_len, |
| 938 callback); | 844 callback); |
| 939 return net::ERR_IO_PENDING; | 845 return net::ERR_IO_PENDING; |
| 940 } | 846 } |
| 941 | 847 |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1100 if (io_callback && completed) | 1006 if (io_callback && completed) |
| 1101 io_callback->Discard(); | 1007 io_callback->Discard(); |
| 1102 | 1008 |
| 1103 if (io_callback) | 1009 if (io_callback) |
| 1104 ReportIOTime(kReadAsync1, start_async); | 1010 ReportIOTime(kReadAsync1, start_async); |
| 1105 | 1011 |
| 1106 ReportIOTime(kRead, start); | 1012 ReportIOTime(kRead, start); |
| 1107 return (completed || !callback) ? buf_len : net::ERR_IO_PENDING; | 1013 return (completed || !callback) ? buf_len : net::ERR_IO_PENDING; |
| 1108 } | 1014 } |
| 1109 | 1015 |
| 1110 int EntryImpl::InternalReadData( | |
| 1111 int index, int offset, net::IOBuffer* buf, int buf_len, | |
| 1112 const net::CompletionCallback& callback) { | |
| 1113 DCHECK(node_.Data()->dirty || read_only_); | |
| 1114 DVLOG(2) << "Read from " << index << " at " << offset << " : " << buf_len; | |
| 1115 if (index < 0 || index >= kNumStreams) | |
| 1116 return net::ERR_INVALID_ARGUMENT; | |
| 1117 | |
| 1118 int entry_size = entry_.Data()->data_size[index]; | |
| 1119 if (offset >= entry_size || offset < 0 || !buf_len) | |
| 1120 return 0; | |
| 1121 | |
| 1122 if (buf_len < 0) | |
| 1123 return net::ERR_INVALID_ARGUMENT; | |
| 1124 | |
| 1125 TimeTicks start = TimeTicks::Now(); | |
| 1126 | |
| 1127 if (offset + buf_len > entry_size) | |
| 1128 buf_len = entry_size - offset; | |
| 1129 | |
| 1130 UpdateRank(false); | |
| 1131 | |
| 1132 backend_->OnEvent(Stats::READ_DATA); | |
| 1133 backend_->OnRead(buf_len); | |
| 1134 | |
| 1135 Addr address(entry_.Data()->data_addr[index]); | |
| 1136 int eof = address.is_initialized() ? entry_size : 0; | |
| 1137 if (user_buffers_[index].get() && | |
| 1138 user_buffers_[index]->PreRead(eof, offset, &buf_len)) { | |
| 1139 // Complete the operation locally. | |
| 1140 buf_len = user_buffers_[index]->Read(offset, buf, buf_len); | |
| 1141 ReportIOTime(kRead, start); | |
| 1142 return buf_len; | |
| 1143 } | |
| 1144 | |
| 1145 address.set_value(entry_.Data()->data_addr[index]); | |
| 1146 DCHECK(address.is_initialized()); | |
| 1147 if (!address.is_initialized()) | |
| 1148 return net::ERR_FAILED; | |
| 1149 | |
| 1150 File* file = GetBackingFile(address, index); | |
| 1151 if (!file) | |
| 1152 return net::ERR_FAILED; | |
| 1153 | |
| 1154 size_t file_offset = offset; | |
| 1155 if (address.is_block_file()) { | |
| 1156 DCHECK_LE(offset + buf_len, kMaxBlockSize); | |
| 1157 file_offset += address.start_block() * address.BlockSize() + | |
| 1158 kBlockHeaderSize; | |
| 1159 } | |
| 1160 | |
| 1161 SyncCallback* io_callback = NULL; | |
| 1162 if (!callback.is_null()) { | |
| 1163 io_callback = new SyncCallback(this, buf, callback, | |
| 1164 net::NetLog::TYPE_ENTRY_READ_DATA); | |
| 1165 } | |
| 1166 | |
| 1167 TimeTicks start_async = TimeTicks::Now(); | |
| 1168 | |
| 1169 bool completed; | |
| 1170 if (!file->Read(buf->data(), buf_len, file_offset, io_callback, &completed)) { | |
| 1171 if (io_callback) | |
| 1172 io_callback->Discard(); | |
| 1173 return net::ERR_FAILED; | |
| 1174 } | |
| 1175 | |
| 1176 if (io_callback && completed) | |
| 1177 io_callback->Discard(); | |
| 1178 | |
| 1179 if (io_callback) | |
| 1180 ReportIOTime(kReadAsync1, start_async); | |
| 1181 | |
| 1182 ReportIOTime(kRead, start); | |
| 1183 return (completed || callback.is_null()) ? buf_len : net::ERR_IO_PENDING; | |
| 1184 } | |
| 1185 | |
| 1186 int EntryImpl::InternalWriteData(int index, int offset, net::IOBuffer* buf, | 1016 int EntryImpl::InternalWriteData(int index, int offset, net::IOBuffer* buf, |
| 1187 int buf_len, OldCompletionCallback* callback, | 1017 int buf_len, OldCompletionCallback* callback, |
| 1188 bool truncate) { | 1018 bool truncate) { |
| 1189 DCHECK(node_.Data()->dirty || read_only_); | 1019 DCHECK(node_.Data()->dirty || read_only_); |
| 1190 DVLOG(2) << "Write to " << index << " at " << offset << " : " << buf_len; | 1020 DVLOG(2) << "Write to " << index << " at " << offset << " : " << buf_len; |
| 1191 if (index < 0 || index >= kNumStreams) | 1021 if (index < 0 || index >= kNumStreams) |
| 1192 return net::ERR_INVALID_ARGUMENT; | 1022 return net::ERR_INVALID_ARGUMENT; |
| 1193 | 1023 |
| 1194 if (offset < 0 || buf_len < 0) | 1024 if (offset < 0 || buf_len < 0) |
| 1195 return net::ERR_INVALID_ARGUMENT; | 1025 return net::ERR_INVALID_ARGUMENT; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1276 if (io_callback && completed) | 1106 if (io_callback && completed) |
| 1277 io_callback->Discard(); | 1107 io_callback->Discard(); |
| 1278 | 1108 |
| 1279 if (io_callback) | 1109 if (io_callback) |
| 1280 ReportIOTime(kWriteAsync1, start_async); | 1110 ReportIOTime(kWriteAsync1, start_async); |
| 1281 | 1111 |
| 1282 ReportIOTime(kWrite, start); | 1112 ReportIOTime(kWrite, start); |
| 1283 return (completed || !callback) ? buf_len : net::ERR_IO_PENDING; | 1113 return (completed || !callback) ? buf_len : net::ERR_IO_PENDING; |
| 1284 } | 1114 } |
| 1285 | 1115 |
| 1286 int EntryImpl::InternalWriteData( | |
| 1287 int index, int offset, net::IOBuffer* buf, int buf_len, | |
| 1288 const net::CompletionCallback& callback, bool truncate) { | |
| 1289 DCHECK(node_.Data()->dirty || read_only_); | |
| 1290 DVLOG(2) << "Write to " << index << " at " << offset << " : " << buf_len; | |
| 1291 if (index < 0 || index >= kNumStreams) | |
| 1292 return net::ERR_INVALID_ARGUMENT; | |
| 1293 | |
| 1294 if (offset < 0 || buf_len < 0) | |
| 1295 return net::ERR_INVALID_ARGUMENT; | |
| 1296 | |
| 1297 int max_file_size = backend_->MaxFileSize(); | |
| 1298 | |
| 1299 // offset or buf_len could be negative numbers. | |
| 1300 if (offset > max_file_size || buf_len > max_file_size || | |
| 1301 offset + buf_len > max_file_size) { | |
| 1302 int size = offset + buf_len; | |
| 1303 if (size <= max_file_size) | |
| 1304 size = kint32max; | |
| 1305 backend_->TooMuchStorageRequested(size); | |
| 1306 return net::ERR_FAILED; | |
| 1307 } | |
| 1308 | |
| 1309 TimeTicks start = TimeTicks::Now(); | |
| 1310 | |
| 1311 // Read the size at this point (it may change inside prepare). | |
| 1312 int entry_size = entry_.Data()->data_size[index]; | |
| 1313 bool extending = entry_size < offset + buf_len; | |
| 1314 truncate = truncate && entry_size > offset + buf_len; | |
| 1315 Trace("To PrepareTarget 0x%x", entry_.address().value()); | |
| 1316 if (!PrepareTarget(index, offset, buf_len, truncate)) | |
| 1317 return net::ERR_FAILED; | |
| 1318 | |
| 1319 Trace("From PrepareTarget 0x%x", entry_.address().value()); | |
| 1320 if (extending || truncate) | |
| 1321 UpdateSize(index, entry_size, offset + buf_len); | |
| 1322 | |
| 1323 UpdateRank(true); | |
| 1324 | |
| 1325 backend_->OnEvent(Stats::WRITE_DATA); | |
| 1326 backend_->OnWrite(buf_len); | |
| 1327 | |
| 1328 if (user_buffers_[index].get()) { | |
| 1329 // Complete the operation locally. | |
| 1330 user_buffers_[index]->Write(offset, buf, buf_len); | |
| 1331 ReportIOTime(kWrite, start); | |
| 1332 return buf_len; | |
| 1333 } | |
| 1334 | |
| 1335 Addr address(entry_.Data()->data_addr[index]); | |
| 1336 if (offset + buf_len == 0) { | |
| 1337 if (truncate) { | |
| 1338 DCHECK(!address.is_initialized()); | |
| 1339 } | |
| 1340 return 0; | |
| 1341 } | |
| 1342 | |
| 1343 File* file = GetBackingFile(address, index); | |
| 1344 if (!file) | |
| 1345 return net::ERR_FAILED; | |
| 1346 | |
| 1347 size_t file_offset = offset; | |
| 1348 if (address.is_block_file()) { | |
| 1349 DCHECK_LE(offset + buf_len, kMaxBlockSize); | |
| 1350 file_offset += address.start_block() * address.BlockSize() + | |
| 1351 kBlockHeaderSize; | |
| 1352 } else if (truncate || (extending && !buf_len)) { | |
| 1353 if (!file->SetLength(offset + buf_len)) | |
| 1354 return net::ERR_FAILED; | |
| 1355 } | |
| 1356 | |
| 1357 if (!buf_len) | |
| 1358 return 0; | |
| 1359 | |
| 1360 SyncCallback* io_callback = NULL; | |
| 1361 if (!callback.is_null()) { | |
| 1362 io_callback = new SyncCallback(this, buf, callback, | |
| 1363 net::NetLog::TYPE_ENTRY_WRITE_DATA); | |
| 1364 } | |
| 1365 | |
| 1366 TimeTicks start_async = TimeTicks::Now(); | |
| 1367 | |
| 1368 bool completed; | |
| 1369 if (!file->Write(buf->data(), buf_len, file_offset, io_callback, | |
| 1370 &completed)) { | |
| 1371 if (io_callback) | |
| 1372 io_callback->Discard(); | |
| 1373 return net::ERR_FAILED; | |
| 1374 } | |
| 1375 | |
| 1376 if (io_callback && completed) | |
| 1377 io_callback->Discard(); | |
| 1378 | |
| 1379 if (io_callback) | |
| 1380 ReportIOTime(kWriteAsync1, start_async); | |
| 1381 | |
| 1382 ReportIOTime(kWrite, start); | |
| 1383 return (completed || callback.is_null()) ? buf_len : net::ERR_IO_PENDING; | |
| 1384 } | |
| 1385 | |
| 1386 // ------------------------------------------------------------------------ | 1116 // ------------------------------------------------------------------------ |
| 1387 | 1117 |
| 1388 bool EntryImpl::CreateDataBlock(int index, int size) { | 1118 bool EntryImpl::CreateDataBlock(int index, int size) { |
| 1389 DCHECK(index >= 0 && index < kNumStreams); | 1119 DCHECK(index >= 0 && index < kNumStreams); |
| 1390 | 1120 |
| 1391 Addr address(entry_.Data()->data_addr[index]); | 1121 Addr address(entry_.Data()->data_addr[index]); |
| 1392 if (!CreateBlock(size, &address)) | 1122 if (!CreateBlock(size, &address)) |
| 1393 return false; | 1123 return false; |
| 1394 | 1124 |
| 1395 entry_.Data()->data_addr[index] = address.value(); | 1125 entry_.Data()->data_addr[index] = address.value(); |
| (...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1759 Trace("%s 0x%p 0x%x 0x%x", msg, reinterpret_cast<void*>(this), | 1489 Trace("%s 0x%p 0x%x 0x%x", msg, reinterpret_cast<void*>(this), |
| 1760 entry_.address().value(), node_.address().value()); | 1490 entry_.address().value(), node_.address().value()); |
| 1761 | 1491 |
| 1762 Trace(" data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0], | 1492 Trace(" data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0], |
| 1763 entry_.Data()->data_addr[1], entry_.Data()->long_key); | 1493 entry_.Data()->data_addr[1], entry_.Data()->long_key); |
| 1764 | 1494 |
| 1765 Trace(" doomed: %d 0x%x", doomed_, dirty); | 1495 Trace(" doomed: %d 0x%x", doomed_, dirty); |
| 1766 } | 1496 } |
| 1767 | 1497 |
| 1768 } // namespace disk_cache | 1498 } // namespace disk_cache |
| OLD | NEW |