OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/shell/android/shell_manager.h" | 5 #include "chrome/android/testshell/tab_manager.h" |
6 | 6 |
| 7 #include "base/logging.h" |
7 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
9 #include "base/android/scoped_java_ref.h" | 10 #include "base/android/scoped_java_ref.h" |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "chrome/browser/android/tab_base_android_impl.h" |
13 #include "content/shell/shell.h" | |
14 #include "content/shell/shell_browser_context.h" | |
15 #include "content/shell/shell_content_browser_client.h" | |
16 #include "content/public/browser/android/compositor.h" | 14 #include "content/public/browser/android/compositor.h" |
17 #include "content/public/browser/android/draw_delegate.h" | 15 #include "content/public/browser/android/draw_delegate.h" |
18 #include "content/public/browser/web_contents.h" | 16 #include "jni/TabManager_jni.h" |
19 #include "content/shell/shell.h" | |
20 #include "googleurl/src/gurl.h" | |
21 #include "jni/ShellManager_jni.h" | |
22 #include "third_party/WebKit/Source/Platform/chromium/public/WebLayer.h" | 17 #include "third_party/WebKit/Source/Platform/chromium/public/WebLayer.h" |
23 #include "ui/gfx/size.h" | |
24 | 18 |
25 #include <android/native_window_jni.h> | 19 #include <android/native_window_jni.h> |
26 | 20 |
27 using base::android::ScopedJavaLocalRef; | 21 using base::android::ScopedJavaLocalRef; |
28 using content::Compositor; | |
29 using content::DrawDelegate; | |
30 | 22 |
31 namespace { | 23 namespace { |
32 | 24 |
33 struct GlobalState { | 25 struct GlobalState { |
34 base::android::ScopedJavaGlobalRef<jobject> j_obj; | |
35 scoped_ptr<content::Compositor> compositor; | 26 scoped_ptr<content::Compositor> compositor; |
36 scoped_ptr<WebKit::WebLayer> root_layer; | 27 scoped_ptr<WebKit::WebLayer> root_layer; |
37 }; | 28 }; |
38 | 29 |
39 base::LazyInstance<GlobalState> g_global_state = LAZY_INSTANCE_INITIALIZER; | 30 base::LazyInstance<GlobalState> g_global_state = LAZY_INSTANCE_INITIALIZER; |
40 | 31 |
41 content::Compositor* GetCompositor() { | 32 content::Compositor* GetCompositor() { |
42 return g_global_state.Get().compositor.get(); | 33 return g_global_state.Get().compositor.get(); |
43 } | 34 } |
44 | 35 |
45 static void SurfacePresented( | 36 static void SurfacePresented( |
46 const DrawDelegate::SurfacePresentedCallback& callback, | 37 const content::DrawDelegate::SurfacePresentedCallback& callback, |
47 uint32 sync_point) { | 38 uint32 sync_point) { |
48 callback.Run(sync_point); | 39 callback.Run(sync_point); |
49 } | 40 } |
50 | 41 |
| 42 void DummyCallback(uint32) { } |
| 43 |
51 static void SurfaceUpdated( | 44 static void SurfaceUpdated( |
52 uint64 texture, | 45 uint64 texture, |
53 content::RenderWidgetHostView* view, | 46 content::RenderWidgetHostView* view, |
54 const DrawDelegate::SurfacePresentedCallback& callback) { | 47 const content::DrawDelegate::SurfacePresentedCallback& callback) { |
55 GetCompositor()->OnSurfaceUpdated(base::Bind( | 48 GetCompositor()->OnSurfaceUpdated(base::Bind( |
56 &SurfacePresented, callback)); | 49 &SurfacePresented, callback)); |
57 } | 50 } |
58 | 51 |
59 } // anonymous namespace | 52 } // anonymous namespace |
60 | 53 |
61 namespace content { | 54 namespace chrome { |
62 | |
63 jobject CreateShellView() { | |
64 JNIEnv* env = base::android::AttachCurrentThread(); | |
65 if (!GetCompositor()) { | |
66 Compositor::Initialize(); | |
67 g_global_state.Get().compositor.reset(Compositor::Create()); | |
68 DCHECK(!g_global_state.Get().root_layer.get()); | |
69 g_global_state.Get().root_layer.reset(WebKit::WebLayer::create()); | |
70 } | |
71 return Java_ShellManager_createShell( | |
72 env, g_global_state.Get().j_obj.obj()).Release(); | |
73 } | |
74 | 55 |
75 // Register native methods | 56 // Register native methods |
76 bool RegisterShellManager(JNIEnv* env) { | 57 bool RegisterTabManager(JNIEnv* env) { |
77 return RegisterNativesImpl(env); | 58 return RegisterNativesImpl(env); |
78 } | 59 } |
79 | 60 |
80 static void Init(JNIEnv* env, jclass clazz, jobject obj) { | 61 static void Init(JNIEnv* env, jclass clazz, jobject obj) { |
81 g_global_state.Get().j_obj.Reset( | 62 content::DrawDelegate::SurfaceUpdatedCallback cb = base::Bind( |
82 base::android::ScopedJavaLocalRef<jobject>(env, obj)); | |
83 DrawDelegate::SurfaceUpdatedCallback cb = base::Bind( | |
84 &SurfaceUpdated); | 63 &SurfaceUpdated); |
85 DrawDelegate::GetInstance()->SetUpdateCallback(cb); | 64 content::DrawDelegate::GetInstance()->SetUpdateCallback(cb); |
| 65 if (!GetCompositor()) { |
| 66 content::Compositor::Initialize(); |
| 67 g_global_state.Get().compositor.reset(content::Compositor::Create()); |
| 68 DCHECK(!g_global_state.Get().root_layer.get()); |
| 69 g_global_state.Get().root_layer.reset(WebKit::WebLayer::create()); |
| 70 } |
86 } | 71 } |
87 | 72 |
88 static void SurfaceCreated( | 73 static void SurfaceCreated(JNIEnv* env, jclass clazz, jobject jsurface) { |
89 JNIEnv* env, jclass clazz, jobject jsurface) { | |
90 ANativeWindow* native_window = ANativeWindow_fromSurface(env, jsurface); | 74 ANativeWindow* native_window = ANativeWindow_fromSurface(env, jsurface); |
91 if (native_window) { | 75 if (native_window) { |
92 GetCompositor()->SetWindowSurface(native_window); | 76 GetCompositor()->SetWindowSurface(native_window); |
93 ANativeWindow_release(native_window); | 77 ANativeWindow_release(native_window); |
94 GetCompositor()->SetRootLayer(g_global_state.Get().root_layer.get()); | 78 GetCompositor()->SetRootLayer(g_global_state.Get().root_layer.get()); |
95 } | 79 } |
96 } | 80 } |
97 | 81 |
98 static void SurfaceDestroyed(JNIEnv* env, jclass clazz) { | 82 static void SurfaceDestroyed(JNIEnv* env, jclass clazz) { |
99 GetCompositor()->SetWindowSurface(NULL); | 83 GetCompositor()->SetWindowSurface(NULL); |
100 } | 84 } |
101 | 85 |
102 static void SurfaceSetSize( | 86 static void SurfaceSetSize( |
103 JNIEnv* env, jclass clazz, jint width, jint height) { | 87 JNIEnv* env, jclass clazz, jint width, jint height) { |
104 gfx::Size size = gfx::Size(width, height); | 88 gfx::Size size = gfx::Size(width, height); |
105 DrawDelegate::GetInstance()->SetBounds(size); | 89 content::DrawDelegate::GetInstance()->SetBounds(size); |
106 GetCompositor()->SetWindowBounds(size); | 90 GetCompositor()->SetWindowBounds(size); |
107 } | 91 } |
108 | 92 |
109 void LaunchShell(JNIEnv* env, jclass clazz, jstring jurl) { | 93 static void ShowTab(JNIEnv* env, jclass clazz, jint jtab) { |
110 ShellBrowserContext* browserContext = | 94 if (!GetCompositor()) |
111 static_cast<ShellContentBrowserClient*>( | 95 return; |
112 GetContentClient()->browser())->browser_context(); | 96 TabBaseAndroidImpl* tab = reinterpret_cast<TabBaseAndroidImpl*>(jtab); |
113 GURL url(base::android::ConvertJavaStringToUTF8(env, jurl)); | 97 g_global_state.Get().root_layer->addChild(tab->tab_layer()); |
114 Shell::CreateNewWindow(browserContext, | 98 GetCompositor()->OnSurfaceUpdated(base::Bind(&DummyCallback)); |
115 url, | |
116 NULL, | |
117 MSG_ROUTING_NONE, | |
118 NULL); | |
119 } | 99 } |
120 | 100 |
121 void ShellAttachLayer(WebKit::WebLayer* layer) { | 101 static void HideTab(JNIEnv* env, jclass clazz, jint jtab) { |
122 g_global_state.Get().root_layer->addChild(layer); | 102 if (!GetCompositor()) |
| 103 return; |
| 104 TabBaseAndroidImpl* tab = reinterpret_cast<TabBaseAndroidImpl*>(jtab); |
| 105 tab->tab_layer()->removeFromParent(); |
123 } | 106 } |
124 | 107 |
125 | 108 } // namespace chrome |
126 void ShellRemoveLayer(WebKit::WebLayer* layer) { | |
127 layer->removeFromParent(); | |
128 } | |
129 | |
130 } // namespace content | |
OLD | NEW |