summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/cros/screen_lock_library.cc
blob: 731ec438d72738bf4d1d0b542f4e17b9ba87ed50 (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
// Copyright (c) 2010 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 "chrome/browser/chromeos/cros/screen_lock_library.h"

#include "base/message_loop.h"
#include "base/string_util.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/chromeos/cros/cros_library.h"

// Allows InvokeLater without adding refcounting. This class is a Singleton and
// won't be deleted until it's last InvokeLater is run.
DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::ScreenLockLibraryImpl);

namespace chromeos {

ScreenLockLibraryImpl::ScreenLockLibraryImpl() {
  if (CrosLibrary::Get()->EnsureLoaded()) {
    Init();
  }
}

ScreenLockLibraryImpl::~ScreenLockLibraryImpl() {
  if (screen_lock_connection_) {
    chromeos::DisconnectScreenLock(screen_lock_connection_);
  }
}

void ScreenLockLibraryImpl::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void ScreenLockLibraryImpl::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

void ScreenLockLibraryImpl::NotifyScreenLockRequested() {
  // Make sure we run on IO thread.
  if (!ChromeThread::CurrentlyOn(ChromeThread::IO)) {
    ChromeThread::PostTask(
        ChromeThread::IO, FROM_HERE,
        NewRunnableMethod(this,
                          &ScreenLockLibraryImpl::NotifyScreenLockRequested));
    return;
  }
  chromeos::NotifyScreenLockRequested();
}

void ScreenLockLibraryImpl::NotifyScreenLockCompleted() {
  // Make sure we run on IO thread.
  if (!ChromeThread::CurrentlyOn(ChromeThread::IO)) {
    ChromeThread::PostTask(
        ChromeThread::IO, FROM_HERE,
        NewRunnableMethod(this,
                          &ScreenLockLibraryImpl::NotifyScreenLockCompleted));
    return;
  }
  chromeos::NotifyScreenLockCompleted();
}

void ScreenLockLibraryImpl::NotifyScreenUnlockRequested() {
  // Make sure we run on IO thread.
  if (!ChromeThread::CurrentlyOn(ChromeThread::IO)) {
    ChromeThread::PostTask(
        ChromeThread::IO, FROM_HERE,
        NewRunnableMethod(this,
                          &ScreenLockLibraryImpl::NotifyScreenUnlockRequested));
    return;
  }
  chromeos::NotifyScreenUnlockRequested();
}

void ScreenLockLibraryImpl::NotifyScreenUnlockCompleted() {
  // Make sure we run on IO thread.
  if (!ChromeThread::CurrentlyOn(ChromeThread::IO)) {
    ChromeThread::PostTask(
        ChromeThread::IO, FROM_HERE,
        NewRunnableMethod(this,
                          &ScreenLockLibraryImpl::NotifyScreenUnlockCompleted));
    return;
  }
  chromeos::NotifyScreenUnlockCompleted();
}

void ScreenLockLibraryImpl::Init() {
  screen_lock_connection_ = chromeos::MonitorScreenLock(
      &ScreenLockedHandler, this);
}

void ScreenLockLibraryImpl::LockScreen() {
  // Make sure we run on UI thread.
  if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) {
    ChromeThread::PostTask(
        ChromeThread::UI, FROM_HERE,
        NewRunnableMethod(this, &ScreenLockLibraryImpl::LockScreen));
    return;
  }
  FOR_EACH_OBSERVER(Observer, observers_, LockScreen(this));
}

void ScreenLockLibraryImpl::UnlockScreen() {
  // Make sure we run on UI thread.
  if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) {
    ChromeThread::PostTask(
        ChromeThread::UI, FROM_HERE,
        NewRunnableMethod(this, &ScreenLockLibraryImpl::UnlockScreen));
    return;
  }
  FOR_EACH_OBSERVER(Observer, observers_, UnlockScreen(this));
}

void ScreenLockLibraryImpl::UnlockScreenFailed() {
  // Make sure we run on UI thread.
  if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) {
    ChromeThread::PostTask(
        ChromeThread::UI, FROM_HERE,
        NewRunnableMethod(this, &ScreenLockLibraryImpl::UnlockScreenFailed));
    return;
  }
  FOR_EACH_OBSERVER(Observer, observers_, UnlockScreenFailed(this));
}

// static
void ScreenLockLibraryImpl::ScreenLockedHandler(void* object,
                                                ScreenLockEvent event) {
  ScreenLockLibraryImpl* self = static_cast<ScreenLockLibraryImpl*>(object);
  switch (event) {
    case chromeos::LockScreen:
      self->LockScreen();
      break;
    case chromeos::UnlockScreen:
      self->UnlockScreen();
      break;
    case chromeos::UnlockScreenFailed:
      self->UnlockScreenFailed();
      break;
    default:
      NOTREACHED();
  }
}

}  // namespace chromeos