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/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 // TODO: Can we check that we dont hold any references to any Javascript | |
Henrik Grunell
2016/03/10 21:33:34
TODO(terelius)
terelius-chromium
2016/03/24 16:21:22
Removed.
| |
49 // callbacks? | |
50 } | |
51 | |
52 void WebRtcEventLogHandler::StartWebRtcEventLogging( | |
53 content::RenderProcessHost* host, | |
54 base::TimeDelta delay, | |
55 const RecordingDoneCallback& callback, | |
56 const RecordingErrorCallback& error_callback) { | |
57 DCHECK(thread_checker_.CalledOnValidThread()); | |
58 | |
59 BrowserThread::PostTaskAndReplyWithResult( | |
60 BrowserThread::FILE, FROM_HERE, | |
61 base::Bind(&WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), | |
62 base::Bind(&WebRtcEventLogHandler::DoStartWebRtcEventLogging, this, host, | |
63 delay, callback, error_callback)); | |
64 } | |
65 | |
66 void WebRtcEventLogHandler::StopWebRtcEventLogging( | |
67 content::RenderProcessHost* host, | |
68 const RecordingDoneCallback& callback, | |
69 const RecordingErrorCallback& error_callback) { | |
70 DCHECK(thread_checker_.CalledOnValidThread()); | |
71 BrowserThread::PostTaskAndReplyWithResult( | |
72 BrowserThread::FILE, FROM_HERE, | |
73 base::Bind(&WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), | |
74 base::Bind(&WebRtcEventLogHandler::DoStopWebRtcEventLogging, this, host, | |
75 true /* manual stop */, current_rtc_event_log_id_, callback, | |
76 error_callback)); | |
77 } | |
78 | |
79 base::FilePath WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists() { | |
80 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
81 base::FilePath log_dir_path = | |
82 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()); | |
83 base::File::Error error; | |
84 if (!base::CreateDirectoryAndGetError(log_dir_path, &error)) { | |
85 DLOG(ERROR) << "Could not create WebRTC log directory, error: " << error; | |
86 return base::FilePath(); | |
87 } | |
88 return log_dir_path; | |
89 } | |
90 | |
91 void WebRtcEventLogHandler::DoStartWebRtcEventLogging( | |
92 content::RenderProcessHost* host, | |
93 base::TimeDelta delay, | |
94 const RecordingDoneCallback& callback, | |
95 const RecordingErrorCallback& error_callback, | |
96 const base::FilePath& log_directory) { | |
97 DCHECK(thread_checker_.CalledOnValidThread()); | |
98 | |
99 if (is_rtc_event_logging_in_progress_) { | |
100 error_callback.Run("RTC event logging already in progress"); | |
101 return; | |
102 } | |
103 | |
104 is_rtc_event_logging_in_progress_ = true; | |
105 base::FilePath prefix_path = | |
106 GetWebRtcEventLogPrefixPath(log_directory, ++current_rtc_event_log_id_); | |
107 host->EnableEventLogRecordings(prefix_path); | |
108 | |
109 if (delay.is_zero()) { | |
110 callback.Run(prefix_path.AsUTF8Unsafe(), false /* not stopped */, | |
111 false /* not manually stopped */); | |
112 return; | |
113 } | |
114 | |
115 BrowserThread::PostDelayedTask( | |
116 BrowserThread::UI, FROM_HERE, | |
117 base::Bind(&WebRtcEventLogHandler::DoStopWebRtcEventLogging, this, host, | |
118 false /* no manual stop */, current_rtc_event_log_id_, | |
119 callback, error_callback, prefix_path), | |
120 delay); | |
121 } | |
122 | |
123 void WebRtcEventLogHandler::DoStopWebRtcEventLogging( | |
124 content::RenderProcessHost* host, | |
125 bool is_manual_stop, | |
126 uint64_t rtc_event_log_id, | |
127 const RecordingDoneCallback& callback, | |
128 const RecordingErrorCallback& error_callback, | |
129 const base::FilePath& log_directory) { | |
130 DCHECK(thread_checker_.CalledOnValidThread()); | |
131 DCHECK_LE(rtc_event_log_id, current_rtc_event_log_id_); | |
132 | |
133 base::FilePath prefix_path = | |
134 GetWebRtcEventLogPrefixPath(log_directory, rtc_event_log_id); | |
135 // Prevent an old posted DoStopWebRtcEventLogging() call to stop a newer dump. | |
136 // This could happen in a sequence like: | |
137 // Start(10); //Start dump 1. Post Stop() to run after 10 seconds. | |
138 // Stop(); // Manually stop dump 1 before 10 seconds; | |
139 // Start(20); // Start dump 2. Posted Stop() for 1 should not stop dump 2. | |
140 if (rtc_event_log_id < current_rtc_event_log_id_) { | |
141 callback.Run(prefix_path.AsUTF8Unsafe(), false /* not stopped */, | |
142 is_manual_stop); | |
143 return; | |
144 } | |
145 | |
146 if (!is_rtc_event_logging_in_progress_) { | |
147 error_callback.Run("No RTC event logging in progress"); | |
148 return; | |
149 } | |
150 | |
151 host->DisableEventLogRecordings(); | |
152 is_rtc_event_logging_in_progress_ = false; | |
153 callback.Run(prefix_path.AsUTF8Unsafe(), true /* stopped */, is_manual_stop); | |
154 } | |
OLD | NEW |