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 state (state_) of the logging into a
29 // chrome-net-export-log.json file.
31 // The following are the possible states
32 // a) Only Start is allowed (state_ == STATE_UNINITIALIZED).
33 // b) Only Stop is allowed (state_ == STATE_ALLOW_STOP).
34 // c) Either Send or Start is allowed (state_ == STATE_ALLOW_START_SEND).
36 // This is created/destroyed on the UI thread, but all other function calls
37 // occur on the FILE_USER_BLOCKING thread.
39 // This relies on the UI thread outlasting all other named threads for thread
41 class NetLogTempFile
{
43 // This enum lists the UI button commands it could receive.
45 DO_START
, // Call StartLog.
46 DO_STOP
, // Call StopLog.
49 virtual ~NetLogTempFile(); // Destructs a NetLogTempFile.
51 // Accepts the button command and executes it.
52 void ProcessCommand(Command command
);
54 // Returns true and the path to the temporary file. If there is no file to
55 // send, then it returns false. It also returns false when actively logging to
57 bool GetFilePath(base::FilePath
* path
);
59 // Creates a Value summary of the state of the NetLogTempFile. The caller is
60 // responsible for deleting the returned value.
61 base::DictionaryValue
* GetState();
64 // Constructs a NetLogTempFile. Only one instance is created in browser
66 explicit NetLogTempFile(ChromeNetLog
* chrome_net_log
);
68 // Returns path name to base::GetTempDir() directory. Returns false if
69 // base::GetTempDir() fails.
70 virtual bool GetNetExportLogDirectory(base::FilePath
* path
);
72 // Returns true if |log_path_| exists.
73 virtual bool NetExportLogExists();
76 friend class ChromeNetLog
;
77 friend class NetLogTempFileTest
;
79 // Allow tests to access our innards for testing purposes.
80 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest
, EnsureInitFailure
);
81 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest
, EnsureInitAllowStart
);
82 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest
, EnsureInitAllowStartOrSend
);
83 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest
, ProcessCommandDoStartAndStop
);
84 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest
, DoStartClearsFile
);
85 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest
, CheckAddEvent
);
87 // This enum lists the possible state NetLogTempFile could be in. It is used
88 // to enable/disable "Start", "Stop" and "Send" (email) UI actions.
91 STATE_ALLOW_START
, // Only DO_START Command is allowed.
92 STATE_ALLOW_STOP
, // Only DO_STOP Command is allowed.
93 STATE_ALLOW_START_SEND
, // Either DO_START or DO_SEND is allowed.
96 // Initializes the |state_| to either STATE_ALLOW_START (if there is no
97 // temporary file from earlier run) or STATE_ALLOW_START_SEND (if there is a
98 // temporary file from earlier run). Returns false if initialization of
102 // Start collecting NetLog data into chrome-net-export-log.json file in
103 // base::GetTempDir() directory. It is a no-op if we are already
104 // collecting data into a file.
107 // Stop collecting NetLog data into the temporary file. It is a no-op if we
108 // are not collecting data into a file.
111 // Updates |log_path_| with base::FilePath to |log_filename_| in the
112 // base::GetTempDir() directory. Returns false if base::GetTempDir()
114 bool GetNetExportLog();
116 // Helper function for unit tests.
117 State
state() const { return state_
; }
119 State state_
; // Current state of NetLogTempFile.
121 // Name of the file. It defaults to chrome-net-export-log.json, but can be
122 // overwritten by unit tests.
123 base::FilePath::StringType log_filename_
;
125 base::FilePath log_path_
; // base::FilePath to the temporary file.
127 // |net_log_logger_| watches the NetLog event stream, and sends all entries to
128 // the file created in StartNetLog().
129 scoped_ptr
<net::NetLogLogger
> net_log_logger_
;
131 // The |chrome_net_log_| is owned by the browser process, cached here to avoid
132 // using global (g_browser_process).
133 ChromeNetLog
* chrome_net_log_
;
135 DISALLOW_COPY_AND_ASSIGN(NetLogTempFile
);
138 #endif // CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_