| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "content/browser/tracing/tracing_controller_impl.h" | 4 #include "content/browser/tracing/tracing_controller_impl.h" |
| 5 | 5 |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
| 9 #include "third_party/zlib/zlib.h" | 9 #include "third_party/zlib/zlib.h" |
| 10 | 10 |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 void Close() override { | 192 void Close() override { |
| 193 BrowserThread::PostTask( | 193 BrowserThread::PostTask( |
| 194 BrowserThread::FILE, FROM_HERE, | 194 BrowserThread::FILE, FROM_HERE, |
| 195 base::Bind(&CompressedStringTraceDataSink::CloseOnFileThread, this)); | 195 base::Bind(&CompressedStringTraceDataSink::CloseOnFileThread, this)); |
| 196 } | 196 } |
| 197 | 197 |
| 198 private: | 198 private: |
| 199 ~CompressedStringTraceDataSink() override {} | 199 ~CompressedStringTraceDataSink() override {} |
| 200 | 200 |
| 201 bool OpenZStreamOnFileThread() { | 201 bool OpenZStreamOnFileThread() { |
| 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 202 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 203 if (stream_) | 203 if (stream_) |
| 204 return true; | 204 return true; |
| 205 | 205 |
| 206 if (already_tried_open_) | 206 if (already_tried_open_) |
| 207 return false; | 207 return false; |
| 208 | 208 |
| 209 already_tried_open_ = true; | 209 already_tried_open_ = true; |
| 210 stream_.reset(new z_stream); | 210 stream_.reset(new z_stream); |
| 211 *stream_ = {0}; | 211 *stream_ = {0}; |
| 212 stream_->zalloc = Z_NULL; | 212 stream_->zalloc = Z_NULL; |
| 213 stream_->zfree = Z_NULL; | 213 stream_->zfree = Z_NULL; |
| 214 stream_->opaque = Z_NULL; | 214 stream_->opaque = Z_NULL; |
| 215 | 215 |
| 216 int result = deflateInit2(stream_.get(), Z_DEFAULT_COMPRESSION, Z_DEFLATED, | 216 int result = deflateInit2(stream_.get(), Z_DEFAULT_COMPRESSION, Z_DEFLATED, |
| 217 // 16 is added to produce a gzip header + trailer. | 217 // 16 is added to produce a gzip header + trailer. |
| 218 MAX_WBITS + 16, | 218 MAX_WBITS + 16, |
| 219 8, // memLevel = 8 is default. | 219 8, // memLevel = 8 is default. |
| 220 Z_DEFAULT_STRATEGY); | 220 Z_DEFAULT_STRATEGY); |
| 221 return result == 0; | 221 return result == 0; |
| 222 } | 222 } |
| 223 | 223 |
| 224 void AddTraceChunkOnFileThread( | 224 void AddTraceChunkOnFileThread( |
| 225 const scoped_refptr<base::RefCountedString> chunk_ptr) { | 225 const scoped_refptr<base::RefCountedString> chunk_ptr) { |
| 226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 226 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 227 std::string trace; | 227 std::string trace; |
| 228 if (compressed_trace_data_.empty()) | 228 if (compressed_trace_data_.empty()) |
| 229 trace = "{\"traceEvents\":["; | 229 trace = "{\"traceEvents\":["; |
| 230 else | 230 else |
| 231 trace = ","; | 231 trace = ","; |
| 232 trace += chunk_ptr->data(); | 232 trace += chunk_ptr->data(); |
| 233 AddTraceChunkAndCompressOnFileThread(trace, false); | 233 AddTraceChunkAndCompressOnFileThread(trace, false); |
| 234 } | 234 } |
| 235 | 235 |
| 236 void AddTraceChunkAndCompressOnFileThread(const std::string& chunk, | 236 void AddTraceChunkAndCompressOnFileThread(const std::string& chunk, |
| 237 bool finished) { | 237 bool finished) { |
| 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 238 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 239 if (!OpenZStreamOnFileThread()) | 239 if (!OpenZStreamOnFileThread()) |
| 240 return; | 240 return; |
| 241 | 241 |
| 242 const int kChunkSize = 0x4000; | 242 const int kChunkSize = 0x4000; |
| 243 | 243 |
| 244 char buffer[kChunkSize]; | 244 char buffer[kChunkSize]; |
| 245 int err; | 245 int err; |
| 246 stream_->avail_in = chunk.size(); | 246 stream_->avail_in = chunk.size(); |
| 247 stream_->next_in = (unsigned char*)chunk.data(); | 247 stream_->next_in = (unsigned char*)chunk.data(); |
| 248 do { | 248 do { |
| 249 stream_->avail_out = kChunkSize; | 249 stream_->avail_out = kChunkSize; |
| 250 stream_->next_out = (unsigned char*)buffer; | 250 stream_->next_out = (unsigned char*)buffer; |
| 251 err = deflate(stream_.get(), finished ? Z_FINISH : Z_NO_FLUSH); | 251 err = deflate(stream_.get(), finished ? Z_FINISH : Z_NO_FLUSH); |
| 252 if (err != Z_OK && (err != Z_STREAM_END && finished)) { | 252 if (err != Z_OK && (err != Z_STREAM_END && finished)) { |
| 253 stream_.reset(); | 253 stream_.reset(); |
| 254 return; | 254 return; |
| 255 } | 255 } |
| 256 | 256 |
| 257 int bytes = kChunkSize - stream_->avail_out; | 257 int bytes = kChunkSize - stream_->avail_out; |
| 258 if (bytes) { | 258 if (bytes) { |
| 259 std::string compressed_chunk = std::string(buffer, bytes); | 259 std::string compressed_chunk = std::string(buffer, bytes); |
| 260 compressed_trace_data_ += compressed_chunk; | 260 compressed_trace_data_ += compressed_chunk; |
| 261 endpoint_->ReceiveTraceChunk(compressed_chunk); | 261 endpoint_->ReceiveTraceChunk(compressed_chunk); |
| 262 } | 262 } |
| 263 } while (stream_->avail_out == 0); | 263 } while (stream_->avail_out == 0); |
| 264 } | 264 } |
| 265 | 265 |
| 266 void CloseOnFileThread() { | 266 void CloseOnFileThread() { |
| 267 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 267 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 268 if (!OpenZStreamOnFileThread()) | 268 if (!OpenZStreamOnFileThread()) |
| 269 return; | 269 return; |
| 270 | 270 |
| 271 if (compressed_trace_data_.empty()) | 271 if (compressed_trace_data_.empty()) |
| 272 AddTraceChunkAndCompressOnFileThread("{\"traceEvents\":[", false); | 272 AddTraceChunkAndCompressOnFileThread("{\"traceEvents\":[", false); |
| 273 | 273 |
| 274 AddTraceChunkAndCompressOnFileThread("]", false); | 274 AddTraceChunkAndCompressOnFileThread("]", false); |
| 275 if (!system_trace_.empty()) { | 275 if (!system_trace_.empty()) { |
| 276 AddTraceChunkAndCompressOnFileThread( | 276 AddTraceChunkAndCompressOnFileThread( |
| 277 ",\"systemTraceEvents\": " + system_trace_, false); | 277 ",\"systemTraceEvents\": " + system_trace_, false); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 return new StringTraceDataEndpoint(callback); | 330 return new StringTraceDataEndpoint(callback); |
| 331 } | 331 } |
| 332 | 332 |
| 333 scoped_refptr<TracingController::TraceDataEndpoint> | 333 scoped_refptr<TracingController::TraceDataEndpoint> |
| 334 TracingController::CreateFileEndpoint(const base::FilePath& file_path, | 334 TracingController::CreateFileEndpoint(const base::FilePath& file_path, |
| 335 const base::Closure& callback) { | 335 const base::Closure& callback) { |
| 336 return new FileTraceDataEndpoint(file_path, callback); | 336 return new FileTraceDataEndpoint(file_path, callback); |
| 337 } | 337 } |
| 338 | 338 |
| 339 } // namespace content | 339 } // namespace content |
| OLD | NEW |