| 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<std::unique_ptr<base::ThreadChecker>>::Leaky |
| 28 NetLogObserver::io_thread_checker_; |
| 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_checker_.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_checker_.Get()->CalledOnValidThread()) |
| 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); | |
| 49 | |
| 50 bool is_begin = entry.phase() == net::NetLog::PHASE_BEGIN; | 53 bool is_begin = entry.phase() == net::NetLog::PHASE_BEGIN; |
| 51 bool is_end = entry.phase() == net::NetLog::PHASE_END; | 54 bool is_end = entry.phase() == net::NetLog::PHASE_END; |
| 52 | 55 |
| 53 if (entry.type() == net::NetLog::TYPE_URL_REQUEST_START_JOB) { | 56 if (entry.type() == net::NetLog::TYPE_URL_REQUEST_START_JOB) { |
| 54 if (is_begin) { | 57 if (is_begin) { |
| 55 if (request_to_info_.size() > kMaxNumEntries) { | 58 if (request_to_info_.size() > kMaxNumEntries) { |
| 56 LOG(WARNING) << "The raw headers observer url request count has grown " | 59 LOG(WARNING) << "The raw headers observer url request count has grown " |
| 57 "larger than expected, resetting"; | 60 "larger than expected, resetting"; |
| 58 request_to_info_.clear(); | 61 request_to_info_.clear(); |
| 59 } | 62 } |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 // SPDY request. | 149 // SPDY request. |
| 147 info->response_headers_text = ""; | 150 info->response_headers_text = ""; |
| 148 } | 151 } |
| 149 break; | 152 break; |
| 150 } | 153 } |
| 151 default: | 154 default: |
| 152 break; | 155 break; |
| 153 } | 156 } |
| 154 } | 157 } |
| 155 | 158 |
| 156 void NetLogObserver::Attach() { | 159 void NetLogObserver::Attach(net::NetLog* net_log) { |
| 157 DCHECK(!instance_); | 160 DCHECK(!instance_); |
| 158 net::NetLog* net_log = GetContentClient()->browser()->GetNetLog(); | 161 io_thread_checker_.Get().reset(new base::ThreadChecker()); |
| 159 if (net_log) { | 162 if (net_log) { |
| 160 instance_ = new NetLogObserver(); | 163 instance_ = new NetLogObserver(); |
| 161 net_log->DeprecatedAddObserver( | 164 net_log->DeprecatedAddObserver( |
| 162 instance_, net::NetLogCaptureMode::IncludeCookiesAndCredentials()); | 165 instance_, net::NetLogCaptureMode::IncludeCookiesAndCredentials()); |
| 163 } | 166 } |
| 164 } | 167 } |
| 165 | 168 |
| 166 void NetLogObserver::Detach() { | 169 void NetLogObserver::Detach() { |
| 167 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 170 DCHECK(io_thread_checker_.Get().get() && |
| 168 | 171 io_thread_checker_.Get()->CalledOnValidThread()); |
| 172 io_thread_checker_.Get().reset(); |
| 169 if (instance_) { | 173 if (instance_) { |
| 170 // Safest not to do this in the destructor to maintain thread safety across | 174 // Safest not to do this in the destructor to maintain thread safety across |
| 171 // refactorings. | 175 // refactorings. |
| 172 instance_->net_log()->DeprecatedRemoveObserver(instance_); | 176 instance_->net_log()->DeprecatedRemoveObserver(instance_); |
| 173 delete instance_; | 177 delete instance_; |
| 174 instance_ = NULL; | 178 instance_ = NULL; |
| 175 } | 179 } |
| 176 } | 180 } |
| 177 | 181 |
| 178 NetLogObserver* NetLogObserver::GetInstance() { | 182 NetLogObserver* NetLogObserver::GetInstance() { |
| 179 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 183 if (!io_thread_checker_.Get().get()) |
| 184 return nullptr; |
| 185 |
| 186 DCHECK(io_thread_checker_.Get()->CalledOnValidThread()); |
| 180 | 187 |
| 181 return instance_; | 188 return instance_; |
| 182 } | 189 } |
| 183 | 190 |
| 184 // static | 191 // static |
| 185 void NetLogObserver::PopulateResponseInfo(net::URLRequest* request, | 192 void NetLogObserver::PopulateResponseInfo(net::URLRequest* request, |
| 186 ResourceResponse* response) { | 193 ResourceResponse* response) { |
| 187 const ResourceRequestInfoImpl* request_info = | 194 const ResourceRequestInfoImpl* request_info = |
| 188 ResourceRequestInfoImpl::ForRequest(request); | 195 ResourceRequestInfoImpl::ForRequest(request); |
| 189 if (!request_info || !request_info->ShouldReportRawHeaders()) | 196 if (!request_info || !request_info->ShouldReportRawHeaders()) |
| 190 return; | 197 return; |
| 191 | 198 |
| 192 uint32_t source_id = request->net_log().source().id; | 199 uint32_t source_id = request->net_log().source().id; |
| 193 NetLogObserver* dev_tools_net_log_observer = NetLogObserver::GetInstance(); | 200 NetLogObserver* dev_tools_net_log_observer = NetLogObserver::GetInstance(); |
| 194 if (dev_tools_net_log_observer == NULL) | 201 if (dev_tools_net_log_observer == NULL) |
| 195 return; | 202 return; |
| 196 response->head.devtools_info = | 203 response->head.devtools_info = |
| 197 dev_tools_net_log_observer->GetResourceInfo(source_id); | 204 dev_tools_net_log_observer->GetResourceInfo(source_id); |
| 198 response->head.encoded_data_length = request->GetTotalReceivedBytes(); | 205 response->head.encoded_data_length = request->GetTotalReceivedBytes(); |
| 199 } | 206 } |
| 200 | 207 |
| 201 } // namespace content | 208 } // namespace content |
| OLD | NEW |