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 |
| 50 void WebRtcEventLogHandler::StartWebRtcEventLogging( |
| 51 content::RenderProcessHost* host, |
| 52 base::TimeDelta delay, |
| 53 const RecordingDoneCallback& callback, |
| 54 const RecordingErrorCallback& error_callback) { |
| 55 DCHECK(thread_checker_.CalledOnValidThread()); |
| 56 |
| 57 BrowserThread::PostTaskAndReplyWithResult( |
| 58 BrowserThread::FILE, FROM_HERE, |
| 59 base::Bind(&WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), |
| 60 base::Bind(&WebRtcEventLogHandler::DoStartWebRtcEventLogging, this, host, |
| 61 delay, callback, error_callback)); |
| 62 } |
| 63 |
| 64 void WebRtcEventLogHandler::StopWebRtcEventLogging( |
| 65 content::RenderProcessHost* host, |
| 66 const RecordingDoneCallback& callback, |
| 67 const RecordingErrorCallback& error_callback) { |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); |
| 69 BrowserThread::PostTaskAndReplyWithResult( |
| 70 BrowserThread::FILE, FROM_HERE, |
| 71 base::Bind(&WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), |
| 72 base::Bind(&WebRtcEventLogHandler::DoStopWebRtcEventLogging, this, host, |
| 73 true /* 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 callback.Run(prefix_path.AsUTF8Unsafe(), false /* not stopped */, |
| 109 false /* not manually stopped */); |
| 110 return; |
| 111 } |
| 112 |
| 113 BrowserThread::PostDelayedTask( |
| 114 BrowserThread::UI, FROM_HERE, |
| 115 base::Bind(&WebRtcEventLogHandler::DoStopWebRtcEventLogging, this, host, |
| 116 false /* no manual stop */, current_rtc_event_log_id_, |
| 117 callback, error_callback, log_directory), |
| 118 delay); |
| 119 } |
| 120 |
| 121 void WebRtcEventLogHandler::DoStopWebRtcEventLogging( |
| 122 content::RenderProcessHost* host, |
| 123 bool is_manual_stop, |
| 124 uint64_t rtc_event_log_id, |
| 125 const RecordingDoneCallback& callback, |
| 126 const RecordingErrorCallback& error_callback, |
| 127 const base::FilePath& log_directory) { |
| 128 DCHECK(thread_checker_.CalledOnValidThread()); |
| 129 DCHECK_LE(rtc_event_log_id, current_rtc_event_log_id_); |
| 130 |
| 131 base::FilePath prefix_path = |
| 132 GetWebRtcEventLogPrefixPath(log_directory, rtc_event_log_id); |
| 133 // Prevent an old posted DoStopWebRtcEventLogging() call to stop a newer dump. |
| 134 // This could happen in a sequence like: |
| 135 // Start(10); //Start dump 1. Post Stop() to run after 10 seconds. |
| 136 // Stop(); // Manually stop dump 1 before 10 seconds; |
| 137 // Start(20); // Start dump 2. Posted Stop() for 1 should not stop dump 2. |
| 138 if (rtc_event_log_id < current_rtc_event_log_id_) { |
| 139 callback.Run(prefix_path.AsUTF8Unsafe(), false /* not stopped */, |
| 140 is_manual_stop); |
| 141 return; |
| 142 } |
| 143 |
| 144 if (!is_rtc_event_logging_in_progress_) { |
| 145 error_callback.Run("No RTC event logging in progress"); |
| 146 return; |
| 147 } |
| 148 |
| 149 host->DisableEventLogRecordings(); |
| 150 is_rtc_event_logging_in_progress_ = false; |
| 151 callback.Run(prefix_path.AsUTF8Unsafe(), true /* stopped */, is_manual_stop); |
| 152 } |
OLD | NEW |