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

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: Sync with trunk Created 10 years 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
« no previous file with comments | « chrome/browser/net/chrome_net_log.h ('k') | chrome/browser/net/chrome_net_log_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
eroman 2010/11/30 19:20:20 nit: include lock.h from the header file instead (
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::ThreadSafeObserver::ThreadSafeObserver(LogLevel log_level)
20 : net_log_(NULL),
21 log_level_(log_level) {
22 }
19 23
20 net::NetLog::LogLevel ChromeNetLog::Observer::log_level() const { 24 ChromeNetLog::ThreadSafeObserver::~ThreadSafeObserver() {
25 DCHECK(!net_log_);
26 }
27
28 net::NetLog::LogLevel ChromeNetLog::ThreadSafeObserver::log_level() const {
21 return log_level_; 29 return log_level_;
22 } 30 }
23 31
24 void ChromeNetLog::Observer::set_log_level(net::NetLog::LogLevel log_level) { 32 void ChromeNetLog::ThreadSafeObserver::AssertNetLogLockAcquired() const {
25 log_level_ = log_level; 33 if (net_log_)
34 net_log_->lock_.AssertAcquired();
26 } 35 }
27 36
37 void ChromeNetLog::ThreadSafeObserver::SetLogLevel(
38 net::NetLog::LogLevel log_level) {
39 DCHECK(net_log_);
40 AutoLock lock(net_log_->lock_);
41 log_level_ = log_level;
42 net_log_->UpdateLogLevel_();
43 }
44
45 ChromeNetLog::Entry::Entry(uint32 order,
46 net::NetLog::EventType type,
47 const base::TimeTicks& time,
48 net::NetLog::Source source,
49 net::NetLog::EventPhase phase,
50 net::NetLog::EventParameters* params)
51 : order(order),
52 type(type),
53 time(time),
54 source(source),
55 phase(phase),
56 params(params) {
57 }
58
59 ChromeNetLog::Entry::~Entry() {}
60
28 ChromeNetLog::ChromeNetLog() 61 ChromeNetLog::ChromeNetLog()
29 : next_id_(1), 62 : last_id_(0),
63 log_level_(LOG_BASIC),
30 passive_collector_(new PassiveLogCollector), 64 passive_collector_(new PassiveLogCollector),
31 load_timing_observer_(new LoadTimingObserver) { 65 load_timing_observer_(new LoadTimingObserver) {
32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
33 AddObserver(passive_collector_.get()); 66 AddObserver(passive_collector_.get());
34 AddObserver(load_timing_observer_.get()); 67 AddObserver(load_timing_observer_.get());
35 68
36 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 69 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
37 if (command_line.HasSwitch(switches::kLogNetLog)) { 70 if (command_line.HasSwitch(switches::kLogNetLog)) {
38 net_log_logger_.reset(new NetLogLogger()); 71 net_log_logger_.reset(new NetLogLogger());
39 AddObserver(net_log_logger_.get()); 72 AddObserver(net_log_logger_.get());
40 } 73 }
41 } 74 }
42 75
43 ChromeNetLog::~ChromeNetLog() { 76 ChromeNetLog::~ChromeNetLog() {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
45 RemoveObserver(passive_collector_.get()); 77 RemoveObserver(passive_collector_.get());
46 RemoveObserver(load_timing_observer_.get()); 78 RemoveObserver(load_timing_observer_.get());
47 if (net_log_logger_.get()) { 79 if (net_log_logger_.get()) {
48 RemoveObserver(net_log_logger_.get()); 80 RemoveObserver(net_log_logger_.get());
49 } 81 }
50 } 82 }
51 83
52 void ChromeNetLog::AddEntry(EventType type, 84 void ChromeNetLog::AddEntry(EventType type,
53 const base::TimeTicks& time, 85 const base::TimeTicks& time,
54 const Source& source, 86 const Source& source,
55 EventPhase phase, 87 EventPhase phase,
56 EventParameters* params) { 88 EventParameters* params) {
57 // This must be invoked when we're on the IO thread, or if the IO thread's 89 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 90
63 // Notify all of the log observers. 91 // Notify all of the log observers.
64 FOR_EACH_OBSERVER(Observer, observers_, 92 FOR_EACH_OBSERVER(ThreadSafeObserver, observers_,
65 OnAddEntry(type, time, source, phase, params)); 93 OnAddEntry(type, time, source, phase, params));
66 } 94 }
67 95
68 uint32 ChromeNetLog::NextID() { 96 uint32 ChromeNetLog::NextID() {
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 97 return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1);
70 return next_id_++;
71 } 98 }
72 99
73 net::NetLog::LogLevel ChromeNetLog::GetLogLevel() const { 100 net::NetLog::LogLevel ChromeNetLog::GetLogLevel() const {
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 101 base::subtle::Atomic32 log_level = base::subtle::NoBarrier_Load(&log_level_);
102 return static_cast<net::NetLog::LogLevel>(log_level);
103 }
104
105 void ChromeNetLog::AddObserver(ThreadSafeObserver* observer) {
106 AutoLock lock(lock_);
107 AddObserverWhileLockHeld(observer);
108 }
109
110 void ChromeNetLog::RemoveObserver(ThreadSafeObserver* observer) {
111 AutoLock lock(lock_);
112 DCHECK_EQ(observer->net_log_, this);
113 observer->net_log_ = NULL;
114 observers_.RemoveObserver(observer);
115 UpdateLogLevel_();
116 }
117
118 void ChromeNetLog::AddObserverAndGetAllPassivelyCapturedEvents(
119 ThreadSafeObserver* observer, EntryList* passive_entries) {
120 AutoLock lock(lock_);
121 AddObserverWhileLockHeld(observer);
122 passive_collector_->GetAllCapturedEvents(passive_entries);
123 }
124
125 void ChromeNetLog::GetAllPassivelyCapturedEvents(EntryList* passive_entries) {
126 AutoLock lock(lock_);
127 passive_collector_->GetAllCapturedEvents(passive_entries);
128 }
129
130 void ChromeNetLog::ClearAllPassivelyCapturedEvents() {
131 AutoLock lock(lock_);
132 passive_collector_->Clear();
133 }
134
135 void ChromeNetLog::UpdateLogLevel_() {
136 lock_.AssertAcquired();
75 137
76 // Look through all the observers and find the finest granularity 138 // Look through all the observers and find the finest granularity
77 // log level (higher values of the enum imply *lower* log levels). 139 // log level (higher values of the enum imply *lower* log levels).
78 LogLevel log_level = LOG_BASIC; 140 LogLevel new_log_level = LOG_BASIC;
79 ObserverListBase<Observer>::Iterator it(observers_); 141 ObserverListBase<ThreadSafeObserver>::Iterator it(observers_);
80 Observer* observer; 142 ThreadSafeObserver* observer;
81 while ((observer = it.GetNext()) != NULL) { 143 while ((observer = it.GetNext()) != NULL) {
82 log_level = std::min(log_level, observer->log_level()); 144 new_log_level = std::min(new_log_level, observer->log_level());
83 } 145 }
84 return log_level; 146 base::subtle::NoBarrier_Store(&log_level_, new_log_level);
85 } 147 }
86 148
87 void ChromeNetLog::AddObserver(Observer* observer) { 149 void ChromeNetLog::AddObserverWhileLockHeld(ThreadSafeObserver* observer) {
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 150 DCHECK(!observer->net_log_);
151 observer->net_log_ = this;
89 observers_.AddObserver(observer); 152 observers_.AddObserver(observer);
153 UpdateLogLevel_();
90 } 154 }
91
92 void ChromeNetLog::RemoveObserver(Observer* observer) {
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
94 observers_.RemoveObserver(observer);
95 }
OLDNEW
« no previous file with comments | « chrome/browser/net/chrome_net_log.h ('k') | chrome/browser/net/chrome_net_log_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698