summaryrefslogtreecommitdiff
path: root/chromium/content/browser/webrtc/webrtc_internals_message_handler.cc
blob: 82c50d4c1da3f146e225f758db9a585a0a6adb26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/webrtc/webrtc_internals_message_handler.h"

#include "content/browser/webrtc/webrtc_internals.h"
#include "content/common/media/peer_connection_tracker_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/common/url_constants.h"

namespace content {

WebRTCInternalsMessageHandler::WebRTCInternalsMessageHandler()
    : WebRTCInternalsMessageHandler(WebRTCInternals::GetInstance()) {}

WebRTCInternalsMessageHandler::WebRTCInternalsMessageHandler(
    WebRTCInternals* webrtc_internals)
    : webrtc_internals_(webrtc_internals) {
  DCHECK(webrtc_internals);
  webrtc_internals_->AddObserver(this);
}

WebRTCInternalsMessageHandler::~WebRTCInternalsMessageHandler() {
  webrtc_internals_->RemoveObserver(this);
}

void WebRTCInternalsMessageHandler::RegisterMessages() {
  web_ui()->RegisterMessageCallback("getAllStats",
      base::Bind(&WebRTCInternalsMessageHandler::OnGetAllStats,
                 base::Unretained(this)));

  web_ui()->RegisterMessageCallback("enableAudioDebugRecordings",
      base::Bind(
          &WebRTCInternalsMessageHandler::OnSetAudioDebugRecordingsEnabled,
          base::Unretained(this),
          true));

  web_ui()->RegisterMessageCallback("disableAudioDebugRecordings",
      base::Bind(
          &WebRTCInternalsMessageHandler::OnSetAudioDebugRecordingsEnabled,
          base::Unretained(this),
          false));

  web_ui()->RegisterMessageCallback(
      "enableEventLogRecordings",
      base::Bind(&WebRTCInternalsMessageHandler::OnSetEventLogRecordingsEnabled,
                 base::Unretained(this), true));

  web_ui()->RegisterMessageCallback(
      "disableEventLogRecordings",
      base::Bind(&WebRTCInternalsMessageHandler::OnSetEventLogRecordingsEnabled,
                 base::Unretained(this), false));

  web_ui()->RegisterMessageCallback("finishedDOMLoad",
      base::Bind(&WebRTCInternalsMessageHandler::OnDOMLoadDone,
                 base::Unretained(this)));
}

RenderFrameHost* WebRTCInternalsMessageHandler::GetWebRTCInternalsHost() const {
  RenderFrameHost* host = web_ui()->GetWebContents()->GetMainFrame();
  if (host) {
    // Make sure we only ever execute the script in the webrtc-internals page.
    const GURL url(host->GetLastCommittedURL());
    if (!url.SchemeIs(kChromeUIScheme) ||
        url.host() != kChromeUIWebRTCInternalsHost) {
      // Some other page is currently loaded even though we might be in the
      // process of loading webrtc-internals.  So, the current RFH is not the
      // one we're waiting for.
      host = nullptr;
    }
  }

  return host;
}

void WebRTCInternalsMessageHandler::OnGetAllStats(
    const base::ListValue* /* unused_list */) {
  for (RenderProcessHost::iterator i(
       content::RenderProcessHost::AllHostsIterator());
       !i.IsAtEnd(); i.Advance()) {
    i.GetCurrentValue()->Send(new PeerConnectionTracker_GetAllStats());
  }
}

void WebRTCInternalsMessageHandler::OnSetAudioDebugRecordingsEnabled(
    bool enable, const base::ListValue* /* unused_list */) {
  if (enable) {
    webrtc_internals_->EnableAudioDebugRecordings(web_ui()->GetWebContents());
  } else {
    webrtc_internals_->DisableAudioDebugRecordings();
  }
}

void WebRTCInternalsMessageHandler::OnSetEventLogRecordingsEnabled(
    bool enable,
    const base::ListValue* /* unused_list */) {
  if (!webrtc_internals_->CanToggleEventLogRecordings()) {
    LOG(WARNING) << "Cannot toggle WebRTC event logging.";
    return;
  }

  if (enable) {
    webrtc_internals_->EnableLocalEventLogRecordings(
        web_ui()->GetWebContents());
  } else {
    webrtc_internals_->DisableLocalEventLogRecordings();
  }
}

void WebRTCInternalsMessageHandler::OnDOMLoadDone(
    const base::ListValue* /* unused_list */) {
  webrtc_internals_->UpdateObserver(this);

  if (webrtc_internals_->IsAudioDebugRecordingsEnabled())
    ExecuteJavascriptCommand("setAudioDebugRecordingsEnabled", nullptr);

  if (webrtc_internals_->IsEventLogRecordingsEnabled())
    ExecuteJavascriptCommand("setEventLogRecordingsEnabled", nullptr);

  const base::Value can_toggle(
      webrtc_internals_->CanToggleEventLogRecordings());
  ExecuteJavascriptCommand("setEventLogRecordingsToggleability", &can_toggle);
}

void WebRTCInternalsMessageHandler::OnUpdate(const char* command,
                                             const base::Value* args) {
  ExecuteJavascriptCommand(command, args);
}

// TODO(eladalon): Make this function accept a vector of base::Values.
// https://crbug.com/817384
void WebRTCInternalsMessageHandler::ExecuteJavascriptCommand(
    const char* command,
    const base::Value* args) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  RenderFrameHost* host = GetWebRTCInternalsHost();
  if (!host)
    return;

  std::vector<const base::Value*> args_vector;
  if (args)
    args_vector.push_back(args);

  base::string16 script = WebUI::GetJavascriptCall(command, args_vector);
  host->ExecuteJavaScript(script);
}

}  // namespace content