Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / net / net_log_temp_file.h
blob2ab197fe59d213b0f6b72e018b331f7218b261d6
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();
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 GetNetExportLogBaseDirectory(
75 base::FilePath* path) const;
77 private:
78 friend class ChromeNetLog;
79 friend class NetLogTempFileTest;
81 // Allow tests to access our innards for testing purposes.
82 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitFailure);
83 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitAllowStart);
84 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitAllowStartOrSend);
85 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, ProcessCommandDoStartAndStop);
86 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, DoStartClearsFile);
87 FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, CheckAddEvent);
89 // This enum lists the possible state NetLogTempFile could be in. It is used
90 // to enable/disable "Start", "Stop" and "Send" (email) UI actions.
91 enum State {
92 STATE_UNINITIALIZED,
93 // Not currently logging to file.
94 STATE_NOT_LOGGING,
95 // Currently logging to file.
96 STATE_LOGGING,
99 // The type of the current log file on disk.
100 enum LogType {
101 // There is no current log file.
102 LOG_TYPE_NONE,
103 // The file predates this session. May or may not have private data.
104 // TODO(davidben): This state is kind of silly.
105 LOG_TYPE_UNKNOWN,
106 // The log includes raw bytes.
107 LOG_TYPE_LOG_BYTES,
108 // The file includes all data.
109 LOG_TYPE_NORMAL,
110 // The file has credentials and cookies stripped.
111 LOG_TYPE_STRIP_PRIVATE_DATA,
114 // Returns the NetLog::CaptureMode corresponding to a LogType.
115 static net::NetLogCaptureMode GetCaptureModeForLogType(LogType log_type);
117 // Initializes the |state_| to STATE_NOT_LOGGING and |log_type_| to
118 // LOG_TYPE_NONE (if there is no temporary file from earlier run) or
119 // LOG_TYPE_UNKNOWN (if there is a temporary file from earlier run). Returns
120 // false if initialization of |log_path_| fails.
121 bool EnsureInit();
123 // Start collecting NetLog data into chrome-net-export-log.json file in
124 // base::GetTempDir() directory, using the specified capture mode. It is a
125 // no-op if we are already collecting data into a file, and |capture_mode| is
126 // ignored.
127 // TODO(mmenke): That's rather weird behavior, think about improving it.
128 void StartNetLog(LogType log_type);
130 // Stop collecting NetLog data into the temporary file. It is a no-op if we
131 // are not collecting data into a file.
132 void StopNetLog();
134 // 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
136 // base::GetTempDir() fails, or unable to create a subdirectory for logging
137 // withinh that directory.
138 bool SetUpNetExportLogPath();
140 // Returns true if a file exists at |log_path_|.
141 bool NetExportLogExists() const;
143 // Helper function for unit tests.
144 State state() const { return state_; }
145 LogType log_type() const { return log_type_; }
147 State state_; // Current state of NetLogTempFile.
148 LogType log_type_; // Type of current log file on disk.
150 base::FilePath log_path_; // base::FilePath to the temporary file.
152 // |write_to_file_observer_| watches the NetLog event stream, and
153 // sends all entries to the file created in StartNetLog().
154 scoped_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_;
156 // The |chrome_net_log_| is owned by the browser process, cached here to avoid
157 // using global (g_browser_process).
158 ChromeNetLog* chrome_net_log_;
160 DISALLOW_COPY_AND_ASSIGN(NetLogTempFile);
163 #endif // CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_