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/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 created in | |
| 24 // "chrome-net-export" folder 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 ~NetLogTempFile(); // Destructs a NetLogTempFile. | |
| 48 | |
| 49 // Accepts the button command and executes them. | |
|
mmenke
2013/01/18 15:58:00
nit: either "commands and executes them" or "comm
ramant (doing other things)
2013/01/18 23:22:33
Done.
| |
| 50 void ProcessCommand(Command command); | |
| 51 | |
| 52 // Returns true and the path to the temporary file. If there is no file to | |
| 53 // send, then it returns false. | |
| 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 private: | |
| 61 friend class ChromeNetLog; | |
| 62 friend class NetLogTempFileTest; | |
| 63 | |
| 64 // Allow tests to access our innards for testing purposes. | |
| 65 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, Init); | |
| 66 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, InitAllowStart); | |
| 67 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, InitAllowStartOrSend); | |
| 68 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, ProcessCommandDoStart); | |
| 69 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, ProcessCommandDoStop); | |
| 70 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, GetFilePath); | |
| 71 | |
| 72 // This enum lists the possible state NetLogTempFile could be in. It is used | |
| 73 // to enable/disable "Start", "Stop" and "Send" (email) UI actions. | |
| 74 enum State { | |
| 75 STATE_UNINITIALIZED, | |
| 76 STATE_ALLOW_START, // Only DO_START Command is allowed. | |
| 77 STATE_ALLOW_STOP, // Only DO_STOP Command is allowed. | |
| 78 STATE_ALLOW_START_SEND, // Either DO_START or DO_SEND is allowed. | |
| 79 }; | |
| 80 | |
| 81 // Constructs a NetLogTempFile. Only one instance is created in | |
| 82 // browser process. | |
| 83 explicit NetLogTempFile(ChromeNetLog* chrome_net_log); | |
| 84 | |
| 85 // Initializes the |state_| to either STATE_ALLOW_START (if there is no | |
| 86 // temporary file from earlier run) or STATE_ALLOW_START_SEND (if there is a | |
| 87 // temporary file from earlier run). | |
| 88 void Init(); | |
| 89 | |
| 90 // Start collecting NetLog data into chrome-net-export/log.json file in | |
| 91 // file_util::GetTempDir() directory. It deletes all the temporary files that | |
| 92 // are in that directory before creating a new temporary file. It is a no-op | |
| 93 // if we are already collecting data into a file. | |
| 94 void StartNetLog(); | |
| 95 | |
| 96 // Stop collecting NetLog data into the temporary file. It is a no-op if we | |
| 97 // are not collecting data into a file. | |
| 98 void StopNetLog(); | |
| 99 | |
| 100 // Helper function for unit tests. | |
| 101 State state() const { return state_; } | |
| 102 | |
| 103 FilePath log_path() const { return log_path_; } | |
| 104 | |
| 105 State state_; // Current state of NetLogTempFile. | |
| 106 | |
| 107 // Name of the temporary directory that is to be created in | |
| 108 // file_util::GetTempDir(). It defaults to "chrome-net-export", but can be | |
| 109 // overwritten by unit tests. | |
| 110 FilePath::StringType chrome_net_export_; | |
| 111 | |
| 112 // Name of the file. It defaults to "log.json", but can be overwritten by | |
| 113 // unit tests. | |
| 114 FilePath::StringType log_filename_; | |
| 115 | |
| 116 FilePath log_path_; // FilePath to the temporary file. | |
| 117 | |
| 118 // |net_log_logger_| watches the NetLog event stream, and sends all entries to | |
| 119 // the file created in StartNetLog(). | |
| 120 scoped_ptr<NetLogLogger> net_log_logger_; | |
| 121 | |
| 122 // The |chrome_net_log_| is owned by the browser process, cached here to avoid | |
| 123 // using global (g_browser_process). | |
| 124 ChromeNetLog* chrome_net_log_; | |
| 125 | |
| 126 DISALLOW_COPY_AND_ASSIGN(NetLogTempFile); | |
| 127 }; | |
| 128 | |
| 129 #endif // CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_ | |
| OLD | NEW |