blob: 043b556acdec79caef55fa276fdf8f3da381291b [file] [log] [blame]
Xiaohui Chena8bced82015-02-27 10:35:26 -08001// Copyright 2015 The Chromium OS 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 "debugd/src/session_manager_proxy.h"
6
Eric Carusocc7106c2017-04-27 14:22:42 -07007#include <base/bind.h>
Xiaohui Chena8bced82015-02-27 10:35:26 -08008#include <base/logging.h>
Eric Carusocc7106c2017-04-27 14:22:42 -07009#include <chromeos/dbus/service_constants.h>
10#include <dbus/message.h>
11#include <dbus/object_proxy.h>
12
13namespace {
14
Eric Carusocc7106c2017-04-27 14:22:42 -070015void OnSignalConnected(const std::string& interface,
16 const std::string& signal,
17 bool success) {
18 if (!success) {
Tom Hughesd6c2d392020-08-24 18:12:11 -070019 LOG(ERROR) << "Could not connect to signal " << signal << " on interface "
20 << interface;
Eric Carusocc7106c2017-04-27 14:22:42 -070021 }
22}
23
24} // namespace
Xiaohui Chena8bced82015-02-27 10:35:26 -080025
26namespace debugd {
27
Eric Carusocc7106c2017-04-27 14:22:42 -070028SessionManagerProxy::SessionManagerProxy(scoped_refptr<dbus::Bus> bus)
29 : bus_(bus),
30 proxy_(bus->GetObjectProxy(
Tom Hughesd6c2d392020-08-24 18:12:11 -070031 login_manager::kSessionManagerServiceName,
32 dbus::ObjectPath(login_manager::kSessionManagerServicePath))),
Eric Carusocc7106c2017-04-27 14:22:42 -070033 weak_ptr_factory_(this) {
Tom Hughesd6c2d392020-08-24 18:12:11 -070034 proxy_->ConnectToSignal(login_manager::kSessionManagerInterface,
35 login_manager::kLoginPromptVisibleSignal,
36 base::Bind(&SessionManagerProxy::OnLoginPromptVisible,
37 weak_ptr_factory_.GetWeakPtr()),
38 base::Bind(&OnSignalConnected));
Eric Carusocc7106c2017-04-27 14:22:42 -070039}
40
Eric Carusocc7106c2017-04-27 14:22:42 -070041void SessionManagerProxy::OnLoginPromptVisible(dbus::Signal*) {
Jorge Lucangeli Obes9d5a1c52015-03-20 15:43:21 +000042 // Try to enable Chrome remote debugging again on Login prompt.
Xiaohui Chena8bced82015-02-27 10:35:26 -080043 // Theoretically it should already be enabled during debugd Init(). But
44 // There might be a timing issue if debugd started too fast. We try again
45 // here if the first attempt in Init() failed.
Jorge Lucangeli Obes9d5a1c52015-03-20 15:43:21 +000046 EnableChromeRemoteDebuggingInternal();
Xiaohui Chena8bced82015-02-27 10:35:26 -080047}
48
49void SessionManagerProxy::EnableChromeRemoteDebugging() {
50 VLOG(1) << "Enable Chrome remote debugging: "
Tom Hughesd6c2d392020-08-24 18:12:11 -070051 << should_enable_chrome_remote_debugging_ << " "
Xiaohui Chena8bced82015-02-27 10:35:26 -080052 << is_chrome_remote_debugging_enabled_;
53 should_enable_chrome_remote_debugging_ = true;
54 EnableChromeRemoteDebuggingInternal();
55}
56
57void SessionManagerProxy::EnableChromeRemoteDebuggingInternal() {
Jorge Lucangeli Obes9d5a1c52015-03-20 15:43:21 +000058 VLOG(1) << "Enable Chrome remote debugging internal: "
Tom Hughesd6c2d392020-08-24 18:12:11 -070059 << should_enable_chrome_remote_debugging_ << " "
Jorge Lucangeli Obes9d5a1c52015-03-20 15:43:21 +000060 << is_chrome_remote_debugging_enabled_;
Xiaohui Chena8bced82015-02-27 10:35:26 -080061 if (!should_enable_chrome_remote_debugging_ ||
62 is_chrome_remote_debugging_enabled_) {
63 return;
64 }
Eric Carusocc7106c2017-04-27 14:22:42 -070065
Daniel Erat8ae9ce62017-12-16 01:02:41 -080066 dbus::MethodCall method_call(
67 login_manager::kSessionManagerInterface,
68 login_manager::kSessionManagerEnableChromeTesting);
Eric Carusocc7106c2017-04-27 14:22:42 -070069 dbus::MessageWriter writer(&method_call);
Daniel Erat8ae9ce62017-12-16 01:02:41 -080070 writer.AppendBool(true); // force_restart
Eric Carusocc7106c2017-04-27 14:22:42 -070071 writer.AppendArrayOfStrings({"--remote-debugging-port=9222"});
Daniel Erat8ae9ce62017-12-16 01:02:41 -080072 writer.AppendArrayOfStrings({}); // extra_environment_variables
Eric Carusocc7106c2017-04-27 14:22:42 -070073 if (proxy_->CallMethodAndBlock(&method_call,
74 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)) {
Xiaohui Chena8bced82015-02-27 10:35:26 -080075 is_chrome_remote_debugging_enabled_ = true;
Eric Carusocc7106c2017-04-27 14:22:42 -070076 } else {
77 LOG(ERROR) << "Failed to enable Chrome remote debugging";
Xiaohui Chena8bced82015-02-27 10:35:26 -080078 }
79}
80
Xiaohui Chena8bced82015-02-27 10:35:26 -080081} // namespace debugd