Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / invalidation / invalidation_controller_android.cc
blob021027c4d0c0bf6fdfdad7d285ecc76e1ea0c320
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.
5 #include "chrome/browser/invalidation/invalidation_controller_android.h"
7 #include <string>
8 #include <vector>
10 #include "base/android/jni_array.h"
11 #include "chrome/jni/InvalidationController_jni.h"
12 #include "google/cacheinvalidation/include/types.h"
14 namespace invalidation {
16 InvalidationControllerAndroid::InvalidationControllerAndroid() {}
18 InvalidationControllerAndroid::~InvalidationControllerAndroid() {}
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()));
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());
41 Java_InvalidationController_setRegisteredObjectIds(
42 env,
43 invalidation_controller_.obj(),
44 base::android::ToJavaIntArray(env, sources).obj(),
45 base::android::ToJavaArrayOfStrings(env, names).obj());
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()));
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());
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());
68 return id;
71 bool RegisterInvalidationController(JNIEnv* env) {
72 return RegisterNativesImpl(env);
75 } // namespace invalidation