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

Side by Side Diff: net/http/http_cache_data_access.cc

Issue 2519473002: Fixes the cache lock issue. (Closed)
Patch Set: Redesigned the fix using DataAccess class for eliminating Orphan API.(Rebased till refs/heads/master@{#442607}) Created 3 years, 11 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
OLDNEW
(Empty)
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <memory>
6 #include <utility>
7 #include "base/callback_helpers.h"
8 #include "net/disk_cache/disk_cache.h"
9 #include "net/http/http_cache_data_access.h"
10 #include "net/http/http_transaction.h"
11
12 namespace net {
13
14 HttpCache::DataAccess::DataAccess(
15 std::unique_ptr<HttpTransaction> network_transaction,
16 ActiveEntry* entry)
17 : entry_(entry), weak_factory_(this) {
18 network_transaction_ = std::move(network_transaction);
19 io_callback_ =
20 base::Bind(&DataAccess::OnIOComplete, weak_factory_.GetWeakPtr());
21 }
22
23 HttpCache::DataAccess::~DataAccess() {}
24
25 int HttpCache::DataAccess::DoLoop(int result) {
26 DCHECK(next_state_ != STATE_NONE);
27
28 int rv = result;
29
30 do {
31 State state = next_state_;
32 next_state_ = STATE_NONE;
33 switch (state) {
34 case STATE_NETWORK_READ:
35 DCHECK_EQ(OK, rv);
36 rv = DoNetworkRead();
37 break;
38 case STATE_NETWORK_READ_COMPLETE:
39 rv = DoNetworkReadComplete(rv);
40 break;
41 case STATE_CACHE_WRITE_DATA:
42 rv = DoCacheWriteData(rv);
43 break;
44 case STATE_CACHE_WRITE_DATA_COMPLETE:
45 rv = DoCacheWriteDataComplete(rv);
46 break;
47 default:
48 NOTREACHED() << "bad state";
49 rv = ERR_FAILED;
50 break;
51 }
52 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
53
54 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
55 read_buf_ = NULL; // Release the buffer before invoking the callback.
56 base::ResetAndReturn(&callback_).Run(rv);
57 }
58
59 return rv;
60 }
61
62 int HttpCache::DataAccess::Read(scoped_refptr<IOBuffer> buf,
63 int buf_len,
64 const CompletionCallback& callback) {
65 DCHECK_EQ(next_state_, STATE_NONE);
66 DCHECK(buf);
67 DCHECK_GT(buf_len, 0);
68 DCHECK(!callback.is_null());
69 DCHECK(callback_.is_null());
Randy Smith (Not in Mondays) 2017/01/19 00:53:42 Both the state invariants and argument invariants
Randy Smith (Not in Mondays) 2017/01/19 00:53:42 Suggestion: Group these by state invariants (i.e.
shivanisha 2017/01/25 19:46:13 N/A since DataAccess class no longer exists.
70
71 read_buf_ = std::move(buf);
Randy Smith (Not in Mondays) 2017/01/19 00:53:42 Should some assertion about read_buf_ being null b
shivanisha 2017/01/25 19:46:13 N/A since DataAccess class no longer exists.
72 io_buf_len_ = buf_len;
73
74 next_state_ = STATE_NETWORK_READ;
75 int rv = DoLoop(OK);
76
77 if (rv == ERR_IO_PENDING) {
78 DCHECK(callback_.is_null());
79 callback_ = callback;
80 }
81 return rv;
82 }
83
84 int HttpCache::DataAccess::DoNetworkRead() {
85 next_state_ = STATE_NETWORK_READ_COMPLETE;
86 return network_transaction_->Read(read_buf_.get(), io_buf_len_, io_callback_);
87 }
88
89 int HttpCache::DataAccess::DoNetworkReadComplete(int result) {
90 return result;
Randy Smith (Not in Mondays) 2017/01/19 00:53:42 Worthwhile nulling out read_buf_ and maybe io_buf_
shivanisha 2017/01/25 19:46:13 N/A since DataAccess class no longer exists.
91 }
92
93 int HttpCache::DataAccess::CacheWrite(scoped_refptr<IOBuffer> buf,
94 int write_len,
95 const CompletionCallback& callback) {
96 DCHECK_EQ(next_state_, STATE_NONE);
97 DCHECK(buf);
98 DCHECK_GE(write_len, 0);
99 DCHECK(!callback.is_null());
100 DCHECK(callback_.is_null());
101
102 read_buf_ = std::move(buf);
103 next_state_ = STATE_CACHE_WRITE_DATA;
104 int rv = DoLoop(write_len);
105
106 if (rv == ERR_IO_PENDING) {
107 DCHECK(callback_.is_null());
108 callback_ = callback;
109 }
110
111 return rv;
112 }
113
114 int HttpCache::DataAccess::DoCacheWriteData(int num_bytes) {
115 next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE;
116 write_len_ = num_bytes;
117 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
118 return entry_->disk_entry->WriteData(kResponseContentIndex, current_size,
119 read_buf_.get(), num_bytes, io_callback_,
120 true);
121 }
122
123 int HttpCache::DataAccess::DoCacheWriteDataComplete(int result) {
124 return result;
125 }
126
127 void HttpCache::DataAccess::OnIOComplete(int result) {
128 DoLoop(result);
129 }
130
131 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698