| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/dom_distiller/content/browser/distillable_page_utils_androi
d.h" | 5 #include "components/dom_distiller/content/browser/distillable_page_utils_androi
d.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "components/dom_distiller/content/browser/distillable_page_utils.h" | 13 #include "components/dom_distiller/content/browser/distillable_page_utils.h" |
| 14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 15 #include "jni/DistillablePageUtils_jni.h" | 15 #include "jni/DistillablePageUtils_jni.h" |
| 16 | 16 |
| 17 using base::android::JavaParamRef; | 17 using base::android::JavaParamRef; |
| 18 using base::android::JavaRef; |
| 18 using base::android::ScopedJavaGlobalRef; | 19 using base::android::ScopedJavaGlobalRef; |
| 19 | 20 |
| 20 namespace dom_distiller { | 21 namespace dom_distiller { |
| 21 namespace android { | 22 namespace android { |
| 22 namespace { | 23 namespace { |
| 23 void OnIsPageDistillableResult( | 24 void OnIsPageDistillableResult(const JavaRef<jobject>& callback, |
| 24 std::unique_ptr<ScopedJavaGlobalRef<jobject>> callback_holder, | 25 bool isDistillable) { |
| 25 bool isDistillable) { | |
| 26 Java_DistillablePageUtils_callOnIsPageDistillableResult( | 26 Java_DistillablePageUtils_callOnIsPageDistillableResult( |
| 27 base::android::AttachCurrentThread(), callback_holder->obj(), | 27 base::android::AttachCurrentThread(), callback, isDistillable); |
| 28 isDistillable); | |
| 29 } | 28 } |
| 30 | 29 |
| 31 void OnIsPageDistillableUpdate( | 30 void OnIsPageDistillableUpdate(const JavaRef<jobject>& callback, |
| 32 ScopedJavaGlobalRef<jobject>* callback_holder, | 31 bool isDistillable, |
| 33 bool isDistillable, bool isLast) { | 32 bool isLast) { |
| 34 Java_DistillablePageUtils_callOnIsPageDistillableUpdate( | 33 Java_DistillablePageUtils_callOnIsPageDistillableUpdate( |
| 35 base::android::AttachCurrentThread(), callback_holder->obj(), | 34 base::android::AttachCurrentThread(), callback, isDistillable, isLast); |
| 36 isDistillable, isLast); | |
| 37 } | 35 } |
| 38 } // namespace | 36 } // namespace |
| 39 | 37 |
| 40 static void IsPageDistillable(JNIEnv* env, | 38 static void IsPageDistillable(JNIEnv* env, |
| 41 const JavaParamRef<jclass>& jcaller, | 39 const JavaParamRef<jclass>& jcaller, |
| 42 const JavaParamRef<jobject>& webContents, | 40 const JavaParamRef<jobject>& webContents, |
| 43 jboolean is_mobile_optimized, | 41 jboolean is_mobile_optimized, |
| 44 const JavaParamRef<jobject>& callback) { | 42 const JavaParamRef<jobject>& callback) { |
| 45 content::WebContents* web_contents( | 43 content::WebContents* web_contents( |
| 46 content::WebContents::FromJavaWebContents(webContents)); | 44 content::WebContents::FromJavaWebContents(webContents)); |
| 47 std::unique_ptr<ScopedJavaGlobalRef<jobject>> callback_holder( | |
| 48 new ScopedJavaGlobalRef<jobject>()); | |
| 49 callback_holder->Reset(env, callback); | |
| 50 | 45 |
| 51 if (!web_contents) { | 46 if (!web_contents) { |
| 52 base::ThreadTaskRunnerHandle::Get()->PostTask( | 47 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 53 FROM_HERE, base::Bind(OnIsPageDistillableResult, | 48 FROM_HERE, |
| 54 base::Passed(&callback_holder), false)); | 49 base::Bind(OnIsPageDistillableResult, |
| 50 ScopedJavaGlobalRef<jobject>(env, callback), false)); |
| 55 return; | 51 return; |
| 56 } | 52 } |
| 57 IsDistillablePage( | 53 IsDistillablePage(web_contents, is_mobile_optimized, |
| 58 web_contents, is_mobile_optimized, | 54 base::Bind(OnIsPageDistillableResult, |
| 59 base::Bind(OnIsPageDistillableResult, base::Passed(&callback_holder))); | 55 ScopedJavaGlobalRef<jobject>(env, callback))); |
| 60 } | 56 } |
| 61 | 57 |
| 62 static void SetDelegate(JNIEnv* env, | 58 static void SetDelegate(JNIEnv* env, |
| 63 const JavaParamRef<jclass>& jcaller, | 59 const JavaParamRef<jclass>& jcaller, |
| 64 const JavaParamRef<jobject>& webContents, | 60 const JavaParamRef<jobject>& webContents, |
| 65 const JavaParamRef<jobject>& callback) { | 61 const JavaParamRef<jobject>& callback) { |
| 66 content::WebContents* web_contents( | 62 content::WebContents* web_contents( |
| 67 content::WebContents::FromJavaWebContents(webContents)); | 63 content::WebContents::FromJavaWebContents(webContents)); |
| 68 if (!web_contents) { | 64 if (!web_contents) { |
| 69 return; | 65 return; |
| 70 } | 66 } |
| 71 | 67 |
| 72 // TODO(wychen): check memory management | 68 DistillabilityDelegate delegate = base::Bind( |
| 73 ScopedJavaGlobalRef<jobject>* callback_holder( | 69 OnIsPageDistillableUpdate, ScopedJavaGlobalRef<jobject>(env, callback)); |
| 74 new ScopedJavaGlobalRef<jobject>()); | |
| 75 callback_holder->Reset(env, callback); | |
| 76 | |
| 77 DistillabilityDelegate delegate = | |
| 78 base::Bind(OnIsPageDistillableUpdate, base::Owned(callback_holder)); | |
| 79 setDelegate(web_contents, delegate); | 70 setDelegate(web_contents, delegate); |
| 80 } | 71 } |
| 81 | 72 |
| 82 bool RegisterDistillablePageUtils(JNIEnv* env) { | 73 bool RegisterDistillablePageUtils(JNIEnv* env) { |
| 83 return RegisterNativesImpl(env); | 74 return RegisterNativesImpl(env); |
| 84 } | 75 } |
| 85 | 76 |
| 86 } | 77 } |
| 87 } | 78 } |
| OLD | NEW |