Chromium Code Reviews| Index: chrome/browser/chromeos/cros/resume_library.cc |
| diff --git a/chrome/browser/chromeos/cros/resume_library.cc b/chrome/browser/chromeos/cros/resume_library.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f9aa64906b41c00ae48a396f49adb6b475e6dc1f |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/cros/resume_library.cc |
| @@ -0,0 +1,89 @@ |
| +// 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/resume_library.h" |
| + |
| +#include "base/message_loop.h" |
| +#include "base/observer_list.h" |
| +#include "chrome/browser/browser_thread.h" |
| +#include "chrome/browser/chromeos/cros/cros_library.h" |
| +#include "third_party/cros/chromeos_resume.h" |
| + |
| +namespace chromeos { |
| + |
| +class ResumeLibraryImpl : public ResumeLibrary { |
| + public: |
| + ResumeLibraryImpl() : connection_(NULL) { |
| + if (CrosLibrary::Get()->EnsureLoaded()) |
| + Init(); |
| + } |
| + |
| + ~ResumeLibraryImpl() { |
| + if (connection_) { |
| + chromeos::DisconnectResume(connection_); |
| + connection_ = NULL; |
| + } |
| + } |
| + |
| + void AddObserver(Observer* observer) { |
| + observers_.AddObserver(observer); |
| + } |
| + |
| + void RemoveObserver(Observer* observer) { |
| + observers_.RemoveObserver(observer); |
| + } |
| + |
| + private: |
| + static void OnSystemResumedHandler(void* object) { |
| + ResumeLibraryImpl* self = static_cast<ResumeLibraryImpl*>(object); |
| + self->OnSystemResumed(); |
| + } |
| + |
| + void Init() { |
| + DCHECK(!connection_) << "Already intialized"; |
| + connection_ = chromeos::MonitorResume(&OnSystemResumedHandler, this); |
| + } |
| + |
| + void OnSystemResumed() { |
| + // Make sure we run on the UI thread. |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + NewRunnableMethod(this, |
| + &ResumeLibraryImpl::OnSystemResumed)); |
| + return; |
| + } |
| + |
| + FOR_EACH_OBSERVER(Observer, |
|
Nikita (slow)
2011/02/11 11:54:51
nit: could fit into 1 line?
glotov
2011/02/11 18:20:56
Done.
|
| + observers_, |
| + SystemResumed()); |
| + } |
| + |
| + chromeos::ResumeConnection connection_; |
| + |
| + ObserverList<Observer> observers_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ResumeLibraryImpl); |
| +}; |
| + |
| +class ResumeLibraryStubImpl : public ResumeLibrary { |
| + public: |
| + ResumeLibraryStubImpl() {} |
| + ~ResumeLibraryStubImpl() {} |
| + void AddObserver(Observer* observer) {} |
| + void RemoveObserver(Observer* observer) {} |
| +}; |
| + |
| +// static |
| +ResumeLibrary* ResumeLibrary::GetImpl(bool stub) { |
| + if (stub) |
| + return new ResumeLibraryStubImpl(); |
| + else |
| + return new ResumeLibraryImpl(); |
| +} |
| + |
| +} // namespace chromeos |
| + |
| +// Needed for NewRunnableMethod() call above. |
|
Nikita (slow)
2011/02/11 11:54:51
Should mention that this class is Singleton.
glotov
2011/02/11 18:20:56
Done.
|
| +DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::ResumeLibraryImpl); |