Chromium Code Reviews

Side by Side Diff: chrome/browser/net/chrome_net_log.cc

Issue 4118004: Update NetLog to be thread safe. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Requires threadsafe observers Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "chrome/browser/net/chrome_net_log.h" 5 #include "chrome/browser/net/chrome_net_log.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/lock.h"
10 #include "base/logging.h" 11 #include "base/logging.h"
11 #include "base/string_util.h" 12 #include "base/string_util.h"
12 #include "chrome/browser/browser_thread.h" 13 #include "base/values.h"
13 #include "chrome/browser/net/load_timing_observer.h" 14 #include "chrome/browser/net/load_timing_observer.h"
14 #include "chrome/browser/net/net_log_logger.h" 15 #include "chrome/browser/net/net_log_logger.h"
15 #include "chrome/browser/net/passive_log_collector.h" 16 #include "chrome/browser/net/passive_log_collector.h"
16 #include "chrome/common/chrome_switches.h" 17 #include "chrome/common/chrome_switches.h"
17 18
18 ChromeNetLog::Observer::Observer(LogLevel log_level) : log_level_(log_level) {} 19 ChromeNetLog::Observer::Observer(LogLevel log_level) : log_level_(log_level) {}
19 20
20 net::NetLog::LogLevel ChromeNetLog::Observer::log_level() const { 21 net::NetLog::LogLevel ChromeNetLog::Observer::log_level() const {
21 return log_level_; 22 return log_level_;
22 } 23 }
23 24
24 void ChromeNetLog::Observer::set_log_level(net::NetLog::LogLevel log_level) { 25 void ChromeNetLog::Observer::set_log_level(net::NetLog::LogLevel log_level,
26 ChromeNetLog* net_log) {
25 log_level_ = log_level; 27 log_level_ = log_level;
28 if (net_log) {
eroman 2010/11/16 17:51:14 Are there any existing cases where this would be N
mmenke 2010/11/16 21:34:49 Never happens at the moment. Change made (Descrip
29 AutoLock lock(net_log->lock_);
30 net_log->UpdateLogLevel_();
31 }
26 } 32 }
27 33
34 ChromeNetLog::Entry::Entry(uint32 order,
35 net::NetLog::EventType type,
36 const base::TimeTicks& time,
37 net::NetLog::Source source,
38 net::NetLog::EventPhase phase,
39 net::NetLog::EventParameters* params)
40 : order(order),
41 type(type),
42 time(time),
43 source(source),
44 phase(phase),
45 params(params) {
46 }
47
48 ChromeNetLog::Entry::~Entry() {}
49
28 ChromeNetLog::ChromeNetLog() 50 ChromeNetLog::ChromeNetLog()
29 : next_id_(1), 51 : last_id_(0),
30 passive_collector_(new PassiveLogCollector), 52 passive_collector_(new PassiveLogCollector),
31 load_timing_observer_(new LoadTimingObserver) { 53 load_timing_observer_(new LoadTimingObserver) {
32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
33 AddObserver(passive_collector_.get()); 54 AddObserver(passive_collector_.get());
34 AddObserver(load_timing_observer_.get()); 55 AddObserver(load_timing_observer_.get());
35 56
36 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 57 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
37 if (command_line.HasSwitch(switches::kLogNetLog)) { 58 if (command_line.HasSwitch(switches::kLogNetLog)) {
38 net_log_logger_.reset(new NetLogLogger()); 59 net_log_logger_.reset(new NetLogLogger());
39 AddObserver(net_log_logger_.get()); 60 AddObserver(net_log_logger_.get());
40 } 61 }
41 } 62 }
42 63
43 ChromeNetLog::~ChromeNetLog() { 64 ChromeNetLog::~ChromeNetLog() {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
45 RemoveObserver(passive_collector_.get()); 65 RemoveObserver(passive_collector_.get());
46 RemoveObserver(load_timing_observer_.get()); 66 RemoveObserver(load_timing_observer_.get());
47 if (net_log_logger_.get()) { 67 if (net_log_logger_.get()) {
48 RemoveObserver(net_log_logger_.get()); 68 RemoveObserver(net_log_logger_.get());
49 } 69 }
50 } 70 }
51 71
52 void ChromeNetLog::AddEntry(EventType type, 72 void ChromeNetLog::AddEntry(EventType type,
53 const base::TimeTicks& time, 73 const base::TimeTicks& time,
54 const Source& source, 74 const Source& source,
55 EventPhase phase, 75 EventPhase phase,
56 EventParameters* params) { 76 EventParameters* params) {
57 // This must be invoked when we're on the IO thread, or if the IO thread's 77 AutoLock lock(lock_);
58 // message loop isn't valid. The later can happen if this is invoked when the
59 // IOThread is shuting down the MessageLoop.
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO) ||
61 !BrowserThread::IsMessageLoopValid(BrowserThread::IO));
62 78
63 // Notify all of the log observers. 79 // Notify all of the log observers.
64 FOR_EACH_OBSERVER(Observer, observers_, 80 FOR_EACH_OBSERVER(Observer, observers_,
65 OnAddEntry(type, time, source, phase, params)); 81 OnAddEntry(type, time, source, phase, params));
66 } 82 }
67 83
68 uint32 ChromeNetLog::NextID() { 84 uint32 ChromeNetLog::NextID() {
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 85 return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1);
70 return next_id_++;
71 } 86 }
72 87
73 net::NetLog::LogLevel ChromeNetLog::GetLogLevel() const { 88 net::NetLog::LogLevel ChromeNetLog::GetLogLevel() const {
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 89 base::subtle::Atomic32 log_level = base::subtle::NoBarrier_Load(&log_level_);
90 return static_cast<net::NetLog::LogLevel>(log_level);
91 }
75 92
93 void ChromeNetLog::AddObserver(Observer* observer) {
94 AutoLock lock(lock_);
95 observers_.AddObserver(observer);
96 UpdateLogLevel_();
97 }
98
99 void ChromeNetLog::RemoveObserver(Observer* observer) {
100 AutoLock lock(lock_);
101 observers_.RemoveObserver(observer);
102 UpdateLogLevel_();
103 }
104
105 void ChromeNetLog::AddObserverAndGetAllCapturedEvents(
106 Observer* observer,
107 EntryList* passive_entries) {
108 AutoLock lock(lock_);
109 observers_.AddObserver(observer);
110 UpdateLogLevel_();
111 passive_collector_->GetAllCapturedEvents(passive_entries);
112 }
113
114 void ChromeNetLog::UpdateLogLevel_() {
eroman 2010/11/16 17:51:14 Can you call lock_.AssertAcquired() ?
mmenke 2010/11/16 21:34:49 Done.
76 // Look through all the observers and find the finest granularity 115 // Look through all the observers and find the finest granularity
77 // log level (higher values of the enum imply *lower* log levels). 116 // log level (higher values of the enum imply *lower* log levels).
78 LogLevel log_level = LOG_BASIC; 117 LogLevel new_log_level = LOG_BASIC;
79 ObserverListBase<Observer>::Iterator it(observers_); 118 ObserverListBase<Observer>::Iterator it(observers_);
80 Observer* observer; 119 Observer* observer;
81 while ((observer = it.GetNext()) != NULL) { 120 while ((observer = it.GetNext()) != NULL) {
82 log_level = std::min(log_level, observer->log_level()); 121 new_log_level = std::min(new_log_level, observer->log_level());
83 } 122 }
84 return log_level; 123 base::subtle::NoBarrier_Store(&log_level_, new_log_level);
85 } 124 }
86 125
87 void ChromeNetLog::AddObserver(Observer* observer) {
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
89 observers_.AddObserver(observer);
90 }
91
92 void ChromeNetLog::RemoveObserver(Observer* observer) {
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
94 observers_.RemoveObserver(observer);
95 }
OLDNEW

Powered by Google App Engine