| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 // destroyed WebContents object. | 58 // destroyed WebContents object. |
| 59 base::LazyInstance<base::hash_set<WebContentsAndroid*> >::Leaky | 59 base::LazyInstance<base::hash_set<WebContentsAndroid*> >::Leaky |
| 60 g_allocated_web_contents_androids = LAZY_INSTANCE_INITIALIZER; | 60 g_allocated_web_contents_androids = LAZY_INSTANCE_INITIALIZER; |
| 61 | 61 |
| 62 void JavaScriptResultCallback(const ScopedJavaGlobalRef<jobject>& callback, | 62 void JavaScriptResultCallback(const ScopedJavaGlobalRef<jobject>& callback, |
| 63 const base::Value* result) { | 63 const base::Value* result) { |
| 64 JNIEnv* env = base::android::AttachCurrentThread(); | 64 JNIEnv* env = base::android::AttachCurrentThread(); |
| 65 std::string json; | 65 std::string json; |
| 66 base::JSONWriter::Write(*result, &json); | 66 base::JSONWriter::Write(*result, &json); |
| 67 ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json); | 67 ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json); |
| 68 Java_WebContentsImpl_onEvaluateJavaScriptResult( | 68 Java_WebContentsImpl_onEvaluateJavaScriptResult(env, j_json, callback); |
| 69 env, j_json.obj(), callback.obj()); | |
| 70 } | 69 } |
| 71 | 70 |
| 72 struct AccessibilitySnapshotParams { | 71 struct AccessibilitySnapshotParams { |
| 73 AccessibilitySnapshotParams() | 72 AccessibilitySnapshotParams() |
| 74 : has_tree_data(false), should_select_leaf_nodes(false) {} | 73 : has_tree_data(false), should_select_leaf_nodes(false) {} |
| 75 | 74 |
| 76 bool has_tree_data; | 75 bool has_tree_data; |
| 77 // 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 |
| 78 // 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 |
| 79 // (selection end). | 78 // (selection end). |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 | 110 |
| 112 const gfx::Rect& absolute_rect = node->GetPageBoundsRect(); | 111 const gfx::Rect& absolute_rect = node->GetPageBoundsRect(); |
| 113 gfx::Rect parent_relative_rect = absolute_rect; | 112 gfx::Rect parent_relative_rect = absolute_rect; |
| 114 bool is_root = node->GetParent() == nullptr; | 113 bool is_root = node->GetParent() == nullptr; |
| 115 if (!is_root) { | 114 if (!is_root) { |
| 116 parent_relative_rect.Offset(-parent_rect.OffsetFromOrigin()); | 115 parent_relative_rect.Offset(-parent_rect.OffsetFromOrigin()); |
| 117 } | 116 } |
| 118 ScopedJavaLocalRef<jobject> j_node = | 117 ScopedJavaLocalRef<jobject> j_node = |
| 119 Java_WebContentsImpl_createAccessibilitySnapshotNode( | 118 Java_WebContentsImpl_createAccessibilitySnapshotNode( |
| 120 env, parent_relative_rect.x(), parent_relative_rect.y(), | 119 env, parent_relative_rect.x(), parent_relative_rect.y(), |
| 121 absolute_rect.width(), absolute_rect.height(), is_root, j_text.obj(), | 120 absolute_rect.width(), absolute_rect.height(), is_root, j_text, color, |
| 122 color, bgcolor, size, text_style, j_class.obj()); | 121 bgcolor, size, text_style, j_class); |
| 123 | 122 |
| 124 if (params->has_tree_data && node->PlatformIsLeaf()) { | 123 if (params->has_tree_data && node->PlatformIsLeaf()) { |
| 125 int start_selection = 0; | 124 int start_selection = 0; |
| 126 int end_selection = 0; | 125 int end_selection = 0; |
| 127 if (params->sel_anchor_object_id == node->GetId()) { | 126 if (params->sel_anchor_object_id == node->GetId()) { |
| 128 start_selection = params->sel_anchor_offset; | 127 start_selection = params->sel_anchor_offset; |
| 129 params->should_select_leaf_nodes = true; | 128 params->should_select_leaf_nodes = true; |
| 130 } | 129 } |
| 131 if (params->should_select_leaf_nodes) | 130 if (params->should_select_leaf_nodes) |
| 132 end_selection = node->GetText().length(); | 131 end_selection = node->GetText().length(); |
| 133 | 132 |
| 134 if (params->sel_focus_object_id == node->GetId()) { | 133 if (params->sel_focus_object_id == node->GetId()) { |
| 135 end_selection = params->sel_focus_offset; | 134 end_selection = params->sel_focus_offset; |
| 136 params->should_select_leaf_nodes = false; | 135 params->should_select_leaf_nodes = false; |
| 137 } | 136 } |
| 138 if (end_selection > 0) | 137 if (end_selection > 0) |
| 139 Java_WebContentsImpl_setAccessibilitySnapshotSelection( | 138 Java_WebContentsImpl_setAccessibilitySnapshotSelection( |
| 140 env, j_node.obj(), start_selection, end_selection); | 139 env, j_node, start_selection, end_selection); |
| 141 } | 140 } |
| 142 | 141 |
| 143 for (uint32_t i = 0; i < node->PlatformChildCount(); i++) { | 142 for (uint32_t i = 0; i < node->PlatformChildCount(); i++) { |
| 144 BrowserAccessibilityAndroid* child = | 143 BrowserAccessibilityAndroid* child = |
| 145 static_cast<BrowserAccessibilityAndroid*>( | 144 static_cast<BrowserAccessibilityAndroid*>( |
| 146 node->PlatformGetChild(i)); | 145 node->PlatformGetChild(i)); |
| 147 Java_WebContentsImpl_addAccessibilityNodeAsChild( | 146 Java_WebContentsImpl_addAccessibilityNodeAsChild( |
| 148 env, j_node.obj(), | 147 env, j_node, WalkAXTreeDepthFirst(env, child, absolute_rect, params)); |
| 149 WalkAXTreeDepthFirst(env, child, absolute_rect, params).obj()); | |
| 150 } | 148 } |
| 151 return j_node; | 149 return j_node; |
| 152 } | 150 } |
| 153 | 151 |
| 154 // Walks over the AXTreeUpdate and creates a light weight snapshot. | 152 // Walks over the AXTreeUpdate and creates a light weight snapshot. |
| 155 void AXTreeSnapshotCallback(const ScopedJavaGlobalRef<jobject>& callback, | 153 void AXTreeSnapshotCallback(const ScopedJavaGlobalRef<jobject>& callback, |
| 156 const ui::AXTreeUpdate& result) { | 154 const ui::AXTreeUpdate& result) { |
| 157 JNIEnv* env = base::android::AttachCurrentThread(); | 155 JNIEnv* env = base::android::AttachCurrentThread(); |
| 158 if (result.nodes.empty()) { | 156 if (result.nodes.empty()) { |
| 159 Java_WebContentsImpl_onAccessibilitySnapshot(env, nullptr, callback.obj()); | 157 Java_WebContentsImpl_onAccessibilitySnapshot(env, nullptr, callback); |
| 160 return; | 158 return; |
| 161 } | 159 } |
| 162 std::unique_ptr<BrowserAccessibilityManagerAndroid> manager( | 160 std::unique_ptr<BrowserAccessibilityManagerAndroid> manager( |
| 163 static_cast<BrowserAccessibilityManagerAndroid*>( | 161 static_cast<BrowserAccessibilityManagerAndroid*>( |
| 164 BrowserAccessibilityManager::Create(result, nullptr))); | 162 BrowserAccessibilityManager::Create(result, nullptr))); |
| 165 manager->set_prune_tree_for_screen_reader(false); | 163 manager->set_prune_tree_for_screen_reader(false); |
| 166 BrowserAccessibilityAndroid* root = | 164 BrowserAccessibilityAndroid* root = |
| 167 static_cast<BrowserAccessibilityAndroid*>(manager->GetRoot()); | 165 static_cast<BrowserAccessibilityAndroid*>(manager->GetRoot()); |
| 168 AccessibilitySnapshotParams params; | 166 AccessibilitySnapshotParams params; |
| 169 if (result.has_tree_data) { | 167 if (result.has_tree_data) { |
| 170 params.has_tree_data = true; | 168 params.has_tree_data = true; |
| 171 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; |
| 172 params.sel_anchor_offset = result.tree_data.sel_anchor_offset; | 170 params.sel_anchor_offset = result.tree_data.sel_anchor_offset; |
| 173 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; |
| 174 params.sel_focus_offset = result.tree_data.sel_focus_offset; | 172 params.sel_focus_offset = result.tree_data.sel_focus_offset; |
| 175 } | 173 } |
| 176 gfx::Rect parent_rect; | 174 gfx::Rect parent_rect; |
| 177 ScopedJavaLocalRef<jobject> j_root = | 175 ScopedJavaLocalRef<jobject> j_root = |
| 178 WalkAXTreeDepthFirst(env, root, parent_rect, ¶ms); | 176 WalkAXTreeDepthFirst(env, root, parent_rect, ¶ms); |
| 179 Java_WebContentsImpl_onAccessibilitySnapshot( | 177 Java_WebContentsImpl_onAccessibilitySnapshot(env, j_root, callback); |
| 180 env, j_root.obj(), callback.obj()); | |
| 181 } | 178 } |
| 182 | 179 |
| 183 } // namespace | 180 } // namespace |
| 184 | 181 |
| 185 // static | 182 // static |
| 186 WebContents* WebContents::FromJavaWebContents( | 183 WebContents* WebContents::FromJavaWebContents( |
| 187 jobject jweb_contents_android) { | 184 jobject jweb_contents_android) { |
| 188 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 185 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 189 if (!jweb_contents_android) | 186 if (!jweb_contents_android) |
| 190 return NULL; | 187 return NULL; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 } | 236 } |
| 240 | 237 |
| 241 WebContentsAndroid::WebContentsAndroid(WebContentsImpl* web_contents) | 238 WebContentsAndroid::WebContentsAndroid(WebContentsImpl* web_contents) |
| 242 : web_contents_(web_contents), | 239 : web_contents_(web_contents), |
| 243 navigation_controller_(&(web_contents->GetController())), | 240 navigation_controller_(&(web_contents->GetController())), |
| 244 synchronous_compositor_client_(nullptr), | 241 synchronous_compositor_client_(nullptr), |
| 245 weak_factory_(this) { | 242 weak_factory_(this) { |
| 246 g_allocated_web_contents_androids.Get().insert(this); | 243 g_allocated_web_contents_androids.Get().insert(this); |
| 247 JNIEnv* env = AttachCurrentThread(); | 244 JNIEnv* env = AttachCurrentThread(); |
| 248 obj_.Reset(env, | 245 obj_.Reset(env, |
| 249 Java_WebContentsImpl_create( | 246 Java_WebContentsImpl_create(env, reinterpret_cast<intptr_t>(this), |
| 250 env, | 247 navigation_controller_.GetJavaObject()) |
| 251 reinterpret_cast<intptr_t>(this), | 248 .obj()); |
| 252 navigation_controller_.GetJavaObject().obj()).obj()); | |
| 253 RendererPreferences* prefs = web_contents_->GetMutableRendererPrefs(); | 249 RendererPreferences* prefs = web_contents_->GetMutableRendererPrefs(); |
| 254 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 250 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 255 prefs->network_contry_iso = | 251 prefs->network_contry_iso = |
| 256 command_line->HasSwitch(switches::kNetworkCountryIso) ? | 252 command_line->HasSwitch(switches::kNetworkCountryIso) ? |
| 257 command_line->GetSwitchValueASCII(switches::kNetworkCountryIso) | 253 command_line->GetSwitchValueASCII(switches::kNetworkCountryIso) |
| 258 : net::android::GetTelephonyNetworkCountryIso(); | 254 : net::android::GetTelephonyNetworkCountryIso(); |
| 259 } | 255 } |
| 260 | 256 |
| 261 WebContentsAndroid::~WebContentsAndroid() { | 257 WebContentsAndroid::~WebContentsAndroid() { |
| 262 DCHECK(g_allocated_web_contents_androids.Get().find(this) != | 258 DCHECK(g_allocated_web_contents_androids.Get().find(this) != |
| 263 g_allocated_web_contents_androids.Get().end()); | 259 g_allocated_web_contents_androids.Get().end()); |
| 264 g_allocated_web_contents_androids.Get().erase(this); | 260 g_allocated_web_contents_androids.Get().erase(this); |
| 265 Java_WebContentsImpl_clearNativePtr(AttachCurrentThread(), obj_.obj()); | 261 Java_WebContentsImpl_clearNativePtr(AttachCurrentThread(), obj_); |
| 266 } | 262 } |
| 267 | 263 |
| 268 base::android::ScopedJavaLocalRef<jobject> | 264 base::android::ScopedJavaLocalRef<jobject> |
| 269 WebContentsAndroid::GetJavaObject() { | 265 WebContentsAndroid::GetJavaObject() { |
| 270 return base::android::ScopedJavaLocalRef<jobject>(obj_); | 266 return base::android::ScopedJavaLocalRef<jobject>(obj_); |
| 271 } | 267 } |
| 272 | 268 |
| 273 ScopedJavaLocalRef<jstring> WebContentsAndroid::GetTitle( | 269 ScopedJavaLocalRef<jstring> WebContentsAndroid::GetTitle( |
| 274 JNIEnv* env, | 270 JNIEnv* env, |
| 275 const JavaParamRef<jobject>& obj) const { | 271 const JavaParamRef<jobject>& obj) const { |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 688 | 684 |
| 689 void WebContentsAndroid::OnFinishGetContentBitmap( | 685 void WebContentsAndroid::OnFinishGetContentBitmap( |
| 690 ScopedJavaGlobalRef<jobject>* obj, | 686 ScopedJavaGlobalRef<jobject>* obj, |
| 691 ScopedJavaGlobalRef<jobject>* callback, | 687 ScopedJavaGlobalRef<jobject>* callback, |
| 692 const SkBitmap& bitmap, | 688 const SkBitmap& bitmap, |
| 693 ReadbackResponse response) { | 689 ReadbackResponse response) { |
| 694 JNIEnv* env = base::android::AttachCurrentThread(); | 690 JNIEnv* env = base::android::AttachCurrentThread(); |
| 695 ScopedJavaLocalRef<jobject> java_bitmap; | 691 ScopedJavaLocalRef<jobject> java_bitmap; |
| 696 if (response == READBACK_SUCCESS) | 692 if (response == READBACK_SUCCESS) |
| 697 java_bitmap = gfx::ConvertToJavaBitmap(&bitmap); | 693 java_bitmap = gfx::ConvertToJavaBitmap(&bitmap); |
| 698 Java_WebContentsImpl_onGetContentBitmapFinished(env, | 694 Java_WebContentsImpl_onGetContentBitmapFinished( |
| 699 obj->obj(), | 695 env, obj->obj(), callback->obj(), java_bitmap, response); |
| 700 callback->obj(), | |
| 701 java_bitmap.obj(), | |
| 702 response); | |
| 703 } | 696 } |
| 704 | 697 |
| 705 void WebContentsAndroid::OnFinishDownloadImage( | 698 void WebContentsAndroid::OnFinishDownloadImage( |
| 706 base::android::ScopedJavaGlobalRef<jobject>* obj, | 699 base::android::ScopedJavaGlobalRef<jobject>* obj, |
| 707 base::android::ScopedJavaGlobalRef<jobject>* callback, | 700 base::android::ScopedJavaGlobalRef<jobject>* callback, |
| 708 int id, | 701 int id, |
| 709 int http_status_code, | 702 int http_status_code, |
| 710 const GURL& url, | 703 const GURL& url, |
| 711 const std::vector<SkBitmap>& bitmaps, | 704 const std::vector<SkBitmap>& bitmaps, |
| 712 const std::vector<gfx::Size>& sizes) { | 705 const std::vector<gfx::Size>& sizes) { |
| 713 JNIEnv* env = base::android::AttachCurrentThread(); | 706 JNIEnv* env = base::android::AttachCurrentThread(); |
| 714 ScopedJavaLocalRef<jobject> jbitmaps = | 707 ScopedJavaLocalRef<jobject> jbitmaps = |
| 715 Java_WebContentsImpl_createBitmapList(env); | 708 Java_WebContentsImpl_createBitmapList(env); |
| 716 ScopedJavaLocalRef<jobject> jsizes = | 709 ScopedJavaLocalRef<jobject> jsizes = |
| 717 Java_WebContentsImpl_createSizeList(env); | 710 Java_WebContentsImpl_createSizeList(env); |
| 718 ScopedJavaLocalRef<jstring> jurl = | 711 ScopedJavaLocalRef<jstring> jurl = |
| 719 base::android::ConvertUTF8ToJavaString(env, url.spec()); | 712 base::android::ConvertUTF8ToJavaString(env, url.spec()); |
| 720 | 713 |
| 721 for (const SkBitmap& bitmap : bitmaps) { | 714 for (const SkBitmap& bitmap : bitmaps) { |
| 722 // WARNING: convering to java bitmaps results in duplicate memory | 715 // WARNING: convering to java bitmaps results in duplicate memory |
| 723 // allocations, which increases the chance of OOMs if DownloadImage() is | 716 // allocations, which increases the chance of OOMs if DownloadImage() is |
| 724 // misused. | 717 // misused. |
| 725 ScopedJavaLocalRef<jobject> jbitmap = gfx::ConvertToJavaBitmap(&bitmap); | 718 ScopedJavaLocalRef<jobject> jbitmap = gfx::ConvertToJavaBitmap(&bitmap); |
| 726 Java_WebContentsImpl_addToBitmapList(env, jbitmaps.obj(), jbitmap.obj()); | 719 Java_WebContentsImpl_addToBitmapList(env, jbitmaps, jbitmap); |
| 727 } | 720 } |
| 728 for (const gfx::Size& size : sizes) { | 721 for (const gfx::Size& size : sizes) { |
| 729 Java_WebContentsImpl_createSizeAndAddToList( | 722 Java_WebContentsImpl_createSizeAndAddToList(env, jsizes, size.width(), |
| 730 env, jsizes.obj(), size.width(), size.height()); | 723 size.height()); |
| 731 } | 724 } |
| 732 Java_WebContentsImpl_onDownloadImageFinished( | 725 Java_WebContentsImpl_onDownloadImageFinished(env, obj->obj(), callback->obj(), |
| 733 env, obj->obj(), callback->obj(), id, | 726 id, http_status_code, jurl, |
| 734 http_status_code, jurl.obj(), jbitmaps.obj(), jsizes.obj()); | 727 jbitmaps, jsizes); |
| 735 } | 728 } |
| 736 } // namespace content | 729 } // namespace content |
| OLD | NEW |