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