Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: content/browser/web_contents/web_contents_android.cc

Issue 1907703002: Fix a nasty scroll bug for Chrome Now-on-tap feature. Also combine the (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: further cleanup Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 const base::Value* result) { 62 const base::Value* result) {
63 JNIEnv* env = base::android::AttachCurrentThread(); 63 JNIEnv* env = base::android::AttachCurrentThread();
64 std::string json; 64 std::string json;
65 base::JSONWriter::Write(*result, &json); 65 base::JSONWriter::Write(*result, &json);
66 ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json); 66 ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json);
67 Java_WebContentsImpl_onEvaluateJavaScriptResult( 67 Java_WebContentsImpl_onEvaluateJavaScriptResult(
68 env, j_json.obj(), callback.obj()); 68 env, j_json.obj(), callback.obj());
69 } 69 }
70 70
71 struct AccessibilitySnapshotParams { 71 struct AccessibilitySnapshotParams {
72 AccessibilitySnapshotParams(float scale, 72 AccessibilitySnapshotParams()
73 float vertical_offset, 73 : has_tree_data(false), should_select_leaf_nodes(false) {}
74 float horizontal_scroll)
75 : scale_factor(scale),
76 y_offset(vertical_offset),
77 x_scroll(horizontal_scroll),
78 has_tree_data(false),
79 should_select_leaf_nodes(false) {}
80 74
81 float scale_factor;
82 float y_offset;
83 float x_scroll;
84 bool has_tree_data; 75 bool has_tree_data;
85 // The current text selection within this tree, if any, expressed as the 76 // The current text selection within this tree, if any, expressed as the
86 // node ID and character offset of the anchor (selection start) and focus 77 // node ID and character offset of the anchor (selection start) and focus
87 // (selection end). 78 // (selection end).
88 int32_t sel_anchor_object_id; 79 int32_t sel_anchor_object_id;
89 int32_t sel_anchor_offset; 80 int32_t sel_anchor_offset;
90 int32_t sel_focus_object_id; 81 int32_t sel_focus_object_id;
91 int32_t sel_focus_offset; 82 int32_t sel_focus_offset;
92 // if the flag is true, mark the leaf node as selected. 83 // if the flag is true, mark the leaf node as selected.
93 bool should_select_leaf_nodes; 84 bool should_select_leaf_nodes;
94 }; 85 };
95 86
96 ScopedJavaLocalRef<jobject> WalkAXTreeDepthFirst( 87 ScopedJavaLocalRef<jobject> WalkAXTreeDepthFirst(
97 JNIEnv* env, 88 JNIEnv* env,
98 BrowserAccessibilityAndroid* node, 89 BrowserAccessibilityAndroid* node,
99 AccessibilitySnapshotParams* params) { 90 AccessibilitySnapshotParams* params) {
100 ScopedJavaLocalRef<jstring> j_text = 91 ScopedJavaLocalRef<jstring> j_text =
101 ConvertUTF16ToJavaString(env, node->GetText()); 92 ConvertUTF16ToJavaString(env, node->GetText());
102 ScopedJavaLocalRef<jstring> j_class = 93 ScopedJavaLocalRef<jstring> j_class =
103 ConvertUTF8ToJavaString(env, node->GetClassName()); 94 ConvertUTF8ToJavaString(env, node->GetClassName());
104 const gfx::Rect& location = node->GetLocalBoundsRect();
105 // The style attributes exists and valid if size attribute exists. Otherwise, 95 // The style attributes exists and valid if size attribute exists. Otherwise,
106 // they are not. Use a negative size information to indicate the existence 96 // they are not. Use a negative size information to indicate the existence
107 // of style information. 97 // of style information.
108 float size = -1.0; 98 float size = -1.0;
109 int color = 0; 99 int color = 0;
110 int bgcolor = 0; 100 int bgcolor = 0;
111 int text_style = 0; 101 int text_style = 0;
112 102
113 if (node->HasFloatAttribute(ui::AX_ATTR_FONT_SIZE)) { 103 if (node->HasFloatAttribute(ui::AX_ATTR_FONT_SIZE)) {
114 color = node->GetIntAttribute(ui::AX_ATTR_COLOR); 104 color = node->GetIntAttribute(ui::AX_ATTR_COLOR);
115 bgcolor = node->GetIntAttribute(ui::AX_ATTR_BACKGROUND_COLOR); 105 bgcolor = node->GetIntAttribute(ui::AX_ATTR_BACKGROUND_COLOR);
116 size = node->GetFloatAttribute(ui::AX_ATTR_FONT_SIZE); 106 size = node->GetFloatAttribute(ui::AX_ATTR_FONT_SIZE);
117 text_style = node->GetIntAttribute(ui::AX_ATTR_TEXT_STYLE); 107 text_style = node->GetIntAttribute(ui::AX_ATTR_TEXT_STYLE);
118 } 108 }
119 float scale_factor = params->scale_factor; 109
110 const gfx::Rect& absolute_rect = node->GetLocalBoundsRect();
111 gfx::Rect parent_relative_rect = absolute_rect;
112 if (node->GetParent()) {
113 gfx::Rect parent_rect = node->GetParent()->GetLocalBoundsRect();
dmazzoni 2016/04/21 17:35:31 Since you're walking the tree depth-first, it'd be
sgurun-gerrit only 2016/04/21 21:02:58 Done.
114 parent_relative_rect.Offset(-parent_rect.OffsetFromOrigin());
115 }
116 bool is_root = node->GetParent() == NULL;
120 ScopedJavaLocalRef<jobject> j_node = 117 ScopedJavaLocalRef<jobject> j_node =
121 Java_WebContentsImpl_createAccessibilitySnapshotNode( 118 Java_WebContentsImpl_createAccessibilitySnapshotNode(
122 env, scale_factor * location.x() - params->x_scroll, 119 env, parent_relative_rect.x(), parent_relative_rect.y(),
123 scale_factor * location.y() + params->y_offset, 120 absolute_rect.width(), absolute_rect.height(), is_root, j_text.obj(),
124 scale_factor * node->GetScrollX(), scale_factor * node->GetScrollY(), 121 color, bgcolor, size, text_style, j_class.obj());
125 scale_factor * location.width(), scale_factor * location.height(),
126 j_text.obj(), color, bgcolor, scale_factor * size, text_style,
127 j_class.obj());
128 122
129 if (params->has_tree_data && node->PlatformIsLeaf()) { 123 if (params->has_tree_data && node->PlatformIsLeaf()) {
130 int start_selection = 0; 124 int start_selection = 0;
131 int end_selection = 0; 125 int end_selection = 0;
132 if (params->sel_anchor_object_id == node->GetId()) { 126 if (params->sel_anchor_object_id == node->GetId()) {
133 start_selection = params->sel_anchor_offset; 127 start_selection = params->sel_anchor_offset;
134 params->should_select_leaf_nodes = true; 128 params->should_select_leaf_nodes = true;
135 } 129 }
136 if (params->should_select_leaf_nodes) 130 if (params->should_select_leaf_nodes)
137 end_selection = node->GetText().length(); 131 end_selection = node->GetText().length();
(...skipping 12 matching lines...) Expand all
150 static_cast<BrowserAccessibilityAndroid*>( 144 static_cast<BrowserAccessibilityAndroid*>(
151 node->PlatformGetChild(i)); 145 node->PlatformGetChild(i));
152 Java_WebContentsImpl_addAccessibilityNodeAsChild( 146 Java_WebContentsImpl_addAccessibilityNodeAsChild(
153 env, j_node.obj(), WalkAXTreeDepthFirst(env, child, params).obj()); 147 env, j_node.obj(), WalkAXTreeDepthFirst(env, child, params).obj());
154 } 148 }
155 return j_node; 149 return j_node;
156 } 150 }
157 151
158 // Walks over the AXTreeUpdate and creates a light weight snapshot. 152 // Walks over the AXTreeUpdate and creates a light weight snapshot.
159 void AXTreeSnapshotCallback(const ScopedJavaGlobalRef<jobject>& callback, 153 void AXTreeSnapshotCallback(const ScopedJavaGlobalRef<jobject>& callback,
160 AccessibilitySnapshotParams* params,
161 const ui::AXTreeUpdate& result) { 154 const ui::AXTreeUpdate& result) {
162 JNIEnv* env = base::android::AttachCurrentThread(); 155 JNIEnv* env = base::android::AttachCurrentThread();
163 if (result.nodes.empty()) { 156 if (result.nodes.empty()) {
164 Java_WebContentsImpl_onAccessibilitySnapshot(env, nullptr, callback.obj()); 157 Java_WebContentsImpl_onAccessibilitySnapshot(env, nullptr, callback.obj());
165 return; 158 return;
166 } 159 }
167 std::unique_ptr<BrowserAccessibilityManagerAndroid> manager( 160 std::unique_ptr<BrowserAccessibilityManagerAndroid> manager(
168 static_cast<BrowserAccessibilityManagerAndroid*>( 161 static_cast<BrowserAccessibilityManagerAndroid*>(
169 BrowserAccessibilityManager::Create(result, nullptr))); 162 BrowserAccessibilityManager::Create(result, nullptr)));
170 manager->set_prune_tree_for_screen_reader(false); 163 manager->set_prune_tree_for_screen_reader(false);
171 BrowserAccessibilityAndroid* root = 164 BrowserAccessibilityAndroid* root =
172 static_cast<BrowserAccessibilityAndroid*>(manager->GetRoot()); 165 static_cast<BrowserAccessibilityAndroid*>(manager->GetRoot());
166 AccessibilitySnapshotParams params;
173 if (result.has_tree_data) { 167 if (result.has_tree_data) {
174 params->has_tree_data = true; 168 params.has_tree_data = true;
175 params->sel_anchor_object_id = result.tree_data.sel_anchor_object_id; 169 params.sel_anchor_object_id = result.tree_data.sel_anchor_object_id;
176 params->sel_anchor_offset = result.tree_data.sel_anchor_offset; 170 params.sel_anchor_offset = result.tree_data.sel_anchor_offset;
177 params->sel_focus_object_id = result.tree_data.sel_focus_object_id; 171 params.sel_focus_object_id = result.tree_data.sel_focus_object_id;
178 params->sel_focus_offset = result.tree_data.sel_focus_offset; 172 params.sel_focus_offset = result.tree_data.sel_focus_offset;
179 } 173 }
180 ScopedJavaLocalRef<jobject> j_root = WalkAXTreeDepthFirst(env, root, params); 174 ScopedJavaLocalRef<jobject> j_root = WalkAXTreeDepthFirst(env, root, &params);
181 Java_WebContentsImpl_onAccessibilitySnapshot( 175 Java_WebContentsImpl_onAccessibilitySnapshot(
182 env, j_root.obj(), callback.obj()); 176 env, j_root.obj(), callback.obj());
183 } 177 }
184 178
185 } // namespace 179 } // namespace
186 180
187 // static 181 // static
188 WebContents* WebContents::FromJavaWebContents( 182 WebContents* WebContents::FromJavaWebContents(
189 jobject jweb_contents_android) { 183 jobject jweb_contents_android) {
190 DCHECK_CURRENTLY_ON(BrowserThread::UI); 184 DCHECK_CURRENTLY_ON(BrowserThread::UI);
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 } 586 }
593 587
594 jint WebContentsAndroid::GetThemeColor(JNIEnv* env, 588 jint WebContentsAndroid::GetThemeColor(JNIEnv* env,
595 const JavaParamRef<jobject>& obj) { 589 const JavaParamRef<jobject>& obj) {
596 return web_contents_->GetThemeColor(); 590 return web_contents_->GetThemeColor();
597 } 591 }
598 592
599 void WebContentsAndroid::RequestAccessibilitySnapshot( 593 void WebContentsAndroid::RequestAccessibilitySnapshot(
600 JNIEnv* env, 594 JNIEnv* env,
601 const JavaParamRef<jobject>& obj, 595 const JavaParamRef<jobject>& obj,
602 const JavaParamRef<jobject>& callback, 596 const JavaParamRef<jobject>& callback) {
603 jfloat y_offset,
604 jfloat x_scroll) {
605 // Secure the Java callback in a scoped object and give ownership of it to the 597 // Secure the Java callback in a scoped object and give ownership of it to the
606 // base::Callback. 598 // base::Callback.
607 ScopedJavaGlobalRef<jobject> j_callback; 599 ScopedJavaGlobalRef<jobject> j_callback;
608 j_callback.Reset(env, callback); 600 j_callback.Reset(env, callback);
609 gfx::DeviceDisplayInfo device_info; 601 gfx::DeviceDisplayInfo device_info;
610 ContentViewCoreImpl* contentViewCore =
611 ContentViewCoreImpl::FromWebContents(web_contents_);
612 602
613 AccessibilitySnapshotParams* params = new AccessibilitySnapshotParams(
614 contentViewCore->GetScaleFactor(), y_offset, x_scroll);
615 WebContentsImpl::AXTreeSnapshotCallback snapshot_callback = 603 WebContentsImpl::AXTreeSnapshotCallback snapshot_callback =
616 base::Bind(&AXTreeSnapshotCallback, j_callback, base::Owned(params)); 604 base::Bind(&AXTreeSnapshotCallback, j_callback);
617 static_cast<WebContentsImpl*>(web_contents_)->RequestAXTreeSnapshot( 605 static_cast<WebContentsImpl*>(web_contents_)->RequestAXTreeSnapshot(
618 snapshot_callback); 606 snapshot_callback);
619 } 607 }
620 608
621 void WebContentsAndroid::ResumeMediaSession(JNIEnv* env, 609 void WebContentsAndroid::ResumeMediaSession(JNIEnv* env,
622 const JavaParamRef<jobject>& obj) { 610 const JavaParamRef<jobject>& obj) {
623 web_contents_->ResumeMediaSession(); 611 web_contents_->ResumeMediaSession();
624 } 612 }
625 613
626 void WebContentsAndroid::SuspendMediaSession(JNIEnv* env, 614 void WebContentsAndroid::SuspendMediaSession(JNIEnv* env,
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
692 if (response == READBACK_SUCCESS) 680 if (response == READBACK_SUCCESS)
693 java_bitmap = gfx::ConvertToJavaBitmap(&bitmap); 681 java_bitmap = gfx::ConvertToJavaBitmap(&bitmap);
694 Java_WebContentsImpl_onGetContentBitmapFinished(env, 682 Java_WebContentsImpl_onGetContentBitmapFinished(env,
695 obj->obj(), 683 obj->obj(),
696 callback->obj(), 684 callback->obj(),
697 java_bitmap.obj(), 685 java_bitmap.obj(),
698 response); 686 response);
699 } 687 }
700 688
701 } // namespace content 689 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698