| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "net/log/trace_net_log_observer.h" | 5 #include "net/log/trace_net_log_observer.h" |
| 6 | 6 |
| 7 #include <memory> |
| 7 #include <string> | 8 #include <string> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/trace_event/trace_buffer.h" | 17 #include "base/trace_event/trace_buffer.h" |
| 18 #include "base/trace_event/trace_event.h" | 18 #include "base/trace_event/trace_event.h" |
| 19 #include "base/trace_event/trace_event_impl.h" | 19 #include "base/trace_event/trace_event_impl.h" |
| 20 #include "base/values.h" | 20 #include "base/values.h" |
| 21 #include "net/log/net_log.h" | 21 #include "net/log/net_log.h" |
| 22 #include "net/log/test_net_log.h" | 22 #include "net/log/test_net_log.h" |
| 23 #include "net/log/test_net_log_entry.h" | 23 #include "net/log/test_net_log_entry.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 | 68 |
| 69 void OnTraceDataCollected( | 69 void OnTraceDataCollected( |
| 70 base::RunLoop* run_loop, | 70 base::RunLoop* run_loop, |
| 71 const scoped_refptr<base::RefCountedString>& events_str, | 71 const scoped_refptr<base::RefCountedString>& events_str, |
| 72 bool has_more_events) { | 72 bool has_more_events) { |
| 73 DCHECK(trace_events_->empty()); | 73 DCHECK(trace_events_->empty()); |
| 74 trace_buffer_.Start(); | 74 trace_buffer_.Start(); |
| 75 trace_buffer_.AddFragment(events_str->data()); | 75 trace_buffer_.AddFragment(events_str->data()); |
| 76 trace_buffer_.Finish(); | 76 trace_buffer_.Finish(); |
| 77 | 77 |
| 78 scoped_ptr<base::Value> trace_value; | 78 std::unique_ptr<base::Value> trace_value; |
| 79 trace_value = base::JSONReader::Read( | 79 trace_value = base::JSONReader::Read( |
| 80 json_output_.json_output, | 80 json_output_.json_output, |
| 81 base::JSON_PARSE_RFC | base::JSON_DETACHABLE_CHILDREN); | 81 base::JSON_PARSE_RFC | base::JSON_DETACHABLE_CHILDREN); |
| 82 | 82 |
| 83 ASSERT_TRUE(trace_value) << json_output_.json_output; | 83 ASSERT_TRUE(trace_value) << json_output_.json_output; |
| 84 base::ListValue* trace_events = NULL; | 84 base::ListValue* trace_events = NULL; |
| 85 ASSERT_TRUE(trace_value->GetAsList(&trace_events)); | 85 ASSERT_TRUE(trace_value->GetAsList(&trace_events)); |
| 86 | 86 |
| 87 trace_events_ = FilterNetLogTraceEvents(*trace_events); | 87 trace_events_ = FilterNetLogTraceEvents(*trace_events); |
| 88 | 88 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 102 TraceLog::GetInstance()->Flush( | 102 TraceLog::GetInstance()->Flush( |
| 103 base::Bind(&TraceNetLogObserverTest::OnTraceDataCollected, | 103 base::Bind(&TraceNetLogObserverTest::OnTraceDataCollected, |
| 104 base::Unretained(this), base::Unretained(&run_loop))); | 104 base::Unretained(this), base::Unretained(&run_loop))); |
| 105 run_loop.Run(); | 105 run_loop.Run(); |
| 106 } | 106 } |
| 107 | 107 |
| 108 void set_trace_net_log_observer(TraceNetLogObserver* trace_net_log_observer) { | 108 void set_trace_net_log_observer(TraceNetLogObserver* trace_net_log_observer) { |
| 109 trace_net_log_observer_.reset(trace_net_log_observer); | 109 trace_net_log_observer_.reset(trace_net_log_observer); |
| 110 } | 110 } |
| 111 | 111 |
| 112 static scoped_ptr<base::ListValue> FilterNetLogTraceEvents( | 112 static std::unique_ptr<base::ListValue> FilterNetLogTraceEvents( |
| 113 const base::ListValue& trace_events) { | 113 const base::ListValue& trace_events) { |
| 114 scoped_ptr<base::ListValue> filtered_trace_events(new base::ListValue()); | 114 std::unique_ptr<base::ListValue> filtered_trace_events( |
| 115 new base::ListValue()); |
| 115 for (size_t i = 0; i < trace_events.GetSize(); i++) { | 116 for (size_t i = 0; i < trace_events.GetSize(); i++) { |
| 116 const base::DictionaryValue* dict = NULL; | 117 const base::DictionaryValue* dict = NULL; |
| 117 if (!trace_events.GetDictionary(i, &dict)) { | 118 if (!trace_events.GetDictionary(i, &dict)) { |
| 118 ADD_FAILURE() << "Unexpected non-dictionary event in trace_events"; | 119 ADD_FAILURE() << "Unexpected non-dictionary event in trace_events"; |
| 119 continue; | 120 continue; |
| 120 } | 121 } |
| 121 std::string category; | 122 std::string category; |
| 122 if (!dict->GetString("cat", &category)) { | 123 if (!dict->GetString("cat", &category)) { |
| 123 ADD_FAILURE() | 124 ADD_FAILURE() |
| 124 << "Unexpected item without a category field in trace_events"; | 125 << "Unexpected item without a category field in trace_events"; |
| 125 continue; | 126 continue; |
| 126 } | 127 } |
| 127 if (category != kNetLogTracingCategory) | 128 if (category != kNetLogTracingCategory) |
| 128 continue; | 129 continue; |
| 129 filtered_trace_events->Append(dict->DeepCopy()); | 130 filtered_trace_events->Append(dict->DeepCopy()); |
| 130 } | 131 } |
| 131 return filtered_trace_events; | 132 return filtered_trace_events; |
| 132 } | 133 } |
| 133 | 134 |
| 134 base::ListValue* trace_events() const { return trace_events_.get(); } | 135 base::ListValue* trace_events() const { return trace_events_.get(); } |
| 135 | 136 |
| 136 TestNetLog* net_log() { return &net_log_; } | 137 TestNetLog* net_log() { return &net_log_; } |
| 137 | 138 |
| 138 TraceNetLogObserver* trace_net_log_observer() const { | 139 TraceNetLogObserver* trace_net_log_observer() const { |
| 139 return trace_net_log_observer_.get(); | 140 return trace_net_log_observer_.get(); |
| 140 } | 141 } |
| 141 | 142 |
| 142 private: | 143 private: |
| 143 scoped_ptr<base::ListValue> trace_events_; | 144 std::unique_ptr<base::ListValue> trace_events_; |
| 144 base::trace_event::TraceResultBuffer trace_buffer_; | 145 base::trace_event::TraceResultBuffer trace_buffer_; |
| 145 base::trace_event::TraceResultBuffer::SimpleOutput json_output_; | 146 base::trace_event::TraceResultBuffer::SimpleOutput json_output_; |
| 146 TestNetLog net_log_; | 147 TestNetLog net_log_; |
| 147 scoped_ptr<TraceNetLogObserver> trace_net_log_observer_; | 148 std::unique_ptr<TraceNetLogObserver> trace_net_log_observer_; |
| 148 }; | 149 }; |
| 149 | 150 |
| 150 TEST_F(TraceNetLogObserverTest, TracingNotEnabled) { | 151 TEST_F(TraceNetLogObserverTest, TracingNotEnabled) { |
| 151 trace_net_log_observer()->WatchForTraceStart(net_log()); | 152 trace_net_log_observer()->WatchForTraceStart(net_log()); |
| 152 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); | 153 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); |
| 153 | 154 |
| 154 EndTraceAndFlush(); | 155 EndTraceAndFlush(); |
| 155 trace_net_log_observer()->StopWatchForTraceStart(); | 156 trace_net_log_observer()->StopWatchForTraceStart(); |
| 156 | 157 |
| 157 EXPECT_EQ(0u, trace_events()->GetSize()); | 158 EXPECT_EQ(0u, trace_events()->GetSize()); |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 EXPECT_TRUE(item1->GetString("args.params.foo", &item1_params)); | 366 EXPECT_TRUE(item1->GetString("args.params.foo", &item1_params)); |
| 366 EXPECT_EQ("bar", item1_params); | 367 EXPECT_EQ("bar", item1_params); |
| 367 | 368 |
| 368 EXPECT_TRUE(item2->GetString("args.params", &item2_params)); | 369 EXPECT_TRUE(item2->GetString("args.params", &item2_params)); |
| 369 EXPECT_TRUE(item2_params.empty()); | 370 EXPECT_TRUE(item2_params.empty()); |
| 370 } | 371 } |
| 371 | 372 |
| 372 } // namespace | 373 } // namespace |
| 373 | 374 |
| 374 } // namespace net | 375 } // namespace net |
| OLD | NEW |