1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_NET_LOG_NET_LOG_FILE_WRITER_H_
#define COMPONENTS_NET_LOG_NET_LOG_FILE_WRITER_H_
#include <memory>
#include <string>
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/threading/thread_checker.h"
namespace base {
class DictionaryValue;
}
namespace net {
class NetLogCaptureMode;
class WriteToFileNetLogObserver;
}
namespace net_log {
class ChromeNetLog;
// NetLogFileWriter logs all the NetLog entries into a specified file.
//
// NetLogFileWriter maintains the current logging state (state_) and log file
// type (log_type_) of the logging into a chrome-net-export-log.json file.
//
// The following are the possible states
// a) Only Start is allowed (STATE_NOT_LOGGING, LOG_TYPE_NONE).
// b) Only Stop is allowed (STATE_LOGGING).
// c) Either Send or Start is allowed (STATE_NOT_LOGGING, anything but
// LOG_TYPE_NONE).
//
// This is created/destroyed on the main thread, but all other function calls
// occur on a background thread.
//
// This relies on the UI thread outlasting all other threads for thread safety.
class NetLogFileWriter {
public:
// This enum lists the UI button commands it could receive.
enum Command {
DO_START_LOG_BYTES, // Call StartNetLog logging all bytes received.
DO_START, // Call StartNetLog.
DO_START_STRIP_PRIVATE_DATA, // Call StartNetLog stripping private data.
DO_STOP, // Call StopNetLog.
};
virtual ~NetLogFileWriter();
// Accepts the button command and executes it.
void ProcessCommand(Command command);
// Returns true and the path to the file. If there is no file to
// send, then it returns false. It also returns false when actively logging to
// the file.
bool GetFilePath(base::FilePath* path);
// Creates a Value summary of the state of the NetLogFileWriter. The caller is
// responsible for deleting the returned value.
base::DictionaryValue* GetState();
// Updates |log_path_| to the |custom_path|.
void SetUpNetExportLogPath(const base::FilePath& custom_path);
protected:
// Constructs a NetLogFileWriter. Only one instance is created in browser
// process.
NetLogFileWriter(ChromeNetLog* chrome_net_log,
const base::CommandLine::StringType& command_line_string,
const std::string& channel_string);
// Returns path name to base::GetTempDir() directory. Returns false if
// base::GetTempDir() fails.
virtual bool GetNetExportLogBaseDirectory(base::FilePath* path) const;
private:
friend class ChromeNetLog;
friend class NetLogFileWriterTest;
// Allow tests to access our innards for testing purposes.
FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, EnsureInitFailure);
FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, EnsureInitAllowStart);
FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, EnsureInitAllowStartOrSend);
FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, ProcessCommandDoStartAndStop);
FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, DoStartClearsFile);
FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, CheckAddEvent);
FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, CheckAddEventWithCustomPath);
// This enum lists the possible state NetLogFileWriter could be in. It is used
// to enable/disable "Start", "Stop" and "Send" (email) UI actions.
enum State {
STATE_UNINITIALIZED,
// Not currently logging to file.
STATE_NOT_LOGGING,
// Currently logging to file.
STATE_LOGGING,
};
// The type of the current log file on disk.
enum LogType {
// There is no current log file.
LOG_TYPE_NONE,
// The file predates this session. May or may not have private data.
// TODO(davidben): This state is kind of silly.
LOG_TYPE_UNKNOWN,
// The log includes raw bytes.
LOG_TYPE_LOG_BYTES,
// The file includes all data.
LOG_TYPE_NORMAL,
// The file has credentials and cookies stripped.
LOG_TYPE_STRIP_PRIVATE_DATA,
};
// Returns the NetLog::CaptureMode corresponding to a LogType.
static net::NetLogCaptureMode GetCaptureModeForLogType(LogType log_type);
// Initializes the |state_| to STATE_NOT_LOGGING and |log_type_| to
// LOG_TYPE_NONE (if there is no file from earlier run) or
// LOG_TYPE_UNKNOWN (if there is a file from earlier run). Returns
// false if initialization of |log_path_| fails.
bool EnsureInit();
// Start collecting NetLog data into chrome-net-export-log.json file in
// a directory, using the specified capture mode. It is a no-op if we are
// already collecting data into a file, and |capture_mode| is ignored.
// ignored.
// TODO(mmenke): That's rather weird behavior, think about improving it.
void StartNetLog(LogType log_type);
// Stop collecting NetLog data into the file. It is a no-op if we
// are not collecting data into a file.
void StopNetLog();
// Updates |log_path_| to be the base::FilePath to use for log files, which
// will be inside the base::GetTempDir() directory. Returns false if
// base::GetTempDir() fails, or unable to create a subdirectory for logging
// within that directory.
bool SetUpDefaultNetExportLogPath();
// Returns true if a file exists at |log_path_|.
bool NetExportLogExists() const;
base::ThreadChecker thread_checker_;
// Helper function for unit tests.
State state() const { return state_; }
LogType log_type() const { return log_type_; }
State state_; // Current state of NetLogFileWriter.
LogType log_type_; // Type of current log file on disk.
base::FilePath log_path_; // base::FilePath to the NetLog file.
// |write_to_file_observer_| watches the NetLog event stream, and
// sends all entries to the file created in StartNetLog().
std::unique_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_;
// The |chrome_net_log_| is owned by the browser process, cached here to avoid
// using global (g_browser_process).
ChromeNetLog* chrome_net_log_;
const base::CommandLine::StringType command_line_string_;
const std::string channel_string_;
DISALLOW_COPY_AND_ASSIGN(NetLogFileWriter);
};
} // namespace net_log
#endif // COMPONENTS_NET_LOG_NET_LOG_FILE_WRITER_H_
|