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 "chrome/browser/invalidation/invalidation_controller_android.h" 6 7 #include <string> 8 #include <vector> 9 10 #include "base/android/jni_array.h" 11 #include "chrome/jni/InvalidationController_jni.h" 12 #include "google/cacheinvalidation/include/types.h" 13 14 namespace invalidation { 15 16 InvalidationControllerAndroid::InvalidationControllerAndroid() {} 17 18 InvalidationControllerAndroid::~InvalidationControllerAndroid() {} 19 20 void InvalidationControllerAndroid::SetRegisteredObjectIds( 21 const syncer::ObjectIdSet& ids) { 22 // Get a reference to the Java invalidation controller. 23 JNIEnv* env = base::android::AttachCurrentThread(); 24 DCHECK(env); 25 if (invalidation_controller_.is_null()) { 26 invalidation_controller_.Reset(Java_InvalidationController_get( 27 env, 28 base::android::GetApplicationContext())); 29 } 30 31 // To call the corresponding method on the Java invalidation controller, split 32 // the object ids into object source and object name arrays. 33 std::vector<int> sources; 34 std::vector<std::string> names; 35 syncer::ObjectIdSet::const_iterator id; 36 for (id = ids.begin(); id != ids.end(); ++id) { 37 sources.push_back(id->source()); 38 names.push_back(id->name()); 39 } 40 41 Java_InvalidationController_setRegisteredObjectIds( 42 env, 43 invalidation_controller_.obj(), 44 base::android::ToJavaIntArray(env, sources).obj(), 45 base::android::ToJavaArrayOfStrings(env, names).obj()); 46 } 47 48 std::string InvalidationControllerAndroid::GetInvalidatorClientId() { 49 JNIEnv* env = base::android::AttachCurrentThread(); 50 DCHECK(env); 51 if (invalidation_controller_.is_null()) { 52 invalidation_controller_.Reset(Java_InvalidationController_get( 53 env, 54 base::android::GetApplicationContext())); 55 } 56 57 // Ask the Java code to for the invalidator ID it's currently using. 58 base::android::ScopedJavaLocalRef<_jbyteArray*> id_bytes_java = 59 Java_InvalidationController_getInvalidatorClientId( 60 env, 61 invalidation_controller_.obj()); 62 63 // Convert it into a more convenient format for C++. 64 std::vector<uint8_t> id_bytes; 65 base::android::JavaByteArrayToByteVector(env, id_bytes_java.obj(), &id_bytes); 66 std::string id(id_bytes.begin(), id_bytes.end()); 67 68 return id; 69 } 70 71 bool RegisterInvalidationController(JNIEnv* env) { 72 return RegisterNativesImpl(env); 73 } 74 75 } // namespace invalidation 76