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 "chrome/browser/net/view_http_cache_job_factory.h" | 5 #include "chrome/browser/net/view_http_cache_job_factory.h" |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/callback.h" | |
7 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/memory/weak_ptr.h" | |
8 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
9 #include "base/string_util.h" | 13 #include "base/string_util.h" |
10 #include "base/task.h" | |
11 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
15 #include "net/base/completion_callback.h" | |
12 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
13 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
14 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
15 #include "net/url_request/url_request_simple_job.h" | 19 #include "net/url_request/url_request_simple_job.h" |
16 #include "net/url_request/view_cache_helper.h" | 20 #include "net/url_request/view_cache_helper.h" |
17 | 21 |
18 namespace { | 22 namespace { |
19 | 23 |
20 // A job subclass that dumps an HTTP cache entry. | 24 // A job subclass that dumps an HTTP cache entry. |
21 class ViewHttpCacheJob : public net::URLRequestJob { | 25 class ViewHttpCacheJob : public net::URLRequestJob { |
22 public: | 26 public: |
23 explicit ViewHttpCacheJob(net::URLRequest* request) | 27 explicit ViewHttpCacheJob(net::URLRequest* request) |
24 : net::URLRequestJob(request), | 28 : net::URLRequestJob(request), |
25 core_(new Core), | 29 core_(new Core), |
26 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | 30 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
27 ALLOW_THIS_IN_INITIALIZER_LIST( | 31 ALLOW_THIS_IN_INITIALIZER_LIST( |
28 callback_(NewCallback(this, | 32 callback_(base::Bind(&ViewHttpCacheJob::OnStartCompleted, |
29 &ViewHttpCacheJob::OnStartCompleted))) {} | 33 base::Unretained(this)))) { |
34 } | |
30 | 35 |
31 virtual void Start(); | 36 // net::URLRequestJob implementation. |
32 virtual void Kill(); | 37 virtual void Start() OVERRIDE; |
33 virtual bool GetMimeType(std::string* mime_type) const { | 38 virtual void Kill() OVERRIDE; |
39 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE{ | |
34 return core_->GetMimeType(mime_type); | 40 return core_->GetMimeType(mime_type); |
35 } | 41 } |
36 virtual bool GetCharset(std::string* charset) { | 42 virtual bool GetCharset(std::string* charset) OVERRIDE{ |
37 return core_->GetCharset(charset); | 43 return core_->GetCharset(charset); |
38 } | 44 } |
39 virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read) { | 45 virtual bool ReadRawData(net::IOBuffer* buf, |
46 int buf_size, int *bytes_read) OVERRIDE{ | |
40 return core_->ReadRawData(buf, buf_size, bytes_read); | 47 return core_->ReadRawData(buf, buf_size, bytes_read); |
41 } | 48 } |
42 | 49 |
43 private: | 50 private: |
44 class Core : public base::RefCounted<Core> { | 51 class Core : public base::RefCounted<Core> { |
45 public: | 52 public: |
46 Core() | 53 Core() |
47 : data_offset_(0), | 54 : data_offset_(0), |
48 ALLOW_THIS_IN_INITIALIZER_LIST( | 55 ALLOW_THIS_IN_INITIALIZER_LIST(callback_( |
49 callback_(this, &Core::OnIOComplete)), | 56 base::Bind(&Core::OnIOComplete, this))) { |
csilv
2011/11/23 18:37:32
I'm a little unclear what this old implementation
| |
50 user_callback_(NULL) {} | 57 } |
51 | 58 |
52 int Start(const net::URLRequest& request, Callback0::Type* callback); | 59 int Start(const net::URLRequest& request, const base::Closure& callback); |
53 | 60 |
54 // Prevents it from invoking its callback. It will self-delete. | 61 // Prevents it from invoking its callback. It will self-delete. |
55 void Orphan() { | 62 void Orphan() { |
56 user_callback_ = NULL; | 63 user_callback_.Reset(); |
57 } | 64 } |
58 | 65 |
59 bool GetMimeType(std::string* mime_type) const; | 66 bool GetMimeType(std::string* mime_type) const; |
60 bool GetCharset(std::string* charset); | 67 bool GetCharset(std::string* charset); |
61 bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read); | 68 bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read); |
62 | 69 |
63 private: | 70 private: |
64 friend class base::RefCounted<Core>; | 71 friend class base::RefCounted<Core>; |
65 | 72 |
66 ~Core() {} | 73 ~Core() {} |
67 | 74 |
68 // Called when ViewCacheHelper completes the operation. | 75 // Called when ViewCacheHelper completes the operation. |
69 void OnIOComplete(int result); | 76 void OnIOComplete(int result); |
70 | 77 |
71 std::string data_; | 78 std::string data_; |
72 int data_offset_; | 79 int data_offset_; |
73 net::ViewCacheHelper cache_helper_; | 80 net::ViewCacheHelper cache_helper_; |
74 net::OldCompletionCallbackImpl<Core> callback_; | 81 net::CompletionCallback callback_; |
75 Callback0::Type* user_callback_; | 82 base::Closure user_callback_; |
76 | 83 |
77 DISALLOW_COPY_AND_ASSIGN(Core); | 84 DISALLOW_COPY_AND_ASSIGN(Core); |
78 }; | 85 }; |
79 | 86 |
80 ~ViewHttpCacheJob() {} | 87 ~ViewHttpCacheJob() {} |
81 | 88 |
82 void StartAsync(); | 89 void StartAsync(); |
83 void OnStartCompleted(); | 90 void OnStartCompleted(); |
84 | 91 |
85 scoped_refptr<Core> core_; | 92 scoped_refptr<Core> core_; |
86 ScopedRunnableMethodFactory<ViewHttpCacheJob> method_factory_; | 93 base::WeakPtrFactory<ViewHttpCacheJob> weak_factory_; |
87 scoped_ptr<Callback0::Type> callback_; | 94 base::Closure callback_; |
88 | 95 |
89 DISALLOW_COPY_AND_ASSIGN(ViewHttpCacheJob); | 96 DISALLOW_COPY_AND_ASSIGN(ViewHttpCacheJob); |
90 }; | 97 }; |
91 | 98 |
92 void ViewHttpCacheJob::Start() { | 99 void ViewHttpCacheJob::Start() { |
93 MessageLoop::current()->PostTask( | 100 MessageLoop::current()->PostTask( |
94 FROM_HERE, | 101 FROM_HERE, |
95 method_factory_.NewRunnableMethod(&ViewHttpCacheJob::StartAsync)); | 102 base::Bind(&ViewHttpCacheJob::StartAsync, weak_factory_.GetWeakPtr())); |
96 } | 103 } |
97 | 104 |
98 void ViewHttpCacheJob::Kill() { | 105 void ViewHttpCacheJob::Kill() { |
99 method_factory_.RevokeAll(); | 106 weak_factory_.InvalidateWeakPtrs(); |
100 if (core_) { | 107 if (core_) { |
101 core_->Orphan(); | 108 core_->Orphan(); |
102 core_ = NULL; | 109 core_ = NULL; |
103 } | 110 } |
104 net::URLRequestJob::Kill(); | 111 net::URLRequestJob::Kill(); |
105 } | 112 } |
106 | 113 |
107 void ViewHttpCacheJob::StartAsync() { | 114 void ViewHttpCacheJob::StartAsync() { |
108 DCHECK(request()); | 115 DCHECK(request()); |
109 | 116 |
110 if (!request()) | 117 if (!request()) |
111 return; | 118 return; |
112 | 119 |
113 int rv = core_->Start(*request(), callback_.get()); | 120 int rv = core_->Start(*request(), callback_); |
114 if (rv != net::ERR_IO_PENDING) { | 121 if (rv != net::ERR_IO_PENDING) { |
115 DCHECK_EQ(net::OK, rv); | 122 DCHECK_EQ(net::OK, rv); |
116 OnStartCompleted(); | 123 OnStartCompleted(); |
117 } | 124 } |
118 } | 125 } |
119 | 126 |
120 void ViewHttpCacheJob::OnStartCompleted() { | 127 void ViewHttpCacheJob::OnStartCompleted() { |
121 NotifyHeadersComplete(); | 128 NotifyHeadersComplete(); |
122 } | 129 } |
123 | 130 |
124 int ViewHttpCacheJob::Core::Start(const net::URLRequest& request, | 131 int ViewHttpCacheJob::Core::Start(const net::URLRequest& request, |
125 Callback0::Type* callback) { | 132 const base::Closure& callback) { |
126 DCHECK(callback); | 133 DCHECK(!callback.is_null()); |
127 DCHECK(!user_callback_); | 134 DCHECK(user_callback_.is_null()); |
128 | 135 |
129 AddRef(); // Released on OnIOComplete(). | 136 AddRef(); // Released on OnIOComplete(). |
130 std::string cache_key = | 137 std::string cache_key = |
131 request.url().spec().substr(strlen(chrome::kChromeUINetworkViewCacheURL)); | 138 request.url().spec().substr(strlen(chrome::kChromeUINetworkViewCacheURL)); |
132 | 139 |
133 int rv; | 140 int rv; |
134 if (cache_key.empty()) { | 141 if (cache_key.empty()) { |
135 rv = cache_helper_.GetContentsHTML(request.context(), | 142 rv = cache_helper_.GetContentsHTML(request.context(), |
136 chrome::kChromeUINetworkViewCacheURL, | 143 chrome::kChromeUINetworkViewCacheURL, |
137 &data_, | 144 &data_, callback_); |
138 &callback_); | |
139 } else { | 145 } else { |
140 rv = cache_helper_.GetEntryInfoHTML(cache_key, request.context(), | 146 rv = cache_helper_.GetEntryInfoHTML(cache_key, request.context(), |
141 &data_, &callback_); | 147 &data_, callback_); |
142 } | 148 } |
143 | 149 |
144 if (rv == net::ERR_IO_PENDING) | 150 if (rv == net::ERR_IO_PENDING) |
145 user_callback_ = callback; | 151 user_callback_ = callback; |
146 | 152 |
147 return rv; | 153 return rv; |
148 } | 154 } |
149 | 155 |
150 bool ViewHttpCacheJob::Core::GetMimeType(std::string* mime_type) const { | 156 bool ViewHttpCacheJob::Core::GetMimeType(std::string* mime_type) const { |
151 mime_type->assign("text/html"); | 157 mime_type->assign("text/html"); |
(...skipping 14 matching lines...) Expand all Loading... | |
166 buf_size = remaining; | 172 buf_size = remaining; |
167 memcpy(buf->data(), data_.data() + data_offset_, buf_size); | 173 memcpy(buf->data(), data_.data() + data_offset_, buf_size); |
168 data_offset_ += buf_size; | 174 data_offset_ += buf_size; |
169 *bytes_read = buf_size; | 175 *bytes_read = buf_size; |
170 return true; | 176 return true; |
171 } | 177 } |
172 | 178 |
173 void ViewHttpCacheJob::Core::OnIOComplete(int result) { | 179 void ViewHttpCacheJob::Core::OnIOComplete(int result) { |
174 DCHECK_EQ(net::OK, result); | 180 DCHECK_EQ(net::OK, result); |
175 | 181 |
176 if (user_callback_) | 182 if (!user_callback_.is_null()) |
177 user_callback_->Run(); | 183 user_callback_.Run(); |
178 | 184 |
179 // We may be holding the last reference to this job. Do not access |this| | 185 // We may be holding the last reference to this job. Do not access |this| |
180 // after Release(). | 186 // after Release(). |
181 Release(); // Acquired on Start(). | 187 Release(); // Acquired on Start(). |
182 } | 188 } |
183 | 189 |
184 } // namespace. | 190 } // namespace. |
185 | 191 |
186 // Static. | 192 // Static. |
187 bool ViewHttpCacheJobFactory::IsSupportedURL(const GURL& url) { | 193 bool ViewHttpCacheJobFactory::IsSupportedURL(const GURL& url) { |
188 return url.SchemeIs(chrome::kChromeUIScheme) && | 194 return url.SchemeIs(chrome::kChromeUIScheme) && |
189 url.host() == chrome::kChromeUINetworkViewCacheHost; | 195 url.host() == chrome::kChromeUINetworkViewCacheHost; |
190 } | 196 } |
191 | 197 |
192 // Static. | 198 // Static. |
193 net::URLRequestJob* ViewHttpCacheJobFactory::CreateJobForRequest( | 199 net::URLRequestJob* ViewHttpCacheJobFactory::CreateJobForRequest( |
194 net::URLRequest* request) { | 200 net::URLRequest* request) { |
195 return new ViewHttpCacheJob(request); | 201 return new ViewHttpCacheJob(request); |
196 } | 202 } |
OLD | NEW |