OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/browser/web_contents/web_contents_android.h" | 5 #include "content/browser/web_contents/web_contents_android.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_array.h" | |
8 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
9 #include "base/command_line.h" | 10 #include "base/command_line.h" |
10 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "content/browser/accessibility/browser_accessibility_android.h" | |
14 #include "content/browser/accessibility/browser_accessibility_manager.h" | |
12 #include "content/browser/android/interstitial_page_delegate_android.h" | 15 #include "content/browser/android/interstitial_page_delegate_android.h" |
13 #include "content/browser/frame_host/interstitial_page_impl.h" | 16 #include "content/browser/frame_host/interstitial_page_impl.h" |
14 #include "content/browser/media/android/browser_media_player_manager.h" | 17 #include "content/browser/media/android/browser_media_player_manager.h" |
15 #include "content/browser/media/media_web_contents_observer.h" | 18 #include "content/browser/media/media_web_contents_observer.h" |
16 #include "content/browser/renderer_host/render_view_host_impl.h" | 19 #include "content/browser/renderer_host/render_view_host_impl.h" |
17 #include "content/browser/web_contents/web_contents_impl.h" | 20 #include "content/browser/web_contents/web_contents_impl.h" |
18 #include "content/common/devtools_messages.h" | 21 #include "content/common/devtools_messages.h" |
19 #include "content/common/frame_messages.h" | 22 #include "content/common/frame_messages.h" |
20 #include "content/common/input_messages.h" | 23 #include "content/common/input_messages.h" |
21 #include "content/common/view_messages.h" | 24 #include "content/common/view_messages.h" |
22 #include "content/public/browser/browser_context.h" | 25 #include "content/public/browser/browser_context.h" |
23 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
24 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
25 #include "content/public/common/content_switches.h" | 28 #include "content/public/common/content_switches.h" |
26 #include "jni/WebContentsImpl_jni.h" | 29 #include "jni/WebContentsImpl_jni.h" |
27 #include "net/android/network_library.h" | 30 #include "net/android/network_library.h" |
31 #include "ui/accessibility/ax_node_data.h" | |
28 | 32 |
29 using base::android::AttachCurrentThread; | 33 using base::android::AttachCurrentThread; |
30 using base::android::ConvertJavaStringToUTF8; | 34 using base::android::ConvertJavaStringToUTF8; |
31 using base::android::ConvertJavaStringToUTF16; | 35 using base::android::ConvertJavaStringToUTF16; |
32 using base::android::ConvertUTF8ToJavaString; | 36 using base::android::ConvertUTF8ToJavaString; |
37 using base::android::ConvertUTF16ToJavaString; | |
33 using base::android::ScopedJavaGlobalRef; | 38 using base::android::ScopedJavaGlobalRef; |
39 using base::android::ToJavaIntArray; | |
40 | |
41 namespace content { | |
34 | 42 |
35 namespace { | 43 namespace { |
36 | 44 |
37 void JavaScriptResultCallback(const ScopedJavaGlobalRef<jobject>& callback, | 45 void JavaScriptResultCallback(const ScopedJavaGlobalRef<jobject>& callback, |
38 const base::Value* result) { | 46 const base::Value* result) { |
39 JNIEnv* env = base::android::AttachCurrentThread(); | 47 JNIEnv* env = base::android::AttachCurrentThread(); |
40 std::string json; | 48 std::string json; |
41 base::JSONWriter::Write(result, &json); | 49 base::JSONWriter::Write(result, &json); |
42 ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json); | 50 ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json); |
43 content::Java_WebContentsImpl_onEvaluateJavaScriptResult( | 51 Java_WebContentsImpl_onEvaluateJavaScriptResult( |
44 env, j_json.obj(), callback.obj()); | 52 env, j_json.obj(), callback.obj()); |
45 } | 53 } |
46 | 54 |
47 void ReleaseAllMediaPlayers(content::WebContents* web_contents, | 55 ScopedJavaLocalRef<jobject> WalkAXTreeDepthFirst(JNIEnv* env, |
48 content::RenderFrameHost* render_frame_host) { | 56 BrowserAccessibilityAndroid* node) { |
49 content::BrowserMediaPlayerManager* manager = | 57 |
50 static_cast<content::WebContentsImpl*>(web_contents)-> | 58 ScopedJavaLocalRef<jstring> j_text = |
59 ConvertUTF16ToJavaString(env,node->GetText()); | |
dmazzoni
2015/04/13 06:55:44
nit: space after comma (throughout)
sgurun-gerrit only
2015/04/14 00:27:12
Done.
| |
60 ScopedJavaLocalRef<jstring> j_class = | |
61 ConvertUTF8ToJavaString(env,node->GetClassName()); | |
62 const gfx::Rect& location = node->GetLocation(); | |
63 ScopedJavaLocalRef<jobject> j_node = | |
64 Java_WebContentsImpl_createAccessibilitySnapshotNode(env, | |
65 node->GetId(), location.x(), location.y(), node->GetScrollX(), | |
66 node->GetScrollY(), location.width(), location.height(), | |
67 j_text.obj(), j_class.obj()); | |
68 | |
69 for(uint32 i = 0; i < node->PlatformChildCount(); i++) { | |
70 BrowserAccessibilityAndroid* child = | |
71 static_cast<BrowserAccessibilityAndroid*>( | |
72 node->PlatformGetChild(i)); | |
73 Java_WebContentsImpl_addAccessibilityNodeAsChild(env, | |
74 j_node.obj(), WalkAXTreeDepthFirst(env, child).obj()); | |
75 } | |
76 return j_node; | |
77 } | |
78 | |
79 // Walks over the AXTreeUpdate and creates a light weight snapshot. | |
80 void AXTreeSnapshotCallback(const ScopedJavaGlobalRef<jobject>& callback, | |
81 const ui::AXTreeUpdate& result) { | |
82 JNIEnv* env = base::android::AttachCurrentThread(); | |
83 if (result.nodes.empty()) { | |
84 Java_WebContentsImpl_onAccessibilitySnapshot(env, nullptr, callback.obj()); | |
85 return; | |
86 } | |
87 scoped_ptr<BrowserAccessibilityManager> manager( | |
88 BrowserAccessibilityManager::Create(result, nullptr)); | |
89 BrowserAccessibilityAndroid* root = | |
90 static_cast<BrowserAccessibilityAndroid*>(manager->GetRoot()); | |
91 ScopedJavaLocalRef<jobject> j_root = WalkAXTreeDepthFirst(env, root); | |
92 Java_WebContentsImpl_onAccessibilitySnapshot( | |
93 env, j_root.obj(), callback.obj()); | |
94 } | |
95 | |
96 void ReleaseAllMediaPlayers(WebContents* web_contents, | |
97 RenderFrameHost* render_frame_host) { | |
98 BrowserMediaPlayerManager* manager = | |
99 static_cast<WebContentsImpl*>(web_contents)-> | |
51 media_web_contents_observer()->GetMediaPlayerManager( | 100 media_web_contents_observer()->GetMediaPlayerManager( |
52 render_frame_host); | 101 render_frame_host); |
53 if (manager) | 102 if (manager) |
54 manager->ReleaseAllMediaPlayers(); | 103 manager->ReleaseAllMediaPlayers(); |
55 } | 104 } |
56 | 105 |
57 } // namespace | 106 } // namespace |
58 | 107 |
59 namespace content { | |
60 | |
61 // static | 108 // static |
62 WebContents* WebContents::FromJavaWebContents( | 109 WebContents* WebContents::FromJavaWebContents( |
63 jobject jweb_contents_android) { | 110 jobject jweb_contents_android) { |
64 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 111 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
65 if (!jweb_contents_android) | 112 if (!jweb_contents_android) |
66 return NULL; | 113 return NULL; |
67 | 114 |
68 WebContentsAndroid* web_contents_android = | 115 WebContentsAndroid* web_contents_android = |
69 reinterpret_cast<WebContentsAndroid*>( | 116 reinterpret_cast<WebContentsAndroid*>( |
70 Java_WebContentsImpl_getNativePointer(AttachCurrentThread(), | 117 Java_WebContentsImpl_getNativePointer(AttachCurrentThread(), |
71 jweb_contents_android)); | 118 jweb_contents_android)); |
72 if (!web_contents_android) | 119 if (!web_contents_android) |
73 return NULL; | 120 return NULL; |
74 return web_contents_android->web_contents(); | 121 return web_contents_android->web_contents(); |
75 } | 122 } |
76 | 123 |
77 // static | 124 // static |
78 static void DestroyWebContents(JNIEnv* env, | 125 static void DestroyWebContents(JNIEnv* env, |
79 jclass clazz, | 126 jclass clazz, |
80 jlong jweb_contents_android_ptr) { | 127 jlong jweb_contents_android_ptr) { |
81 WebContentsAndroid* web_contents_android = | 128 WebContentsAndroid* web_contents_android = |
82 reinterpret_cast<WebContentsAndroid*>(jweb_contents_android_ptr); | 129 reinterpret_cast<WebContentsAndroid*>(jweb_contents_android_ptr); |
83 if (!web_contents_android) | 130 if (!web_contents_android) |
84 return; | 131 return; |
85 | 132 |
86 content::WebContents* web_contents = web_contents_android->web_contents(); | 133 WebContents* web_contents = web_contents_android->web_contents(); |
87 if (!web_contents) | 134 if (!web_contents) |
88 return; | 135 return; |
89 | 136 |
90 delete web_contents; | 137 delete web_contents; |
91 } | 138 } |
92 | 139 |
93 // static | 140 // static |
94 bool WebContentsAndroid::Register(JNIEnv* env) { | 141 bool WebContentsAndroid::Register(JNIEnv* env) { |
95 return RegisterNativesImpl(env); | 142 return RegisterNativesImpl(env); |
96 } | 143 } |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
468 // No callback requested. | 515 // No callback requested. |
469 web_contents_->GetMainFrame()->ExecuteJavaScript( | 516 web_contents_->GetMainFrame()->ExecuteJavaScript( |
470 ConvertJavaStringToUTF16(env, script)); | 517 ConvertJavaStringToUTF16(env, script)); |
471 return; | 518 return; |
472 } | 519 } |
473 | 520 |
474 // Secure the Java callback in a scoped object and give ownership of it to the | 521 // Secure the Java callback in a scoped object and give ownership of it to the |
475 // base::Callback. | 522 // base::Callback. |
476 ScopedJavaGlobalRef<jobject> j_callback; | 523 ScopedJavaGlobalRef<jobject> j_callback; |
477 j_callback.Reset(env, callback); | 524 j_callback.Reset(env, callback); |
478 content::RenderFrameHost::JavaScriptResultCallback js_callback = | 525 RenderFrameHost::JavaScriptResultCallback js_callback = |
479 base::Bind(&JavaScriptResultCallback, j_callback); | 526 base::Bind(&JavaScriptResultCallback, j_callback); |
480 | 527 |
481 web_contents_->GetMainFrame()->ExecuteJavaScript( | 528 web_contents_->GetMainFrame()->ExecuteJavaScript( |
482 ConvertJavaStringToUTF16(env, script), js_callback); | 529 ConvertJavaStringToUTF16(env, script), js_callback); |
483 } | 530 } |
484 | 531 |
485 void WebContentsAndroid::AddMessageToDevToolsConsole(JNIEnv* env, | 532 void WebContentsAndroid::AddMessageToDevToolsConsole(JNIEnv* env, |
486 jobject jobj, | 533 jobject jobj, |
487 jint level, | 534 jint level, |
488 jstring message) { | 535 jstring message) { |
489 DCHECK_GE(level, 0); | 536 DCHECK_GE(level, 0); |
490 DCHECK_LE(level, CONSOLE_MESSAGE_LEVEL_LAST); | 537 DCHECK_LE(level, CONSOLE_MESSAGE_LEVEL_LAST); |
491 | 538 |
492 web_contents_->GetMainFrame()->Send(new DevToolsAgentMsg_AddMessageToConsole( | 539 web_contents_->GetMainFrame()->Send(new DevToolsAgentMsg_AddMessageToConsole( |
493 web_contents_->GetMainFrame()->GetRoutingID(), | 540 web_contents_->GetMainFrame()->GetRoutingID(), |
494 static_cast<ConsoleMessageLevel>(level), | 541 static_cast<ConsoleMessageLevel>(level), |
495 ConvertJavaStringToUTF8(env, message))); | 542 ConvertJavaStringToUTF8(env, message))); |
496 } | 543 } |
497 | 544 |
498 jboolean WebContentsAndroid::HasAccessedInitialDocument( | 545 jboolean WebContentsAndroid::HasAccessedInitialDocument( |
499 JNIEnv* env, | 546 JNIEnv* env, |
500 jobject jobj) { | 547 jobject jobj) { |
501 return static_cast<content::WebContentsImpl*>(web_contents_)-> | 548 return static_cast<WebContentsImpl*>(web_contents_)-> |
502 HasAccessedInitialDocument(); | 549 HasAccessedInitialDocument(); |
503 } | 550 } |
504 | 551 |
505 jint WebContentsAndroid::GetThemeColor(JNIEnv* env, jobject obj) { | 552 jint WebContentsAndroid::GetThemeColor(JNIEnv* env, jobject obj) { |
506 return web_contents_->GetThemeColor(); | 553 return web_contents_->GetThemeColor(); |
507 } | 554 } |
508 | 555 |
556 void WebContentsAndroid::RequestAccessibilitySnapshot(JNIEnv* env, | |
557 jobject obj, | |
558 jobject callback) { | |
559 // Secure the Java callback in a scoped object and give ownership of it to the | |
560 // base::Callback. | |
561 ScopedJavaGlobalRef<jobject> j_callback; | |
562 j_callback.Reset(env, callback); | |
563 WebContentsImpl::AXTreeSnapshotCallback snapshot_callback = | |
564 base::Bind(&AXTreeSnapshotCallback, j_callback); | |
565 | |
566 static_cast<WebContentsImpl*>(web_contents_)->RequestAXTreeSnapshot( | |
567 snapshot_callback); | |
568 } | |
569 | |
509 } // namespace content | 570 } // namespace content |
OLD | NEW |