summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/chrome_net_log.cc
blob: 212dc460172799debdc93ca40ea51eaef713e3ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/net/chrome_net_log.h"

#include <algorithm>

#include "base/command_line.h"
#include "base/logging.h"
#include "base/string_number_conversions.h"
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/browser/net/load_timing_observer.h"
#include "chrome/browser/net/net_log_logger.h"
#include "chrome/browser/net/passive_log_collector.h"
#include "chrome/common/chrome_switches.h"

ChromeNetLog::ThreadSafeObserverImpl::ThreadSafeObserverImpl(LogLevel log_level)
    : net_log_(NULL),
      ALLOW_THIS_IN_INITIALIZER_LIST(internal_observer_(this, log_level)) {
}

ChromeNetLog::ThreadSafeObserverImpl::~ThreadSafeObserverImpl() {
  DCHECK(!net_log_);
}

void ChromeNetLog::ThreadSafeObserverImpl::AddAsObserver(
    ChromeNetLog* net_log) {
  DCHECK(!net_log_);
  net_log_ = net_log;
  net_log_->AddThreadSafeObserver(&internal_observer_);
}

void ChromeNetLog::ThreadSafeObserverImpl::RemoveAsObserver() {
  DCHECK(net_log_);
  net_log_->RemoveThreadSafeObserver(&internal_observer_);
  net_log_ = NULL;
}

void ChromeNetLog::ThreadSafeObserverImpl::SetLogLevel(
    net::NetLog::LogLevel log_level) {
  DCHECK(net_log_);
  base::AutoLock lock(net_log_->lock_);
  internal_observer_.SetLogLevel(log_level);
  net_log_->UpdateLogLevel();
}

void ChromeNetLog::ThreadSafeObserverImpl::
AddAsObserverAndGetAllPassivelyCapturedEvents(
    ChromeNetLog* net_log,
    EntryList* entries) {
  DCHECK(!net_log_);
  net_log_ = net_log;
  net_log_->AddObserverAndGetAllPassivelyCapturedEvents(&internal_observer_,
                                                        entries);
}

void ChromeNetLog::ThreadSafeObserverImpl::AssertNetLogLockAcquired() const {
  if (net_log_)
    net_log_->lock_.AssertAcquired();
}

ChromeNetLog::ThreadSafeObserverImpl::PassThroughObserver::PassThroughObserver(
    ChromeNetLog::ThreadSafeObserverImpl* owner,
    net::NetLog::LogLevel log_level)
    : net::NetLog::ThreadSafeObserver(log_level),
      ALLOW_THIS_IN_INITIALIZER_LIST(owner_(owner)) {
}

void ChromeNetLog::ThreadSafeObserverImpl::PassThroughObserver::OnAddEntry(
    net::NetLog::EventType type,
    const base::TimeTicks& time,
    const net::NetLog::Source& source,
    net::NetLog::EventPhase phase,
    net::NetLog::EventParameters* params) {
  owner_->OnAddEntry(type, time, source, phase, params);
}

void ChromeNetLog::ThreadSafeObserverImpl::PassThroughObserver::SetLogLevel(
    net::NetLog::LogLevel log_level) {
  log_level_ = log_level;
}

ChromeNetLog::Entry::Entry(uint32 order,
                           net::NetLog::EventType type,
                           const base::TimeTicks& time,
                           net::NetLog::Source source,
                           net::NetLog::EventPhase phase,
                           net::NetLog::EventParameters* params)
    : order(order),
      type(type),
      time(time),
      source(source),
      phase(phase),
      params(params) {
}

ChromeNetLog::Entry::~Entry() {}

ChromeNetLog::ChromeNetLog()
    : last_id_(0),
      base_log_level_(LOG_BASIC),
      effective_log_level_(LOG_BASIC),
      passive_collector_(new PassiveLogCollector),
      load_timing_observer_(new LoadTimingObserver) {
  const CommandLine* command_line = CommandLine::ForCurrentProcess();
  // Adjust base log level based on command line switch, if present.
  // This is done before adding any observers so the call to UpdateLogLevel when
  // an observers is added will set |effective_log_level_| correctly.
  if (command_line->HasSwitch(switches::kNetLogLevel)) {
    std::string log_level_string =
        command_line->GetSwitchValueASCII(switches::kNetLogLevel);
    int command_line_log_level;
    if (base::StringToInt(log_level_string, &command_line_log_level) &&
        command_line_log_level >= LOG_ALL &&
        command_line_log_level <= LOG_BASIC) {
      base_log_level_ = static_cast<LogLevel>(command_line_log_level);
    }
  }

  passive_collector_->AddAsObserver(this);
  load_timing_observer_->AddAsObserver(this);

  if (command_line->HasSwitch(switches::kLogNetLog)) {
    net_log_logger_.reset(new NetLogLogger(
        command_line->GetSwitchValuePath(switches::kLogNetLog)));
    net_log_logger_->AddAsObserver(this);
  }
}

ChromeNetLog::~ChromeNetLog() {
  passive_collector_->RemoveAsObserver();
  load_timing_observer_->RemoveAsObserver();
  if (net_log_logger_.get()) {
    net_log_logger_->RemoveAsObserver();
  }
}

void ChromeNetLog::AddEntry(EventType type,
                            const base::TimeTicks& time,
                            const Source& source,
                            EventPhase phase,
                            EventParameters* params) {
  base::AutoLock lock(lock_);

  // Notify all of the log observers.
  FOR_EACH_OBSERVER(ThreadSafeObserver, observers_,
                    OnAddEntry(type, time, source, phase, params));
}

uint32 ChromeNetLog::NextID() {
  return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1);
}

net::NetLog::LogLevel ChromeNetLog::GetLogLevel() const {
  base::subtle::Atomic32 log_level =
      base::subtle::NoBarrier_Load(&effective_log_level_);
  return static_cast<net::NetLog::LogLevel>(log_level);
}

void ChromeNetLog::AddThreadSafeObserver(
    net::NetLog::ThreadSafeObserver* observer) {
  base::AutoLock lock(lock_);
  AddObserverWhileLockHeld(observer);
}

void ChromeNetLog::RemoveThreadSafeObserver(
    net::NetLog::ThreadSafeObserver* observer) {
  base::AutoLock lock(lock_);
  observers_.RemoveObserver(observer);
  UpdateLogLevel();
}

void ChromeNetLog::AddObserverAndGetAllPassivelyCapturedEvents(
    net::NetLog::ThreadSafeObserver* observer, EntryList* passive_entries) {
  base::AutoLock lock(lock_);
  AddObserverWhileLockHeld(observer);
  passive_collector_->GetAllCapturedEvents(passive_entries);
}

void ChromeNetLog::GetAllPassivelyCapturedEvents(EntryList* passive_entries) {
  base::AutoLock lock(lock_);
  passive_collector_->GetAllCapturedEvents(passive_entries);
}

void ChromeNetLog::ClearAllPassivelyCapturedEvents() {
  base::AutoLock lock(lock_);
  passive_collector_->Clear();
}

void ChromeNetLog::UpdateLogLevel() {
  lock_.AssertAcquired();

  // Look through all the observers and find the finest granularity
  // log level (higher values of the enum imply *lower* log levels).
  LogLevel new_effective_log_level = base_log_level_;
  ObserverListBase<ThreadSafeObserver>::Iterator it(observers_);
  ThreadSafeObserver* observer;
  while ((observer = it.GetNext()) != NULL) {
    new_effective_log_level =
        std::min(new_effective_log_level, observer->log_level());
  }
  base::subtle::NoBarrier_Store(&effective_log_level_,
                                new_effective_log_level);
}

void ChromeNetLog::AddObserverWhileLockHeld(
    net::NetLog::ThreadSafeObserver* observer) {
  observers_.AddObserver(observer);
  UpdateLogLevel();
}