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