Add a webstorePrivate API to show a permission prompt for delegated bundle installs
[chromium-blink-merge.git] / chrome / browser / net / net_log_temp_file.h
blob0ea28ab066a6ee4801aff207f0580b7382dc03eb
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.
5 #ifndef CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_
6 #define CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/files/file_path.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "net/log/net_log.h"
16 namespace base {
17 class DictionaryValue;
20 namespace net {
21 class WriteToFileNetLogObserver;
24 class ChromeNetLog;
26 // NetLogTempFile logs all the NetLog entries into a temporary file
27 // "chrome-net-export-log.json" created in base::GetTempDir() directory.
29 // 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.
32 // The following are the possible states
33 // a) Only Start is allowed (STATE_NOT_LOGGING, LOG_TYPE_NONE).
34 // b) Only Stop is allowed (STATE_LOGGING).
35 // c) Either Send or Start is allowed (STATE_NOT_LOGGING, anything but
36 // LOG_TYPE_NONE).
38 // This is created/destroyed on the UI thread, but all other function calls
39 // occur on the FILE_USER_BLOCKING thread.
41 // This relies on the UI thread outlasting all other named threads for thread
42 // safety.
43 class NetLogTempFile {
44 public:
45 // This enum lists the UI button commands it could receive.
46 enum Command {
47 DO_START_LOG_BYTES, // Call StartNetLog logging all bytes received.
48 DO_START, // Call StartNetLog.
49 DO_START_STRIP_PRIVATE_DATA, // Call StartNetLog stripping private data.
50 DO_STOP, // Call StopNetLog.
53 virtual ~NetLogTempFile(); // Destructs a NetLogTempFile.
55 // Accepts the button command and executes it.
56 void ProcessCommand(Command command);
58 // 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
60 // the file.
61 bool GetFilePath(base::FilePath* path);
63 // Creates a Value summary of the state of the NetLogTempFile. The caller is
64 // responsible for deleting the returned value.
65 base::DictionaryValue* GetState();
67 protected:
68 // Constructs a NetLogTempFile. Only one instance is created in browser
69 // process.
70 explicit NetLogTempFile(ChromeNetLog* chrome_net_log);
72 // Returns path name to base::GetTempDir() directory. Returns false if
73 // base::GetTempDir() fails.
74 virtual bool GetNetExportLogDirectory(base::FilePath* path);
76 // Returns true if |log_path_| exists.
77 virtual bool NetExportLogExists();
79 private:
80 friend class ChromeNetLog;
81 friend class NetLogTempFileTest;
83 // Allow tests to access our innards for testing purposes.
84 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitFailure);
85 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitAllowStart);
86 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitAllowStartOrSend);
87 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, ProcessCommandDoStartAndStop);
88 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, DoStartClearsFile);
89 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, CheckAddEvent);
91 // This enum lists the possible state NetLogTempFile could be in. It is used
92 // to enable/disable "Start", "Stop" and "Send" (email) UI actions.
93 enum State {
94 STATE_UNINITIALIZED,
95 // Not currently logging to file.
96 STATE_NOT_LOGGING,
97 // Currently logging to file.
98 STATE_LOGGING,
101 // The type of the current log file on disk.
102 enum LogType {
103 // There is no current log file.
104 LOG_TYPE_NONE,
105 // The file predates this session. May or may not have private data.
106 // TODO(davidben): This state is kind of silly.
107 LOG_TYPE_UNKNOWN,
108 // The log includes raw bytes.
109 LOG_TYPE_LOG_BYTES,
110 // The file includes all data.
111 LOG_TYPE_NORMAL,
112 // The file has credentials and cookies stripped.
113 LOG_TYPE_STRIP_PRIVATE_DATA,
116 // Returns the NetLog::CaptureMode corresponding to a LogType.
117 static net::NetLogCaptureMode GetCaptureModeForLogType(LogType log_type);
119 // Initializes the |state_| to STATE_NOT_LOGGING and |log_type_| to
120 // LOG_TYPE_NONE (if there is no temporary file from earlier run) or
121 // LOG_TYPE_UNKNOWN (if there is a temporary file from earlier run). Returns
122 // false if initialization of |log_path_| fails.
123 bool EnsureInit();
125 // Start collecting NetLog data into chrome-net-export-log.json file in
126 // base::GetTempDir() directory, using the specified capture mode. It is a
127 // no-op if we are already collecting data into a file, and |capture_mode| is
128 // ignored.
129 // TODO(mmenke): That's rather weird behavior, think about improving it.
130 void StartNetLog(LogType log_type);
132 // Stop collecting NetLog data into the temporary file. It is a no-op if we
133 // are not collecting data into a file.
134 void StopNetLog();
136 // Updates |log_path_| with base::FilePath to |log_filename_| in the
137 // base::GetTempDir() directory. Returns false if base::GetTempDir()
138 // fails.
139 bool GetNetExportLog();
141 // Helper function for unit tests.
142 State state() const { return state_; }
143 LogType log_type() const { return log_type_; }
145 State state_; // Current state of NetLogTempFile.
146 LogType log_type_; // Type of current log file on disk.
148 // Name of the file. It defaults to chrome-net-export-log.json, but can be
149 // overwritten by unit tests.
150 base::FilePath::StringType log_filename_;
152 base::FilePath log_path_; // base::FilePath to the temporary file.
154 // |write_to_file_observer_| watches the NetLog event stream, and
155 // sends all entries to the file created in StartNetLog().
156 scoped_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_;
158 // The |chrome_net_log_| is owned by the browser process, cached here to avoid
159 // using global (g_browser_process).
160 ChromeNetLog* chrome_net_log_;
162 DISALLOW_COPY_AND_ASSIGN(NetLogTempFile);
165 #endif // CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_