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
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_DEBUGGER_DEVTOOLS_NETLOG_OBSERVER_H_
6#define CHROME_BROWSER_DEBUGGER_DEVTOOLS_NETLOG_OBSERVER_H_
7#pragma once
8
9#include "base/hash_tables.h"
10#include "base/memory/ref_counted.h"
11#include "chrome/browser/net/chrome_net_log.h"
12#include "webkit/glue/resource_loader_bridge.h"
13
14namespace net {
15class URLRequest;
16}  // namespace net
17
18class IOThread;
19struct ResourceResponse;
20
21// DevToolsNetLogObserver watches the NetLog event stream and collects the
22// stuff that may be of interest to DevTools. Currently, this only includes
23// actual HTTP/SPDY headers sent and received over the network.
24//
25// As DevToolsNetLogObserver shares live data with objects that live on the
26// IO Thread, it must also reside on the IO Thread.  Only OnAddEntry can be
27// called from other threads.
28class DevToolsNetLogObserver: public ChromeNetLog::ThreadSafeObserver {
29  typedef webkit_glue::ResourceDevToolsInfo ResourceInfo;
30
31 public:
32  // ThreadSafeObserver implementation:
33  virtual void OnAddEntry(net::NetLog::EventType type,
34                          const base::TimeTicks& time,
35                          const net::NetLog::Source& source,
36                          net::NetLog::EventPhase phase,
37                          net::NetLog::EventParameters* params);
38
39  void OnAddURLRequestEntry(net::NetLog::EventType type,
40                            const base::TimeTicks& time,
41                            const net::NetLog::Source& source,
42                            net::NetLog::EventPhase phase,
43                            net::NetLog::EventParameters* params);
44
45  void OnAddHTTPStreamJobEntry(net::NetLog::EventType type,
46                               const base::TimeTicks& time,
47                               const net::NetLog::Source& source,
48                               net::NetLog::EventPhase phase,
49                               net::NetLog::EventParameters* params);
50
51  void OnAddSocketEntry(net::NetLog::EventType type,
52                        const base::TimeTicks& time,
53                        const net::NetLog::Source& source,
54                        net::NetLog::EventPhase phase,
55                        net::NetLog::EventParameters* params);
56
57  static void Attach(IOThread* thread);
58  static void Detach();
59
60  // Must be called on the IO thread. May return NULL if no observers
61  // are active.
62  static DevToolsNetLogObserver* GetInstance();
63  static void PopulateResponseInfo(net::URLRequest*, ResourceResponse*);
64  static int GetAndResetEncodedDataLength(net::URLRequest* request);
65
66 private:
67  static DevToolsNetLogObserver* instance_;
68
69  explicit DevToolsNetLogObserver(ChromeNetLog* chrome_net_log);
70  ~DevToolsNetLogObserver();
71
72  ResourceInfo* GetResourceInfo(uint32 id);
73
74  ChromeNetLog* chrome_net_log_;
75  typedef base::hash_map<uint32, scoped_refptr<ResourceInfo> > RequestToInfoMap;
76  typedef base::hash_map<uint32, int> RequestToEncodedDataLengthMap;
77  typedef base::hash_map<uint32, uint32> HTTPStreamJobToSocketMap;
78  typedef base::hash_map<uint32, uint32> SocketToRequestMap;
79  RequestToInfoMap request_to_info_;
80  RequestToEncodedDataLengthMap request_to_encoded_data_length_;
81  HTTPStreamJobToSocketMap http_stream_job_to_socket_;
82  SocketToRequestMap socket_to_request_;
83
84  DISALLOW_COPY_AND_ASSIGN(DevToolsNetLogObserver);
85};
86
87#endif  // CHROME_BROWSER_DEBUGGER_DEVTOOLS_NETLOG_OBSERVER_H_
88