Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
|
nyquist
2014/09/04 09:01:56
2014 and no (c) now, since the move detection did
maxbogue
2014/09/05 16:42:46
Done.
| |
| 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 "components/invalidation/invalidation_service_android.h" | |
| 6 | |
| 7 #include "base/android/jni_android.h" | |
| 8 #include "base/android/jni_array.h" | |
| 9 #include "base/android/jni_string.h" | |
| 10 #include "base/callback.h" | |
| 11 #include "components/invalidation/object_id_invalidation_map.h" | |
| 12 #include "google/cacheinvalidation/types.pb.h" | |
| 13 #include "jni/InvalidationService_jni.h" | |
| 14 | |
| 15 using base::android::ConvertJavaStringToUTF8; | |
| 16 using base::android::ConvertUTF8ToJavaString; | |
| 17 | |
| 18 namespace invalidation { | |
| 19 | |
| 20 InvalidationServiceAndroid::InvalidationServiceAndroid(jobject context) | |
| 21 : invalidator_state_(syncer::INVALIDATIONS_ENABLED), | |
| 22 logger_() { | |
| 23 DCHECK(CalledOnValidThread()); | |
| 24 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 25 base::android::ScopedJavaLocalRef<jobject> local_java_ref = | |
| 26 Java_InvalidationService_create(env, | |
| 27 context, | |
| 28 reinterpret_cast<intptr_t>(this)); | |
| 29 java_ref_.Reset(env, local_java_ref.obj()); | |
| 30 } | |
| 31 | |
| 32 InvalidationServiceAndroid::~InvalidationServiceAndroid() { } | |
| 33 | |
| 34 void InvalidationServiceAndroid::RegisterInvalidationHandler( | |
| 35 syncer::InvalidationHandler* handler) { | |
| 36 DCHECK(CalledOnValidThread()); | |
| 37 invalidator_registrar_.RegisterHandler(handler); | |
| 38 logger_.OnRegistration(handler->GetOwnerName()); | |
| 39 } | |
| 40 | |
| 41 void InvalidationServiceAndroid::UpdateRegisteredInvalidationIds( | |
| 42 syncer::InvalidationHandler* handler, | |
| 43 const syncer::ObjectIdSet& ids) { | |
| 44 DCHECK(CalledOnValidThread()); | |
| 45 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 46 DCHECK(env); | |
| 47 | |
| 48 invalidator_registrar_.UpdateRegisteredIds(handler, ids); | |
| 49 const syncer::ObjectIdSet& registered_ids = | |
| 50 invalidator_registrar_.GetAllRegisteredIds(); | |
| 51 | |
| 52 // To call the corresponding method on the Java invalidation service, split | |
| 53 // the object ids into object source and object name arrays. | |
| 54 std::vector<int> sources; | |
| 55 std::vector<std::string> names; | |
| 56 syncer::ObjectIdSet::const_iterator id; | |
| 57 for (id = registered_ids.begin(); id != registered_ids.end(); ++id) { | |
| 58 sources.push_back(id->source()); | |
| 59 names.push_back(id->name()); | |
| 60 } | |
| 61 | |
| 62 Java_InvalidationService_setRegisteredObjectIds( | |
| 63 env, | |
| 64 java_ref_.obj(), | |
| 65 base::android::ToJavaIntArray(env, sources).obj(), | |
| 66 base::android::ToJavaArrayOfStrings(env, names).obj()); | |
| 67 | |
| 68 logger_.OnUpdateIds(invalidator_registrar_.GetSanitizedHandlersIdsMap()); | |
| 69 } | |
| 70 | |
| 71 void InvalidationServiceAndroid::UnregisterInvalidationHandler( | |
| 72 syncer::InvalidationHandler* handler) { | |
| 73 DCHECK(CalledOnValidThread()); | |
| 74 invalidator_registrar_.UnregisterHandler(handler); | |
| 75 logger_.OnUnregistration(handler->GetOwnerName()); | |
| 76 } | |
| 77 | |
| 78 syncer::InvalidatorState | |
| 79 InvalidationServiceAndroid::GetInvalidatorState() const { | |
| 80 DCHECK(CalledOnValidThread()); | |
| 81 return invalidator_state_; | |
| 82 } | |
| 83 | |
| 84 std::string InvalidationServiceAndroid::GetInvalidatorClientId() const { | |
| 85 DCHECK(CalledOnValidThread()); | |
| 86 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 87 DCHECK(env); | |
| 88 | |
| 89 // Ask the Java code to for the invalidator ID it's currently using. | |
| 90 base::android::ScopedJavaLocalRef<_jbyteArray*> id_bytes_java = | |
| 91 Java_InvalidationService_getInvalidatorClientId(env, java_ref_.obj()); | |
| 92 | |
| 93 // Convert it into a more convenient format for C++. | |
| 94 std::vector<uint8_t> id_bytes; | |
| 95 base::android::JavaByteArrayToByteVector(env, id_bytes_java.obj(), &id_bytes); | |
| 96 std::string id(id_bytes.begin(), id_bytes.end()); | |
| 97 | |
| 98 return id; | |
| 99 } | |
| 100 | |
| 101 InvalidationLogger* InvalidationServiceAndroid::GetInvalidationLogger() { | |
| 102 return &logger_; | |
| 103 } | |
| 104 | |
| 105 void InvalidationServiceAndroid::RequestDetailedStatus( | |
| 106 base::Callback<void(const base::DictionaryValue&)> return_callback) const { | |
| 107 } | |
| 108 | |
| 109 IdentityProvider* InvalidationServiceAndroid::GetIdentityProvider() { | |
| 110 return NULL; | |
| 111 } | |
| 112 void InvalidationServiceAndroid::TriggerStateChangeForTest( | |
|
nyquist
2014/09/04 09:01:56
Missing newline before this function.
maxbogue
2014/09/05 16:42:46
Done.
| |
| 113 syncer::InvalidatorState state) { | |
| 114 DCHECK(CalledOnValidThread()); | |
| 115 invalidator_state_ = state; | |
| 116 invalidator_registrar_.UpdateInvalidatorState(invalidator_state_); | |
| 117 } | |
| 118 | |
| 119 void InvalidationServiceAndroid::RequestSync(JNIEnv* env, | |
| 120 jobject obj, | |
| 121 jint object_source, | |
| 122 jstring java_object_id, | |
| 123 jlong version, | |
| 124 jstring java_state) { | |
| 125 invalidation::ObjectId object_id(object_source, | |
| 126 ConvertJavaStringToUTF8(env, java_object_id)); | |
| 127 | |
| 128 syncer::ObjectIdInvalidationMap object_ids_with_states; | |
| 129 | |
| 130 if (version == ipc::invalidation::Constants::UNKNOWN) { | |
| 131 object_ids_with_states.Insert( | |
| 132 syncer::Invalidation::InitUnknownVersion(object_id)); | |
| 133 } else { | |
| 134 ObjectIdVersionMap::iterator it = | |
| 135 max_invalidation_versions_.find(object_id); | |
| 136 if ((it != max_invalidation_versions_.end()) && | |
| 137 (version <= it->second)) { | |
| 138 DVLOG(1) << "Dropping redundant invalidation with version " << version; | |
| 139 return; | |
| 140 } | |
| 141 max_invalidation_versions_[object_id] = version; | |
| 142 object_ids_with_states.Insert( | |
| 143 syncer::Invalidation::Init(object_id, version, | |
| 144 ConvertJavaStringToUTF8(env, java_state))); | |
| 145 } | |
| 146 | |
| 147 DispatchInvalidations(object_ids_with_states); | |
| 148 } | |
| 149 | |
| 150 void InvalidationServiceAndroid::RequestSyncForAllTypes(JNIEnv* env, | |
| 151 jobject obj) { | |
| 152 syncer::ObjectIdInvalidationMap object_ids_with_states; | |
| 153 DispatchInvalidations(object_ids_with_states); | |
| 154 } | |
| 155 | |
| 156 void InvalidationServiceAndroid::DispatchInvalidations( | |
| 157 syncer::ObjectIdInvalidationMap& object_invalidation_map) { | |
| 158 // An empty map implies that we should invalidate all. | |
| 159 const syncer::ObjectIdInvalidationMap& effective_invalidation_map = | |
| 160 object_invalidation_map.Empty() ? | |
| 161 syncer::ObjectIdInvalidationMap::InvalidateAll( | |
| 162 invalidator_registrar_.GetAllRegisteredIds()) : | |
| 163 object_invalidation_map; | |
| 164 | |
| 165 invalidator_registrar_.DispatchInvalidationsToHandlers( | |
| 166 effective_invalidation_map); | |
| 167 logger_.OnInvalidation(effective_invalidation_map); | |
| 168 } | |
| 169 | |
| 170 // static | |
| 171 bool InvalidationServiceAndroid::RegisterJni(JNIEnv* env) { | |
| 172 return RegisterNativesImpl(env); | |
| 173 } | |
| 174 | |
| 175 } // namespace invalidation | |
|
nyquist
2014/09/04 09:01:56
extra space before //, not after.
maxbogue
2014/09/05 16:42:46
Done.
| |
| OLD | NEW |