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 state (state_) of the logging into a
     29 // chrome-net-export-log.json file.
     30 //
     31 // The following are the possible states
     32 // a) Only Start is allowed (state_ == STATE_UNINITIALIZED).
     33 // b) Only Stop is allowed (state_ == STATE_ALLOW_STOP).
     34 // c) Either Send or Start is allowed (state_ == STATE_ALLOW_START_SEND).
     35 //
     36 // This is created/destroyed on the UI thread, but all other function calls
     37 // occur on the FILE_USER_BLOCKING thread.
     38 //
     39 // This relies on the UI thread outlasting all other named threads for thread
     40 // safety.
     41 class NetLogTempFile {
     42  public:
     43   // This enum lists the UI button commands it could receive.
     44   enum Command {
     45     DO_START,  // Call StartLog.
     46     DO_STOP,   // Call StopLog.
     47   };
     48 
     49   virtual ~NetLogTempFile();  // Destructs a NetLogTempFile.
     50 
     51   // Accepts the button command and executes it.
     52   void ProcessCommand(Command command);
     53 
     54   // Returns true and the path to the temporary file. If there is no file to
     55   // send, then it returns false. It also returns false when actively logging to
     56   // the file.
     57   bool GetFilePath(base::FilePath* path);
     58 
     59   // Creates a Value summary of the state of the NetLogTempFile. The caller is
     60   // responsible for deleting the returned value.
     61   base::DictionaryValue* GetState();
     62 
     63  protected:
     64   // Constructs a NetLogTempFile. Only one instance is created in browser
     65   // process.
     66   explicit NetLogTempFile(ChromeNetLog* chrome_net_log);
     67 
     68   // Returns path name to base::GetTempDir() directory. Returns false if
     69   // base::GetTempDir() fails.
     70   virtual bool GetNetExportLogDirectory(base::FilePath* path);
     71 
     72   // Returns true if |log_path_| exists.
     73   virtual bool NetExportLogExists();
     74 
     75  private:
     76   friend class ChromeNetLog;
     77   friend class NetLogTempFileTest;
     78 
     79   // Allow tests to access our innards for testing purposes.
     80   FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitFailure);
     81   FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitAllowStart);
     82   FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, EnsureInitAllowStartOrSend);
     83   FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, ProcessCommandDoStartAndStop);
     84   FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, DoStartClearsFile);
     85   FRIEND_TEST_ALL_PREFIXES(NetLogTempFileTest, CheckAddEvent);
     86 
     87   // This enum lists the possible state NetLogTempFile could be in. It is used
     88   // to enable/disable "Start", "Stop" and "Send" (email) UI actions.
     89   enum State {
     90     STATE_UNINITIALIZED,
     91     STATE_ALLOW_START,       // Only DO_START Command is allowed.
     92     STATE_ALLOW_STOP,        // Only DO_STOP Command is allowed.
     93     STATE_ALLOW_START_SEND,  // Either DO_START or DO_SEND is allowed.
     94   };
     95 
     96   // Initializes the |state_| to either STATE_ALLOW_START (if there is no
     97   // temporary file from earlier run) or STATE_ALLOW_START_SEND (if there is a
     98   // temporary file from earlier run). Returns false if initialization of
     99   // |log_path_| fails.
    100   bool EnsureInit();
    101 
    102   // Start collecting NetLog data into chrome-net-export-log.json file in
    103   // base::GetTempDir() directory. It is a no-op if we are already
    104   // collecting data into a file.
    105   void StartNetLog();
    106 
    107   // Stop collecting NetLog data into the temporary file. It is a no-op if we
    108   // are not collecting data into a file.
    109   void StopNetLog();
    110 
    111   // Updates |log_path_| with base::FilePath to |log_filename_| in the
    112   // base::GetTempDir() directory. Returns false if base::GetTempDir()
    113   // fails.
    114   bool GetNetExportLog();
    115 
    116   // Helper function for unit tests.
    117   State state() const { return state_; }
    118 
    119   State state_;  // Current state of NetLogTempFile.
    120 
    121   // Name of the file. It defaults to chrome-net-export-log.json, but can be
    122   // overwritten by unit tests.
    123   base::FilePath::StringType log_filename_;
    124 
    125   base::FilePath log_path_;  // base::FilePath to the temporary file.
    126 
    127   // |net_log_logger_| watches the NetLog event stream, and sends all entries to
    128   // the file created in StartNetLog().
    129   scoped_ptr<net::NetLogLogger> net_log_logger_;
    130 
    131   // The |chrome_net_log_| is owned by the browser process, cached here to avoid
    132   // using global (g_browser_process).
    133   ChromeNetLog* chrome_net_log_;
    134 
    135   DISALLOW_COPY_AND_ASSIGN(NetLogTempFile);
    136 };
    137 
    138 #endif  // CHROME_BROWSER_NET_NET_LOG_TEMP_FILE_H_
    139