1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/contextualsearch/contextual_search_manager.h"
#include <memory>
#include <set>
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/supports_user_data.h"
#include "base/time/time.h"
#include "chrome/browser/android/contextualsearch/native_contextual_search_context.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "components/contextual_search/core/browser/contextual_search_delegate_impl.h"
#include "components/contextual_search/core/browser/resolved_search_term.h"
#include "components/history/core/browser/history_service.h"
#include "components/navigation_interception/intercept_navigation_delegate.h"
#include "components/search_engines/search_terms_data.h"
#include "components/search_engines/template_url_service.h"
#include "components/variations/variations_associated_data.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/android/chrome_jni_headers/ContextualSearchManager_jni.h"
#include "chrome/android/chrome_jni_headers/ContextualSearchPolicy_jni.h"
using base::android::JavaParamRef;
using base::android::JavaRef;
using content::WebContents;
namespace {
const int kHistoryDeletionWindowSeconds = 2;
} // namespace
// This class manages the native behavior of the Contextual Search feature.
// Instances of this class are owned by the Java ContextualSearchManager.
// Most of the work is actually done in an associated delegate to this class:
// the ContextualSearchDelegate.
ContextualSearchManager::ContextualSearchManager(JNIEnv* env,
const JavaRef<jobject>& obj,
Profile* profile)
: profile_(profile) {
java_manager_.Reset(obj);
Java_ContextualSearchManager_setNativeManager(
env, obj, reinterpret_cast<intptr_t>(this));
delegate_ = std::make_unique<ContextualSearchDelegateImpl>(
profile->GetURLLoaderFactory(),
TemplateURLServiceFactory::GetForProfile(profile));
}
ContextualSearchManager::~ContextualSearchManager() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_ContextualSearchManager_clearNativeManager(env, java_manager_);
}
void ContextualSearchManager::Destroy(JNIEnv* env,
const JavaParamRef<jobject>& obj) {
delete this;
}
void ContextualSearchManager::StartSearchTermResolutionRequest(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jobject>& j_contextual_search_context,
const JavaParamRef<jobject>& j_base_web_contents) {
WebContents* base_web_contents =
WebContents::FromJavaWebContents(j_base_web_contents);
DCHECK(base_web_contents);
base::WeakPtr<NativeContextualSearchContext> contextual_search_context =
NativeContextualSearchContext::FromJavaContextualSearchContext(
j_contextual_search_context);
// Calls back to OnSearchTermResolutionResponse.
delegate_->StartSearchTermResolutionRequest(
contextual_search_context, base_web_contents,
base::BindRepeating(
&ContextualSearchManager::OnSearchTermResolutionResponse,
base::Unretained(this)));
}
void ContextualSearchManager::GatherSurroundingText(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jobject>& j_contextual_search_context,
const JavaParamRef<jobject>& j_base_web_contents) {
WebContents* base_web_contents =
WebContents::FromJavaWebContents(j_base_web_contents);
DCHECK(base_web_contents);
base::WeakPtr<NativeContextualSearchContext> contextual_search_context =
NativeContextualSearchContext::FromJavaContextualSearchContext(
j_contextual_search_context);
delegate_->GatherAndSaveSurroundingText(
contextual_search_context, base_web_contents,
base::BindRepeating(
&ContextualSearchManager::OnTextSurroundingSelectionAvailable,
base::Unretained(this)));
}
void ContextualSearchManager::RemoveLastHistoryEntry(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
std::string& search_url,
jlong search_start_time_ms) {
// The deletion window is from the time a search URL was put in history, up
// to a short amount of time later.
base::Time begin_time =
base::Time::FromMillisecondsSinceUnixEpoch(search_start_time_ms);
base::Time end_time =
begin_time + base::Seconds(kHistoryDeletionWindowSeconds);
history::HistoryService* service = HistoryServiceFactory::GetForProfile(
profile_, ServiceAccessType::EXPLICIT_ACCESS);
if (service) {
// NOTE(mathp): We are only removing |search_url| from the local history
// because search results that are not promoted to a Tab do not make it to
// the web history, only local.
std::set<GURL> restrict_set;
restrict_set.insert(GURL(search_url));
service->ExpireHistoryBetween(
restrict_set, history::kNoAppIdFilter, begin_time, end_time,
/*user_initiated*/ false, base::DoNothing(), &history_task_tracker_);
}
}
void ContextualSearchManager::OnSearchTermResolutionResponse(
const ResolvedSearchTerm& resolved_search_term) {
// Notify the Java UX of the result.
JNIEnv* env = base::android::AttachCurrentThread();
Java_ContextualSearchManager_onSearchTermResolutionResponse(
env, java_manager_, resolved_search_term.is_invalid,
resolved_search_term.response_code, resolved_search_term.search_term,
resolved_search_term.display_text, resolved_search_term.alternate_term,
resolved_search_term.mid, resolved_search_term.prevent_preload,
resolved_search_term.selection_start_adjust,
resolved_search_term.selection_end_adjust,
resolved_search_term.context_language, resolved_search_term.thumbnail_url,
resolved_search_term.caption, resolved_search_term.quick_action_uri,
resolved_search_term.quick_action_category,
resolved_search_term.search_url_full,
resolved_search_term.search_url_preload,
resolved_search_term.coca_card_tag,
resolved_search_term.related_searches_json);
}
void ContextualSearchManager::OnTextSurroundingSelectionAvailable(
const std::string& encoding,
const std::u16string& surrounding_text,
size_t start_offset,
size_t end_offset) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_ContextualSearchManager_onTextSurroundingSelectionAvailable(
env, java_manager_, encoding, surrounding_text, start_offset, end_offset);
}
jlong JNI_ContextualSearchManager_Init(JNIEnv* env,
const JavaParamRef<jobject>& obj,
Profile* profile) {
ContextualSearchManager* manager =
new ContextualSearchManager(env, obj, profile);
return reinterpret_cast<intptr_t>(manager);
}
jboolean JNI_ContextualSearchPolicy_IsContextualSearchResolutionUrlValid(
JNIEnv* env,
Profile* profile) {
// Attempt to resolve a (empty) query. Return whether resulting URL is
// navigable.
auto* template_url_service =
TemplateURLServiceFactory::GetForProfile(profile);
if (!template_url_service) {
return false;
}
auto* template_url = template_url_service->GetDefaultSearchProvider();
if (!template_url) {
return false;
}
auto contextual_search_url_ref = template_url->contextual_search_url_ref();
GURL url(contextual_search_url_ref.ReplaceSearchTerms(
{}, template_url_service->search_terms_data(), NULL));
return url.is_valid();
}
|