diff options
Diffstat (limited to 'net/base')
-rw-r--r-- | net/base/capturing_net_log.cc | 24 | ||||
-rw-r--r-- | net/base/capturing_net_log.h | 21 | ||||
-rw-r--r-- | net/base/forwarding_net_log.cc | 96 | ||||
-rw-r--r-- | net/base/forwarding_net_log.h | 54 | ||||
-rw-r--r-- | net/base/forwarding_net_log_unittest.cc | 84 | ||||
-rw-r--r-- | net/base/host_resolver_impl_unittest.cc | 55 | ||||
-rw-r--r-- | net/base/net_log.h | 3 |
7 files changed, 61 insertions, 276 deletions
diff --git a/net/base/capturing_net_log.cc b/net/base/capturing_net_log.cc index c488e06..e785990 100644 --- a/net/base/capturing_net_log.cc +++ b/net/base/capturing_net_log.cc @@ -18,7 +18,7 @@ CapturingNetLog::Entry::Entry(EventType type, CapturingNetLog::Entry::~Entry() {} CapturingNetLog::CapturingNetLog(size_t max_num_entries) - : next_id_(0), max_num_entries_(max_num_entries) { + : last_id_(-1), max_num_entries_(max_num_entries) { } CapturingNetLog::~CapturingNetLog() {} @@ -28,16 +28,23 @@ void CapturingNetLog::AddEntry(EventType type, const Source& source, EventPhase phase, EventParameters* extra_parameters) { + AutoLock lock(lock_); Entry entry(type, time, source, phase, extra_parameters); if (entries_.size() + 1 < max_num_entries_) entries_.push_back(entry); } uint32 CapturingNetLog::NextID() { - return next_id_++; + return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1); +} + +void CapturingNetLog::GetEntries(EntryList* entry_list) const { + AutoLock lock(lock_); + *entry_list = entries_; } void CapturingNetLog::Clear() { + AutoLock lock(lock_); entries_.clear(); } @@ -51,16 +58,13 @@ CapturingBoundNetLog::CapturingBoundNetLog(size_t max_num_entries) CapturingBoundNetLog::~CapturingBoundNetLog() {} -void CapturingBoundNetLog::Clear() { - capturing_net_log_->Clear(); +void CapturingBoundNetLog::GetEntries( + CapturingNetLog::EntryList* entry_list) const { + capturing_net_log_->GetEntries(entry_list); } -void CapturingBoundNetLog::AppendTo(const BoundNetLog& net_log) const { - for (size_t i = 0; i < entries().size(); ++i) { - const CapturingNetLog::Entry& entry = entries()[i]; - net_log.AddEntryWithTime(entry.type, entry.time, entry.phase, - entry.extra_parameters); - } +void CapturingBoundNetLog::Clear() { + capturing_net_log_->Clear(); } } // namespace net diff --git a/net/base/capturing_net_log.h b/net/base/capturing_net_log.h index ff7cf16..3b6a39e 100644 --- a/net/base/capturing_net_log.h +++ b/net/base/capturing_net_log.h @@ -8,7 +8,9 @@ #include <vector> +#include "base/atomicops.h" #include "base/basictypes.h" +#include "base/lock.h" #include "base/ref_counted.h" #include "base/scoped_ptr.h" #include "base/time.h" @@ -55,12 +57,17 @@ class CapturingNetLog : public NetLog { virtual LogLevel GetLogLevel() const { return LOG_ALL_BUT_BYTES; } // Returns the list of all entries in the log. - const EntryList& entries() const { return entries_; } + void GetEntries(EntryList* entry_list) const; void Clear(); private: - uint32 next_id_; + // Needs to be "mutable" so can use it in GetEntries(). + mutable Lock lock_; + + // Last assigned source ID. Incremented to get the next one. + base::subtle::Atomic32 last_id_; + size_t max_num_entries_; EntryList entries_; @@ -85,17 +92,11 @@ class CapturingBoundNetLog { return BoundNetLog(source_, capturing_net_log_.get()); } - // Returns the list of all entries in the log. - const CapturingNetLog::EntryList& entries() const { - return capturing_net_log_->entries(); - } + // Fills |entry_list| with all entries in the log. + void GetEntries(CapturingNetLog::EntryList* entry_list) const; void Clear(); - // Sends all of captured messages to |net_log|, using the same source ID - // as |net_log|. - void AppendTo(const BoundNetLog& net_log) const; - private: NetLog::Source source_; scoped_ptr<CapturingNetLog> capturing_net_log_; diff --git a/net/base/forwarding_net_log.cc b/net/base/forwarding_net_log.cc deleted file mode 100644 index 7cfd6a9..0000000 --- a/net/base/forwarding_net_log.cc +++ /dev/null @@ -1,96 +0,0 @@ -// Copyright (c) 2010 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 "net/base/forwarding_net_log.h" - -#include "base/lock.h" -#include "base/logging.h" -#include "base/message_loop.h" - -namespace net { - -// Reference-counted wrapper, so we can use PostThread and it can safely -// outlive the parent ForwardingNetLog. -class ForwardingNetLog::Core - : public base::RefCountedThreadSafe<ForwardingNetLog::Core> { - public: - Core(NetLog* impl, MessageLoop* loop) : impl_(impl), loop_(loop) { - DCHECK(impl); - DCHECK(loop); - } - - // Called once the parent ForwardingNetLog is being destroyed. It - // is invalid to access |loop_| and |impl_| afterwards. - void Orphan() { - AutoLock l(lock_); - loop_ = NULL; - impl_ = NULL; - } - - void AddEntry(EventType type, - const base::TimeTicks& time, - const Source& source, - EventPhase phase, - EventParameters* params) { - AutoLock l(lock_); - if (!loop_) - return; // Was orphaned. - - loop_->PostTask( - FROM_HERE, - NewRunnableMethod( - this, &Core::AddEntryOnLoop, type, time, source, phase, - scoped_refptr<EventParameters>(params))); - } - - private: - void AddEntryOnLoop(EventType type, - const base::TimeTicks& time, - const Source& source, - EventPhase phase, - scoped_refptr<EventParameters> params) { - AutoLock l(lock_); - if (!loop_) - return; // Was orphaned. - - DCHECK_EQ(MessageLoop::current(), loop_); - - impl_->AddEntry(type, time, source, phase, params); - } - - Lock lock_; - NetLog* impl_; - MessageLoop* loop_; -}; - -ForwardingNetLog::ForwardingNetLog(NetLog* impl, MessageLoop* loop) - : core_(new Core(impl, loop)) { -} - -ForwardingNetLog::~ForwardingNetLog() { - core_->Orphan(); -} - -void ForwardingNetLog::AddEntry(EventType type, - const base::TimeTicks& time, - const Source& source, - EventPhase phase, - EventParameters* params) { - core_->AddEntry(type, time, source, phase, params); -} - -uint32 ForwardingNetLog::NextID() { - // Can't forward a synchronous API. - CHECK(false) << "Not supported"; - return 0; -} - -NetLog::LogLevel ForwardingNetLog::GetLogLevel() const { - // Can't forward a synchronous API. - CHECK(false) << "Not supported"; - return LOG_ALL_BUT_BYTES; -} - -} // namespace net - diff --git a/net/base/forwarding_net_log.h b/net/base/forwarding_net_log.h deleted file mode 100644 index 257b4c7..0000000 --- a/net/base/forwarding_net_log.h +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright (c) 2010 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. - -#ifndef NET_BASE_FORWARDING_NET_LOG_H_ -#define NET_BASE_FORWARDING_NET_LOG_H_ -#pragma once - -#include "base/basictypes.h" -#include "net/base/net_log.h" - -class MessageLoop; - -namespace net { - -// ForwardingNetLog is a wrapper that can be called on any thread, and will -// forward any calls to NetLog::AddEntry() over to |impl| on the specified -// message loop. -// -// This allows using a non-threadsafe NetLog implementation from another -// thread. -// -// TODO(eroman): Explore making NetLog threadsafe and obviating the need -// for this class. -class ForwardingNetLog : public NetLog { - public: - // Both |impl| and |loop| must outlive the lifetime of this instance. - // |impl| will be operated only from |loop|. - ForwardingNetLog(NetLog* impl, MessageLoop* loop); - - // On destruction any outstanding call to AddEntry() which didn't make - // it to |loop| yet will be cancelled. - ~ForwardingNetLog(); - - // NetLog methods: - virtual void AddEntry(EventType type, - const base::TimeTicks& time, - const Source& source, - EventPhase phase, - EventParameters* params); - virtual uint32 NextID(); - virtual LogLevel GetLogLevel() const; - - private: - class Core; - scoped_refptr<Core> core_; - - DISALLOW_COPY_AND_ASSIGN(ForwardingNetLog); -}; - -} // namespace net - -#endif // NET_BASE_FORWARDING_NET_LOG_H_ - diff --git a/net/base/forwarding_net_log_unittest.cc b/net/base/forwarding_net_log_unittest.cc deleted file mode 100644 index 3f25129..0000000 --- a/net/base/forwarding_net_log_unittest.cc +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright (c) 2010 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 "net/base/forwarding_net_log.h" - -#include "base/message_loop.h" -#include "net/base/capturing_net_log.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace net { - -namespace { - -// Test forwarding a call to AddEntry() to another implementation, operating -// on this same message loop. -TEST(ForwardingNetLogTest, Basic) { - // Create a forwarding NetLog that sends messages to this same thread. - CapturingNetLog log(CapturingNetLog::kUnbounded); - ForwardingNetLog forwarding(&log, MessageLoop::current()); - - EXPECT_EQ(0u, log.entries().size()); - - NetLogStringParameter* params = new NetLogStringParameter("xxx", "yyy"); - - forwarding.AddEntry( - NetLog::TYPE_PAC_JAVASCRIPT_ALERT, - base::TimeTicks(), - NetLog::Source(), - NetLog::PHASE_NONE, - params); - - // Should still be empty, since we posted an async task. - EXPECT_EQ(0u, log.entries().size()); - - MessageLoop::current()->RunAllPending(); - - // After draining the message loop, we should now have executed the task - // and hence emitted the log entry. - ASSERT_EQ(1u, log.entries().size()); - - // Check that the forwarded call contained received all the right inputs. - EXPECT_EQ(NetLog::TYPE_PAC_JAVASCRIPT_ALERT, log.entries()[0].type); - EXPECT_EQ(NetLog::SOURCE_NONE, log.entries()[0].source.type); - EXPECT_EQ(NetLog::PHASE_NONE, log.entries()[0].phase); - EXPECT_EQ(params, log.entries()[0].extra_parameters.get()); - - // Check that the parameters is still referenced. (if the reference was - // lost then this will be a memory error and probaby crash). - EXPECT_EQ("yyy", params->value()); -} - -// Test forwarding a call to AddEntry() to another implementation that runs -// on the same message loop. However destroy the forwarder before the posted -// task has a chance to run. -TEST(ForwardingNetLogTest, Orphan) { - // Create a forwarding NetLog that sends messages to this same thread. - CapturingNetLog log(CapturingNetLog::kUnbounded); - { - ForwardingNetLog forwarding(&log, MessageLoop::current()); - EXPECT_EQ(0u, log.entries().size()); - - forwarding.AddEntry( - NetLog::TYPE_PAC_JAVASCRIPT_ALERT, - base::TimeTicks(), - NetLog::Source(), - NetLog::PHASE_NONE, - NULL); - - // Should still be empty, since we posted an async task. - EXPECT_EQ(0u, log.entries().size()); - } - - // At this point the ForwardingNetLog is deleted. However it had already - // posted a task to the message loop. Once we drain the message loop, we - // verify that the task didn't actually try to emit to the NetLog. - MessageLoop::current()->RunAllPending(); - EXPECT_EQ(0u, log.entries().size()); -} - -} // namespace - -} // namespace net - diff --git a/net/base/host_resolver_impl_unittest.cc b/net/base/host_resolver_impl_unittest.cc index f3bdb74..4d26ab9 100644 --- a/net/base/host_resolver_impl_unittest.cc +++ b/net/base/host_resolver_impl_unittest.cc @@ -271,11 +271,14 @@ TEST_F(HostResolverImplTest, SynchronousLookup) { int err = host_resolver->Resolve(info, &addrlist, NULL, NULL, log.bound()); EXPECT_EQ(OK, err); - EXPECT_EQ(2u, log.entries().size()); + net::CapturingNetLog::EntryList entries; + log.GetEntries(&entries); + + EXPECT_EQ(2u, entries.size()); EXPECT_TRUE(LogContainsBeginEvent( - log.entries(), 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); + entries, 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); EXPECT_TRUE(LogContainsEndEvent( - log.entries(), 1, NetLog::TYPE_HOST_RESOLVER_IMPL)); + entries, 1, NetLog::TYPE_HOST_RESOLVER_IMPL)); const struct addrinfo* ainfo = addrlist.head(); EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); @@ -304,18 +307,23 @@ TEST_F(HostResolverImplTest, AsynchronousLookup) { log.bound()); EXPECT_EQ(ERR_IO_PENDING, err); - EXPECT_EQ(1u, log.entries().size()); + net::CapturingNetLog::EntryList entries; + log.GetEntries(&entries); + + EXPECT_EQ(1u, entries.size()); EXPECT_TRUE(LogContainsBeginEvent( - log.entries(), 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); + entries, 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); MessageLoop::current()->Run(); ASSERT_TRUE(callback_called_); ASSERT_EQ(OK, callback_result_); - EXPECT_EQ(2u, log.entries().size()); + log.GetEntries(&entries); + + EXPECT_EQ(2u, entries.size()); EXPECT_TRUE(LogContainsEndEvent( - log.entries(), 1, NetLog::TYPE_HOST_RESOLVER_IMPL)); + entries, 1, NetLog::TYPE_HOST_RESOLVER_IMPL)); const struct addrinfo* ainfo = addrlist.head(); EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); @@ -356,31 +364,37 @@ TEST_F(HostResolverImplTest, CanceledAsynchronousLookup) { resolver_proc->Signal(); - EXPECT_EQ(2u, log.entries().size()); + net::CapturingNetLog::EntryList entries; + log.GetEntries(&entries); + + EXPECT_EQ(2u, entries.size()); EXPECT_TRUE(LogContainsBeginEvent( - log.entries(), 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); + entries, 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); EXPECT_TRUE(LogContainsEndEvent( - log.entries(), 1, NetLog::TYPE_HOST_RESOLVER_IMPL)); + entries, 1, NetLog::TYPE_HOST_RESOLVER_IMPL)); + + net::CapturingNetLog::EntryList net_log_entries; + net_log.GetEntries(&net_log_entries); - int pos = net::ExpectLogContainsSomewhereAfter(net_log.entries(), 0, + int pos = net::ExpectLogContainsSomewhereAfter(net_log_entries, 0, net::NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, net::NetLog::PHASE_BEGIN); - pos = net::ExpectLogContainsSomewhereAfter(net_log.entries(), pos + 1, + pos = net::ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, net::NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, net::NetLog::PHASE_BEGIN); // Both Job and Request need to be cancelled. - pos = net::ExpectLogContainsSomewhereAfter(net_log.entries(), pos + 1, + pos = net::ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, net::NetLog::TYPE_CANCELLED, net::NetLog::PHASE_NONE); // Don't care about order in which they end, or when the other one is // cancelled. - net::ExpectLogContainsSomewhereAfter(net_log.entries(), pos + 1, + net::ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, net::NetLog::TYPE_CANCELLED, net::NetLog::PHASE_NONE); - net::ExpectLogContainsSomewhereAfter(net_log.entries(), pos + 1, + net::ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, net::NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, net::NetLog::PHASE_END); - net::ExpectLogContainsSomewhereAfter(net_log.entries(), pos + 1, + net::ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1, net::NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, net::NetLog::PHASE_END); @@ -943,11 +957,14 @@ TEST_F(HostResolverImplTest, Observers) { int rv = host_resolver->Resolve(info1, &addrlist, NULL, NULL, log.bound()); EXPECT_EQ(OK, rv); - EXPECT_EQ(2u, log.entries().size()); + net::CapturingNetLog::EntryList entries; + log.GetEntries(&entries); + + EXPECT_EQ(2u, entries.size()); EXPECT_TRUE(LogContainsBeginEvent( - log.entries(), 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); + entries, 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); EXPECT_TRUE(LogContainsEndEvent( - log.entries(), 1, NetLog::TYPE_HOST_RESOLVER_IMPL)); + entries, 1, NetLog::TYPE_HOST_RESOLVER_IMPL)); EXPECT_EQ(1U, observer.start_log.size()); EXPECT_EQ(1U, observer.finish_log.size()); diff --git a/net/base/net_log.h b/net/base/net_log.h index 9c670ec..5a8f197 100644 --- a/net/base/net_log.h +++ b/net/base/net_log.h @@ -29,12 +29,9 @@ namespace net { // is usually accessed through a BoundNetLog, which will always pass in a // specific source ID. // -// Note that NetLog is NOT THREADSAFE. -// // ******** The NetLog (and associated logging) is a work in progress ******** // // TODO(eroman): Remove the 'const' qualitifer from the BoundNetLog methods. -// TODO(eroman): Make the DNS jobs emit into the NetLog. // TODO(eroman): Start a new Source each time net::URLRequest redirects // (simpler to reason about each as a separate entity). |