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

content / browser / screenlock_monitor / screenlock_monitor_device_source.h [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_SCREENLOCK_MONITOR_SCREENLOCK_MONITOR_DEVICE_SOURCE_H_
#define CONTENT_BROWSER_SCREENLOCK_MONITOR_SCREENLOCK_MONITOR_DEVICE_SOURCE_H_

#include <memory>

#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "content/browser/screenlock_monitor/screenlock_monitor_source.h"
#include "content/common/content_export.h"

#if BUILDFLAG(IS_WIN)
#include <windows.h>

#include <wtsapi32.h>
#endif  // BUILDFLAG(IS_WIN)

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include <optional>

#include "components/session_manager/core/session_manager_observer.h"
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

#if BUILDFLAG(IS_CHROMEOS_LACROS)
#include <optional>

#include "chromeos/crosapi/mojom/login_state.mojom.h"  // nogncheck
#include "mojo/public/cpp/bindings/receiver.h"
#endif  // BUILDFLAG(IS_CHROMEOS_LACROS)

#if BUILDFLAG(IS_WIN)
namespace gfx {
class SingletonHwndObserver;
}  // namespace gfx
#endif  // BUILDFLAG(IS_WIN)

namespace content {

// A class used to monitor the screenlock state change on each supported
// platform and notify the change event to monitor.
class CONTENT_EXPORT ScreenlockMonitorDeviceSource
    : public ScreenlockMonitorSource {
 public:
  ScreenlockMonitorDeviceSource();

  ScreenlockMonitorDeviceSource(const ScreenlockMonitorDeviceSource&) = delete;
  ScreenlockMonitorDeviceSource& operator=(
      const ScreenlockMonitorDeviceSource&) = delete;

  ~ScreenlockMonitorDeviceSource() override;

#if BUILDFLAG(IS_WIN)
  // Fake session notification registration/unregistration APIs allow us to test
  // receiving and handling messages that look as if they are sent by other
  // sessions, without having to create a session host and a second session.
  using WTSRegisterSessionNotificationFunction = bool (*)(HWND hwnd,
                                                          DWORD flags);
  using WTSUnRegisterSessionNotificationFunction = bool (*)(HWND hwnd);
  static void SetFakeNotificationAPIsForTesting(
      WTSRegisterSessionNotificationFunction register_function,
      WTSUnRegisterSessionNotificationFunction unregister_function);
#endif  // BUILDFLAG(IS_WIN)

 private:
#if BUILDFLAG(IS_WIN)
  // Represents a singleton hwnd for screenlock message handling on Win.
  // Only allow ScreenlockMonitor to create it.
  class SessionMessageWindow {
   public:
    SessionMessageWindow();

    SessionMessageWindow(const SessionMessageWindow&) = delete;
    SessionMessageWindow& operator=(const SessionMessageWindow&) = delete;

    ~SessionMessageWindow();

    static void SetFakeNotificationAPIsForTesting(
        WTSRegisterSessionNotificationFunction register_function,
        WTSUnRegisterSessionNotificationFunction unregister_function);

   private:
    void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
    void ProcessWTSSessionLockMessage(WPARAM event_id);

    static WTSRegisterSessionNotificationFunction
        register_session_notification_function_;
    static WTSUnRegisterSessionNotificationFunction
        unregister_session_notification_function_;
    std::unique_ptr<gfx::SingletonHwndObserver> singleton_hwnd_observer_;
  };

  SessionMessageWindow session_message_window_;
#endif  // BUILDFLAG(IS_WIN)

#if BUILDFLAG(IS_MAC)
  void StartListeningForScreenlock();
  void StopListeningForScreenlock();
#endif  // BUILDFLAG(IS_MAC)

#if BUILDFLAG(IS_CHROMEOS_ASH)
  class ScreenLockListener : public session_manager::SessionManagerObserver {
   public:
    ScreenLockListener();

    ScreenLockListener(const ScreenLockListener&) = delete;
    ScreenLockListener& operator=(const ScreenLockListener&) = delete;

    ~ScreenLockListener() override;

    // session_manager::SessionManagerObserver:
    void OnSessionStateChanged() override;

   private:
    std::optional<ScreenlockEvent> prev_event_;
  };

  ScreenLockListener screenlock_listener_;
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

#if BUILDFLAG(IS_CHROMEOS_LACROS)
  class ScreenLockListener
      : public crosapi::mojom::SessionStateChangedEventObserver {
   public:
    ScreenLockListener();

    ScreenLockListener(const ScreenLockListener&) = delete;
    ScreenLockListener& operator=(const ScreenLockListener&) = delete;

    ~ScreenLockListener() override;

    // crosapi::mojom::SessionStateChangedEventObserver:
    void OnSessionStateChanged(crosapi::mojom::SessionState state) override;

   private:
    std::optional<ScreenlockEvent> prev_event_;
    mojo::Receiver<crosapi::mojom::SessionStateChangedEventObserver> receiver_;
  };

  ScreenLockListener screenlock_listener_;
#endif  // BUILDFLAG(IS_CHROMEOS_LACROS)
};

}  // namespace content

#endif  // CONTENT_BROWSER_SCREENLOCK_MONITOR_SCREENLOCK_MONITOR_DEVICE_SOURCE_H_