| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef NET_LOG_NET_LOG_H_ | 5 #ifndef NET_LOG_NET_LOG_H_ |
| 6 #define NET_LOG_NET_LOG_H_ | 6 #define NET_LOG_NET_LOG_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | |
| 11 #include <string> | 10 #include <string> |
| 12 | 11 |
| 13 #include "base/atomicops.h" | 12 #include "base/atomicops.h" |
| 14 #include "base/callback_forward.h" | |
| 15 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 16 #include "base/macros.h" | 14 #include "base/macros.h" |
| 17 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 18 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
| 19 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 20 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 21 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 22 #include "net/base/net_export.h" | 20 #include "net/base/net_export.h" |
| 23 #include "net/log/net_log_capture_mode.h" | 21 #include "net/log/net_log_capture_mode.h" |
| 22 #include "net/log/net_log_entry.h" |
| 24 #include "net/log/net_log_event_type.h" | 23 #include "net/log/net_log_event_type.h" |
| 24 #include "net/log/net_log_parameters_callback_typedef.h" |
| 25 #include "net/log/net_log_source.h" |
| 25 #include "net/log/net_log_source_type.h" | 26 #include "net/log/net_log_source_type.h" |
| 26 | 27 |
| 27 namespace base { | 28 namespace base { |
| 28 class DictionaryValue; | 29 class DictionaryValue; |
| 29 class Value; | 30 class Value; |
| 30 } | 31 } |
| 31 | 32 |
| 32 namespace net { | 33 namespace net { |
| 33 | 34 |
| 34 // NetLog is the destination for log messages generated by the network stack. | 35 // NetLog is the destination for log messages generated by the network stack. |
| 35 // Each log message has a "source" field which identifies the specific entity | 36 // Each log message has a "source" field which identifies the specific entity |
| 36 // that generated the message (for example, which URLRequest or which | 37 // that generated the message (for example, which URLRequest or which |
| 37 // SpdySession). | 38 // SpdySession). |
| 38 // | 39 // |
| 39 // To avoid needing to pass in the "source ID" to the logging functions, NetLog | 40 // To avoid needing to pass in the "source ID" to the logging functions, NetLog |
| 40 // is usually accessed through a NetLogWithSource, which will always pass in a | 41 // is usually accessed through a NetLogWithSource, which will always pass in a |
| 41 // specific source ID. | 42 // specific source ID. |
| 42 // | 43 // |
| 43 // All methods are thread safe, with the exception that no NetLog or | 44 // All methods are thread safe, with the exception that no NetLog or |
| 44 // NetLog::ThreadSafeObserver functions may be called by an observer's | 45 // NetLog::ThreadSafeObserver functions may be called by an observer's |
| 45 // OnAddEntry() method. Doing so will result in a deadlock. | 46 // OnAddEntry() method. Doing so will result in a deadlock. |
| 46 // | 47 // |
| 47 // For a broader introduction see the design document: | 48 // For a broader introduction see the design document: |
| 48 // https://sites.google.com/a/chromium.org/dev/developers/design-documents/netwo
rk-stack/netlog | 49 // https://sites.google.com/a/chromium.org/dev/developers/design-documents/netwo
rk-stack/netlog |
| 49 class NET_EXPORT NetLog { | 50 class NET_EXPORT NetLog { |
| 50 public: | 51 public: |
| 51 | 52 |
| 52 // A callback that returns a Value representation of the parameters | |
| 53 // associated with an event. If called, it will be called synchronously, | |
| 54 // so it need not have owning references. May be called more than once, or | |
| 55 // not at all. May return NULL. | |
| 56 typedef base::Callback<std::unique_ptr<base::Value>(NetLogCaptureMode)> | |
| 57 ParametersCallback; | |
| 58 | |
| 59 // Identifies the entity that generated this log. The |id| field should | |
| 60 // uniquely identify the source, and is used by log observers to infer | |
| 61 // message groupings. Can use NetLog::NextID() to create unique IDs. | |
| 62 struct NET_EXPORT Source { | |
| 63 static const uint32_t kInvalidId; | |
| 64 | |
| 65 Source(); | |
| 66 Source(NetLogSourceType type, uint32_t id); | |
| 67 bool IsValid() const; | |
| 68 | |
| 69 // Adds the source to a DictionaryValue containing event parameters, | |
| 70 // using the name "source_dependency". | |
| 71 void AddToEventParameters(base::DictionaryValue* event_params) const; | |
| 72 | |
| 73 // Returns a callback that returns a dictionary with a single entry | |
| 74 // named "source_dependency" that describes |this|. | |
| 75 ParametersCallback ToEventParametersCallback() const; | |
| 76 | |
| 77 // Attempts to extract a Source from a set of event parameters. Returns | |
| 78 // true and writes the result to |source| on success. Returns false and | |
| 79 // makes |source| an invalid source on failure. | |
| 80 // TODO(mmenke): Long term, we want to remove this. | |
| 81 static bool FromEventParameters(base::Value* event_params, Source* source); | |
| 82 | |
| 83 NetLogSourceType type; | |
| 84 uint32_t id; | |
| 85 }; | |
| 86 | |
| 87 struct NET_EXPORT EntryData { | |
| 88 EntryData(NetLogEventType type, | |
| 89 Source source, | |
| 90 NetLogEventPhase phase, | |
| 91 base::TimeTicks time, | |
| 92 const ParametersCallback* parameters_callback); | |
| 93 ~EntryData(); | |
| 94 | |
| 95 const NetLogEventType type; | |
| 96 const Source source; | |
| 97 const NetLogEventPhase phase; | |
| 98 const base::TimeTicks time; | |
| 99 const ParametersCallback* const parameters_callback; | |
| 100 }; | |
| 101 | |
| 102 // An Entry pre-binds EntryData to a capture mode, so observers will observe | |
| 103 // the output of ToValue() and ParametersToValue() at their log capture mode | |
| 104 // rather than the current maximum. | |
| 105 class NET_EXPORT Entry { | |
| 106 public: | |
| 107 Entry(const EntryData* data, NetLogCaptureMode capture_mode); | |
| 108 ~Entry(); | |
| 109 | |
| 110 NetLogEventType type() const { return data_->type; } | |
| 111 Source source() const { return data_->source; } | |
| 112 NetLogEventPhase phase() const { return data_->phase; } | |
| 113 | |
| 114 // Serializes the specified event to a Value. The Value also includes the | |
| 115 // current time. Takes in a time to allow back-dating entries. | |
| 116 std::unique_ptr<base::Value> ToValue() const; | |
| 117 | |
| 118 // Returns the parameters as a Value. Returns NULL if there are no | |
| 119 // parameters. | |
| 120 std::unique_ptr<base::Value> ParametersToValue() const; | |
| 121 | |
| 122 private: | |
| 123 const EntryData* const data_; | |
| 124 | |
| 125 // Log capture mode when the event occurred. | |
| 126 const NetLogCaptureMode capture_mode_; | |
| 127 | |
| 128 // It is not safe to copy this class, since |parameters_callback_| may | |
| 129 // include pointers that become stale immediately after the event is added, | |
| 130 // even if the code were modified to keep its own copy of the callback. | |
| 131 DISALLOW_COPY_AND_ASSIGN(Entry); | |
| 132 }; | |
| 133 | |
| 134 // An observer that is notified of entries added to the NetLog. The | 53 // An observer that is notified of entries added to the NetLog. The |
| 135 // "ThreadSafe" prefix of the name emphasizes that this observer may be | 54 // "ThreadSafe" prefix of the name emphasizes that this observer may be |
| 136 // called from different threads then the one which added it as an observer. | 55 // called from different threads then the one which added it as an observer. |
| 137 class NET_EXPORT ThreadSafeObserver { | 56 class NET_EXPORT ThreadSafeObserver { |
| 138 public: | 57 public: |
| 139 // Constructs an observer that wants to see network events, with | 58 // Constructs an observer that wants to see network events, with |
| 140 // the specified minimum event granularity. A ThreadSafeObserver can only | 59 // the specified minimum event granularity. A ThreadSafeObserver can only |
| 141 // observe a single NetLog at a time. | 60 // observe a single NetLog at a time. |
| 142 // | 61 // |
| 143 // Observers will be called on the same thread an entry is added on, | 62 // Observers will be called on the same thread an entry is added on, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 162 // | 81 // |
| 163 // Whenever OnAddEntry() is invoked, the NetLog's mutex is held. The | 82 // Whenever OnAddEntry() is invoked, the NetLog's mutex is held. The |
| 164 // consequences of this are: | 83 // consequences of this are: |
| 165 // | 84 // |
| 166 // * OnAddEntry() will never be called concurrently -- implementations | 85 // * OnAddEntry() will never be called concurrently -- implementations |
| 167 // can rely on this to avoid needing their own synchronization. | 86 // can rely on this to avoid needing their own synchronization. |
| 168 // | 87 // |
| 169 // * It is illegal for an observer to call back into the NetLog, or the | 88 // * It is illegal for an observer to call back into the NetLog, or the |
| 170 // observer itself, as this can result in deadlock or violating | 89 // observer itself, as this can result in deadlock or violating |
| 171 // expectations of non re-entrancy into ThreadSafeObserver. | 90 // expectations of non re-entrancy into ThreadSafeObserver. |
| 172 virtual void OnAddEntry(const Entry& entry) = 0; | 91 virtual void OnAddEntry(const NetLogEntry& entry) = 0; |
| 173 | 92 |
| 174 protected: | 93 protected: |
| 175 virtual ~ThreadSafeObserver(); | 94 virtual ~ThreadSafeObserver(); |
| 176 | 95 |
| 177 private: | 96 private: |
| 178 friend class NetLog; | 97 friend class NetLog; |
| 179 | 98 |
| 180 void OnAddEntryData(const EntryData& entry_data); | 99 void OnAddEntryData(const NetLogEntryData& entry_data); |
| 181 | 100 |
| 182 // Both of these values are only modified by the NetLog. | 101 // Both of these values are only modified by the NetLog. |
| 183 NetLogCaptureMode capture_mode_; | 102 NetLogCaptureMode capture_mode_; |
| 184 NetLog* net_log_; | 103 NetLog* net_log_; |
| 185 | 104 |
| 186 DISALLOW_COPY_AND_ASSIGN(ThreadSafeObserver); | 105 DISALLOW_COPY_AND_ASSIGN(ThreadSafeObserver); |
| 187 }; | 106 }; |
| 188 | 107 |
| 189 NetLog(); | 108 NetLog(); |
| 190 virtual ~NetLog(); | 109 virtual ~NetLog(); |
| 191 | 110 |
| 192 // Emits a global event to the log stream, with its own unique source ID. | 111 // Emits a global event to the log stream, with its own unique source ID. |
| 193 void AddGlobalEntry(NetLogEventType type); | 112 void AddGlobalEntry(NetLogEventType type); |
| 194 void AddGlobalEntry(NetLogEventType type, | 113 void AddGlobalEntry(NetLogEventType type, |
| 195 const NetLog::ParametersCallback& parameters_callback); | 114 const NetLogParametersCallback& parameters_callback); |
| 196 | 115 |
| 197 // Returns a unique ID which can be used as a source ID. All returned IDs | 116 // Returns a unique ID which can be used as a source ID. All returned IDs |
| 198 // will be unique and greater than 0. | 117 // will be unique and greater than 0. |
| 199 uint32_t NextID(); | 118 uint32_t NextID(); |
| 200 | 119 |
| 201 // Returns true if there are any observers attached to the NetLog. This can be | 120 // Returns true if there are any observers attached to the NetLog. This can be |
| 202 // used as an optimization to avoid emitting log entries when there is no | 121 // used as an optimization to avoid emitting log entries when there is no |
| 203 // chance that the data will be consumed. | 122 // chance that the data will be consumed. |
| 204 bool IsCapturing() const; | 123 bool IsCapturing() const; |
| 205 | 124 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 // Returns a C-String symbolic name for |source_type|. | 161 // Returns a C-String symbolic name for |source_type|. |
| 243 static const char* SourceTypeToString(NetLogSourceType source_type); | 162 static const char* SourceTypeToString(NetLogSourceType source_type); |
| 244 | 163 |
| 245 // Returns a dictionary that maps source type symbolic names to their enum | 164 // Returns a dictionary that maps source type symbolic names to their enum |
| 246 // values. Caller takes ownership of the returned Value. | 165 // values. Caller takes ownership of the returned Value. |
| 247 static base::Value* GetSourceTypesAsValue(); | 166 static base::Value* GetSourceTypesAsValue(); |
| 248 | 167 |
| 249 // Returns a C-String symbolic name for |event_phase|. | 168 // Returns a C-String symbolic name for |event_phase|. |
| 250 static const char* EventPhaseToString(NetLogEventPhase event_phase); | 169 static const char* EventPhaseToString(NetLogEventPhase event_phase); |
| 251 | 170 |
| 252 // Creates a ParametersCallback that encapsulates a single bool. | 171 // Creates a NetLogParametersCallback that encapsulates a single bool. |
| 253 // Warning: |name| must remain valid for the life of the callback. | 172 // Warning: |name| must remain valid for the life of the callback. |
| 254 static ParametersCallback BoolCallback(const char* name, bool value); | 173 static NetLogParametersCallback BoolCallback(const char* name, bool value); |
| 255 | 174 |
| 256 // Warning: |name| must remain valid for the life of the callback. | 175 // Warning: |name| must remain valid for the life of the callback. |
| 257 static ParametersCallback IntCallback(const char* name, int value); | 176 static NetLogParametersCallback IntCallback(const char* name, int value); |
| 258 | 177 |
| 259 // Creates a ParametersCallback that encapsulates a single int64_t. The | 178 // Creates a NetLogParametersCallback that encapsulates a single int64_t. The |
| 260 // callback will return the value as a StringValue, since IntegerValues | 179 // callback will return the value as a StringValue, since IntegerValues |
| 261 // only support 32-bit values. | 180 // only support 32-bit values. |
| 262 // Warning: |name| must remain valid for the life of the callback. | 181 // Warning: |name| must remain valid for the life of the callback. |
| 263 static ParametersCallback Int64Callback(const char* name, int64_t value); | 182 static NetLogParametersCallback Int64Callback(const char* name, |
| 183 int64_t value); |
| 264 | 184 |
| 265 // Creates a ParametersCallback that encapsulates a single UTF8 string. Takes | 185 // Creates a NetLogParametersCallback that encapsulates a single UTF8 string. |
| 186 // Takes |
| 266 // |value| as a pointer to avoid copying, and emphasize it must be valid for | 187 // |value| as a pointer to avoid copying, and emphasize it must be valid for |
| 267 // the life of the callback. |value| may not be NULL. | 188 // the life of the callback. |value| may not be NULL. |
| 268 // Warning: |name| and |value| must remain valid for the life of the callback. | 189 // Warning: |name| and |value| must remain valid for the life of the callback. |
| 269 static ParametersCallback StringCallback(const char* name, | 190 static NetLogParametersCallback StringCallback(const char* name, |
| 270 const std::string* value); | 191 const std::string* value); |
| 271 static ParametersCallback StringCallback(const char* name, const char* value); | 192 static NetLogParametersCallback StringCallback(const char* name, |
| 193 const char* value); |
| 272 | 194 |
| 273 // Same as above, but takes in a UTF16 string. | 195 // Same as above, but takes in a UTF16 string. |
| 274 static ParametersCallback StringCallback(const char* name, | 196 static NetLogParametersCallback StringCallback(const char* name, |
| 275 const base::string16* value); | 197 const base::string16* value); |
| 276 | 198 |
| 277 private: | 199 private: |
| 278 friend class NetLogWithSource; | 200 friend class NetLogWithSource; |
| 279 | 201 |
| 280 void AddEntry(NetLogEventType type, | 202 void AddEntry(NetLogEventType type, |
| 281 const Source& source, | 203 const NetLogSource& source, |
| 282 NetLogEventPhase phase, | 204 NetLogEventPhase phase, |
| 283 const NetLog::ParametersCallback* parameters_callback); | 205 const NetLogParametersCallback* parameters_callback); |
| 284 | 206 |
| 285 // Called whenever an observer is added or removed, to update | 207 // Called whenever an observer is added or removed, to update |
| 286 // |has_observers_|. Must have acquired |lock_| prior to calling. | 208 // |has_observers_|. Must have acquired |lock_| prior to calling. |
| 287 void UpdateIsCapturing(); | 209 void UpdateIsCapturing(); |
| 288 | 210 |
| 289 // |lock_| protects access to |observers_|. | 211 // |lock_| protects access to |observers_|. |
| 290 base::Lock lock_; | 212 base::Lock lock_; |
| 291 | 213 |
| 292 // Last assigned source ID. Incremented to get the next one. | 214 // Last assigned source ID. Incremented to get the next one. |
| 293 base::subtle::Atomic32 last_id_; | 215 base::subtle::Atomic32 last_id_; |
| 294 | 216 |
| 295 // |is_capturing_| will be 0 when there are no observers watching the NetLog, | 217 // |is_capturing_| will be 0 when there are no observers watching the NetLog, |
| 296 // 1 otherwise. Note that this is stored as an Atomic32 rather than a boolean | 218 // 1 otherwise. Note that this is stored as an Atomic32 rather than a boolean |
| 297 // so it can be accessed without needing a lock. | 219 // so it can be accessed without needing a lock. |
| 298 base::subtle::Atomic32 is_capturing_; | 220 base::subtle::Atomic32 is_capturing_; |
| 299 | 221 |
| 300 // |lock_| must be acquired whenever reading or writing to this. | 222 // |lock_| must be acquired whenever reading or writing to this. |
| 301 base::ObserverList<ThreadSafeObserver, true> observers_; | 223 base::ObserverList<ThreadSafeObserver, true> observers_; |
| 302 | 224 |
| 303 DISALLOW_COPY_AND_ASSIGN(NetLog); | 225 DISALLOW_COPY_AND_ASSIGN(NetLog); |
| 304 }; | 226 }; |
| 305 | 227 |
| 306 // Helper that binds a Source to a NetLog, and exposes convenience methods to | |
| 307 // output log messages without needing to pass in the source. | |
| 308 class NET_EXPORT NetLogWithSource { | |
| 309 public: | |
| 310 NetLogWithSource() : net_log_(NULL) {} | |
| 311 ~NetLogWithSource(); | |
| 312 | |
| 313 // Add a log entry to the NetLog for the bound source. | |
| 314 void AddEntry(NetLogEventType type, NetLogEventPhase phase) const; | |
| 315 void AddEntry(NetLogEventType type, | |
| 316 NetLogEventPhase phase, | |
| 317 const NetLog::ParametersCallback& get_parameters) const; | |
| 318 | |
| 319 // Convenience methods that call AddEntry with a fixed "capture phase" | |
| 320 // (begin, end, or none). | |
| 321 void BeginEvent(NetLogEventType type) const; | |
| 322 void BeginEvent(NetLogEventType type, | |
| 323 const NetLog::ParametersCallback& get_parameters) const; | |
| 324 | |
| 325 void EndEvent(NetLogEventType type) const; | |
| 326 void EndEvent(NetLogEventType type, | |
| 327 const NetLog::ParametersCallback& get_parameters) const; | |
| 328 | |
| 329 void AddEvent(NetLogEventType type) const; | |
| 330 void AddEvent(NetLogEventType type, | |
| 331 const NetLog::ParametersCallback& get_parameters) const; | |
| 332 | |
| 333 // Just like AddEvent, except |net_error| is a net error code. A parameter | |
| 334 // called "net_error" with the indicated value will be recorded for the event. | |
| 335 // |net_error| must be negative, and not ERR_IO_PENDING, as it's not a true | |
| 336 // error. | |
| 337 void AddEventWithNetErrorCode(NetLogEventType event_type, | |
| 338 int net_error) const; | |
| 339 | |
| 340 // Just like EndEvent, except |net_error| is a net error code. If it's | |
| 341 // negative, a parameter called "net_error" with a value of |net_error| is | |
| 342 // associated with the event. Otherwise, the end event has no parameters. | |
| 343 // |net_error| must not be ERR_IO_PENDING, as it's not a true error. | |
| 344 void EndEventWithNetErrorCode(NetLogEventType event_type, | |
| 345 int net_error) const; | |
| 346 | |
| 347 // Logs a byte transfer event to the NetLog. Determines whether to log the | |
| 348 // received bytes or not based on the current logging level. | |
| 349 void AddByteTransferEvent(NetLogEventType event_type, | |
| 350 int byte_count, | |
| 351 const char* bytes) const; | |
| 352 | |
| 353 bool IsCapturing() const; | |
| 354 | |
| 355 // Helper to create a NetLogWithSource given a NetLog and a NetLogSourceType. | |
| 356 // Takes care of creating a unique source ID, and handles | |
| 357 // the case of NULL net_log. | |
| 358 static NetLogWithSource Make(NetLog* net_log, NetLogSourceType source_type); | |
| 359 | |
| 360 const NetLog::Source& source() const { return source_; } | |
| 361 NetLog* net_log() const { return net_log_; } | |
| 362 | |
| 363 private: | |
| 364 // TODO(eroman): Temporary until crbug.com/467797 is solved. | |
| 365 enum Liveness { | |
| 366 ALIVE = 0xCA11AB13, | |
| 367 DEAD = 0xDEADBEEF, | |
| 368 }; | |
| 369 | |
| 370 NetLogWithSource(const NetLog::Source& source, NetLog* net_log) | |
| 371 : source_(source), net_log_(net_log) {} | |
| 372 | |
| 373 // TODO(eroman): Temporary until crbug.com/467797 is solved. | |
| 374 void CrashIfInvalid() const; | |
| 375 | |
| 376 NetLog::Source source_; | |
| 377 NetLog* net_log_; | |
| 378 | |
| 379 // TODO(eroman): Temporary until crbug.com/467797 is solved. | |
| 380 Liveness liveness_ = ALIVE; | |
| 381 }; | |
| 382 | |
| 383 } // namespace net | 228 } // namespace net |
| 384 | 229 |
| 385 #endif // NET_LOG_NET_LOG_H_ | 230 #endif // NET_LOG_NET_LOG_H_ |
| OLD | NEW |