OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #include "chrome/browser/media/webrtc_event_log_handler.h" |
| 6 |
| 7 #include <string> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" |
| 12 #include "base/files/file_util.h" |
| 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/time/time.h" |
| 15 #include "chrome/browser/media/webrtc_log_list.h" |
| 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/common/chrome_switches.h" |
| 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/render_process_host.h" |
| 20 |
| 21 using content::BrowserThread; |
| 22 |
| 23 // Keys used to attach handler to the RenderProcessHost |
| 24 const char WebRtcEventLogHandler::kWebRtcEventLogHandlerKey[] = |
| 25 "kWebRtcEventLogHandlerKey"; |
| 26 |
| 27 namespace { |
| 28 |
| 29 // Returns a path name to be used as prefix for RTC event log files. |
| 30 base::FilePath GetWebRtcEventLogPrefixPath(const base::FilePath& directory, |
| 31 uint64_t rtc_event_log_id) { |
| 32 static const char kWebRtcEventLogFilePrefix[] = "WebRtcEventLog."; |
| 33 return directory.AppendASCII(kWebRtcEventLogFilePrefix + |
| 34 base::Int64ToString(rtc_event_log_id)); |
| 35 } |
| 36 |
| 37 } // namespace |
| 38 |
| 39 WebRtcEventLogHandler::WebRtcEventLogHandler(Profile* profile) |
| 40 : profile_(profile), |
| 41 is_rtc_event_logging_in_progress_(false), |
| 42 current_rtc_event_log_id_(0) { |
| 43 DCHECK(profile_); |
| 44 thread_checker_.DetachFromThread(); |
| 45 } |
| 46 |
| 47 WebRtcEventLogHandler::~WebRtcEventLogHandler() {} |
| 48 |
| 49 void WebRtcEventLogHandler::StartWebRtcEventLogging( |
| 50 content::RenderProcessHost* host, |
| 51 base::TimeDelta delay, |
| 52 const RecordingDoneCallback& callback, |
| 53 const RecordingErrorCallback& error_callback) { |
| 54 DCHECK(thread_checker_.CalledOnValidThread()); |
| 55 |
| 56 BrowserThread::PostTaskAndReplyWithResult( |
| 57 BrowserThread::FILE, FROM_HERE, |
| 58 base::Bind(&WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), |
| 59 base::Bind(&WebRtcEventLogHandler::DoStartWebRtcEventLogging, this, host, |
| 60 delay, callback, error_callback)); |
| 61 } |
| 62 |
| 63 void WebRtcEventLogHandler::StopWebRtcEventLogging( |
| 64 content::RenderProcessHost* host, |
| 65 const RecordingDoneCallback& callback, |
| 66 const RecordingErrorCallback& error_callback) { |
| 67 DCHECK(thread_checker_.CalledOnValidThread()); |
| 68 const bool is_manual_stop = true; |
| 69 BrowserThread::PostTaskAndReplyWithResult( |
| 70 BrowserThread::FILE, FROM_HERE, |
| 71 base::Bind(&WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), |
| 72 base::Bind(&WebRtcEventLogHandler::DoStopWebRtcEventLogging, this, host, |
| 73 is_manual_stop, current_rtc_event_log_id_, callback, |
| 74 error_callback)); |
| 75 } |
| 76 |
| 77 base::FilePath WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists() { |
| 78 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 79 base::FilePath log_dir_path = |
| 80 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()); |
| 81 base::File::Error error; |
| 82 if (!base::CreateDirectoryAndGetError(log_dir_path, &error)) { |
| 83 DLOG(ERROR) << "Could not create WebRTC log directory, error: " << error; |
| 84 return base::FilePath(); |
| 85 } |
| 86 return log_dir_path; |
| 87 } |
| 88 |
| 89 void WebRtcEventLogHandler::DoStartWebRtcEventLogging( |
| 90 content::RenderProcessHost* host, |
| 91 base::TimeDelta delay, |
| 92 const RecordingDoneCallback& callback, |
| 93 const RecordingErrorCallback& error_callback, |
| 94 const base::FilePath& log_directory) { |
| 95 DCHECK(thread_checker_.CalledOnValidThread()); |
| 96 |
| 97 if (is_rtc_event_logging_in_progress_) { |
| 98 error_callback.Run("RTC event logging already in progress"); |
| 99 return; |
| 100 } |
| 101 |
| 102 is_rtc_event_logging_in_progress_ = true; |
| 103 base::FilePath prefix_path = |
| 104 GetWebRtcEventLogPrefixPath(log_directory, ++current_rtc_event_log_id_); |
| 105 host->EnableEventLogRecordings(prefix_path); |
| 106 |
| 107 if (delay.is_zero()) { |
| 108 const bool is_stopped = false, is_manual_stop = false; |
| 109 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); |
| 110 return; |
| 111 } |
| 112 |
| 113 const bool is_manual_stop = false; |
| 114 BrowserThread::PostDelayedTask( |
| 115 BrowserThread::UI, FROM_HERE, |
| 116 base::Bind(&WebRtcEventLogHandler::DoStopWebRtcEventLogging, this, host, |
| 117 is_manual_stop, current_rtc_event_log_id_, callback, |
| 118 error_callback, log_directory), |
| 119 delay); |
| 120 } |
| 121 |
| 122 void WebRtcEventLogHandler::DoStopWebRtcEventLogging( |
| 123 content::RenderProcessHost* host, |
| 124 bool is_manual_stop, |
| 125 uint64_t rtc_event_log_id, |
| 126 const RecordingDoneCallback& callback, |
| 127 const RecordingErrorCallback& error_callback, |
| 128 const base::FilePath& log_directory) { |
| 129 DCHECK(thread_checker_.CalledOnValidThread()); |
| 130 DCHECK_LE(rtc_event_log_id, current_rtc_event_log_id_); |
| 131 |
| 132 base::FilePath prefix_path = |
| 133 GetWebRtcEventLogPrefixPath(log_directory, rtc_event_log_id); |
| 134 // Prevent an old posted DoStopWebRtcEventLogging() call to stop a newer dump. |
| 135 // This could happen in a sequence like: |
| 136 // Start(10); // Start dump 1. Post Stop() to run after 10 seconds. |
| 137 // Stop(); // Manually stop dump 1 before 10 seconds; |
| 138 // Start(20); // Start dump 2. Posted Stop() for 1 should not stop dump 2. |
| 139 if (rtc_event_log_id < current_rtc_event_log_id_) { |
| 140 const bool is_stopped = false; |
| 141 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); |
| 142 return; |
| 143 } |
| 144 |
| 145 if (!is_rtc_event_logging_in_progress_) { |
| 146 error_callback.Run("No RTC event logging in progress"); |
| 147 return; |
| 148 } |
| 149 |
| 150 host->DisableEventLogRecordings(); |
| 151 is_rtc_event_logging_in_progress_ = false; |
| 152 const bool is_stopped = true; |
| 153 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); |
| 154 } |
OLD | NEW |