Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(311)

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: Response to comments Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | 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::SetLogLevel(net::NetLog::LogLevel log_level,
26 ChromeNetLog* net_log) {
27 DCHECK(net_log);
28 AutoLock lock(net_log->lock_);
25 log_level_ = log_level; 29 log_level_ = log_level;
30 net_log->UpdateLogLevel_();
26 } 31 }
27 32
33 ChromeNetLog::Entry::Entry(uint32 order,
34 net::NetLog::EventType type,
35 const base::TimeTicks& time,
36 net::NetLog::Source source,
37 net::NetLog::EventPhase phase,
38 net::NetLog::EventParameters* params)
39 : order(order),
40 type(type),
41 time(time),
42 source(source),
43 phase(phase),
44 params(params) {
45 }
46
47 ChromeNetLog::Entry::~Entry() {}
48
28 ChromeNetLog::ChromeNetLog() 49 ChromeNetLog::ChromeNetLog()
29 : next_id_(1), 50 : last_id_(0),
eroman 2010/11/17 05:59:02 Where is log_level_ initialized? (it may still be
mmenke 2010/11/17 21:42:14 It's actually initialized on the first call to Add
30 passive_collector_(new PassiveLogCollector), 51 passive_collector_(new PassiveLogCollector),
31 load_timing_observer_(new LoadTimingObserver) { 52 load_timing_observer_(new LoadTimingObserver) {
32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
33 AddObserver(passive_collector_.get()); 53 AddObserver(passive_collector_.get());
34 AddObserver(load_timing_observer_.get()); 54 AddObserver(load_timing_observer_.get());
35 55
36 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 56 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
eroman 2010/11/17 05:59:02 Just to check -- Since ChromeNetLog is now being c
mmenke 2010/11/17 21:42:14 Yes, it is. It's initialized really early in the
37 if (command_line.HasSwitch(switches::kLogNetLog)) { 57 if (command_line.HasSwitch(switches::kLogNetLog)) {
38 net_log_logger_.reset(new NetLogLogger()); 58 net_log_logger_.reset(new NetLogLogger());
39 AddObserver(net_log_logger_.get()); 59 AddObserver(net_log_logger_.get());
40 } 60 }
41 } 61 }
42 62
43 ChromeNetLog::~ChromeNetLog() { 63 ChromeNetLog::~ChromeNetLog() {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
45 RemoveObserver(passive_collector_.get()); 64 RemoveObserver(passive_collector_.get());
46 RemoveObserver(load_timing_observer_.get()); 65 RemoveObserver(load_timing_observer_.get());
47 if (net_log_logger_.get()) { 66 if (net_log_logger_.get()) {
48 RemoveObserver(net_log_logger_.get()); 67 RemoveObserver(net_log_logger_.get());
49 } 68 }
50 } 69 }
51 70
52 void ChromeNetLog::AddEntry(EventType type, 71 void ChromeNetLog::AddEntry(EventType type,
53 const base::TimeTicks& time, 72 const base::TimeTicks& time,
54 const Source& source, 73 const Source& source,
55 EventPhase phase, 74 EventPhase phase,
56 EventParameters* params) { 75 EventParameters* params) {
57 // This must be invoked when we're on the IO thread, or if the IO thread's 76 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 77
63 // Notify all of the log observers. 78 // Notify all of the log observers.
64 FOR_EACH_OBSERVER(Observer, observers_, 79 FOR_EACH_OBSERVER(Observer, observers_,
65 OnAddEntry(type, time, source, phase, params)); 80 OnAddEntry(type, time, source, phase, params));
66 } 81 }
67 82
68 uint32 ChromeNetLog::NextID() { 83 uint32 ChromeNetLog::NextID() {
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 84 return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1);
70 return next_id_++;
71 } 85 }
72 86
73 net::NetLog::LogLevel ChromeNetLog::GetLogLevel() const { 87 net::NetLog::LogLevel ChromeNetLog::GetLogLevel() const {
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 88 base::subtle::Atomic32 log_level = base::subtle::NoBarrier_Load(&log_level_);
eroman 2010/11/17 05:59:02 I need to buff up on my concurrency... what is the
mmenke 2010/11/17 21:42:14 I'm pretty weak here, too, but the barrier will ma
89 return static_cast<net::NetLog::LogLevel>(log_level);
90 }
91
92 void ChromeNetLog::AddObserver(Observer* observer) {
93 AutoLock lock(lock_);
94 observers_.AddObserver(observer);
95 UpdateLogLevel_();
96 }
97
98 void ChromeNetLog::RemoveObserver(Observer* observer) {
99 AutoLock lock(lock_);
100 observers_.RemoveObserver(observer);
101 UpdateLogLevel_();
102 }
103
104 void ChromeNetLog::AddObserverAndGetAllCapturedEvents(
105 Observer* observer,
106 EntryList* passive_entries) {
107 AutoLock lock(lock_);
108 observers_.AddObserver(observer);
109 UpdateLogLevel_();
110 passive_collector_->GetAllCapturedEvents(passive_entries);
111 }
112
113 void ChromeNetLog::UpdateLogLevel_() {
114 lock_.AssertAcquired();
75 115
76 // Look through all the observers and find the finest granularity 116 // Look through all the observers and find the finest granularity
77 // log level (higher values of the enum imply *lower* log levels). 117 // log level (higher values of the enum imply *lower* log levels).
78 LogLevel log_level = LOG_BASIC; 118 LogLevel new_log_level = LOG_BASIC;
79 ObserverListBase<Observer>::Iterator it(observers_); 119 ObserverListBase<Observer>::Iterator it(observers_);
80 Observer* observer; 120 Observer* observer;
81 while ((observer = it.GetNext()) != NULL) { 121 while ((observer = it.GetNext()) != NULL) {
82 log_level = std::min(log_level, observer->log_level()); 122 new_log_level = std::min(new_log_level, observer->log_level());
83 } 123 }
84 return log_level; 124 base::subtle::NoBarrier_Store(&log_level_, new_log_level);
85 } 125 }
86 126
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
This is Rietveld 408576698