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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "components/dom_distiller/content/browser/distillable_page_utils.h" | 9 #include "components/dom_distiller/content/browser/distillable_page_utils.h" |
10 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
11 #include "jni/DistillablePageUtils_jni.h" | 11 #include "jni/DistillablePageUtils_jni.h" |
12 | 12 |
13 using base::android::ScopedJavaGlobalRef; | 13 using base::android::ScopedJavaGlobalRef; |
14 | 14 |
15 namespace dom_distiller { | 15 namespace dom_distiller { |
16 namespace android { | 16 namespace android { |
17 namespace { | 17 namespace { |
18 void OnIsPageDistillableResult( | 18 void OnIsPageDistillableResult( |
19 scoped_ptr<ScopedJavaGlobalRef<jobject>> callback_holder, | 19 scoped_ptr<ScopedJavaGlobalRef<jobject>> callback_holder, |
20 bool isDistillable) { | 20 bool isDistillable) { |
21 Java_DistillablePageUtils_callOnIsPageDistillableResult( | 21 Java_DistillablePageUtils_callOnIsPageDistillableResult( |
22 base::android::AttachCurrentThread(), callback_holder->obj(), | 22 base::android::AttachCurrentThread(), callback_holder->obj(), |
23 isDistillable); | 23 isDistillable); |
24 } | 24 } |
25 | |
26 void OnIsPageDistillableUpdate( | |
27 ScopedJavaGlobalRef<jobject>* callback_holder, | |
28 bool isDistillable, bool isLast) { | |
29 Java_DistillablePageUtils_callOnIsPageDistillableUpdate( | |
30 base::android::AttachCurrentThread(), callback_holder->obj(), | |
31 isDistillable, isLast); | |
32 } | |
25 } // namespace | 33 } // namespace |
26 | 34 |
27 static void IsPageDistillable(JNIEnv* env, | 35 static void IsPageDistillable(JNIEnv* env, |
28 const JavaParamRef<jclass>& jcaller, | 36 const JavaParamRef<jclass>& jcaller, |
29 const JavaParamRef<jobject>& webContents, | 37 const JavaParamRef<jobject>& webContents, |
30 jboolean is_mobile_optimized, | 38 jboolean is_mobile_optimized, |
31 const JavaParamRef<jobject>& callback) { | 39 const JavaParamRef<jobject>& callback) { |
32 content::WebContents* web_contents( | 40 content::WebContents* web_contents( |
33 content::WebContents::FromJavaWebContents(webContents)); | 41 content::WebContents::FromJavaWebContents(webContents)); |
34 scoped_ptr<ScopedJavaGlobalRef<jobject>> callback_holder( | 42 scoped_ptr<ScopedJavaGlobalRef<jobject>> callback_holder( |
35 new ScopedJavaGlobalRef<jobject>()); | 43 new ScopedJavaGlobalRef<jobject>()); |
36 callback_holder->Reset(env, callback); | 44 callback_holder->Reset(env, callback); |
37 | 45 |
38 if (!web_contents) { | 46 if (!web_contents) { |
39 base::MessageLoop::current()->PostTask( | 47 base::MessageLoop::current()->PostTask( |
40 FROM_HERE, base::Bind(OnIsPageDistillableResult, | 48 FROM_HERE, base::Bind(OnIsPageDistillableResult, |
41 base::Passed(&callback_holder), false)); | 49 base::Passed(&callback_holder), false)); |
42 return; | 50 return; |
43 } | 51 } |
44 IsDistillablePage( | 52 IsDistillablePage( |
45 web_contents, is_mobile_optimized, | 53 web_contents, is_mobile_optimized, |
46 base::Bind(OnIsPageDistillableResult, base::Passed(&callback_holder))); | 54 base::Bind(OnIsPageDistillableResult, base::Passed(&callback_holder))); |
47 } | 55 } |
48 | 56 |
57 static void SetDelegate(JNIEnv* env, | |
58 const JavaParamRef<jclass>& jcaller, | |
59 const JavaParamRef<jobject>& webContents, | |
60 const JavaParamRef<jobject>& callback) { | |
61 content::WebContents* web_contents( | |
62 content::WebContents::FromJavaWebContents(webContents)); | |
63 if (!web_contents) { | |
64 return; | |
65 } | |
66 | |
67 // FIXME(wychen): check memory management | |
68 ScopedJavaGlobalRef<jobject>* callback_holder( | |
mdjones
2015/11/05 01:09:12
Try extending jobject to log when it is destructed
wychen
2015/11/12 05:39:28
Turned out to be more difficult than I thought. Sk
| |
69 new ScopedJavaGlobalRef<jobject>()); | |
70 callback_holder->Reset(env, callback); | |
71 | |
72 DistillabilityDelegate delegate = | |
73 base::Bind(OnIsPageDistillableUpdate, base::Owned(callback_holder)); | |
74 setDelegate(web_contents, delegate); | |
75 } | |
76 | |
49 bool RegisterDistillablePageUtils(JNIEnv* env) { | 77 bool RegisterDistillablePageUtils(JNIEnv* env) { |
50 return RegisterNativesImpl(env); | 78 return RegisterNativesImpl(env); |
51 } | 79 } |
52 | 80 |
53 } | 81 } |
54 } | 82 } |
OLD | NEW |