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 #include "base/android/jni_string.h" | 7 #include "base/android/jni_string.h" |
8 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
9 | 9 |
10 #include "jni/ContextualSearchContext_jni.h" | 10 #include "jni/ContextualSearchContext_jni.h" |
11 | 11 |
12 ContextualSearchContext::ContextualSearchContext(JNIEnv* env, jobject obj) | 12 ContextualSearchContext::ContextualSearchContext(JNIEnv* env, jobject obj) |
13 : can_resolve(false), | 13 : can_resolve(false), |
14 can_send_base_page_url(false), | 14 can_send_base_page_url(false), |
15 selected_text(std::string()), | |
16 home_country(std::string()), | 15 home_country(std::string()), |
17 base_page_url(GURL()), | 16 base_page_url(GURL()), |
18 surrounding_text(base::string16()), | 17 surrounding_text(base::string16()), |
19 start_offset(0), | 18 start_offset(0), |
20 end_offset(0), | 19 end_offset(0), |
21 weak_factory_(this) { | 20 weak_factory_(this) { |
22 java_object_.Reset(env, obj); | 21 java_object_.Reset(env, obj); |
23 } | 22 } |
24 | 23 |
25 ContextualSearchContext::ContextualSearchContext( | 24 ContextualSearchContext::ContextualSearchContext( |
26 const std::string& selected_text, | |
27 const std::string& home_country, | 25 const std::string& home_country, |
28 const GURL& page_url, | 26 const GURL& page_url, |
29 const std::string& encoding) | 27 const std::string& encoding) |
30 : selected_text(selected_text), | 28 : home_country(home_country), |
31 home_country(home_country), | |
32 base_page_url(page_url), | 29 base_page_url(page_url), |
33 base_page_encoding(encoding), | 30 base_page_encoding(encoding), |
34 weak_factory_(this) { | 31 weak_factory_(this) { |
35 java_object_ = nullptr; | 32 java_object_ = nullptr; |
36 } | 33 } |
37 | 34 |
38 ContextualSearchContext::~ContextualSearchContext() { | 35 ContextualSearchContext::~ContextualSearchContext() { |
39 } | 36 } |
40 | 37 |
41 // static | 38 // static |
42 base::WeakPtr<ContextualSearchContext> | 39 base::WeakPtr<ContextualSearchContext> |
43 ContextualSearchContext::FromJavaContextualSearchContext( | 40 ContextualSearchContext::FromJavaContextualSearchContext( |
44 const base::android::JavaRef<jobject>& j_contextual_search_context) { | 41 const base::android::JavaRef<jobject>& j_contextual_search_context) { |
45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 42 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
46 if (j_contextual_search_context.is_null()) | 43 if (j_contextual_search_context.is_null()) |
47 return NULL; | 44 return NULL; |
48 | 45 |
49 ContextualSearchContext* contextual_search_context = | 46 ContextualSearchContext* contextual_search_context = |
50 reinterpret_cast<ContextualSearchContext*>( | 47 reinterpret_cast<ContextualSearchContext*>( |
51 Java_ContextualSearchContext_getNativePointer( | 48 Java_ContextualSearchContext_getNativePointer( |
52 base::android::AttachCurrentThread(), | 49 base::android::AttachCurrentThread(), |
53 j_contextual_search_context)); | 50 j_contextual_search_context)); |
54 return contextual_search_context->GetWeakPtr(); | 51 return contextual_search_context->GetWeakPtr(); |
55 } | 52 } |
56 | 53 |
57 void ContextualSearchContext::SetResolveProperties( | 54 void ContextualSearchContext::SetResolveProperties( |
58 JNIEnv* env, | 55 JNIEnv* env, |
59 jobject obj, | 56 jobject obj, |
60 const base::android::JavaParamRef<jstring>& j_selection, | |
61 const base::android::JavaParamRef<jstring>& j_home_country, | 57 const base::android::JavaParamRef<jstring>& j_home_country, |
62 jboolean j_may_send_base_page_url) { | 58 jboolean j_may_send_base_page_url) { |
63 can_resolve = true; | 59 can_resolve = true; |
64 selected_text = base::android::ConvertJavaStringToUTF8(env, j_selection); | |
65 home_country = base::android::ConvertJavaStringToUTF8(env, j_home_country); | 60 home_country = base::android::ConvertJavaStringToUTF8(env, j_home_country); |
66 can_send_base_page_url = j_may_send_base_page_url; | 61 can_send_base_page_url = j_may_send_base_page_url; |
67 } | 62 } |
68 | 63 |
| 64 void ContextualSearchContext::AdjustSelection(JNIEnv* env, |
| 65 jobject obj, |
| 66 jint j_start_adjust, |
| 67 jint j_end_adjust) { |
| 68 DCHECK(start_offset + j_start_adjust >= 0); |
| 69 DCHECK(start_offset + j_start_adjust <= (int)surrounding_text.length()); |
| 70 DCHECK(end_offset + j_end_adjust >= 0); |
| 71 DCHECK(end_offset + j_end_adjust <= (int)surrounding_text.length()); |
| 72 start_offset += j_start_adjust; |
| 73 end_offset += j_end_adjust; |
| 74 } |
| 75 |
69 // Accessors | 76 // Accessors |
70 | 77 |
71 bool ContextualSearchContext::CanResolve() const { | 78 bool ContextualSearchContext::CanResolve() const { |
72 return can_resolve; | 79 return can_resolve; |
73 } | 80 } |
74 | 81 |
75 bool ContextualSearchContext::CanSendBasePageUrl() const { | 82 bool ContextualSearchContext::CanSendBasePageUrl() const { |
76 return can_send_base_page_url; | 83 return can_send_base_page_url; |
77 } | 84 } |
78 | 85 |
(...skipping 20 matching lines...) Expand all Loading... |
99 | 106 |
100 void ContextualSearchContext::SetSelectionSurroundings( | 107 void ContextualSearchContext::SetSelectionSurroundings( |
101 int start_offset, | 108 int start_offset, |
102 int end_offset, | 109 int end_offset, |
103 const base::string16& surrounding_text) { | 110 const base::string16& surrounding_text) { |
104 this->start_offset = start_offset; | 111 this->start_offset = start_offset; |
105 this->end_offset = end_offset; | 112 this->end_offset = end_offset; |
106 this->surrounding_text = surrounding_text; | 113 this->surrounding_text = surrounding_text; |
107 } | 114 } |
108 | 115 |
109 const std::string ContextualSearchContext::GetOriginalSelectedText() const { | |
110 return selected_text; | |
111 } | |
112 | |
113 const base::string16 ContextualSearchContext::GetSurroundingText() const { | 116 const base::string16 ContextualSearchContext::GetSurroundingText() const { |
114 return surrounding_text; | 117 return surrounding_text; |
115 } | 118 } |
116 | 119 |
117 int ContextualSearchContext::GetStartOffset() const { | 120 int ContextualSearchContext::GetStartOffset() const { |
118 return start_offset; | 121 return start_offset; |
119 } | 122 } |
120 | 123 |
121 int ContextualSearchContext::GetEndOffset() const { | 124 int ContextualSearchContext::GetEndOffset() const { |
122 return end_offset; | 125 return end_offset; |
(...skipping 12 matching lines...) Expand all Loading... |
135 } | 138 } |
136 | 139 |
137 bool RegisterContextualSearchContext(JNIEnv* env) { | 140 bool RegisterContextualSearchContext(JNIEnv* env) { |
138 return RegisterNativesImpl(env); | 141 return RegisterNativesImpl(env); |
139 } | 142 } |
140 | 143 |
141 jlong Init(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj) { | 144 jlong Init(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj) { |
142 ContextualSearchContext* context = new ContextualSearchContext(env, obj); | 145 ContextualSearchContext* context = new ContextualSearchContext(env, obj); |
143 return reinterpret_cast<intptr_t>(context); | 146 return reinterpret_cast<intptr_t>(context); |
144 } | 147 } |
OLD | NEW |