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 CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_
6 #define CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_
10 #include "base/basictypes.h"
11 #include "base/files/file_path.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
16 class DictionaryValue
;
25 // NetLogTempFile logs all the NetLog entries into a temporary file
26 // "chrome-net-export-log.json" created in base::GetTempDir() directory.
28 // NetLogTempFile maintains the current logging state (state_) and log file type
29 // (log_type_) of the logging into a chrome-net-export-log.json file.
31 // The following are the possible states
32 // a) Only Start is allowed (STATE_NOT_LOGGING, LOG_TYPE_NONE).
33 // b) Only Stop is allowed (STATE_LOGGING).
34 // c) Either Send or Start is allowed (STATE_NOT_LOGGING, anything but
37 // This is created/destroyed on the UI thread, but all other function calls
38 // occur on the FILE_USER_BLOCKING thread.
40 // This relies on the UI thread outlasting all other named threads for thread
42 class NetLogTempFile
{
44 // This enum lists the UI button commands it could receive.
46 DO_START
, // Call StartNetLog.
47 DO_START_STRIP_PRIVATE_DATA
, // Call StartNetLog stripping private data.
48 DO_STOP
, // Call StopNetLog.
51 virtual ~NetLogTempFile(); // Destructs a NetLogTempFile.
53 // Accepts the button command and executes it.
54 void ProcessCommand(Command command
);
56 // Returns true and the path to the temporary file. If there is no file to
57 // send, then it returns false. It also returns false when actively logging to
59 bool GetFilePath(base::FilePath
* path
);
61 // Creates a Value summary of the state of the NetLogTempFile. The caller is
62 // responsible for deleting the returned value.
63 base::DictionaryValue
* GetState();
66 // Constructs a NetLogTempFile. Only one instance is created in browser
68 explicit NetLogTempFile(ChromeNetLog
* chrome_net_log
);
70 // Returns path name to base::GetTempDir() directory. Returns false if
71 // base::GetTempDir() fails.
72 virtual bool GetNetExportLogDirectory(base::FilePath
* path
);
74 // Returns true if |log_path_| exists.
75 virtual bool NetExportLogExists();
78 friend class ChromeNetLog
;
79 friend class NetLogTempFileTest
;
81 // Allow tests to access our innards for testing purposes.
82 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest
, EnsureInitFailure
);
83 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest
, EnsureInitAllowStart
);
84 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest
, EnsureInitAllowStartOrSend
);
85 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest
, ProcessCommandDoStartAndStop
);
86 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest
, DoStartClearsFile
);
87 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest
, CheckAddEvent
);
89 // This enum lists the possible state NetLogTempFile could be in. It is used
90 // to enable/disable "Start", "Stop" and "Send" (email) UI actions.
93 // Not currently logging to file.
95 // Currently logging to file.
99 // The type of the current log file on disk.
101 // There is no current log file.
103 // The file predates this session. May or may not have private data.
104 // TODO(davidben): This state is kind of silly.
106 // The file has credentials and cookies stripped.
107 LOG_TYPE_STRIP_PRIVATE_DATA
,
108 // The file includes all data.
112 // Initializes the |state_| to STATE_NOT_LOGGING and |log_type_| to
113 // LOG_TYPE_NONE (if there is no temporary file from earlier run) or
114 // LOG_TYPE_UNKNOWN (if there is a temporary file from earlier run). Returns
115 // false if initialization of |log_path_| fails.
118 // Start collecting NetLog data into chrome-net-export-log.json file in
119 // base::GetTempDir() directory. If |strip_private_data| is true, do not log
120 // cookies and credentials. It is a no-op if we are already collecting data
122 void StartNetLog(bool strip_private_data
);
124 // Stop collecting NetLog data into the temporary file. It is a no-op if we
125 // are not collecting data into a file.
128 // Updates |log_path_| with base::FilePath to |log_filename_| in the
129 // base::GetTempDir() directory. Returns false if base::GetTempDir()
131 bool GetNetExportLog();
133 // Helper function for unit tests.
134 State
state() const { return state_
; }
135 LogType
log_type() const { return log_type_
; }
137 State state_
; // Current state of NetLogTempFile.
138 LogType log_type_
; // Type of current log file on disk.
140 // Name of the file. It defaults to chrome-net-export-log.json, but can be
141 // overwritten by unit tests.
142 base::FilePath::StringType log_filename_
;
144 base::FilePath log_path_
; // base::FilePath to the temporary file.
146 // |net_log_logger_| watches the NetLog event stream, and sends all entries to
147 // the file created in StartNetLog().
148 scoped_ptr
<net::NetLogLogger
> net_log_logger_
;
150 // The |chrome_net_log_| is owned by the browser process, cached here to avoid
151 // using global (g_browser_process).
152 ChromeNetLog
* chrome_net_log_
;
154 DISALLOW_COPY_AND_ASSIGN(NetLogTempFile
);
157 #endif // CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_