Home | History | Annotate | Download | only in media
      1 // Copyright 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_MEDIA_WEBRTC_LOGGING_HANDLER_HOST_H_
      6 #define CHROME_BROWSER_MEDIA_WEBRTC_LOGGING_HANDLER_HOST_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/memory/shared_memory.h"
     10 #include "content/public/browser/browser_message_filter.h"
     11 
     12 namespace net {
     13 class URLRequestContextGetter;
     14 }  // namespace net
     15 
     16 class PartialCircularBuffer;
     17 class Profile;
     18 class RenderProcessHost;
     19 
     20 // WebRtcLoggingHandlerHost handles operations regarding the WebRTC logging:
     21 // - Opens a shared memory buffer that the handler in the render process
     22 //   writes to.
     23 // - Writes basic machine info to the log.
     24 // - Informs the handler in the render process when to stop logging.
     25 // - Closes the shared memory (and thereby discarding it) or triggers uploading
     26 //   of the log.
     27 // - Detects when channel, i.e. renderer, is going away and possibly triggers
     28 //   uploading the log.
     29 class WebRtcLoggingHandlerHost : public content::BrowserMessageFilter {
     30  public:
     31   typedef base::Callback<void(bool, const std::string&)> GenericDoneCallback;
     32   typedef base::Callback<void(bool, const std::string&, const std::string&)>
     33       UploadDoneCallback;
     34 
     35   explicit WebRtcLoggingHandlerHost(Profile* profile);
     36 
     37   // Sets meta data that will be uploaded along with the log and also written
     38   // in the beginning of the log. Must be called on the IO thread before calling
     39   // StartLogging.
     40   void SetMetaData(const std::map<std::string, std::string>& meta_data,
     41                    const GenericDoneCallback& callback);
     42 
     43   // Opens a log and starts logging. Must be called on the IO thread.
     44   void StartLogging(const GenericDoneCallback& callback);
     45 
     46   // Stops logging. Log will remain open until UploadLog or DiscardLog is
     47   // called. Must be called on the IO thread.
     48   void StopLogging(const GenericDoneCallback& callback);
     49 
     50   // Uploads the log and discards the local copy. May only be called after
     51   // logging has stopped. Must be called on the IO thread.
     52   void UploadLog(const UploadDoneCallback& callback);
     53 
     54   // Called by WebRtcLogUploader when uploading has finished. Must be called on
     55   // the IO thread.
     56   void UploadLogDone();
     57 
     58   // Discards the log. May only be called after logging has stopped. Must be
     59   // called on the IO thread.
     60   void DiscardLog(const GenericDoneCallback& callback);
     61 
     62   // May be called on any thread. |upload_log_on_render_close_| is used
     63   // for decision making and it's OK if it changes before the execution based
     64   // on that decision has finished.
     65   void set_upload_log_on_render_close(bool should_upload) {
     66     upload_log_on_render_close_ = should_upload;
     67   }
     68 
     69  private:
     70   // States used for protecting from function calls made at non-allowed points
     71   // in time. For example, StartLogging() is only allowed in CLOSED state.
     72   // Transitions: SetMetaData(): CLOSED -> CLOSED.
     73   //              StartLogging(): CLOSED -> STARTING.
     74   //              Start done: STARTING -> STARTED.
     75   //              StopLogging(): STARTED -> STOPPING.
     76   //              Stop done: STOPPING -> STOPPED.
     77   //              UploadLog(): STOPPED -> UPLOADING.
     78   //              Upload done: UPLOADING -> CLOSED.
     79   //              DiscardLog(): STOPPED -> CLOSED.
     80   enum LoggingState {
     81     CLOSED,    // Logging not started, no log in memory.
     82     STARTING,  // Start logging is in progress.
     83     STARTED,   // Logging started.
     84     STOPPING,  // Stop logging is in progress.
     85     STOPPED,   // Logging has been stopped, log still open in memory.
     86     UPLOADING  // Uploading log is in progress.
     87   };
     88 
     89   friend class content::BrowserThread;
     90   friend class base::DeleteHelper<WebRtcLoggingHandlerHost>;
     91 
     92   virtual ~WebRtcLoggingHandlerHost();
     93 
     94   // BrowserMessageFilter implementation.
     95   virtual void OnChannelClosing() OVERRIDE;
     96   virtual void OnDestruct() const OVERRIDE;
     97   virtual bool OnMessageReceived(const IPC::Message& message,
     98                                  bool* message_was_ok) OVERRIDE;
     99 
    100   void OnAddLogMessage(const std::string& message);
    101   void OnLoggingStoppedInRenderer();
    102 
    103   void StartLoggingIfAllowed();
    104   void DoStartLogging();
    105   void LogMachineInfo();
    106   void NotifyLoggingStarted();
    107 
    108   void TriggerUploadLog();
    109 
    110   void FireGenericDoneCallback(GenericDoneCallback* callback,
    111                                bool success,
    112                                const std::string& error_message);
    113 
    114   scoped_refptr<net::URLRequestContextGetter> system_request_context_;
    115 
    116   scoped_ptr<unsigned char[]> log_buffer_;
    117   scoped_ptr<PartialCircularBuffer> circular_buffer_;
    118 
    119   // The profile associated with our renderer process.
    120   Profile* profile_;
    121 
    122   // These are only accessed on the IO thread, except when in STARTING state. In
    123   // this state we are protected since entering any function that alters the
    124   // state is not allowed.
    125   std::map<std::string, std::string> meta_data_;
    126 
    127   // These are only accessed on the IO thread.
    128   GenericDoneCallback start_callback_;
    129   GenericDoneCallback stop_callback_;
    130   UploadDoneCallback upload_callback_;
    131 
    132   // Only accessed on the IO thread, except when in STARTING, STOPPING or
    133   // UPLOADING state if the action fails and the state must be reset. In these
    134   // states however, we are protected since entering any function that alters
    135   // the state is not allowed.
    136   LoggingState logging_state_;
    137 
    138   // Only accessed on the IO thread.
    139   bool upload_log_on_render_close_;
    140 
    141   // This is the handle to be passed to the render process. It's stored so that
    142   // it doesn't have to be passed on when posting messages between threads.
    143   // It's only accessed on the IO thread.
    144   base::SharedMemoryHandle foreign_memory_handle_;
    145 
    146   DISALLOW_COPY_AND_ASSIGN(WebRtcLoggingHandlerHost);
    147 };
    148 
    149 #endif  // CHROME_BROWSER_MEDIA_WEBRTC_LOGGING_HANDLER_HOST_H_
    150