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