OLD | NEW |
---|---|
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 "content/browser/loader/netlog_observer.h" | 5 #include "content/browser/loader/netlog_observer.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "content/browser/loader/resource_request_info_impl.h" | 11 #include "content/browser/loader/resource_request_info_impl.h" |
12 #include "content/public/browser/browser_thread.h" | |
13 #include "content/public/browser/content_browser_client.h" | |
14 #include "content/public/common/resource_response.h" | 12 #include "content/public/common/resource_response.h" |
15 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
16 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
17 #include "net/http/http_util.h" | 15 #include "net/http/http_util.h" |
18 #include "net/spdy/spdy_header_block.h" | 16 #include "net/spdy/spdy_header_block.h" |
19 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
20 #include "net/url_request/url_request_netlog_params.h" | 18 #include "net/url_request/url_request_netlog_params.h" |
21 | 19 |
22 namespace content { | 20 namespace content { |
23 const size_t kMaxNumEntries = 1000; | 21 const size_t kMaxNumEntries = 1000; |
24 | 22 |
23 // static | |
25 NetLogObserver* NetLogObserver::instance_ = NULL; | 24 NetLogObserver* NetLogObserver::instance_ = NULL; |
26 | 25 |
26 // static | |
27 base::LazyInstance<scoped_refptr<base::SingleThreadTaskRunner>>::Leaky | |
28 NetLogObserver::io_thread_task_runner_; | |
29 | |
27 NetLogObserver::NetLogObserver() {} | 30 NetLogObserver::NetLogObserver() {} |
28 | 31 |
29 NetLogObserver::~NetLogObserver() {} | 32 NetLogObserver::~NetLogObserver() {} |
30 | 33 |
31 NetLogObserver::ResourceInfo* NetLogObserver::GetResourceInfo(uint32_t id) { | 34 NetLogObserver::ResourceInfo* NetLogObserver::GetResourceInfo(uint32_t id) { |
32 RequestToInfoMap::iterator it = request_to_info_.find(id); | 35 RequestToInfoMap::iterator it = request_to_info_.find(id); |
33 if (it != request_to_info_.end()) | 36 if (it != request_to_info_.end()) |
34 return it->second.get(); | 37 return it->second.get(); |
35 return NULL; | 38 return NULL; |
36 } | 39 } |
37 | 40 |
38 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { | 41 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { |
42 DCHECK(io_thread_task_runner_.Get().get()); | |
43 | |
39 // The events that the Observer is interested in only occur on the IO thread. | 44 // The events that the Observer is interested in only occur on the IO thread. |
40 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) | 45 if (!io_thread_task_runner_.Get()->BelongsToCurrentThread()) |
41 return; | 46 return; |
42 | 47 |
43 if (entry.source().type == net::NetLog::SOURCE_URL_REQUEST) | 48 if (entry.source().type == net::NetLog::SOURCE_URL_REQUEST) |
44 OnAddURLRequestEntry(entry); | 49 OnAddURLRequestEntry(entry); |
45 } | 50 } |
46 | 51 |
47 void NetLogObserver::OnAddURLRequestEntry(const net::NetLog::Entry& entry) { | 52 void NetLogObserver::OnAddURLRequestEntry(const net::NetLog::Entry& entry) { |
pfeldman
2016/07/13 17:17:49
nit: seems like this method could be private, then
ananta
2016/07/13 18:12:26
Done.
| |
48 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 53 DCHECK(io_thread_task_runner_.Get().get() && |
54 io_thread_task_runner_.Get()->BelongsToCurrentThread()); | |
49 | 55 |
50 bool is_begin = entry.phase() == net::NetLog::PHASE_BEGIN; | 56 bool is_begin = entry.phase() == net::NetLog::PHASE_BEGIN; |
51 bool is_end = entry.phase() == net::NetLog::PHASE_END; | 57 bool is_end = entry.phase() == net::NetLog::PHASE_END; |
52 | 58 |
53 if (entry.type() == net::NetLog::TYPE_URL_REQUEST_START_JOB) { | 59 if (entry.type() == net::NetLog::TYPE_URL_REQUEST_START_JOB) { |
54 if (is_begin) { | 60 if (is_begin) { |
55 if (request_to_info_.size() > kMaxNumEntries) { | 61 if (request_to_info_.size() > kMaxNumEntries) { |
56 LOG(WARNING) << "The raw headers observer url request count has grown " | 62 LOG(WARNING) << "The raw headers observer url request count has grown " |
57 "larger than expected, resetting"; | 63 "larger than expected, resetting"; |
58 request_to_info_.clear(); | 64 request_to_info_.clear(); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 // SPDY request. | 152 // SPDY request. |
147 info->response_headers_text = ""; | 153 info->response_headers_text = ""; |
148 } | 154 } |
149 break; | 155 break; |
150 } | 156 } |
151 default: | 157 default: |
152 break; | 158 break; |
153 } | 159 } |
154 } | 160 } |
155 | 161 |
156 void NetLogObserver::Attach() { | 162 void NetLogObserver::Attach( |
163 net::NetLog* net_log, | |
164 scoped_refptr<base::SingleThreadTaskRunner> io_thread_runner) { | |
157 DCHECK(!instance_); | 165 DCHECK(!instance_); |
158 net::NetLog* net_log = GetContentClient()->browser()->GetNetLog(); | 166 io_thread_task_runner_.Get() = io_thread_runner; |
159 if (net_log) { | 167 if (net_log) { |
160 instance_ = new NetLogObserver(); | 168 instance_ = new NetLogObserver(); |
161 net_log->DeprecatedAddObserver( | 169 net_log->DeprecatedAddObserver( |
162 instance_, net::NetLogCaptureMode::IncludeCookiesAndCredentials()); | 170 instance_, net::NetLogCaptureMode::IncludeCookiesAndCredentials()); |
163 } | 171 } |
164 } | 172 } |
165 | 173 |
166 void NetLogObserver::Detach() { | 174 void NetLogObserver::Detach() { |
167 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 175 DCHECK(io_thread_task_runner_.Get().get() && |
168 | 176 io_thread_task_runner_.Get()->BelongsToCurrentThread()); |
pfeldman
2016/07/13 17:17:49
reset io_thread_task_runner_ (or checker) for symm
ananta
2016/07/13 18:12:26
Done.
| |
169 if (instance_) { | 177 if (instance_) { |
170 // Safest not to do this in the destructor to maintain thread safety across | 178 // Safest not to do this in the destructor to maintain thread safety across |
171 // refactorings. | 179 // refactorings. |
172 instance_->net_log()->DeprecatedRemoveObserver(instance_); | 180 instance_->net_log()->DeprecatedRemoveObserver(instance_); |
173 delete instance_; | 181 delete instance_; |
174 instance_ = NULL; | 182 instance_ = NULL; |
175 } | 183 } |
176 } | 184 } |
177 | 185 |
178 NetLogObserver* NetLogObserver::GetInstance() { | 186 NetLogObserver* NetLogObserver::GetInstance() { |
179 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 187 DCHECK(io_thread_task_runner_.Get().get() && |
pfeldman
2016/07/13 17:17:49
This logic has changed - we used to return nullptr
ananta
2016/07/13 18:12:26
Please clarify. The only change in this patch is t
| |
188 io_thread_task_runner_.Get()->BelongsToCurrentThread()); | |
180 | 189 |
181 return instance_; | 190 return instance_; |
182 } | 191 } |
183 | 192 |
184 // static | 193 // static |
185 void NetLogObserver::PopulateResponseInfo(net::URLRequest* request, | 194 void NetLogObserver::PopulateResponseInfo(net::URLRequest* request, |
186 ResourceResponse* response) { | 195 ResourceResponse* response) { |
187 const ResourceRequestInfoImpl* request_info = | 196 const ResourceRequestInfoImpl* request_info = |
188 ResourceRequestInfoImpl::ForRequest(request); | 197 ResourceRequestInfoImpl::ForRequest(request); |
189 if (!request_info || !request_info->ShouldReportRawHeaders()) | 198 if (!request_info || !request_info->ShouldReportRawHeaders()) |
190 return; | 199 return; |
191 | 200 |
192 uint32_t source_id = request->net_log().source().id; | 201 uint32_t source_id = request->net_log().source().id; |
193 NetLogObserver* dev_tools_net_log_observer = NetLogObserver::GetInstance(); | 202 NetLogObserver* dev_tools_net_log_observer = NetLogObserver::GetInstance(); |
194 if (dev_tools_net_log_observer == NULL) | 203 if (dev_tools_net_log_observer == NULL) |
195 return; | 204 return; |
196 response->head.devtools_info = | 205 response->head.devtools_info = |
197 dev_tools_net_log_observer->GetResourceInfo(source_id); | 206 dev_tools_net_log_observer->GetResourceInfo(source_id); |
198 response->head.encoded_data_length = request->GetTotalReceivedBytes(); | 207 response->head.encoded_data_length = request->GetTotalReceivedBytes(); |
199 } | 208 } |
200 | 209 |
201 } // namespace content | 210 } // namespace content |
OLD | NEW |