Chromium Code Reviews| 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/rtc_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/prefs/pref_service.h" | |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "base/sys_info.h" | |
| 16 #include "base/time/time.h" | |
| 17 #include "chrome/browser/bad_message.h" | |
| 18 #include "chrome/browser/browser_process.h" | |
| 19 #include "chrome/browser/media/webrtc_log_list.h" | |
| 20 #include "chrome/browser/profiles/profile.h" | |
| 21 #include "chrome/common/channel_info.h" | |
| 22 #include "chrome/common/chrome_switches.h" | |
| 23 #include "content/public/browser/browser_thread.h" | |
| 24 #include "content/public/browser/content_browser_client.h" | |
| 25 #include "content/public/browser/render_process_host.h" | |
| 26 #include "net/base/net_util.h" | |
| 27 #include "net/url_request/url_request_context_getter.h" | |
| 28 | |
| 29 using content::BrowserThread; | |
| 30 | |
| 31 // Keys used to attach handler to the RenderProcessHost | |
| 32 const char RtcEventLogHandler::kRtcEventLogHandlerKey[] = | |
| 33 "kRtcEventLogHandlerKey"; | |
| 34 | |
| 35 namespace { | |
| 36 | |
| 37 // Returns a path name to be used as prefix for RTC event log files. | |
| 38 base::FilePath GetRtcEventLogPrefixPath(const base::FilePath& directory, | |
| 39 uint64_t rtc_event_log_id) { | |
| 40 static const char kRtcEventLogFilePrefix[] = "RtcEventLog."; | |
| 41 return directory.AppendASCII(kRtcEventLogFilePrefix + | |
| 42 base::Int64ToString(rtc_event_log_id)); | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 RtcEventLogHandler::RtcEventLogHandler(Profile* profile) | |
| 48 : profile_(profile), | |
| 49 is_rtc_event_logging_in_progress_(false), | |
| 50 current_rtc_event_log_id_(0) { | |
| 51 DCHECK(profile_); | |
| 52 } | |
| 53 | |
| 54 RtcEventLogHandler::~RtcEventLogHandler() { | |
| 55 // TODO: Can we check that we dont hold any references to any Javascript | |
| 56 // callbacks? | |
| 57 } | |
| 58 | |
| 59 void RtcEventLogHandler::StartRtcEventLogging( | |
| 60 content::RenderProcessHost* host, | |
| 61 base::TimeDelta delay, | |
| 62 const TimeLimitedRecordingCallback& callback, | |
| 63 const TimeLimitedRecordingErrorCallback& error_callback) { | |
| 64 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
|
Henrik Grunell
2016/02/23 15:29:10
Use base::ThreadChecker instead for all UI thread
terelius-chromium
2016/03/02 10:01:10
Done.
| |
| 65 | |
| 66 BrowserThread::PostTaskAndReplyWithResult( | |
| 67 BrowserThread::FILE, FROM_HERE, | |
| 68 base::Bind(&RtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), | |
| 69 base::Bind(&RtcEventLogHandler::DoStartRtcEventLogging, this, host, delay, | |
| 70 callback, error_callback)); | |
| 71 } | |
| 72 | |
| 73 void RtcEventLogHandler::StopRtcEventLogging( | |
| 74 content::RenderProcessHost* host, | |
| 75 const TimeLimitedRecordingCallback& callback, | |
| 76 const TimeLimitedRecordingErrorCallback& error_callback) { | |
| 77 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 78 BrowserThread::PostTaskAndReplyWithResult( | |
| 79 BrowserThread::FILE, FROM_HERE, | |
| 80 base::Bind(&RtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), | |
| 81 base::Bind(&RtcEventLogHandler::DoStopRtcEventLogging, this, host, | |
| 82 true /* manual stop */, current_rtc_event_log_id_, callback, | |
| 83 error_callback)); | |
| 84 } | |
| 85 | |
| 86 base::FilePath RtcEventLogHandler::GetLogDirectoryAndEnsureExists() { | |
| 87 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
| 88 base::FilePath log_dir_path = | |
| 89 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()); | |
| 90 base::File::Error error; | |
| 91 if (!base::CreateDirectoryAndGetError(log_dir_path, &error)) { | |
| 92 DLOG(ERROR) << "Could not create WebRTC log directory, error: " << error; | |
| 93 return base::FilePath(); | |
| 94 } | |
| 95 return log_dir_path; | |
| 96 } | |
| 97 | |
| 98 void RtcEventLogHandler::DoStartRtcEventLogging( | |
| 99 content::RenderProcessHost* host, | |
| 100 base::TimeDelta delay, | |
| 101 const TimeLimitedRecordingCallback& callback, | |
| 102 const TimeLimitedRecordingErrorCallback& error_callback, | |
| 103 const base::FilePath& log_directory) { | |
| 104 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 105 | |
| 106 if (is_rtc_event_logging_in_progress_) { | |
| 107 error_callback.Run("RTC event logging already in progress"); | |
| 108 return; | |
| 109 } | |
| 110 | |
| 111 is_rtc_event_logging_in_progress_ = true; | |
| 112 base::FilePath prefix_path = | |
| 113 GetRtcEventLogPrefixPath(log_directory, ++current_rtc_event_log_id_); | |
| 114 host->EnableEventLogRecordings(prefix_path); | |
| 115 | |
| 116 if (delay.is_zero()) { | |
| 117 callback.Run(prefix_path.AsUTF8Unsafe(), false /* not stopped */, | |
| 118 false /* not manually stopped */); | |
| 119 return; | |
| 120 } | |
| 121 | |
| 122 BrowserThread::PostDelayedTask( | |
| 123 BrowserThread::UI, FROM_HERE, | |
| 124 base::Bind(&RtcEventLogHandler::DoStopRtcEventLogging, this, host, | |
| 125 false /* no manual stop */, current_rtc_event_log_id_, | |
| 126 callback, error_callback, prefix_path), | |
| 127 delay); | |
| 128 } | |
| 129 | |
| 130 void RtcEventLogHandler::DoStopRtcEventLogging( | |
| 131 content::RenderProcessHost* host, | |
| 132 bool is_manual_stop, | |
| 133 uint64_t rtc_event_log_id, | |
| 134 const TimeLimitedRecordingCallback& callback, | |
| 135 const TimeLimitedRecordingErrorCallback& error_callback, | |
| 136 const base::FilePath& log_directory) { | |
| 137 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 138 DCHECK_LE(rtc_event_log_id, current_rtc_event_log_id_); | |
| 139 | |
| 140 base::FilePath prefix_path = | |
| 141 GetRtcEventLogPrefixPath(log_directory, rtc_event_log_id); | |
| 142 // Prevent an old posted DoStopRtcEventLogging() call to stop a newer dump. | |
| 143 // This could happen in a sequence like: | |
| 144 // Start(10); //Start dump 1. Post Stop() to run after 10 seconds. | |
| 145 // Stop(); // Manually stop dump 1 before 10 seconds; | |
| 146 // Start(20); // Start dump 2. Posted Stop() for 1 should not stop dump 2. | |
| 147 if (rtc_event_log_id < current_rtc_event_log_id_) { | |
| 148 callback.Run(prefix_path.AsUTF8Unsafe(), false /* not stopped */, | |
| 149 is_manual_stop); | |
| 150 return; | |
| 151 } | |
| 152 | |
| 153 if (!is_rtc_event_logging_in_progress_) { | |
| 154 error_callback.Run("No RTC event logging in progress"); | |
| 155 return; | |
| 156 } | |
| 157 | |
| 158 host->DisableEventLogRecordings(); | |
| 159 is_rtc_event_logging_in_progress_ = false; | |
| 160 callback.Run(prefix_path.AsUTF8Unsafe(), true /* stopped */, is_manual_stop); | |
| 161 } | |
| OLD | NEW |