Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / net_log / net_log_temp_file.h
blobbdc596edafa097cb9a6946099681da45b58f42f4
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_NET_LOG_NET_LOG_TEMP_FILE_H_
6 #define COMPONENTS_NET_LOG_NET_LOG_TEMP_FILE_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/command_line.h"
12 #include "base/files/file_path.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/threading/thread_checker.h"
16 #include "net/log/net_log.h"
18 namespace base {
19 class DictionaryValue;
22 namespace net {
23 class WriteToFileNetLogObserver;
26 namespace net_log {
28 class ChromeNetLog;
30 // NetLogTempFile logs all the NetLog entries into a temporary file
31 // "chrome-net-export-log.json" created in base::GetTempDir() directory.
33 // NetLogTempFile maintains the current logging state (state_) and log file type
34 // (log_type_) of the logging into a chrome-net-export-log.json file.
36 // The following are the possible states
37 // a) Only Start is allowed (STATE_NOT_LOGGING, LOG_TYPE_NONE).
38 // b) Only Stop is allowed (STATE_LOGGING).
39 // c) Either Send or Start is allowed (STATE_NOT_LOGGING, anything but
40 // LOG_TYPE_NONE).
42 // This is created/destroyed on the main thread, but all other function calls
43 // occur on a background thread.
45 // This relies on the UI thread outlasting all other threads for thread safety.
46 class NetLogTempFile {
47 public:
48 // This enum lists the UI button commands it could receive.
49 enum Command {
50 DO_START_LOG_BYTES, // Call StartNetLog logging all bytes received.
51 DO_START, // Call StartNetLog.
52 DO_START_STRIP_PRIVATE_DATA, // Call StartNetLog stripping private data.
53 DO_STOP, // Call StopNetLog.
56 virtual ~NetLogTempFile();
58 // Accepts the button command and executes it.
59 void ProcessCommand(Command command);
61 // Returns true and the path to the temporary file. If there is no file to
62 // send, then it returns false. It also returns false when actively logging to
63 // the file.
64 bool GetFilePath(base::FilePath* path);
66 // Creates a Value summary of the state of the NetLogTempFile. The caller is
67 // responsible for deleting the returned value.
68 base::DictionaryValue* GetState();
70 protected:
71 // Constructs a NetLogTempFile. Only one instance is created in browser
72 // process.
73 NetLogTempFile(ChromeNetLog* chrome_net_log,
74 const base::CommandLine::StringType& command_line_string,
75 const std::string& channel_string);
77 // Returns path name to base::GetTempDir() directory. Returns false if
78 // base::GetTempDir() fails.
79 virtual bool GetNetExportLogBaseDirectory(base::FilePath* path) const;
81 private:
82 friend class ChromeNetLog;
83 friend class NetLogTempFileTest;
85 // Allow tests to access our innards for testing purposes.
86 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitFailure);
87 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitAllowStart);
88 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitAllowStartOrSend);
89 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, ProcessCommandDoStartAndStop);
90 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, DoStartClearsFile);
91 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, CheckAddEvent);
93 // This enum lists the possible state NetLogTempFile could be in. It is used
94 // to enable/disable "Start", "Stop" and "Send" (email) UI actions.
95 enum State {
96 STATE_UNINITIALIZED,
97 // Not currently logging to file.
98 STATE_NOT_LOGGING,
99 // Currently logging to file.
100 STATE_LOGGING,
103 // The type of the current log file on disk.
104 enum LogType {
105 // There is no current log file.
106 LOG_TYPE_NONE,
107 // The file predates this session. May or may not have private data.
108 // TODO(davidben): This state is kind of silly.
109 LOG_TYPE_UNKNOWN,
110 // The log includes raw bytes.
111 LOG_TYPE_LOG_BYTES,
112 // The file includes all data.
113 LOG_TYPE_NORMAL,
114 // The file has credentials and cookies stripped.
115 LOG_TYPE_STRIP_PRIVATE_DATA,
118 // Returns the NetLog::CaptureMode corresponding to a LogType.
119 static net::NetLogCaptureMode GetCaptureModeForLogType(LogType log_type);
121 // Initializes the |state_| to STATE_NOT_LOGGING and |log_type_| to
122 // LOG_TYPE_NONE (if there is no temporary file from earlier run) or
123 // LOG_TYPE_UNKNOWN (if there is a temporary file from earlier run). Returns
124 // false if initialization of |log_path_| fails.
125 bool EnsureInit();
127 // Start collecting NetLog data into chrome-net-export-log.json file in
128 // base::GetTempDir() directory, using the specified capture mode. It is a
129 // no-op if we are already collecting data into a file, and |capture_mode| is
130 // ignored.
131 // TODO(mmenke): That's rather weird behavior, think about improving it.
132 void StartNetLog(LogType log_type);
134 // Stop collecting NetLog data into the temporary file. It is a no-op if we
135 // are not collecting data into a file.
136 void StopNetLog();
138 // Updates |log_path_| to be the base::FilePath to use for log files, which
139 // will be inside the base::GetTempDir() directory. Returns false if
140 // base::GetTempDir() fails, or unable to create a subdirectory for logging
141 // withinh that directory.
142 bool SetUpNetExportLogPath();
144 // Returns true if a file exists at |log_path_|.
145 bool NetExportLogExists() const;
147 base::ThreadChecker thread_checker_;
149 // Helper function for unit tests.
150 State state() const { return state_; }
151 LogType log_type() const { return log_type_; }
153 State state_; // Current state of NetLogTempFile.
154 LogType log_type_; // Type of current log file on disk.
156 base::FilePath log_path_; // base::FilePath to the temporary file.
158 // |write_to_file_observer_| watches the NetLog event stream, and
159 // sends all entries to the file created in StartNetLog().
160 scoped_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_;
162 // The |chrome_net_log_| is owned by the browser process, cached here to avoid
163 // using global (g_browser_process).
164 ChromeNetLog* chrome_net_log_;
166 const base::CommandLine::StringType command_line_string_;
167 const std::string channel_string_;
169 DISALLOW_COPY_AND_ASSIGN(NetLogTempFile);
172 } // namespace net_log
174 #endif // COMPONENTS_NET_LOG_NET_LOG_TEMP_FILE_H_