1 // Copyright 2014 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.
5 // An implementation of SyncNotifier that wraps InvalidationNotifier
8 #ifndef COMPONENTS_INVALIDATION_NON_BLOCKING_INVALIDATOR_H_
9 #define COMPONENTS_INVALIDATION_NON_BLOCKING_INVALIDATOR_H_
13 #include "base/basictypes.h"
14 #include "base/callback.h"
15 #include "base/compiler_specific.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/weak_ptr.h"
18 #include "components/invalidation/invalidation_export.h"
19 #include "components/invalidation/invalidation_state_tracker.h"
20 #include "components/invalidation/invalidator.h"
21 #include "components/invalidation/invalidator_registrar.h"
22 #include "components/invalidation/invalidator_state.h"
23 #include "components/invalidation/unacked_invalidation_set.h"
24 #include "jingle/notifier/base/notifier_options.h"
27 class SingleThreadTaskRunner
;
31 class SyncNetworkChannel
;
32 class GCMNetworkChannelDelegate
;
34 // Callback type for function that creates SyncNetworkChannel. This function
35 // gets passed into NonBlockingInvalidator constructor.
36 typedef base::Callback
<scoped_ptr
<SyncNetworkChannel
>(void)>
37 NetworkChannelCreator
;
39 class INVALIDATION_EXPORT_PRIVATE NonBlockingInvalidator
41 public InvalidationStateTracker
{
43 // |invalidation_state_tracker| must be initialized and must outlive |this|.
44 NonBlockingInvalidator(
45 NetworkChannelCreator network_channel_creator
,
46 const std::string
& invalidator_client_id
,
47 const UnackedInvalidationsMap
& saved_invalidations
,
48 const std::string
& invalidation_bootstrap_data
,
49 InvalidationStateTracker
* invalidation_state_tracker
,
50 const std::string
& client_info
,
51 const scoped_refptr
<net::URLRequestContextGetter
>&
52 request_context_getter
);
54 virtual ~NonBlockingInvalidator();
56 // Invalidator implementation.
57 virtual void RegisterHandler(InvalidationHandler
* handler
) OVERRIDE
;
58 virtual void UpdateRegisteredIds(InvalidationHandler
* handler
,
59 const ObjectIdSet
& ids
) OVERRIDE
;
60 virtual void UnregisterHandler(InvalidationHandler
* handler
) OVERRIDE
;
61 virtual InvalidatorState
GetInvalidatorState() const OVERRIDE
;
62 virtual void UpdateCredentials(
63 const std::string
& email
, const std::string
& token
) OVERRIDE
;
64 virtual void RequestDetailedStatus(
65 base::Callback
<void(const base::DictionaryValue
&)> callback
) const
68 // Static functions to construct callback that creates network channel for
69 // SyncSystemResources. The goal is to pass network channel to invalidator at
70 // the same time not exposing channel specific parameters to invalidator and
71 // channel implementation to client of invalidator.
72 static NetworkChannelCreator
MakePushClientChannelCreator(
73 const notifier::NotifierOptions
& notifier_options
);
74 static NetworkChannelCreator
MakeGCMNetworkChannelCreator(
75 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter
,
76 scoped_ptr
<GCMNetworkChannelDelegate
> delegate
);
78 // These methods are forwarded to the invalidation_state_tracker_.
79 virtual void ClearAndSetNewClientId(const std::string
& data
) OVERRIDE
;
80 virtual std::string
GetInvalidatorClientId() const OVERRIDE
;
81 virtual void SetBootstrapData(const std::string
& data
) OVERRIDE
;
82 virtual std::string
GetBootstrapData() const OVERRIDE
;
83 virtual void SetSavedInvalidations(
84 const UnackedInvalidationsMap
& states
) OVERRIDE
;
85 virtual UnackedInvalidationsMap
GetSavedInvalidations() const OVERRIDE
;
86 virtual void Clear() OVERRIDE
;
89 void OnInvalidatorStateChange(InvalidatorState state
);
90 void OnIncomingInvalidation(const ObjectIdInvalidationMap
& invalidation_map
);
91 std::string
GetOwnerName() const;
93 friend class NonBlockingInvalidatorTestDelegate
;
95 struct InitializeOptions
;
98 InvalidatorRegistrar registrar_
;
99 InvalidationStateTracker
* invalidation_state_tracker_
;
101 // The real guts of NonBlockingInvalidator, which allows this class to live
102 // completely on the parent thread.
103 scoped_refptr
<Core
> core_
;
104 scoped_refptr
<base::SingleThreadTaskRunner
> parent_task_runner_
;
105 scoped_refptr
<base::SingleThreadTaskRunner
> network_task_runner_
;
107 base::WeakPtrFactory
<NonBlockingInvalidator
> weak_ptr_factory_
;
109 DISALLOW_COPY_AND_ASSIGN(NonBlockingInvalidator
);
112 } // namespace syncer
114 #endif // COMPONENTS_INVALIDATION_NON_BLOCKING_INVALIDATOR_H_