| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_ | 5 #ifndef COMPONENTS_NET_LOG_NET_LOG_TEMP_FILE_H_ |
| 6 #define CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_ | 6 #define COMPONENTS_NET_LOG_NET_LOG_TEMP_FILE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/threading/thread_checker.h" |
| 14 #include "net/log/net_log.h" | 15 #include "net/log/net_log.h" |
| 15 | 16 |
| 16 namespace base { | 17 namespace base { |
| 17 class DictionaryValue; | 18 class DictionaryValue; |
| 18 } | 19 } |
| 19 | 20 |
| 20 namespace net { | 21 namespace net { |
| 21 class WriteToFileNetLogObserver; | 22 class WriteToFileNetLogObserver; |
| 22 } | 23 } |
| 23 | 24 |
| 25 namespace net_log { |
| 26 |
| 24 class ChromeNetLog; | 27 class ChromeNetLog; |
| 25 | 28 |
| 26 // NetLogTempFile logs all the NetLog entries into a temporary file | 29 // NetLogTempFile logs all the NetLog entries into a temporary file |
| 27 // "chrome-net-export-log.json" created in base::GetTempDir() directory. | 30 // "chrome-net-export-log.json" created in base::GetTempDir() directory. |
| 28 // | 31 // |
| 29 // NetLogTempFile maintains the current logging state (state_) and log file type | 32 // NetLogTempFile maintains the current logging state (state_) and log file type |
| 30 // (log_type_) of the logging into a chrome-net-export-log.json file. | 33 // (log_type_) of the logging into a chrome-net-export-log.json file. |
| 31 // | 34 // |
| 32 // The following are the possible states | 35 // The following are the possible states |
| 33 // a) Only Start is allowed (STATE_NOT_LOGGING, LOG_TYPE_NONE). | 36 // a) Only Start is allowed (STATE_NOT_LOGGING, LOG_TYPE_NONE). |
| 34 // b) Only Stop is allowed (STATE_LOGGING). | 37 // b) Only Stop is allowed (STATE_LOGGING). |
| 35 // c) Either Send or Start is allowed (STATE_NOT_LOGGING, anything but | 38 // c) Either Send or Start is allowed (STATE_NOT_LOGGING, anything but |
| 36 // LOG_TYPE_NONE). | 39 // LOG_TYPE_NONE). |
| 37 // | 40 // |
| 38 // This is created/destroyed on the UI thread, but all other function calls | 41 // This is created/destroyed on the UI thread, but all other function calls |
| 39 // occur on the FILE_USER_BLOCKING thread. | 42 // occur on the FILE_USER_BLOCKING thread. |
| 40 // | 43 // |
| 41 // This relies on the UI thread outlasting all other named threads for thread | 44 // This relies on the UI thread outlasting all other named threads for thread |
| 42 // safety. | 45 // safety. |
| 43 class NetLogTempFile { | 46 class NetLogTempFile { |
| 44 public: | 47 public: |
| 45 // This enum lists the UI button commands it could receive. | 48 // This enum lists the UI button commands it could receive. |
| 46 enum Command { | 49 enum Command { |
| 47 DO_START_LOG_BYTES, // Call StartNetLog logging all bytes received. | 50 DO_START_LOG_BYTES, // Call StartNetLog logging all bytes received. |
| 48 DO_START, // Call StartNetLog. | 51 DO_START, // Call StartNetLog. |
| 49 DO_START_STRIP_PRIVATE_DATA, // Call StartNetLog stripping private data. | 52 DO_START_STRIP_PRIVATE_DATA, // Call StartNetLog stripping private data. |
| 50 DO_STOP, // Call StopNetLog. | 53 DO_STOP, // Call StopNetLog. |
| 51 }; | 54 }; |
| 52 | 55 |
| 53 virtual ~NetLogTempFile(); | 56 virtual ~NetLogTempFile(); |
| 54 | 57 |
| 55 // Accepts the button command and executes it. | 58 // Accepts the button command and executes it. |
| 56 void ProcessCommand(Command command); | 59 void ProcessCommand(Command command); |
| 57 | 60 |
| 58 // Returns true and the path to the temporary file. If there is no file to | 61 // Returns true and the path to the temporary file. If there is no file to |
| 59 // send, then it returns false. It also returns false when actively logging to | 62 // send, then it returns false. It also returns false when actively logging to |
| 60 // the file. | 63 // the file. |
| 61 bool GetFilePath(base::FilePath* path); | 64 bool GetFilePath(base::FilePath* path); |
| 62 | 65 |
| 63 // Creates a Value summary of the state of the NetLogTempFile. The caller is | 66 // Creates a Value summary of the state of the NetLogTempFile. The caller is |
| 64 // responsible for deleting the returned value. | 67 // responsible for deleting the returned value. |
| 65 base::DictionaryValue* GetState(); | 68 base::DictionaryValue* GetState(); |
| 66 | 69 |
| 67 protected: | 70 protected: |
| 68 // Constructs a NetLogTempFile. Only one instance is created in browser | 71 // Constructs a NetLogTempFile. Only one instance is created in browser |
| 69 // process. | 72 // process. |
| 70 explicit NetLogTempFile(ChromeNetLog* chrome_net_log); | 73 NetLogTempFile(ChromeNetLog* chrome_net_log, |
| 74 const std::string& channel_string); |
| 71 | 75 |
| 72 // Returns path name to base::GetTempDir() directory. Returns false if | 76 // Returns path name to base::GetTempDir() directory. Returns false if |
| 73 // base::GetTempDir() fails. | 77 // base::GetTempDir() fails. |
| 74 virtual bool GetNetExportLogBaseDirectory( | 78 virtual bool GetNetExportLogBaseDirectory(base::FilePath* path) const; |
| 75 base::FilePath* path) const; | |
| 76 | 79 |
| 77 private: | 80 private: |
| 78 friend class ChromeNetLog; | 81 friend class ChromeNetLog; |
| 79 friend class NetLogTempFileTest; | 82 friend class NetLogTempFileTest; |
| 80 | 83 |
| 81 // Allow tests to access our innards for testing purposes. | 84 // Allow tests to access our innards for testing purposes. |
| 82 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitFailure); | 85 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitFailure); |
| 83 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitAllowStart); | 86 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitAllowStart); |
| 84 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitAllowStartOrSend); | 87 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitAllowStartOrSend); |
| 85 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, ProcessCommandDoStartAndStop); | 88 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, ProcessCommandDoStartAndStop); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 | 136 |
| 134 // Updates |log_path_| to be the base::FilePath to use for log files, which | 137 // Updates |log_path_| to be the base::FilePath to use for log files, which |
| 135 // will be inside the base::GetTempDir() directory. Returns false if | 138 // will be inside the base::GetTempDir() directory. Returns false if |
| 136 // base::GetTempDir() fails, or unable to create a subdirectory for logging | 139 // base::GetTempDir() fails, or unable to create a subdirectory for logging |
| 137 // withinh that directory. | 140 // withinh that directory. |
| 138 bool SetUpNetExportLogPath(); | 141 bool SetUpNetExportLogPath(); |
| 139 | 142 |
| 140 // Returns true if a file exists at |log_path_|. | 143 // Returns true if a file exists at |log_path_|. |
| 141 bool NetExportLogExists() const; | 144 bool NetExportLogExists() const; |
| 142 | 145 |
| 146 base::ThreadChecker thread_checker_; |
| 147 |
| 143 // Helper function for unit tests. | 148 // Helper function for unit tests. |
| 144 State state() const { return state_; } | 149 State state() const { return state_; } |
| 145 LogType log_type() const { return log_type_; } | 150 LogType log_type() const { return log_type_; } |
| 146 | 151 |
| 147 State state_; // Current state of NetLogTempFile. | 152 State state_; // Current state of NetLogTempFile. |
| 148 LogType log_type_; // Type of current log file on disk. | 153 LogType log_type_; // Type of current log file on disk. |
| 149 | 154 |
| 150 base::FilePath log_path_; // base::FilePath to the temporary file. | 155 base::FilePath log_path_; // base::FilePath to the temporary file. |
| 151 | 156 |
| 152 // |write_to_file_observer_| watches the NetLog event stream, and | 157 // |write_to_file_observer_| watches the NetLog event stream, and |
| 153 // sends all entries to the file created in StartNetLog(). | 158 // sends all entries to the file created in StartNetLog(). |
| 154 scoped_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_; | 159 scoped_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_; |
| 155 | 160 |
| 156 // The |chrome_net_log_| is owned by the browser process, cached here to avoid | 161 // The |chrome_net_log_| is owned by the browser process, cached here to avoid |
| 157 // using global (g_browser_process). | 162 // using global (g_browser_process). |
| 158 ChromeNetLog* chrome_net_log_; | 163 ChromeNetLog* chrome_net_log_; |
| 159 | 164 |
| 165 std::string channel_string_; |
| 166 |
| 160 DISALLOW_COPY_AND_ASSIGN(NetLogTempFile); | 167 DISALLOW_COPY_AND_ASSIGN(NetLogTempFile); |
| 161 }; | 168 }; |
| 162 | 169 |
| 163 #endif // CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_ | 170 } // namespace net_log |
| 171 |
| 172 #endif // COMPONENTS_NET_LOG_NET_LOG_TEMP_FILE_H_ |
| OLD | NEW |