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_context.h" | 5 #include <chrome/browser/android/contextualsearch/contextual_search_context.h> |
6 | 6 |
7 ContextualSearchContext::ContextualSearchContext( | 7 #include "base/android/jni_string.h" |
8 const std::string& selected_text, | 8 #include "content/public/browser/browser_thread.h" |
9 const std::string& home_country, | 9 |
10 const GURL& page_url, | 10 #include "jni/ContextualSearchContext_jni.h" |
11 const std::string& encoding) | 11 |
12 : selected_text(selected_text), | 12 ContextualSearchContext::ContextualSearchContext(JNIEnv* env, jobject obj) { |
13 home_country(home_country), | 13 java_object_.Reset(env, obj); |
14 page_url(page_url), | 14 } |
15 encoding(encoding) {} | 15 |
| 16 // Constructor for tests. |
| 17 ContextualSearchContext::ContextualSearchContext() { |
| 18 java_object_ = nullptr; |
| 19 } |
16 | 20 |
17 ContextualSearchContext::~ContextualSearchContext() { | 21 ContextualSearchContext::~ContextualSearchContext() { |
| 22 JNIEnv* env = base::android::AttachCurrentThread(); |
| 23 if (java_object_.obj() != nullptr) |
| 24 Java_ContextualSearchContext_clearNativePointer(env, java_object_); |
18 } | 25 } |
| 26 |
| 27 // static |
| 28 ContextualSearchContext* |
| 29 ContextualSearchContext::FromJavaContextualSearchContext( |
| 30 const base::android::JavaRef<jobject>& j_contextual_search_context) { |
| 31 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 32 if (j_contextual_search_context.is_null()) |
| 33 return NULL; |
| 34 |
| 35 ContextualSearchContext* contextual_search_context = |
| 36 reinterpret_cast<ContextualSearchContext*>( |
| 37 Java_ContextualSearchContext_getNativePointer( |
| 38 base::android::AttachCurrentThread(), |
| 39 j_contextual_search_context)); |
| 40 return contextual_search_context; |
| 41 } |
| 42 |
| 43 void ContextualSearchContext::SetUseBriefPageContent( |
| 44 JNIEnv* env, |
| 45 jobject obj, |
| 46 jboolean j_use_brief_page_content) { |
| 47 is_brief_surrounding_text = j_use_brief_page_content; |
| 48 } |
| 49 |
| 50 void ContextualSearchContext::SetProperties( |
| 51 JNIEnv* env, |
| 52 jobject obj, |
| 53 const base::android::JavaParamRef<jstring>& j_selection, |
| 54 const base::android::JavaParamRef<jstring>& j_home_country, |
| 55 jboolean j_may_send_base_page_url) { |
| 56 SetPropertiesInternal( |
| 57 base::android::ConvertJavaStringToUTF8(env, j_selection), |
| 58 base::android::ConvertJavaStringToUTF8(env, j_home_country), |
| 59 j_may_send_base_page_url); |
| 60 } |
| 61 |
| 62 void ContextualSearchContext::SetPropertiesInternal( |
| 63 std::string selection, |
| 64 std::string home_country, |
| 65 bool may_send_base_page_url) { |
| 66 selected_text = selection; |
| 67 this->home_country = home_country; |
| 68 this->may_send_base_page_url = may_send_base_page_url; |
| 69 } |
| 70 |
| 71 bool ContextualSearchContext::IsBrief() { |
| 72 return is_brief_surrounding_text; |
| 73 } |
| 74 |
| 75 bool ContextualSearchContext::MaySendBasePageUrl() { |
| 76 return may_send_base_page_url; |
| 77 } |
| 78 |
| 79 // Java wrapper boilerplate |
| 80 |
| 81 void ContextualSearchContext::Destroy( |
| 82 JNIEnv* env, |
| 83 const base::android::JavaParamRef<jobject>& obj) { |
| 84 delete this; |
| 85 } |
| 86 |
| 87 bool RegisterContextualSearchContext(JNIEnv* env) { |
| 88 return RegisterNativesImpl(env); |
| 89 } |
| 90 |
| 91 jlong Init(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj) { |
| 92 ContextualSearchContext* context = new ContextualSearchContext(env, obj); |
| 93 return reinterpret_cast<intptr_t>(context); |
| 94 } |
OLD | NEW |