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 "ui/base/android/window_android.h" 6 7 #include "base/android/jni_android.h" 8 #include "base/android/jni_array.h" 9 #include "base/android/jni_helper.h" 10 #include "base/android/scoped_java_ref.h" 11 #include "jni/WindowAndroid_jni.h" 12 #include "ui/base/android/window_android_observer.h" 13 14 namespace ui { 15 16 using base::android::AttachCurrentThread; 17 using base::android::ScopedJavaLocalRef; 18 19 WindowAndroid::WindowAndroid(JNIEnv* env, jobject obj) 20 : weak_java_window_(env, obj) { 21 } 22 23 void WindowAndroid::Destroy(JNIEnv* env, jobject obj) { 24 delete this; 25 } 26 27 ScopedJavaLocalRef<jobject> WindowAndroid::GetJavaObject() { 28 return weak_java_window_.get(AttachCurrentThread()); 29 } 30 31 bool WindowAndroid::RegisterWindowAndroid(JNIEnv* env) { 32 return RegisterNativesImpl(env); 33 } 34 35 WindowAndroid::~WindowAndroid() { 36 } 37 38 bool WindowAndroid::GrabSnapshot( 39 int content_x, int content_y, int width, int height, 40 std::vector<unsigned char>* png_representation) { 41 JNIEnv* env = AttachCurrentThread(); 42 ScopedJavaLocalRef<jbyteArray> result = 43 Java_WindowAndroid_grabSnapshot(env, GetJavaObject().obj(), 44 content_x + content_offset_.x(), 45 content_y + content_offset_.y(), 46 width, height); 47 if (result.is_null()) 48 return false; 49 base::android::JavaByteArrayToByteVector( 50 env, result.obj(), png_representation); 51 return true; 52 } 53 54 void WindowAndroid::OnCompositingDidCommit() { 55 FOR_EACH_OBSERVER(WindowAndroidObserver, 56 observer_list_, 57 OnCompositingDidCommit()); 58 } 59 60 void WindowAndroid::AddObserver(WindowAndroidObserver* observer) { 61 if (!observer_list_.HasObserver(observer)) 62 observer_list_.AddObserver(observer); 63 } 64 65 void WindowAndroid::RemoveObserver(WindowAndroidObserver* observer) { 66 observer_list_.RemoveObserver(observer); 67 } 68 69 void WindowAndroid::AttachCompositor() { 70 FOR_EACH_OBSERVER(WindowAndroidObserver, 71 observer_list_, 72 OnAttachCompositor()); 73 } 74 75 void WindowAndroid::DetachCompositor() { 76 FOR_EACH_OBSERVER(WindowAndroidObserver, 77 observer_list_, 78 OnDetachCompositor()); 79 observer_list_.Clear(); 80 } 81 82 // ---------------------------------------------------------------------------- 83 // Native JNI methods 84 // ---------------------------------------------------------------------------- 85 86 jlong Init(JNIEnv* env, jobject obj) { 87 WindowAndroid* window = new WindowAndroid(env, obj); 88 return reinterpret_cast<intptr_t>(window); 89 } 90 91 } // namespace ui 92