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
3 // found in the LICENSE file.
5 #include "chrome/browser/android/contextualsearch/contextual_search_manager.h"
9 #include "base/android/jni_string.h"
10 #include "base/callback.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/android/contextualsearch/contextual_search_delegate.h"
14 #include "chrome/browser/android/tab_android.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/search_engines/template_url_service_factory.h"
17 #include "chrome/browser/ui/android/window_android_helper.h"
18 #include "components/navigation_interception/intercept_navigation_delegate.h"
19 #include "components/variations/variations_associated_data.h"
20 #include "content/public/browser/android/content_view_core.h"
21 #include "jni/ContextualSearchManager_jni.h"
22 #include "net/url_request/url_fetcher_impl.h"
24 using content::ContentViewCore
;
26 // This class manages the native behavior of the Contextual Search feature.
27 // Instances of this class are owned by the Java ContextualSearchManager.
28 // Most of the work is actually done in an associated delegate to this class:
29 // the ContextualSearchDelegate.
30 ContextualSearchManager::ContextualSearchManager(JNIEnv
* env
, jobject obj
) {
31 java_manager_
.Reset(env
, obj
);
32 Java_ContextualSearchManager_setNativeManager(
33 env
, obj
, reinterpret_cast<intptr_t>(this));
34 Profile
* profile
= ProfileManager::GetActiveUserProfile();
35 delegate_
.reset(new ContextualSearchDelegate(
36 profile
->GetRequestContext(),
37 TemplateURLServiceFactory::GetForProfile(profile
),
38 base::Bind(&ContextualSearchManager::OnSearchTermResolutionResponse
,
39 base::Unretained(this)),
40 base::Bind(&ContextualSearchManager::OnSurroundingTextAvailable
,
41 base::Unretained(this)),
42 base::Bind(&ContextualSearchManager::OnIcingSelectionAvailable
,
43 base::Unretained(this))));
46 ContextualSearchManager::~ContextualSearchManager() {
47 JNIEnv
* env
= base::android::AttachCurrentThread();
48 Java_ContextualSearchManager_clearNativeManager(env
, java_manager_
.obj());
51 void ContextualSearchManager::Destroy(JNIEnv
* env
, jobject obj
) { delete this; }
53 void ContextualSearchManager::StartSearchTermResolutionRequest(
57 jboolean j_use_resolved_search_term
,
58 jobject j_base_content_view_core
,
59 jboolean j_may_send_base_page_url
) {
60 ContentViewCore
* base_content_view_core
=
61 ContentViewCore::GetNativeContentViewCore(env
, j_base_content_view_core
);
62 DCHECK(base_content_view_core
);
63 std::string
selection(
64 base::android::ConvertJavaStringToUTF8(env
, j_selection
));
65 bool use_resolved_search_term
= j_use_resolved_search_term
;
66 bool may_send_base_page_url
= j_may_send_base_page_url
;
67 // Calls back to OnSearchTermResolutionResponse.
68 delegate_
->StartSearchTermResolutionRequest(
69 selection
, use_resolved_search_term
, base_content_view_core
,
70 may_send_base_page_url
);
73 void ContextualSearchManager::GatherSurroundingText(
77 jboolean j_use_resolved_search_term
,
78 jobject j_base_content_view_core
,
79 jboolean j_may_send_base_page_url
) {
80 ContentViewCore
* base_content_view_core
=
81 ContentViewCore::GetNativeContentViewCore(env
, j_base_content_view_core
);
82 DCHECK(base_content_view_core
);
83 std::string
selection(
84 base::android::ConvertJavaStringToUTF8(env
, j_selection
));
85 bool use_resolved_search_term
= j_use_resolved_search_term
;
86 bool may_send_base_page_url
= j_may_send_base_page_url
;
87 delegate_
->GatherAndSaveSurroundingText(selection
, use_resolved_search_term
,
88 base_content_view_core
,
89 may_send_base_page_url
);
92 void ContextualSearchManager::OnSearchTermResolutionResponse(
95 const std::string
& search_term
,
96 const std::string
& display_text
,
97 const std::string
& alternate_term
,
99 int selection_start_adjust
,
100 int selection_end_adjust
) {
101 // Notify the Java UX of the result.
102 JNIEnv
* env
= base::android::AttachCurrentThread();
103 base::android::ScopedJavaLocalRef
<jstring
> j_search_term
=
104 base::android::ConvertUTF8ToJavaString(env
, search_term
.c_str());
105 base::android::ScopedJavaLocalRef
<jstring
> j_display_text
=
106 base::android::ConvertUTF8ToJavaString(env
, display_text
.c_str());
107 base::android::ScopedJavaLocalRef
<jstring
> j_alternate_term
=
108 base::android::ConvertUTF8ToJavaString(env
, alternate_term
.c_str());
109 Java_ContextualSearchManager_onSearchTermResolutionResponse(
110 env
, java_manager_
.obj(), is_invalid
, response_code
, j_search_term
.obj(),
111 j_display_text
.obj(), j_alternate_term
.obj(), prevent_preload
,
112 selection_start_adjust
, selection_end_adjust
);
115 void ContextualSearchManager::OnSurroundingTextAvailable(
116 const std::string
& before_text
,
117 const std::string
& after_text
) {
118 JNIEnv
* env
= base::android::AttachCurrentThread();
119 base::android::ScopedJavaLocalRef
<jstring
> j_before_text
=
120 base::android::ConvertUTF8ToJavaString(env
, before_text
.c_str());
121 base::android::ScopedJavaLocalRef
<jstring
> j_after_text
=
122 base::android::ConvertUTF8ToJavaString(env
, after_text
.c_str());
123 Java_ContextualSearchManager_onSurroundingTextAvailable(
130 void ContextualSearchManager::OnIcingSelectionAvailable(
131 const std::string
& encoding
,
132 const base::string16
& surrounding_text
,
135 JNIEnv
* env
= base::android::AttachCurrentThread();
136 base::android::ScopedJavaLocalRef
<jstring
> j_encoding
=
137 base::android::ConvertUTF8ToJavaString(env
, encoding
.c_str());
138 base::android::ScopedJavaLocalRef
<jstring
> j_surrounding_text
=
139 base::android::ConvertUTF16ToJavaString(env
, surrounding_text
.c_str());
140 Java_ContextualSearchManager_onIcingSelectionAvailable(
141 env
, java_manager_
.obj(), j_encoding
.obj(), j_surrounding_text
.obj(),
142 start_offset
, end_offset
);
145 bool RegisterContextualSearchManager(JNIEnv
* env
) {
146 return RegisterNativesImpl(env
);
149 jlong
Init(JNIEnv
* env
, const JavaParamRef
<jobject
>& obj
) {
150 ContextualSearchManager
* manager
= new ContextualSearchManager(env
, obj
);
151 return reinterpret_cast<intptr_t>(manager
);