OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_ | |
6 #define CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/file_path.h" | |
12 #include "base/gtest_prod_util.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 | |
16 namespace base { | |
17 class DictionaryValue; | |
18 } | |
19 | |
20 class NetLogLogger; | |
21 class ChromeNetLog; | |
22 | |
23 // NetLogTempFile logs all the NetLog entries into a temporary file | |
24 // "chrome-net-export-log.json" created in file_util::GetTempDir() directory. | |
25 // | |
26 // NetLogTempFile maintains the current state (state_) of the logging into a | |
27 // chrome-net-export-log.json file. | |
28 // | |
29 // The following are the possible states | |
30 // a) Only Start is allowed (state_ == STATE_UNINITIALIZED). | |
31 // b) Only Stop is allowed (state_ == STATE_ALLOW_STOP). | |
32 // c) Either Send or Start is allowed (state_ == STATE_ALLOW_START_SEND). | |
33 // | |
34 // This is created/destroyed on the UI thread, but all other function calls | |
35 // occur on the FILE_USER_BLOCKING thread. | |
36 // | |
37 // This relies on the UI thread outlasting all other named threads for thread | |
38 // safety. | |
39 class NetLogTempFile { | |
40 public: | |
41 // This enum lists the UI button commands it could receive. | |
42 enum Command { | |
43 DO_START, // Call StartLog. | |
44 DO_STOP, // Call StopLog. | |
45 }; | |
46 | |
47 // Constructs a NetLogTempFile. Only one instance is created in browser | |
48 // process. | |
49 explicit NetLogTempFile(ChromeNetLog* chrome_net_log); | |
mmenke
2013/01/24 03:55:34
This can still be protected, can't it?
ramant (doing other things)
2013/01/24 21:56:49
Done.
| |
50 | |
51 virtual ~NetLogTempFile(); // Destructs a NetLogTempFile. | |
52 | |
53 // Accepts the button command and executes it. | |
54 void ProcessCommand(Command command); | |
55 | |
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 | |
58 // the file. | |
59 bool GetFilePath(FilePath* path); | |
60 | |
61 // Returns true if |log_path_| exists. | |
62 virtual bool NetExportLogExists(); | |
mmenke
2013/01/24 03:55:34
This can be protected, too - if a subclass overrid
ramant (doing other things)
2013/01/24 21:56:49
Done.
| |
63 | |
64 // Creates a Value summary of the state of the NetLogTempFile. The caller is | |
65 // responsible for deleting the returned value. | |
66 base::DictionaryValue* GetState(); | |
67 | |
68 private: | |
69 friend class ChromeNetLog; | |
70 friend class NetLogTempFileTest; | |
71 | |
72 // Allow tests to access our innards for testing purposes. | |
73 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, Init); | |
74 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, InitAllowStart); | |
75 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, InitAllowStartOrSend); | |
76 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, ProcessCommandDoStartAndStop); | |
77 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, DoStartClearsFile); | |
78 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, CheckAddEvent); | |
79 | |
80 // This enum lists the possible state NetLogTempFile could be in. It is used | |
81 // to enable/disable "Start", "Stop" and "Send" (email) UI actions. | |
82 enum State { | |
83 STATE_UNINITIALIZED, | |
84 STATE_ALLOW_START, // Only DO_START Command is allowed. | |
85 STATE_ALLOW_STOP, // Only DO_STOP Command is allowed. | |
86 STATE_ALLOW_START_SEND, // Either DO_START or DO_SEND is allowed. | |
87 }; | |
88 | |
89 // Initializes the |state_| to either STATE_ALLOW_START (if there is no | |
90 // temporary file from earlier run) or STATE_ALLOW_START_SEND (if there is a | |
91 // temporary file from earlier run). | |
92 void Init(); | |
93 | |
94 // Start collecting NetLog data into chrome-net-export-log.json file in | |
95 // file_util::GetTempDir() directory. It is a no-op if we are already | |
96 // collecting data into a file. | |
97 void StartNetLog(); | |
98 | |
99 // Stop collecting NetLog data into the temporary file. It is a no-op if we | |
100 // are not collecting data into a file. | |
101 void StopNetLog(); | |
102 | |
103 // Updates |log_path_| with FilePath to |log_filename_| in the | |
104 // file_util::GetTempDir() directory. Returns false if file_util::GetTempDir() | |
105 // fails. | |
106 bool GetNetExportLog(); | |
107 | |
108 // Helper function for unit tests. | |
109 State state() const { return state_; } | |
110 | |
111 State state_; // Current state of NetLogTempFile. | |
112 | |
113 // Name of the file. It defaults to chrome-net-export-log.json, but can be | |
114 // overwritten by unit tests. | |
115 FilePath::StringType log_filename_; | |
116 | |
117 FilePath log_path_; // FilePath to the temporary file. | |
118 | |
119 // |net_log_logger_| watches the NetLog event stream, and sends all entries to | |
120 // the file created in StartNetLog(). | |
121 scoped_ptr<NetLogLogger> net_log_logger_; | |
122 | |
123 // The |chrome_net_log_| is owned by the browser process, cached here to avoid | |
124 // using global (g_browser_process). | |
125 ChromeNetLog* chrome_net_log_; | |
126 | |
127 DISALLOW_COPY_AND_ASSIGN(NetLogTempFile); | |
128 }; | |
129 | |
130 #endif // CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_ | |
OLD | NEW |