summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/cros/screen_lock_library.cc
blob: 96853cb289db179a673e5aabb79a4de301ef029d (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
// Copyright (c) 2011 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/basictypes.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/observer_list.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/cros/chromeos_screen_lock.h"

namespace chromeos {

class ScreenLockLibraryImpl : public ScreenLockLibrary {
 public:
  ScreenLockLibraryImpl() : screen_lock_connection_(NULL) {}

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

  // Begin ScreenLockLibrary implementation.
  virtual void Init() OVERRIDE {
    DCHECK(CrosLibrary::Get()->libcros_loaded());
    screen_lock_connection_ =
        chromeos::MonitorScreenLock(&ScreenLockedHandler, this);
  }

  virtual void AddObserver(Observer* observer) OVERRIDE {
    observers_.AddObserver(observer);
  }

  virtual void RemoveObserver(Observer* observer) OVERRIDE {
    observers_.RemoveObserver(observer);
  }

  virtual void NotifyScreenLockRequested() OVERRIDE {
    chromeos::NotifyScreenLockRequested();
  }

  virtual void NotifyScreenLockCompleted() OVERRIDE {
    chromeos::NotifyScreenLockCompleted();
  }

  virtual void NotifyScreenUnlockRequested() OVERRIDE {
    chromeos::NotifyScreenUnlockRequested();
  }

  virtual void NotifyScreenUnlockCompleted() OVERRIDE {
    chromeos::NotifyScreenUnlockCompleted();
  }
  // End ScreenLockLibrary implementation.

 private:
  static void 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();
        break;
    }
  }

  void LockScreen() {
    // Called from ScreenLockedHandler, a libcros callback which should
    // always run on UI thread.
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    FOR_EACH_OBSERVER(Observer, observers_, LockScreen(this));
  }

  void UnlockScreen() {
    // Called from ScreenLockedHandler, a libcros callback which should
    // always run on UI thread.
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    FOR_EACH_OBSERVER(Observer, observers_, UnlockScreen(this));
  }

  void UnlockScreenFailed() {
    // Called from ScreenLockedHandler, a libcros callback which should
    // always run on UI thread.
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    FOR_EACH_OBSERVER(Observer, observers_, UnlockScreenFailed(this));
  }

  chromeos::ScreenLockConnection screen_lock_connection_;

  ObserverList<Observer> observers_;

  DISALLOW_COPY_AND_ASSIGN(ScreenLockLibraryImpl);
};

class ScreenLockLibraryStubImpl : public ScreenLockLibrary {
 public:
  ScreenLockLibraryStubImpl() {}
  virtual ~ScreenLockLibraryStubImpl() {}
  virtual void Init() OVERRIDE {}
  virtual void AddObserver(Observer* observer) OVERRIDE {}
  virtual void RemoveObserver(Observer* observer) OVERRIDE {}
  virtual void NotifyScreenLockRequested() OVERRIDE {}
  virtual void NotifyScreenLockCompleted() OVERRIDE {}
  virtual void NotifyScreenUnlockRequested() OVERRIDE {}
  virtual void NotifyScreenUnlockCompleted() OVERRIDE {}
};

// static
ScreenLockLibrary* ScreenLockLibrary::GetImpl(bool stub) {
  ScreenLockLibrary* impl;
  if (stub)
    impl = new ScreenLockLibraryStubImpl();
  else
    impl = new ScreenLockLibraryImpl();
  impl->Init();
  return impl;
}

}  // namespace chromeos