diff options
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/net/chrome_net_log.cc | 69 | ||||
-rw-r--r-- | chrome/browser/net/chrome_net_log.h | 32 | ||||
-rw-r--r-- | chrome/browser/net/chrome_net_log_unittest.cc | 271 |
3 files changed, 2 insertions, 370 deletions
diff --git a/chrome/browser/net/chrome_net_log.cc b/chrome/browser/net/chrome_net_log.cc index b1594dc..7e9a2fc 100644 --- a/chrome/browser/net/chrome_net_log.cc +++ b/chrome/browser/net/chrome_net_log.cc @@ -16,10 +16,7 @@ #include "chrome/common/chrome_switches.h" ChromeNetLog::ChromeNetLog() - : last_id_(0), - base_log_level_(LOG_NONE), - effective_log_level_(LOG_NONE), - net_log_temp_file_(new NetLogTempFile(this)) { + : net_log_temp_file_(new NetLogTempFile(this)) { 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 @@ -31,7 +28,7 @@ ChromeNetLog::ChromeNetLog() if (base::StringToInt(log_level_string, &command_line_log_level) && command_line_log_level >= LOG_ALL && command_line_log_level <= LOG_NONE) { - base_log_level_ = static_cast<LogLevel>(command_line_log_level); + SetBaseLogLevel(static_cast<LogLevel>(command_line_log_level)); } } @@ -68,65 +65,3 @@ ChromeNetLog::~ChromeNetLog() { RemoveThreadSafeObserver(net_log_logger_.get()); } -void ChromeNetLog::OnAddEntry(const net::NetLog::Entry& entry) { - base::AutoLock lock(lock_); - - // Notify all of the log observers. - FOR_EACH_OBSERVER(ThreadSafeObserver, observers_, OnAddEntry(entry)); -} - -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, - LogLevel log_level) { - base::AutoLock lock(lock_); - - observers_.AddObserver(observer); - OnAddObserver(observer, log_level); - UpdateLogLevel(); -} - -void ChromeNetLog::SetObserverLogLevel( - net::NetLog::ThreadSafeObserver* observer, - LogLevel log_level) { - base::AutoLock lock(lock_); - - DCHECK(observers_.HasObserver(observer)); - OnSetObserverLogLevel(observer, log_level); - UpdateLogLevel(); -} - -void ChromeNetLog::RemoveThreadSafeObserver( - net::NetLog::ThreadSafeObserver* observer) { - base::AutoLock lock(lock_); - - DCHECK(observers_.HasObserver(observer)); - observers_.RemoveObserver(observer); - OnRemoveObserver(observer); - UpdateLogLevel(); -} - -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); -} diff --git a/chrome/browser/net/chrome_net_log.h b/chrome/browser/net/chrome_net_log.h index 9bbf152..10e45fb 100644 --- a/chrome/browser/net/chrome_net_log.h +++ b/chrome/browser/net/chrome_net_log.h @@ -25,46 +25,14 @@ class ChromeNetLog : public net::NetLog { ChromeNetLog(); virtual ~ChromeNetLog(); - // NetLog implementation: - virtual uint32 NextID() OVERRIDE; - virtual LogLevel GetLogLevel() const OVERRIDE; - virtual void AddThreadSafeObserver(ThreadSafeObserver* observer, - LogLevel log_level) OVERRIDE; - virtual void SetObserverLogLevel(ThreadSafeObserver* observer, - LogLevel log_level) OVERRIDE; - virtual void RemoveThreadSafeObserver(ThreadSafeObserver* observer) OVERRIDE; - NetLogTempFile* net_log_temp_file() { return net_log_temp_file_.get(); } private: - // NetLog implementation: - virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE; - - // Called whenever an observer is added or removed, or has its log level - // changed. Must have acquired |lock_| prior to calling. - void UpdateLogLevel(); - - // |lock_| protects access to |observers_|. - base::Lock lock_; - - // Last assigned source ID. Incremented to get the next one. - base::subtle::Atomic32 last_id_; - - // The lowest allowed log level, regardless of any ChromeNetLogObservers. - // Normally defaults to LOG_BASIC, but can be changed with command line flags. - LogLevel base_log_level_; - - // The current log level. - base::subtle::Atomic32 effective_log_level_; - scoped_ptr<NetLogLogger> net_log_logger_; scoped_ptr<NetLogTempFile> net_log_temp_file_; - // |lock_| must be acquired whenever reading or writing to this. - ObserverList<ThreadSafeObserver, true> observers_; - DISALLOW_COPY_AND_ASSIGN(ChromeNetLog); }; diff --git a/chrome/browser/net/chrome_net_log_unittest.cc b/chrome/browser/net/chrome_net_log_unittest.cc deleted file mode 100644 index 6eb0912..0000000 --- a/chrome/browser/net/chrome_net_log_unittest.cc +++ /dev/null @@ -1,271 +0,0 @@ -// Copyright (c) 2012 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 "base/synchronization/waitable_event.h" -#include "base/threading/simple_thread.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace { - -const int kThreads = 10; -const int kEvents = 100; - -class CountingObserver : public net::NetLog::ThreadSafeObserver { - public: - CountingObserver() : count_(0) {} - - virtual ~CountingObserver() { - if (net_log()) - net_log()->RemoveThreadSafeObserver(this); - } - - virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE { - ++count_; - } - - int count() const { return count_; } - - private: - int count_; -}; - -void AddEvent(ChromeNetLog* net_log) { - net_log->AddGlobalEntry(net::NetLog::TYPE_CANCELLED); -} - -// A thread that waits until an event has been signalled before calling -// RunTestThread. -class ChromeNetLogTestThread : public base::SimpleThread { - public: - ChromeNetLogTestThread() : base::SimpleThread("ChromeNetLogTest"), - net_log_(NULL), - start_event_(NULL) { - } - - // We'll wait for |start_event| to be triggered before calling a subclass's - // subclass's RunTestThread() function. - void Init(ChromeNetLog* net_log, base::WaitableEvent* start_event) { - start_event_ = start_event; - net_log_ = net_log; - } - - virtual void Run() OVERRIDE { - start_event_->Wait(); - RunTestThread(); - } - - // Subclasses must override this with the code they want to run on their - // thread. - virtual void RunTestThread() = 0; - - protected: - ChromeNetLog* net_log_; - - private: - // Only triggered once all threads have been created, to make it less likely - // each thread completes before the next one starts. - base::WaitableEvent* start_event_; - - DISALLOW_COPY_AND_ASSIGN(ChromeNetLogTestThread); -}; - -// A thread that adds a bunch of events to the NetLog. -class AddEventsTestThread : public ChromeNetLogTestThread { - public: - AddEventsTestThread() {} - virtual ~AddEventsTestThread() {} - - private: - virtual void RunTestThread() OVERRIDE { - for (int i = 0; i < kEvents; ++i) - AddEvent(net_log_); - } - - DISALLOW_COPY_AND_ASSIGN(AddEventsTestThread); -}; - -// A thread that adds and removes an observer from the NetLog repeatedly. -class AddRemoveObserverTestThread : public ChromeNetLogTestThread { - public: - AddRemoveObserverTestThread() {} - - virtual ~AddRemoveObserverTestThread() { - EXPECT_TRUE(!observer_.net_log()); - } - - private: - virtual void RunTestThread() OVERRIDE { - for (int i = 0; i < kEvents; ++i) { - ASSERT_FALSE(observer_.net_log()); - - net_log_->AddThreadSafeObserver(&observer_, net::NetLog::LOG_BASIC); - ASSERT_EQ(net_log_, observer_.net_log()); - ASSERT_EQ(net::NetLog::LOG_BASIC, observer_.log_level()); - - net_log_->SetObserverLogLevel(&observer_, net::NetLog::LOG_ALL_BUT_BYTES); - ASSERT_EQ(net_log_, observer_.net_log()); - ASSERT_EQ(net::NetLog::LOG_ALL_BUT_BYTES, observer_.log_level()); - ASSERT_LE(net_log_->GetLogLevel(), net::NetLog::LOG_ALL_BUT_BYTES); - - net_log_->SetObserverLogLevel(&observer_, net::NetLog::LOG_ALL); - ASSERT_EQ(net_log_, observer_.net_log()); - ASSERT_EQ(net::NetLog::LOG_ALL, observer_.log_level()); - ASSERT_LE(net_log_->GetLogLevel(), net::NetLog::LOG_ALL); - - net_log_->RemoveThreadSafeObserver(&observer_); - ASSERT_TRUE(!observer_.net_log()); - } - } - - CountingObserver observer_; - - DISALLOW_COPY_AND_ASSIGN(AddRemoveObserverTestThread); -}; - -// Creates |kThreads| threads of type |ThreadType| and then runs them all -// to completion. -template<class ThreadType> -void RunTestThreads(ChromeNetLog* net_log) { - ThreadType threads[kThreads]; - base::WaitableEvent start_event(true, false); - - for (size_t i = 0; i < arraysize(threads); ++i) { - threads[i].Init(net_log, &start_event); - threads[i].Start(); - } - - start_event.Signal(); - - for (size_t i = 0; i < arraysize(threads); ++i) - threads[i].Join(); -} - -// Makes sure that events on multiple threads are dispatched to all observers. -TEST(ChromeNetLogTest, NetLogEventThreads) { - ChromeNetLog net_log; - - // Attach some observers. Since they're created after |net_log|, they'll - // safely detach themselves on destruction. - CountingObserver observers[3]; - for (size_t i = 0; i < arraysize(observers); ++i) - net_log.AddThreadSafeObserver(&observers[i], net::NetLog::LOG_BASIC); - - // Run a bunch of threads to completion, each of which will emit events to - // |net_log|. - RunTestThreads<AddEventsTestThread>(&net_log); - - // Check that each observer saw the emitted events. - const int kTotalEvents = kThreads * kEvents; - for (size_t i = 0; i < arraysize(observers); ++i) - EXPECT_EQ(kTotalEvents, observers[i].count()); -} - -// Test adding and removing a single observer. -TEST(ChromeNetLogTest, NetLogAddRemoveObserver) { - ChromeNetLog net_log; - CountingObserver observer; - - AddEvent(&net_log); - EXPECT_EQ(0, observer.count()); - EXPECT_EQ(NULL, observer.net_log()); - EXPECT_EQ(net::NetLog::LOG_NONE, net_log.GetLogLevel()); - - // Add the observer and add an event. - net_log.AddThreadSafeObserver(&observer, net::NetLog::LOG_BASIC); - EXPECT_EQ(&net_log, observer.net_log()); - EXPECT_EQ(net::NetLog::LOG_BASIC, observer.log_level()); - EXPECT_EQ(net::NetLog::LOG_BASIC, net_log.GetLogLevel()); - - AddEvent(&net_log); - EXPECT_EQ(1, observer.count()); - - // Change the observer's logging level and add an event. - net_log.SetObserverLogLevel(&observer, net::NetLog::LOG_ALL); - EXPECT_EQ(&net_log, observer.net_log()); - EXPECT_EQ(net::NetLog::LOG_ALL, observer.log_level()); - EXPECT_EQ(net::NetLog::LOG_ALL, net_log.GetLogLevel()); - - AddEvent(&net_log); - EXPECT_EQ(2, observer.count()); - - // Remove observer and add an event. - net_log.RemoveThreadSafeObserver(&observer); - EXPECT_EQ(NULL, observer.net_log()); - EXPECT_EQ(net::NetLog::LOG_NONE, net_log.GetLogLevel()); - - AddEvent(&net_log); - EXPECT_EQ(2, observer.count()); - - // Add the observer a final time, and add an event. - net_log.AddThreadSafeObserver(&observer, net::NetLog::LOG_ALL); - EXPECT_EQ(&net_log, observer.net_log()); - EXPECT_EQ(net::NetLog::LOG_ALL, observer.log_level()); - EXPECT_EQ(net::NetLog::LOG_ALL, net_log.GetLogLevel()); - - AddEvent(&net_log); - EXPECT_EQ(3, observer.count()); -} - -// Test adding and removing two observers. -TEST(ChromeNetLogTest, NetLogTwoObservers) { - ChromeNetLog net_log; - CountingObserver observer[2]; - - // Add first observer. - net_log.AddThreadSafeObserver(&observer[0], net::NetLog::LOG_ALL_BUT_BYTES); - EXPECT_EQ(&net_log, observer[0].net_log()); - EXPECT_EQ(NULL, observer[1].net_log()); - EXPECT_EQ(net::NetLog::LOG_ALL_BUT_BYTES, observer[0].log_level()); - EXPECT_EQ(net::NetLog::LOG_ALL_BUT_BYTES, net_log.GetLogLevel()); - - // Add second observer observer. - net_log.AddThreadSafeObserver(&observer[1], net::NetLog::LOG_ALL); - EXPECT_EQ(&net_log, observer[0].net_log()); - EXPECT_EQ(&net_log, observer[1].net_log()); - EXPECT_EQ(net::NetLog::LOG_ALL_BUT_BYTES, observer[0].log_level()); - EXPECT_EQ(net::NetLog::LOG_ALL, observer[1].log_level()); - EXPECT_EQ(net::NetLog::LOG_ALL, net_log.GetLogLevel()); - - // Add event and make sure both observers receive it. - AddEvent(&net_log); - EXPECT_EQ(1, observer[0].count()); - EXPECT_EQ(1, observer[1].count()); - - // Remove second observer. - net_log.RemoveThreadSafeObserver(&observer[1]); - EXPECT_EQ(&net_log, observer[0].net_log()); - EXPECT_EQ(NULL, observer[1].net_log()); - EXPECT_EQ(net::NetLog::LOG_ALL_BUT_BYTES, observer[0].log_level()); - EXPECT_EQ(net::NetLog::LOG_ALL_BUT_BYTES, net_log.GetLogLevel()); - - // Add event and make sure only second observer gets it. - AddEvent(&net_log); - EXPECT_EQ(2, observer[0].count()); - EXPECT_EQ(1, observer[1].count()); - - // Remove first observer. - net_log.RemoveThreadSafeObserver(&observer[0]); - EXPECT_EQ(NULL, observer[0].net_log()); - EXPECT_EQ(NULL, observer[1].net_log()); - EXPECT_EQ(net::NetLog::LOG_NONE, net_log.GetLogLevel()); - - // Add event and make sure neither observer gets it. - AddEvent(&net_log); - EXPECT_EQ(2, observer[0].count()); - EXPECT_EQ(1, observer[1].count()); -} - -// Makes sure that adding and removing observers simultaneously on different -// threads works. -TEST(ChromeNetLogTest, NetLogAddRemoveObserverThreads) { - ChromeNetLog net_log; - - // Run a bunch of threads to completion, each of which will repeatedly add - // and remove an observer, and set its logging level. - RunTestThreads<AddRemoveObserverTestThread>(&net_log); -} - -} // namespace |