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 "chrome/browser/android/contextualsearch/contextual_search_manager.h" | 5 #include "chrome/browser/android/contextualsearch/contextual_search_manager.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "chrome/browser/android/contextualsearch/contextual_search_delegate.h" | 13 #include "chrome/browser/android/contextualsearch/contextual_search_delegate.h" |
14 #include "chrome/browser/android/contextualsearch/resolved_search_term.h" | 14 #include "chrome/browser/android/contextualsearch/resolved_search_term.h" |
15 #include "chrome/browser/android/tab_android.h" | 15 #include "chrome/browser/android/tab_android.h" |
16 #include "chrome/browser/profiles/profile_manager.h" | 16 #include "chrome/browser/profiles/profile_manager.h" |
17 #include "chrome/browser/search_engines/template_url_service_factory.h" | 17 #include "chrome/browser/search_engines/template_url_service_factory.h" |
18 #include "components/contextual_search/browser/contextual_search_js_api_service_
impl.h" | 18 #include "components/contextual_search/browser/contextual_search_js_api_service_
impl.h" |
19 #include "components/contextual_search/common/overlay_page_notifier_service.mojo
m.h" | 19 #include "components/contextual_search/common/overlay_page_notifier_service.mojo
m.h" |
20 #include "components/navigation_interception/intercept_navigation_delegate.h" | 20 #include "components/navigation_interception/intercept_navigation_delegate.h" |
21 #include "components/variations/variations_associated_data.h" | 21 #include "components/variations/variations_associated_data.h" |
22 #include "content/public/browser/android/content_view_core.h" | |
23 #include "content/public/browser/render_frame_host.h" | 22 #include "content/public/browser/render_frame_host.h" |
24 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
| 24 #include "content/public/browser/web_contents.h" |
25 #include "jni/ContextualSearchManager_jni.h" | 25 #include "jni/ContextualSearchManager_jni.h" |
26 #include "net/url_request/url_fetcher_impl.h" | 26 #include "net/url_request/url_fetcher_impl.h" |
27 #include "services/shell/public/cpp/interface_provider.h" | 27 #include "services/shell/public/cpp/interface_provider.h" |
28 #include "services/shell/public/cpp/interface_registry.h" | 28 #include "services/shell/public/cpp/interface_registry.h" |
29 | 29 |
30 using content::ContentViewCore; | 30 using content::WebContents; |
31 | 31 |
32 // This class manages the native behavior of the Contextual Search feature. | 32 // This class manages the native behavior of the Contextual Search feature. |
33 // Instances of this class are owned by the Java ContextualSearchManager. | 33 // Instances of this class are owned by the Java ContextualSearchManager. |
34 // Most of the work is actually done in an associated delegate to this class: | 34 // Most of the work is actually done in an associated delegate to this class: |
35 // the ContextualSearchDelegate. | 35 // the ContextualSearchDelegate. |
36 ContextualSearchManager::ContextualSearchManager(JNIEnv* env, jobject obj) { | 36 ContextualSearchManager::ContextualSearchManager(JNIEnv* env, jobject obj) { |
37 java_manager_.Reset(env, obj); | 37 java_manager_.Reset(env, obj); |
38 Java_ContextualSearchManager_setNativeManager( | 38 Java_ContextualSearchManager_setNativeManager( |
39 env, obj, reinterpret_cast<intptr_t>(this)); | 39 env, obj, reinterpret_cast<intptr_t>(this)); |
40 Profile* profile = ProfileManager::GetActiveUserProfile(); | 40 Profile* profile = ProfileManager::GetActiveUserProfile(); |
(...skipping 16 matching lines...) Expand all Loading... |
57 void ContextualSearchManager::Destroy(JNIEnv* env, | 57 void ContextualSearchManager::Destroy(JNIEnv* env, |
58 const JavaParamRef<jobject>& obj) { | 58 const JavaParamRef<jobject>& obj) { |
59 delete this; | 59 delete this; |
60 } | 60 } |
61 | 61 |
62 void ContextualSearchManager::StartSearchTermResolutionRequest( | 62 void ContextualSearchManager::StartSearchTermResolutionRequest( |
63 JNIEnv* env, | 63 JNIEnv* env, |
64 const JavaParamRef<jobject>& obj, | 64 const JavaParamRef<jobject>& obj, |
65 const JavaParamRef<jstring>& j_selection, | 65 const JavaParamRef<jstring>& j_selection, |
66 jboolean j_use_resolved_search_term, | 66 jboolean j_use_resolved_search_term, |
67 const JavaParamRef<jobject>& j_base_content_view_core, | 67 const JavaParamRef<jobject>& j_base_web_contents, |
68 jboolean j_may_send_base_page_url) { | 68 jboolean j_may_send_base_page_url) { |
69 ContentViewCore* base_content_view_core = | 69 WebContents* base_web_contents = |
70 ContentViewCore::GetNativeContentViewCore(env, j_base_content_view_core); | 70 WebContents::FromJavaWebContents(j_base_web_contents.obj()); |
71 DCHECK(base_content_view_core); | 71 DCHECK(base_web_contents); |
72 std::string selection( | 72 std::string selection( |
73 base::android::ConvertJavaStringToUTF8(env, j_selection)); | 73 base::android::ConvertJavaStringToUTF8(env, j_selection)); |
74 bool use_resolved_search_term = j_use_resolved_search_term; | 74 bool use_resolved_search_term = j_use_resolved_search_term; |
75 bool may_send_base_page_url = j_may_send_base_page_url; | 75 bool may_send_base_page_url = j_may_send_base_page_url; |
76 // Calls back to OnSearchTermResolutionResponse. | 76 // Calls back to OnSearchTermResolutionResponse. |
77 delegate_->StartSearchTermResolutionRequest( | 77 delegate_->StartSearchTermResolutionRequest( |
78 selection, use_resolved_search_term, base_content_view_core, | 78 selection, use_resolved_search_term, base_web_contents, |
79 may_send_base_page_url); | 79 may_send_base_page_url); |
80 } | 80 } |
81 | 81 |
82 void ContextualSearchManager::GatherSurroundingText( | 82 void ContextualSearchManager::GatherSurroundingText( |
83 JNIEnv* env, | 83 JNIEnv* env, |
84 const JavaParamRef<jobject>& obj, | 84 const JavaParamRef<jobject>& obj, |
85 const JavaParamRef<jstring>& j_selection, | 85 const JavaParamRef<jstring>& j_selection, |
86 jboolean j_use_resolved_search_term, | 86 jboolean j_use_resolved_search_term, |
87 const JavaParamRef<jobject>& j_base_content_view_core, | 87 const JavaParamRef<jobject>& j_base_web_contents, |
88 jboolean j_may_send_base_page_url) { | 88 jboolean j_may_send_base_page_url) { |
89 ContentViewCore* base_content_view_core = | 89 WebContents* base_web_contents = |
90 ContentViewCore::GetNativeContentViewCore(env, j_base_content_view_core); | 90 WebContents::FromJavaWebContents(j_base_web_contents.obj()); |
91 DCHECK(base_content_view_core); | 91 DCHECK(base_web_contents); |
92 std::string selection( | 92 std::string selection( |
93 base::android::ConvertJavaStringToUTF8(env, j_selection)); | 93 base::android::ConvertJavaStringToUTF8(env, j_selection)); |
94 bool use_resolved_search_term = j_use_resolved_search_term; | 94 bool use_resolved_search_term = j_use_resolved_search_term; |
95 bool may_send_base_page_url = j_may_send_base_page_url; | 95 bool may_send_base_page_url = j_may_send_base_page_url; |
96 delegate_->GatherAndSaveSurroundingText(selection, use_resolved_search_term, | 96 delegate_->GatherAndSaveSurroundingText(selection, use_resolved_search_term, |
97 base_content_view_core, | 97 base_web_contents, |
98 may_send_base_page_url); | 98 may_send_base_page_url); |
99 } | 99 } |
100 | 100 |
101 base::android::ScopedJavaLocalRef<jstring> | 101 base::android::ScopedJavaLocalRef<jstring> |
102 ContextualSearchManager::GetTargetLanguage(JNIEnv* env, | 102 ContextualSearchManager::GetTargetLanguage(JNIEnv* env, |
103 const JavaParamRef<jobject>& obj) { | 103 const JavaParamRef<jobject>& obj) { |
104 DCHECK(delegate_); | 104 DCHECK(delegate_); |
105 std::string target_language(delegate_->GetTargetLanguage()); | 105 std::string target_language(delegate_->GetTargetLanguage()); |
106 base::android::ScopedJavaLocalRef<jstring> j_target_language = | 106 base::android::ScopedJavaLocalRef<jstring> j_target_language = |
107 base::android::ConvertUTF8ToJavaString(env, target_language.c_str()); | 107 base::android::ConvertUTF8ToJavaString(env, target_language.c_str()); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 base::android::ScopedJavaLocalRef<jstring> j_surrounding_text = | 164 base::android::ScopedJavaLocalRef<jstring> j_surrounding_text = |
165 base::android::ConvertUTF16ToJavaString(env, surrounding_text.c_str()); | 165 base::android::ConvertUTF16ToJavaString(env, surrounding_text.c_str()); |
166 Java_ContextualSearchManager_onIcingSelectionAvailable( | 166 Java_ContextualSearchManager_onIcingSelectionAvailable( |
167 env, java_manager_.obj(), j_encoding.obj(), j_surrounding_text.obj(), | 167 env, java_manager_.obj(), j_encoding.obj(), j_surrounding_text.obj(), |
168 start_offset, end_offset); | 168 start_offset, end_offset); |
169 } | 169 } |
170 | 170 |
171 void ContextualSearchManager::EnableContextualSearchJsApiForOverlay( | 171 void ContextualSearchManager::EnableContextualSearchJsApiForOverlay( |
172 JNIEnv* env, | 172 JNIEnv* env, |
173 jobject obj, | 173 jobject obj, |
174 jobject j_overlay_content_view_core) { | 174 const JavaParamRef<jobject>& j_overlay_web_contents) { |
175 ContentViewCore* overlay_content_view_core = | 175 WebContents* overlay_web_contents = |
176 ContentViewCore::GetNativeContentViewCore(env, | 176 WebContents::FromJavaWebContents(j_overlay_web_contents.obj()); |
177 j_overlay_content_view_core); | 177 DCHECK(overlay_web_contents); |
178 // Tell our Overlay Notifier Service that this is currently a CS page. | 178 // Tell our Overlay Notifier Service that this is currently a CS page. |
179 content::RenderFrameHost* render_frame_host = | 179 content::RenderFrameHost* render_frame_host = |
180 overlay_content_view_core->GetWebContents() | 180 overlay_web_contents->GetRenderViewHost()->GetMainFrame(); |
181 ->GetRenderViewHost() | |
182 ->GetMainFrame(); | |
183 DCHECK(render_frame_host); | 181 DCHECK(render_frame_host); |
184 contextual_search::mojom::OverlayPageNotifierServicePtr page_notifier_service; | 182 contextual_search::mojom::OverlayPageNotifierServicePtr page_notifier_service; |
185 render_frame_host->GetRemoteInterfaces()->GetInterface( | 183 render_frame_host->GetRemoteInterfaces()->GetInterface( |
186 &page_notifier_service); | 184 &page_notifier_service); |
187 DCHECK(page_notifier_service); | 185 DCHECK(page_notifier_service); |
188 page_notifier_service->NotifyIsContextualSearchOverlay(); | 186 page_notifier_service->NotifyIsContextualSearchOverlay(); |
189 | 187 |
190 // Also set up the backchannel to call into this class from the JS API. | 188 // Also set up the backchannel to call into this class from the JS API. |
191 render_frame_host->GetInterfaceRegistry()->AddInterface( | 189 render_frame_host->GetInterfaceRegistry()->AddInterface( |
192 base::Bind(&contextual_search::CreateContextualSearchJsApiService, this)); | 190 base::Bind(&contextual_search::CreateContextualSearchJsApiService, this)); |
193 } | 191 } |
194 | 192 |
195 bool RegisterContextualSearchManager(JNIEnv* env) { | 193 bool RegisterContextualSearchManager(JNIEnv* env) { |
196 return RegisterNativesImpl(env); | 194 return RegisterNativesImpl(env); |
197 } | 195 } |
198 | 196 |
199 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 197 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
200 ContextualSearchManager* manager = new ContextualSearchManager(env, obj); | 198 ContextualSearchManager* manager = new ContextualSearchManager(env, obj); |
201 return reinterpret_cast<intptr_t>(manager); | 199 return reinterpret_cast<intptr_t>(manager); |
202 } | 200 } |
203 | 201 |
204 void ContextualSearchManager::SetCaption(std::string caption, | 202 void ContextualSearchManager::SetCaption(std::string caption, |
205 bool does_answer) { | 203 bool does_answer) { |
206 JNIEnv* env = base::android::AttachCurrentThread(); | 204 JNIEnv* env = base::android::AttachCurrentThread(); |
207 base::android::ScopedJavaLocalRef<jstring> j_caption = | 205 base::android::ScopedJavaLocalRef<jstring> j_caption = |
208 base::android::ConvertUTF8ToJavaString(env, caption.c_str()); | 206 base::android::ConvertUTF8ToJavaString(env, caption.c_str()); |
209 Java_ContextualSearchManager_onSetCaption(env, java_manager_.obj(), | 207 Java_ContextualSearchManager_onSetCaption(env, java_manager_.obj(), |
210 j_caption.obj(), does_answer); | 208 j_caption.obj(), does_answer); |
211 } | 209 } |
OLD | NEW |