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::Uint64ToString(rtc_event_log_id)); | |
35 } | |
36 | |
37 } // namespace | |
38 | |
39 WebRtcEventLogHandler::WebRtcEventLogHandler(Profile* profile) | |
40 : profile_(profile), | |
41 current_rtc_event_log_id_(0) { | |
42 DCHECK(profile_); | |
43 thread_checker_.DetachFromThread(); | |
44 } | |
45 | |
46 WebRtcEventLogHandler::~WebRtcEventLogHandler() {} | |
47 | |
48 void WebRtcEventLogHandler::StartWebRtcEventLogging( | |
49 content::RenderProcessHost* host, | |
50 base::TimeDelta delay, | |
51 const RecordingDoneCallback& callback, | |
52 const RecordingErrorCallback& error_callback) { | |
53 DCHECK(thread_checker_.CalledOnValidThread()); | |
54 | |
55 BrowserThread::PostTaskAndReplyWithResult( | |
56 BrowserThread::FILE, FROM_HERE, | |
57 base::Bind(&WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), | |
58 base::Bind(&WebRtcEventLogHandler::DoStartWebRtcEventLogging, this, host, | |
59 delay, callback, error_callback)); | |
60 } | |
61 | |
62 void WebRtcEventLogHandler::StopWebRtcEventLogging( | |
63 content::RenderProcessHost* host, | |
64 const RecordingDoneCallback& callback, | |
65 const RecordingErrorCallback& error_callback) { | |
66 DCHECK(thread_checker_.CalledOnValidThread()); | |
67 const bool is_manual_stop = true; | |
68 BrowserThread::PostTaskAndReplyWithResult( | |
69 BrowserThread::FILE, FROM_HERE, | |
70 base::Bind(&WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), | |
71 base::Bind(&WebRtcEventLogHandler::DoStopWebRtcEventLogging, this, host, | |
72 is_manual_stop, current_rtc_event_log_id_, callback, | |
73 error_callback)); | |
74 } | |
75 | |
76 base::FilePath WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists() { | |
77 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
78 base::FilePath log_dir_path = | |
79 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()); | |
80 base::File::Error error; | |
81 if (!base::CreateDirectoryAndGetError(log_dir_path, &error)) { | |
82 DLOG(ERROR) << "Could not create WebRTC log directory, error: " << error; | |
83 return base::FilePath(); | |
84 } | |
85 return log_dir_path; | |
86 } | |
87 | |
88 void WebRtcEventLogHandler::DoStartWebRtcEventLogging( | |
89 content::RenderProcessHost* host, | |
90 base::TimeDelta delay, | |
91 const RecordingDoneCallback& callback, | |
92 const RecordingErrorCallback& error_callback, | |
93 const base::FilePath& log_directory) { | |
94 DCHECK(thread_checker_.CalledOnValidThread()); | |
95 | |
96 base::FilePath prefix_path = | |
97 GetWebRtcEventLogPrefixPath(log_directory, ++current_rtc_event_log_id_); | |
98 if (!host->StartWebRTCEventLog(prefix_path)) { | |
99 error_callback.Run("RTC event logging already in progress"); | |
100 return; | |
101 } | |
102 | |
103 if (delay.is_zero()) { | |
104 const bool is_stopped = false, is_manual_stop = false; | |
105 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); | |
106 return; | |
107 } | |
108 | |
109 const bool is_manual_stop = false; | |
110 BrowserThread::PostDelayedTask( | |
111 BrowserThread::UI, FROM_HERE, | |
112 base::Bind(&WebRtcEventLogHandler::DoStopWebRtcEventLogging, this, host, | |
113 is_manual_stop, current_rtc_event_log_id_, callback, | |
114 error_callback, log_directory), | |
115 delay); | |
116 } | |
117 | |
118 void WebRtcEventLogHandler::DoStopWebRtcEventLogging( | |
119 content::RenderProcessHost* host, | |
120 bool is_manual_stop, | |
121 uint64_t rtc_event_log_id, | |
122 const RecordingDoneCallback& callback, | |
123 const RecordingErrorCallback& error_callback, | |
124 const base::FilePath& log_directory) { | |
125 DCHECK(thread_checker_.CalledOnValidThread()); | |
126 DCHECK_LE(rtc_event_log_id, current_rtc_event_log_id_); | |
127 | |
128 base::FilePath prefix_path = | |
129 GetWebRtcEventLogPrefixPath(log_directory, rtc_event_log_id); | |
130 // Prevent an old posted DoStopWebRtcEventLogging() call to stop a newer dump. | |
131 // This could happen in a sequence like: | |
132 // Start(10); // Start dump 1. Post Stop() to run after 10 seconds. | |
133 // Stop(); // Manually stop dump 1 before 10 seconds; | |
134 // Start(20); // Start dump 2. Posted Stop() for 1 should not stop dump 2. | |
135 if (rtc_event_log_id < current_rtc_event_log_id_) { | |
136 const bool is_stopped = false; | |
137 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); | |
138 return; | |
139 } | |
140 | |
141 if (!host->StopWebRTCEventLog()) { | |
142 error_callback.Run("No RTC event logging in progress"); | |
143 return; | |
144 } | |
145 | |
146 const bool is_stopped = true; | |
147 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); | |
148 } | |
OLD | NEW |