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 | |
| 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 virtual ~NetLogTempFile(); // Destructs a NetLogTempFile. | |
| 48 | |
| 49 // Accepts the button command and executes it. | |
| 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. | |
|
mmenke
2013/01/23 19:01:27
nit: Think it's worth mentioning that it also ret
ramant (doing other things)
2013/01/24 03:23:55
Done.
| |
| 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 the FilePath to chrome-net-export-log.json file in | |
| 66 // file_util::GetTempDir() directory in |path| argument. Returns false if | |
| 67 // ".../chrome-net-export-log.json" file doesn't exist or if | |
| 68 // file_util::GetTempDir() fails. | |
| 69 virtual bool GetNetExportLog(FilePath* path); | |
| 70 | |
| 71 private: | |
| 72 friend class ChromeNetLog; | |
| 73 friend class CustomNetLogTempFile; | |
|
mmenke
2013/01/23 19:01:27
Can we just make anything thing this class accesse
ramant (doing other things)
2013/01/24 03:23:55
Made NetLogTempFile and NetExportLogExists so that
| |
| 74 friend class NetLogTempFileTest; | |
| 75 | |
| 76 // Allow tests to access our innards for testing purposes. | |
| 77 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, Init); | |
| 78 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, InitAllowStart); | |
| 79 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, InitAllowStartOrSend); | |
| 80 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, ProcessCommandDoStart); | |
| 81 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, ProcessCommandDoStop); | |
| 82 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, DoStartClearsFile); | |
| 83 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, GetFilePath); | |
| 84 | |
| 85 // This enum lists the possible state NetLogTempFile could be in. It is used | |
| 86 // to enable/disable "Start", "Stop" and "Send" (email) UI actions. | |
| 87 enum State { | |
| 88 STATE_UNINITIALIZED, | |
| 89 STATE_ALLOW_START, // Only DO_START Command is allowed. | |
| 90 STATE_ALLOW_STOP, // Only DO_STOP Command is allowed. | |
| 91 STATE_ALLOW_START_SEND, // Either DO_START or DO_SEND is allowed. | |
| 92 }; | |
| 93 | |
| 94 // Initializes the |state_| to either STATE_ALLOW_START (if there is no | |
| 95 // temporary file from earlier run) or STATE_ALLOW_START_SEND (if there is a | |
| 96 // temporary file from earlier run). | |
| 97 void Init(); | |
| 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 // Helper function for unit tests. | |
| 109 State state() const { return state_; } | |
| 110 | |
| 111 FilePath log_path() const { return log_path_; } | |
|
mmenke
2013/01/23 19:01:27
Think we can get rid of this, and not depend on it
ramant (doing other things)
2013/01/24 03:23:55
Done.
| |
| 112 | |
| 113 State state_; // Current state of NetLogTempFile. | |
| 114 | |
| 115 // Name of the file. It defaults to chrome-net-export-log.json, but can be | |
| 116 // overwritten by unit tests. | |
| 117 FilePath::StringType log_filename_; | |
| 118 | |
| 119 FilePath log_path_; // FilePath to the temporary file. | |
| 120 | |
| 121 // |net_log_logger_| watches the NetLog event stream, and sends all entries to | |
| 122 // the file created in StartNetLog(). | |
| 123 scoped_ptr<NetLogLogger> net_log_logger_; | |
| 124 | |
| 125 // The |chrome_net_log_| is owned by the browser process, cached here to avoid | |
| 126 // using global (g_browser_process). | |
| 127 ChromeNetLog* chrome_net_log_; | |
| 128 | |
| 129 DISALLOW_COPY_AND_ASSIGN(NetLogTempFile); | |
| 130 }; | |
| 131 | |
| 132 #endif // CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_ | |
| OLD | NEW |