Home | History | Annotate | Download | only in base
      1 // Copyright 2013 The Chromium 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 "sync/internal_api/public/base/cancelation_signal.h"
      6 
      7 #include "base/logging.h"
      8 #include "sync/internal_api/public/base/cancelation_observer.h"
      9 
     10 namespace syncer {
     11 
     12 CancelationSignal::CancelationSignal()
     13   : signalled_(false),
     14     handler_(NULL) { }
     15 
     16 CancelationSignal::~CancelationSignal() {
     17   DCHECK(!handler_);
     18 }
     19 
     20 bool CancelationSignal::TryRegisterHandler(CancelationObserver* handler) {
     21   base::AutoLock lock(signal_lock_);
     22   DCHECK(!handler_);
     23 
     24   if (signalled_)
     25     return false;
     26 
     27   handler_ = handler;
     28   return true;
     29 }
     30 
     31 void CancelationSignal::UnregisterHandler(CancelationObserver* handler) {
     32   base::AutoLock lock(signal_lock_);
     33   DCHECK_EQ(handler_, handler);
     34   handler_ = NULL;
     35 }
     36 
     37 bool CancelationSignal::IsSignalled() {
     38   base::AutoLock lock(signal_lock_);
     39   return signalled_;
     40 }
     41 
     42 void CancelationSignal::Signal() {
     43   base::AutoLock lock(signal_lock_);
     44   DCHECK(!signalled_);
     45 
     46   signalled_ = true;
     47   if (handler_) {
     48     handler_->OnSignalReceived();
     49   }
     50 }
     51 
     52 }  // namespace syncer
     53