- add sources.
[platform/framework/web/crosswalk.git] / src / ui / base / android / window_android.cc
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
13 namespace ui {
14
15 using base::android::AttachCurrentThread;
16 using base::android::ScopedJavaLocalRef;
17
18 WindowAndroid::WindowAndroid(JNIEnv* env, jobject obj)
19   : weak_java_window_(env, obj) {
20 }
21
22 void WindowAndroid::Destroy(JNIEnv* env, jobject obj) {
23   delete this;
24 }
25
26 ScopedJavaLocalRef<jobject> WindowAndroid::GetJavaObject() {
27   return weak_java_window_.get(AttachCurrentThread());
28 }
29
30 bool WindowAndroid::RegisterWindowAndroid(JNIEnv* env) {
31   return RegisterNativesImpl(env);
32 }
33
34 WindowAndroid::~WindowAndroid() {
35 }
36
37 bool WindowAndroid::GrabSnapshot(
38     int content_x, int content_y, int width, int height,
39     std::vector<unsigned char>* png_representation) {
40   JNIEnv* env = AttachCurrentThread();
41   ScopedJavaLocalRef<jbyteArray> result =
42       Java_WindowAndroid_grabSnapshot(env, GetJavaObject().obj(),
43                                       content_x + content_offset_.x(),
44                                       content_y + content_offset_.y(),
45                                       width, height);
46   if (result.is_null())
47     return false;
48   base::android::JavaByteArrayToByteVector(
49       env, result.obj(), png_representation);
50   return true;
51 }
52
53 // ----------------------------------------------------------------------------
54 // Native JNI methods
55 // ----------------------------------------------------------------------------
56
57 jint Init(JNIEnv* env, jobject obj) {
58   WindowAndroid* window = new WindowAndroid(env, obj);
59   return reinterpret_cast<jint>(window);
60 }
61
62 }  // namespace ui