Home | History | Annotate | Download | only in net
      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/files/file_path.h"
     12 #include "base/gtest_prod_util.h"
     13 #include "base/memory/scoped_ptr.h"
     14 
     15 namespace base {
     16 class DictionaryValue;
     17 }
     18 
     19 namespace net {
     20 class NetLogLogger;
     21 }
     22 
     23 class ChromeNetLog;
     24 
     25 // NetLogTempFile logs all the NetLog entries into a temporary file
     26 // "chrome-net-export-log.json" created in base::GetTempDir() directory.
     27 //
     28 // NetLogTempFile maintains the current logging state (state_) and log file type
     29 // (log_type_) of the logging into a chrome-net-export-log.json file.
     30 //
     31 // The following are the possible states
     32 // a) Only Start is allowed (STATE_NOT_LOGGING, LOG_TYPE_NONE).
     33 // b) Only Stop is allowed (STATE_LOGGING).
     34 // c) Either Send or Start is allowed (STATE_NOT_LOGGING, anything but
     35 //    LOG_TYPE_NONE).
     36 //
     37 // This is created/destroyed on the UI thread, but all other function calls
     38 // occur on the FILE_USER_BLOCKING thread.
     39 //
     40 // This relies on the UI thread outlasting all other named threads for thread
     41 // safety.
     42 class NetLogTempFile {
     43  public:
     44   // This enum lists the UI button commands it could receive.
     45   enum Command {
     46     DO_START,  // Call StartNetLog.
     47     DO_START_STRIP_PRIVATE_DATA,  // Call StartNetLog stripping private data.
     48     DO_STOP,   // Call StopNetLog.
     49   };
     50 
     51   virtual ~NetLogTempFile();  // Destructs a NetLogTempFile.
     52 
     53   // Accepts the button command and executes it.
     54   void ProcessCommand(Command command);
     55 
     56   // Returns true and the path to the temporary file. If there is no file to
     57   // send, then it returns false. It also returns false when actively logging to
     58   // the file.
     59   bool GetFilePath(base::FilePath* path);
     60 
     61   // Creates a Value summary of the state of the NetLogTempFile. The caller is
     62   // responsible for deleting the returned value.
     63   base::DictionaryValue* GetState();
     64 
     65  protected:
     66   // Constructs a NetLogTempFile. Only one instance is created in browser
     67   // process.
     68   explicit NetLogTempFile(ChromeNetLog* chrome_net_log);
     69 
     70   // Returns path name to base::GetTempDir() directory. Returns false if
     71   // base::GetTempDir() fails.
     72   virtual bool GetNetExportLogDirectory(base::FilePath* path);
     73 
     74   // Returns true if |log_path_| exists.
     75   virtual bool NetExportLogExists();
     76 
     77  private:
     78   friend class ChromeNetLog;
     79   friend class NetLogTempFileTest;
     80 
     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);
     88 
     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,
     97   };
     98 
     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 file has credentials and cookies stripped.
    107     LOG_TYPE_STRIP_PRIVATE_DATA,
    108     // The file includes all data.
    109     LOG_TYPE_NORMAL,
    110   };
    111 
    112   // Initializes the |state_| to STATE_NOT_LOGGING and |log_type_| to
    113   // LOG_TYPE_NONE (if there is no temporary file from earlier run) or
    114   // LOG_TYPE_UNKNOWN (if there is a temporary file from earlier run). Returns
    115   // false if initialization of |log_path_| fails.
    116   bool EnsureInit();
    117 
    118   // Start collecting NetLog data into chrome-net-export-log.json file in
    119   // base::GetTempDir() directory. If |strip_private_data| is true, do not log
    120   // cookies and credentials. It is a no-op if we are already collecting data
    121   // into a file.
    122   void StartNetLog(bool strip_private_data);
    123 
    124   // Stop collecting NetLog data into the temporary file. It is a no-op if we
    125   // are not collecting data into a file.
    126   void StopNetLog();
    127 
    128   // Updates |log_path_| with base::FilePath to |log_filename_| in the
    129   // base::GetTempDir() directory. Returns false if base::GetTempDir()
    130   // fails.
    131   bool GetNetExportLog();
    132 
    133   // Helper function for unit tests.
    134   State state() const { return state_; }
    135   LogType log_type() const { return log_type_; }
    136 
    137   State state_;  // Current state of NetLogTempFile.
    138   LogType log_type_;  // Type of current log file on disk.
    139 
    140   // Name of the file. It defaults to chrome-net-export-log.json, but can be
    141   // overwritten by unit tests.
    142   base::FilePath::StringType log_filename_;
    143 
    144   base::FilePath log_path_;  // base::FilePath to the temporary file.
    145 
    146   // |net_log_logger_| watches the NetLog event stream, and sends all entries to
    147   // the file created in StartNetLog().
    148   scoped_ptr<net::NetLogLogger> net_log_logger_;
    149 
    150   // The |chrome_net_log_| is owned by the browser process, cached here to avoid
    151   // using global (g_browser_process).
    152   ChromeNetLog* chrome_net_log_;
    153 
    154   DISALLOW_COPY_AND_ASSIGN(NetLogTempFile);
    155 };
    156 
    157 #endif  // CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_
    158