Home | History | Annotate | Download | only in listener
      1 // Copyright (c) 2012 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 #ifndef JINGLE_NOTIFIER_LISTENER_NON_BLOCKING_PUSH_CLIENT_H_
      6 #define JINGLE_NOTIFIER_LISTENER_NON_BLOCKING_PUSH_CLIENT_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/callback_forward.h"
     10 #include "base/compiler_specific.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/observer_list.h"
     15 #include "base/threading/thread_checker.h"
     16 #include "jingle/notifier/listener/push_client.h"
     17 #include "jingle/notifier/listener/push_client_observer.h"
     18 
     19 namespace base {
     20 class SingleThreadTaskRunner;
     21 } // namespace base
     22 
     23 namespace notifier {
     24 
     25 // This class implements a PushClient that doesn't block; it delegates
     26 // to another blocking PushClient on a separate thread.
     27 //
     28 // This class must be used on a single thread.
     29 class NonBlockingPushClient : public PushClient {
     30  public:
     31   // The type for a function that creates a (blocking) PushClient.
     32   // Will be called on the delegate task runner.
     33   typedef base::Callback<scoped_ptr<PushClient>()>
     34       CreateBlockingPushClientCallback;
     35 
     36   // Runs the given callback on the given task runner, and delegates
     37   // to that PushClient.
     38   explicit NonBlockingPushClient(
     39       const scoped_refptr<base::SingleThreadTaskRunner>& delegate_task_runner,
     40       const CreateBlockingPushClientCallback&
     41           create_blocking_push_client_callback);
     42   virtual ~NonBlockingPushClient();
     43 
     44   // PushClient implementation.
     45   virtual void AddObserver(PushClientObserver* observer) OVERRIDE;
     46   virtual void RemoveObserver(PushClientObserver* observer) OVERRIDE;
     47   virtual void UpdateSubscriptions(
     48       const SubscriptionList& subscriptions) OVERRIDE;
     49   virtual void UpdateCredentials(
     50       const std::string& email, const std::string& token) OVERRIDE;
     51   virtual void SendNotification(const Notification& notification) OVERRIDE;
     52   virtual void SendPing() OVERRIDE;
     53 
     54  private:
     55   class Core;
     56 
     57   void OnNotificationsEnabled();
     58   void OnNotificationsDisabled(NotificationsDisabledReason reason);
     59   void OnIncomingNotification(const Notification& notification);
     60   void OnPingResponse();
     61 
     62   base::ThreadChecker thread_checker_;
     63   base::WeakPtrFactory<NonBlockingPushClient> weak_ptr_factory_;
     64   const scoped_refptr<base::SingleThreadTaskRunner> delegate_task_runner_;
     65   const scoped_refptr<Core> core_;
     66 
     67   ObserverList<PushClientObserver> observers_;
     68 
     69   DISALLOW_COPY_AND_ASSIGN(NonBlockingPushClient);
     70 };
     71 
     72 }  // namespace notifier
     73 
     74 #endif  // JINGLE_NOTIFIER_LISTENER_NON_BLOCKING_PUSH_CLIENT_H_
     75