Chromium Code Reviews| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 #include "content/common/view_messages.h" | 31 #include "content/common/view_messages.h" |
| 32 #include "content/public/browser/browser_context.h" | 32 #include "content/public/browser/browser_context.h" |
| 33 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
| 34 #include "content/public/browser/message_port_provider.h" | 34 #include "content/public/browser/message_port_provider.h" |
| 35 #include "content/public/browser/render_widget_host.h" | 35 #include "content/public/browser/render_widget_host.h" |
| 36 #include "content/public/browser/web_contents.h" | 36 #include "content/public/browser/web_contents.h" |
| 37 #include "content/public/common/content_switches.h" | 37 #include "content/public/common/content_switches.h" |
| 38 #include "jni/WebContentsImpl_jni.h" | 38 #include "jni/WebContentsImpl_jni.h" |
| 39 #include "net/android/network_library.h" | 39 #include "net/android/network_library.h" |
| 40 #include "ui/accessibility/ax_node_data.h" | 40 #include "ui/accessibility/ax_node_data.h" |
| 41 #include "ui/accessibility/platform/ax_android_snapshot.h" | |
| 41 #include "ui/android/overscroll_refresh_handler.h" | 42 #include "ui/android/overscroll_refresh_handler.h" |
| 42 #include "ui/android/window_android.h" | 43 #include "ui/android/window_android.h" |
| 43 #include "ui/gfx/android/java_bitmap.h" | 44 #include "ui/gfx/android/java_bitmap.h" |
| 44 #include "ui/gfx/geometry/point.h" | 45 #include "ui/gfx/geometry/point.h" |
| 45 #include "ui/gfx/geometry/rect.h" | 46 #include "ui/gfx/geometry/rect.h" |
| 46 | 47 |
| 47 using base::android::AttachCurrentThread; | 48 using base::android::AttachCurrentThread; |
| 48 using base::android::ConvertJavaStringToUTF8; | 49 using base::android::ConvertJavaStringToUTF8; |
| 49 using base::android::ConvertJavaStringToUTF16; | 50 using base::android::ConvertJavaStringToUTF16; |
| 50 using base::android::ConvertUTF8ToJavaString; | 51 using base::android::ConvertUTF8ToJavaString; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 75 | 76 |
| 76 void SmartClipCallback(const ScopedJavaGlobalRef<jobject>& callback, | 77 void SmartClipCallback(const ScopedJavaGlobalRef<jobject>& callback, |
| 77 const base::string16& text, | 78 const base::string16& text, |
| 78 const base::string16& html) { | 79 const base::string16& html) { |
| 79 JNIEnv* env = base::android::AttachCurrentThread(); | 80 JNIEnv* env = base::android::AttachCurrentThread(); |
| 80 ScopedJavaLocalRef<jstring> jtext = ConvertUTF16ToJavaString(env, text); | 81 ScopedJavaLocalRef<jstring> jtext = ConvertUTF16ToJavaString(env, text); |
| 81 ScopedJavaLocalRef<jstring> jhtml = ConvertUTF16ToJavaString(env, html); | 82 ScopedJavaLocalRef<jstring> jhtml = ConvertUTF16ToJavaString(env, html); |
| 82 Java_WebContentsImpl_onSmartClipDataExtracted(env, jtext, jhtml, callback); | 83 Java_WebContentsImpl_onSmartClipDataExtracted(env, jtext, jhtml, callback); |
| 83 } | 84 } |
| 84 | 85 |
| 85 struct AccessibilitySnapshotParams { | 86 ScopedJavaLocalRef<jobject> CreateJavaAXSnapshot( |
| 86 AccessibilitySnapshotParams() | 87 JNIEnv* env, |
| 87 : has_tree_data(false), should_select_leaf_nodes(false) {} | 88 const ui::AXSnapshotNodeAndroid* node, |
| 89 bool is_root) { | |
| 90 ScopedJavaLocalRef<jstring> j_text = | |
| 91 ConvertUTF16ToJavaString(env, node->text); | |
| 92 ScopedJavaLocalRef<jstring> j_class = | |
| 93 ConvertUTF8ToJavaString(env, node->class_name); | |
| 94 ScopedJavaLocalRef<jobject> j_node = | |
| 95 Java_WebContentsImpl_createAccessibilitySnapshotNode( | |
| 96 env, node->rect.x(), node->rect.y(), node->rect.width(), | |
| 97 node->rect.height(), is_root, j_text, node->color, node->bgcolor, | |
| 98 node->text_size, node->bold, node->italic, node->underline, | |
| 99 node->line_through, j_class); | |
| 88 | 100 |
| 89 bool has_tree_data; | 101 if (node->has_selection) { |
| 90 // The current text selection within this tree, if any, expressed as the | 102 Java_WebContentsImpl_setAccessibilitySnapshotSelection( |
| 91 // node ID and character offset of the anchor (selection start) and focus | 103 env, j_node, node->start_selection, node->end_selection); |
| 92 // (selection end). | |
| 93 int32_t sel_anchor_object_id; | |
| 94 int32_t sel_anchor_offset; | |
| 95 int32_t sel_focus_object_id; | |
| 96 int32_t sel_focus_offset; | |
| 97 // if the flag is true, mark the leaf node as selected. | |
| 98 bool should_select_leaf_nodes; | |
| 99 }; | |
| 100 | |
| 101 ScopedJavaLocalRef<jobject> WalkAXTreeDepthFirst( | |
| 102 JNIEnv* env, | |
| 103 BrowserAccessibilityAndroid* node, | |
| 104 const gfx::Rect& parent_rect, | |
| 105 AccessibilitySnapshotParams* params) { | |
| 106 ScopedJavaLocalRef<jstring> j_text = | |
| 107 ConvertUTF16ToJavaString(env, node->GetText()); | |
| 108 ScopedJavaLocalRef<jstring> j_class = | |
| 109 ConvertUTF8ToJavaString(env, node->GetClassName()); | |
| 110 // The style attributes exists and valid if size attribute exists. Otherwise, | |
| 111 // they are not. Use a negative size information to indicate the existence | |
| 112 // of style information. | |
| 113 float size = -1.0; | |
| 114 int color = 0; | |
| 115 int bgcolor = 0; | |
| 116 int text_style = 0; | |
| 117 | |
| 118 if (node->HasFloatAttribute(ui::AX_ATTR_FONT_SIZE)) { | |
| 119 color = node->GetIntAttribute(ui::AX_ATTR_COLOR); | |
| 120 bgcolor = node->GetIntAttribute(ui::AX_ATTR_BACKGROUND_COLOR); | |
| 121 text_style = node->GetIntAttribute(ui::AX_ATTR_TEXT_STYLE); | |
| 122 | |
| 123 // The font size is just the computed style for that element; apply | |
| 124 // transformations to get the actual pixel size. | |
| 125 gfx::RectF text_size_rect( | |
| 126 0, 0, 1, node->GetFloatAttribute(ui::AX_ATTR_FONT_SIZE)); | |
| 127 gfx::Rect scaled_text_size_rect = node->RelativeToAbsoluteBounds( | |
| 128 text_size_rect, false); | |
| 129 size = scaled_text_size_rect.height(); | |
| 130 } | 104 } |
| 131 | 105 |
| 132 const gfx::Rect& absolute_rect = node->GetPageBoundsRect(); | 106 for (auto& child : node->children) { |
| 133 gfx::Rect parent_relative_rect = absolute_rect; | |
| 134 bool is_root = node->PlatformGetParent() == nullptr; | |
| 135 if (!is_root) { | |
| 136 parent_relative_rect.Offset(-parent_rect.OffsetFromOrigin()); | |
| 137 } | |
| 138 ScopedJavaLocalRef<jobject> j_node = | |
| 139 Java_WebContentsImpl_createAccessibilitySnapshotNode( | |
| 140 env, parent_relative_rect.x(), parent_relative_rect.y(), | |
| 141 absolute_rect.width(), absolute_rect.height(), is_root, j_text, color, | |
| 142 bgcolor, size, text_style, j_class); | |
| 143 | |
| 144 if (params->has_tree_data && node->PlatformIsLeaf()) { | |
| 145 int start_selection = 0; | |
| 146 int end_selection = 0; | |
| 147 if (params->sel_anchor_object_id == node->GetId()) { | |
| 148 start_selection = params->sel_anchor_offset; | |
| 149 params->should_select_leaf_nodes = true; | |
| 150 } | |
| 151 if (params->should_select_leaf_nodes) | |
| 152 end_selection = node->GetText().length(); | |
| 153 | |
| 154 if (params->sel_focus_object_id == node->GetId()) { | |
| 155 end_selection = params->sel_focus_offset; | |
| 156 params->should_select_leaf_nodes = false; | |
| 157 } | |
| 158 if (end_selection > 0) | |
| 159 Java_WebContentsImpl_setAccessibilitySnapshotSelection( | |
| 160 env, j_node, start_selection, end_selection); | |
| 161 } | |
| 162 | |
| 163 for (uint32_t i = 0; i < node->PlatformChildCount(); i++) { | |
| 164 BrowserAccessibilityAndroid* child = | |
| 165 static_cast<BrowserAccessibilityAndroid*>( | |
| 166 node->PlatformGetChild(i)); | |
| 167 Java_WebContentsImpl_addAccessibilityNodeAsChild( | 107 Java_WebContentsImpl_addAccessibilityNodeAsChild( |
| 168 env, j_node, WalkAXTreeDepthFirst(env, child, absolute_rect, params)); | 108 env, j_node, CreateJavaAXSnapshot(env, child.get(), false)); |
| 169 } | 109 } |
| 170 return j_node; | 110 return j_node; |
| 171 } | 111 } |
| 172 | 112 |
| 173 // Walks over the AXTreeUpdate and creates a light weight snapshot. | 113 // Walks over the AXTreeUpdate and creates a light weight snapshot. |
| 174 void AXTreeSnapshotCallback(const ScopedJavaGlobalRef<jobject>& callback, | 114 void AXTreeSnapshotCallback(const ScopedJavaGlobalRef<jobject>& callback, |
| 175 const ui::AXTreeUpdate& result) { | 115 const ui::AXTreeUpdate& result) { |
| 176 JNIEnv* env = base::android::AttachCurrentThread(); | 116 JNIEnv* env = base::android::AttachCurrentThread(); |
| 177 if (result.nodes.empty()) { | 117 if (result.nodes.empty()) { |
| 178 Java_WebContentsImpl_onAccessibilitySnapshot(env, nullptr, callback); | 118 Java_WebContentsImpl_onAccessibilitySnapshot(env, nullptr, callback); |
| 179 return; | 119 return; |
| 180 } | 120 } |
| 181 std::unique_ptr<BrowserAccessibilityManagerAndroid> manager( | 121 auto snapshot = ui::AXSnapshotNodeAndroid::Create(result); |
| 182 static_cast<BrowserAccessibilityManagerAndroid*>( | |
| 183 BrowserAccessibilityManager::Create(result, nullptr))); | |
| 184 manager->set_prune_tree_for_screen_reader(false); | |
|
sgurun-gerrit only
2017/04/20 23:25:01
where is this code now?
Muyuan
2017/04/21 18:45:11
The code logic is consistent with this path taken.
sgurun-gerrit only
2017/04/21 21:48:22
I don't know this part of the code well so leaving
| |
| 185 BrowserAccessibilityAndroid* root = | |
| 186 static_cast<BrowserAccessibilityAndroid*>(manager->GetRoot()); | |
| 187 AccessibilitySnapshotParams params; | |
| 188 if (result.has_tree_data) { | |
| 189 params.has_tree_data = true; | |
| 190 params.sel_anchor_object_id = result.tree_data.sel_anchor_object_id; | |
| 191 params.sel_anchor_offset = result.tree_data.sel_anchor_offset; | |
| 192 params.sel_focus_object_id = result.tree_data.sel_focus_object_id; | |
| 193 params.sel_focus_offset = result.tree_data.sel_focus_offset; | |
| 194 } | |
| 195 gfx::Rect parent_rect; | |
| 196 ScopedJavaLocalRef<jobject> j_root = | 122 ScopedJavaLocalRef<jobject> j_root = |
| 197 WalkAXTreeDepthFirst(env, root, parent_rect, ¶ms); | 123 CreateJavaAXSnapshot(env, snapshot.get(), true); |
| 198 Java_WebContentsImpl_onAccessibilitySnapshot(env, j_root, callback); | 124 Java_WebContentsImpl_onAccessibilitySnapshot(env, j_root, callback); |
| 199 } | 125 } |
| 200 | 126 |
| 201 } // namespace | 127 } // namespace |
| 202 | 128 |
| 203 // static | 129 // static |
| 204 WebContents* WebContents::FromJavaWebContents( | 130 WebContents* WebContents::FromJavaWebContents( |
| 205 const JavaRef<jobject>& jweb_contents_android) { | 131 const JavaRef<jobject>& jweb_contents_android) { |
| 206 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 132 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 207 if (jweb_contents_android.is_null()) | 133 if (jweb_contents_android.is_null()) |
| (...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 783 env, obj, callback, id, http_status_code, jurl, jbitmaps, jsizes); | 709 env, obj, callback, id, http_status_code, jurl, jbitmaps, jsizes); |
| 784 } | 710 } |
| 785 | 711 |
| 786 void WebContentsAndroid::SetMediaSession( | 712 void WebContentsAndroid::SetMediaSession( |
| 787 const ScopedJavaLocalRef<jobject>& j_media_session) { | 713 const ScopedJavaLocalRef<jobject>& j_media_session) { |
| 788 JNIEnv* env = base::android::AttachCurrentThread(); | 714 JNIEnv* env = base::android::AttachCurrentThread(); |
| 789 Java_WebContentsImpl_setMediaSession(env, obj_, j_media_session); | 715 Java_WebContentsImpl_setMediaSession(env, obj_, j_media_session); |
| 790 } | 716 } |
| 791 | 717 |
| 792 } // namespace content | 718 } // namespace content |
| OLD | NEW |