OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/invalidation/invalidation_service_android.h" | 5 #include "chrome/browser/invalidation/invalidation_service_android.h" |
6 | 6 |
| 7 #include "base/android/jni_array.h" |
7 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
8 #include "content/public/browser/notification_service.h" | 9 #include "content/public/browser/notification_service.h" |
| 10 #include "google/cacheinvalidation/types.pb.h" |
| 11 #include "sync/jni/InvalidationController_jni.h" |
9 | 12 |
10 namespace invalidation { | 13 namespace invalidation { |
11 | 14 |
12 InvalidationServiceAndroid::InvalidationServiceAndroid(Profile* profile) | 15 InvalidationServiceAndroid::InvalidationServiceAndroid(Profile* profile) |
13 : invalidator_state_(syncer::INVALIDATIONS_ENABLED) { | 16 : invalidator_state_(syncer::INVALIDATIONS_ENABLED) { |
14 DCHECK(CalledOnValidThread()); | 17 DCHECK(CalledOnValidThread()); |
15 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | 18 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, |
16 content::Source<Profile>(profile)); | 19 content::Source<Profile>(profile)); |
17 } | 20 } |
18 | 21 |
19 InvalidationServiceAndroid::~InvalidationServiceAndroid() { } | 22 InvalidationServiceAndroid::~InvalidationServiceAndroid() { } |
20 | 23 |
21 void InvalidationServiceAndroid::RegisterInvalidationHandler( | 24 void InvalidationServiceAndroid::RegisterInvalidationHandler( |
22 syncer::InvalidationHandler* handler) { | 25 syncer::InvalidationHandler* handler) { |
23 DCHECK(CalledOnValidThread()); | 26 DCHECK(CalledOnValidThread()); |
24 invalidator_registrar_.RegisterHandler(handler); | 27 invalidator_registrar_.RegisterHandler(handler); |
25 } | 28 } |
26 | 29 |
27 void InvalidationServiceAndroid::UpdateRegisteredInvalidationIds( | 30 void InvalidationServiceAndroid::UpdateRegisteredInvalidationIds( |
28 syncer::InvalidationHandler* handler, | 31 syncer::InvalidationHandler* handler, |
29 const syncer::ObjectIdSet& ids) { | 32 const syncer::ObjectIdSet& ids) { |
30 DCHECK(CalledOnValidThread()); | 33 DCHECK(CalledOnValidThread()); |
| 34 // Registration with the Android invalidation controller is needed if any of |
| 35 // the objects in the current or replacement object sets require it. |
| 36 bool isRegistrationRequired = IsRegistrationRequired(ids) || |
| 37 IsRegistrationRequired(invalidator_registrar_.GetRegisteredIds(handler)); |
31 invalidator_registrar_.UpdateRegisteredIds(handler, ids); | 38 invalidator_registrar_.UpdateRegisteredIds(handler, ids); |
| 39 if (isRegistrationRequired) |
| 40 UpdateRegistration(); |
32 } | 41 } |
33 | 42 |
34 void InvalidationServiceAndroid::UnregisterInvalidationHandler( | 43 void InvalidationServiceAndroid::UnregisterInvalidationHandler( |
35 syncer::InvalidationHandler* handler) { | 44 syncer::InvalidationHandler* handler) { |
36 DCHECK(CalledOnValidThread()); | 45 DCHECK(CalledOnValidThread()); |
37 invalidator_registrar_.UnregisterHandler(handler); | 46 invalidator_registrar_.UnregisterHandler(handler); |
38 } | 47 } |
39 | 48 |
40 void InvalidationServiceAndroid::AcknowledgeInvalidation( | 49 void InvalidationServiceAndroid::AcknowledgeInvalidation( |
41 const invalidation::ObjectId& id, | 50 const invalidation::ObjectId& id, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 effective_invalidation_map); | 90 effective_invalidation_map); |
82 } | 91 } |
83 | 92 |
84 void InvalidationServiceAndroid::TriggerStateChangeForTest( | 93 void InvalidationServiceAndroid::TriggerStateChangeForTest( |
85 syncer::InvalidatorState state) { | 94 syncer::InvalidatorState state) { |
86 DCHECK(CalledOnValidThread()); | 95 DCHECK(CalledOnValidThread()); |
87 invalidator_state_ = state; | 96 invalidator_state_ = state; |
88 invalidator_registrar_.UpdateInvalidatorState(invalidator_state_); | 97 invalidator_registrar_.UpdateInvalidatorState(invalidator_state_); |
89 } | 98 } |
90 | 99 |
| 100 void InvalidationServiceAndroid::SetUpdateRegistrationCallbackForTest( |
| 101 const UpdateRegistrationCallback& callback) { |
| 102 update_registration_callback_ = callback; |
| 103 } |
| 104 |
| 105 void InvalidationServiceAndroid::UpdateRegistration() { |
| 106 // Create a list of object sources and names which require registration. |
| 107 std::vector<int> sources; |
| 108 std::vector<std::string> names; |
| 109 syncer::ObjectIdSet ids = invalidator_registrar_.GetAllRegisteredIds(); |
| 110 syncer::ObjectIdSet::const_iterator id; |
| 111 for (id = ids.begin(); id != ids.end(); ++id) { |
| 112 if (IsRegistrationRequired(*id)) { |
| 113 sources.push_back(id->source()); |
| 114 names.push_back(id->name()); |
| 115 } |
| 116 } |
| 117 |
| 118 // Invoke callback for testing if provided. |
| 119 if (!update_registration_callback_.is_null()) { |
| 120 update_registration_callback_.Run(sources, names); |
| 121 return; |
| 122 } |
| 123 |
| 124 // Get a reference to the invalidation controller. |
| 125 JNIEnv* env = base::android::AttachCurrentThread(); |
| 126 DCHECK(env); |
| 127 if (invalidation_controller_.is_null()) { |
| 128 invalidation_controller_.Reset(Java_InvalidationController_get( |
| 129 env, |
| 130 base::android::GetApplicationContext())); |
| 131 } |
| 132 |
| 133 // Update the registration with the invalidation controller. |
| 134 Java_InvalidationController_setRegisteredObjectIds( |
| 135 env, |
| 136 invalidation_controller_.obj(), |
| 137 base::android::ToJavaIntArray(env, sources).obj(), |
| 138 base::android::ToJavaArrayOfStrings(env, names).obj()); |
| 139 } |
| 140 |
| 141 bool InvalidationServiceAndroid::IsRegistrationRequired( |
| 142 const syncer::ObjectIdSet& ids) { |
| 143 syncer::ObjectIdSet::const_iterator id; |
| 144 for (id = ids.begin(); id != ids.end(); ++id) |
| 145 if (IsRegistrationRequired(*id)) |
| 146 return true; |
| 147 return false; |
| 148 } |
| 149 |
| 150 bool InvalidationServiceAndroid::IsRegistrationRequired( |
| 151 const invalidation::ObjectId& id) { |
| 152 // Sync types are registered on the Java side, so only non-Sync objects must |
| 153 // be registered with the Android invalidation controller by this class. |
| 154 return id.source() != ipc::invalidation::ObjectSource::CHROME_SYNC; |
| 155 } |
| 156 |
| 157 bool RegisterInvalidationController(JNIEnv* env) { |
| 158 return RegisterNativesImpl(env); |
| 159 } |
| 160 |
91 } // namespace invalidation | 161 } // namespace invalidation |
OLD | NEW |