Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Unified Diff: chrome/browser/android/contextualsearch/contextual_search_context.cc

Issue 2706333002: [TTS] Add a Java Context linked to existing native (Closed)
Patch Set: DCHECK that the context is created on the browser thread. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/android/contextualsearch/contextual_search_context.cc
diff --git a/chrome/browser/android/contextualsearch/contextual_search_context.cc b/chrome/browser/android/contextualsearch/contextual_search_context.cc
index e0db7ccd9fad109ebae3a8f91615029c4ed2dc0d..fe1ffe1a863d2f2ae00aaa5fad357c1db5f0c5e0 100644
--- a/chrome/browser/android/contextualsearch/contextual_search_context.cc
+++ b/chrome/browser/android/contextualsearch/contextual_search_context.cc
@@ -2,17 +2,93 @@
// 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_context.h"
-
-ContextualSearchContext::ContextualSearchContext(
- const std::string& selected_text,
- const std::string& home_country,
- const GURL& page_url,
- const std::string& encoding)
- : selected_text(selected_text),
- home_country(home_country),
- page_url(page_url),
- encoding(encoding) {}
+#include <chrome/browser/android/contextualsearch/contextual_search_context.h>
+
+#include "base/android/jni_string.h"
+#include "content/public/browser/browser_thread.h"
+
+#include "jni/ContextualSearchContext_jni.h"
+
+ContextualSearchContext::ContextualSearchContext(JNIEnv* env, jobject obj) {
+ java_object_.Reset(env, obj);
+}
+
+// Constructor for tests.
+ContextualSearchContext::ContextualSearchContext() {
+ java_object_ = nullptr;
+}
ContextualSearchContext::~ContextualSearchContext() {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ if (java_object_.obj() != nullptr)
+ Java_ContextualSearchContext_clearNativePointer(env, java_object_);
+}
+
+// static
+ContextualSearchContext*
+ContextualSearchContext::FromJavaContextualSearchContext(
+ const base::android::JavaRef<jobject>& j_contextual_search_context) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ if (j_contextual_search_context.is_null())
+ return NULL;
+
+ ContextualSearchContext* contextual_search_context =
+ reinterpret_cast<ContextualSearchContext*>(
+ Java_ContextualSearchContext_getNativePointer(
+ base::android::AttachCurrentThread(),
+ j_contextual_search_context));
+ return contextual_search_context;
+}
+
+void ContextualSearchContext::SetUseBriefPageContent(
+ JNIEnv* env,
+ jobject obj,
+ jboolean j_use_brief_page_content) {
+ is_brief_surrounding_text = j_use_brief_page_content;
+}
+
+void ContextualSearchContext::SetProperties(
+ JNIEnv* env,
+ jobject obj,
+ const base::android::JavaParamRef<jstring>& j_selection,
+ const base::android::JavaParamRef<jstring>& j_home_country,
+ jboolean j_may_send_base_page_url) {
+ SetPropertiesInternal(
+ base::android::ConvertJavaStringToUTF8(env, j_selection),
+ base::android::ConvertJavaStringToUTF8(env, j_home_country),
+ j_may_send_base_page_url);
+}
+
+void ContextualSearchContext::SetPropertiesInternal(
+ std::string selection,
+ std::string home_country,
+ bool may_send_base_page_url) {
+ selected_text = selection;
+ this->home_country = home_country;
+ this->may_send_base_page_url = may_send_base_page_url;
+}
+
+bool ContextualSearchContext::IsBrief() {
+ return is_brief_surrounding_text;
+}
+
+bool ContextualSearchContext::MaySendBasePageUrl() {
+ return may_send_base_page_url;
+}
+
+// Java wrapper boilerplate
+
+void ContextualSearchContext::Destroy(
+ JNIEnv* env,
+ const base::android::JavaParamRef<jobject>& obj) {
+ delete this;
+}
+
+bool RegisterContextualSearchContext(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+jlong Init(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj) {
+ ContextualSearchContext* context = new ContextualSearchContext(env, obj);
+ return reinterpret_cast<intptr_t>(context);
}

Powered by Google App Engine
This is Rietveld 408576698