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

Side by Side Diff: content/browser/loader/netlog_observer.cc

Issue 2115823004: Avoid using BrowserThread and ContentBrowserClient in NetLogObserver. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix build error Created 4 years, 5 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
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) {
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
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(net::NetLog* net_log) {
157 DCHECK(!instance_); 163 DCHECK(!instance_);
158 net::NetLog* net_log = GetContentClient()->browser()->GetNetLog();
159 if (net_log) { 164 if (net_log) {
160 instance_ = new NetLogObserver(); 165 instance_ = new NetLogObserver();
161 net_log->DeprecatedAddObserver( 166 net_log->DeprecatedAddObserver(
162 instance_, net::NetLogCaptureMode::IncludeCookiesAndCredentials()); 167 instance_, net::NetLogCaptureMode::IncludeCookiesAndCredentials());
163 } 168 }
164 } 169 }
165 170
166 void NetLogObserver::Detach() { 171 void NetLogObserver::Detach() {
167 DCHECK_CURRENTLY_ON(BrowserThread::IO); 172 DCHECK(io_thread_task_runner_.Get().get() &&
168 173 io_thread_task_runner_.Get()->BelongsToCurrentThread());
169 if (instance_) { 174 if (instance_) {
170 // Safest not to do this in the destructor to maintain thread safety across 175 // Safest not to do this in the destructor to maintain thread safety across
171 // refactorings. 176 // refactorings.
172 instance_->net_log()->DeprecatedRemoveObserver(instance_); 177 instance_->net_log()->DeprecatedRemoveObserver(instance_);
173 delete instance_; 178 delete instance_;
174 instance_ = NULL; 179 instance_ = NULL;
175 } 180 }
176 } 181 }
177 182
178 NetLogObserver* NetLogObserver::GetInstance() { 183 NetLogObserver* NetLogObserver::GetInstance() {
179 DCHECK_CURRENTLY_ON(BrowserThread::IO); 184 DCHECK(io_thread_task_runner_.Get().get() &&
185 io_thread_task_runner_.Get()->BelongsToCurrentThread());
180 186
181 return instance_; 187 return instance_;
182 } 188 }
183 189
184 // static 190 // static
185 void NetLogObserver::PopulateResponseInfo(net::URLRequest* request, 191 void NetLogObserver::PopulateResponseInfo(net::URLRequest* request,
186 ResourceResponse* response) { 192 ResourceResponse* response) {
187 const ResourceRequestInfoImpl* request_info = 193 const ResourceRequestInfoImpl* request_info =
188 ResourceRequestInfoImpl::ForRequest(request); 194 ResourceRequestInfoImpl::ForRequest(request);
189 if (!request_info || !request_info->ShouldReportRawHeaders()) 195 if (!request_info || !request_info->ShouldReportRawHeaders())
190 return; 196 return;
191 197
192 uint32_t source_id = request->net_log().source().id; 198 uint32_t source_id = request->net_log().source().id;
193 NetLogObserver* dev_tools_net_log_observer = NetLogObserver::GetInstance(); 199 NetLogObserver* dev_tools_net_log_observer = NetLogObserver::GetInstance();
194 if (dev_tools_net_log_observer == NULL) 200 if (dev_tools_net_log_observer == NULL)
195 return; 201 return;
196 response->head.devtools_info = 202 response->head.devtools_info =
197 dev_tools_net_log_observer->GetResourceInfo(source_id); 203 dev_tools_net_log_observer->GetResourceInfo(source_id);
198 response->head.encoded_data_length = request->GetTotalReceivedBytes(); 204 response->head.encoded_data_length = request->GetTotalReceivedBytes();
199 } 205 }
200 206
207 // static
208 void NetLogObserver::SetIOThreadTaskRunner(
209 scoped_refptr<base::SingleThreadTaskRunner> io_thread_runner) {
210 io_thread_task_runner_.Get() = io_thread_runner;
pfeldman 2016/07/06 18:36:14 Could you instantiate a thread checker in the ::At
ananta 2016/07/08 14:11:02 Done.
211 }
212
201 } // namespace content 213 } // namespace content
OLDNEW
« content/browser/devtools/devtools_manager.cc ('K') | « content/browser/loader/netlog_observer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698