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_
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"
19 class DictionaryValue
;
23 class WriteToFileNetLogObserver
;
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
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
{
48 // This enum lists the UI button commands it could receive.
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
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();
71 // Constructs a NetLogTempFile. Only one instance is created in browser
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;
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.
97 // Not currently logging to file.
99 // Currently logging to file.
103 // The type of the current log file on disk.
105 // There is no current log file.
107 // The file predates this session. May or may not have private data.
108 // TODO(davidben): This state is kind of silly.
110 // The log includes raw bytes.
112 // The file includes all data.
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.
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
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.
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_