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 #ifndef CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_CONTEXT_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_CONTEXT_H_ |
6 #define CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_CONTEXT_H_ | 6 #define CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_CONTEXT_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
| 10 #include "base/android/jni_android.h" |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
11 #include "url/gurl.h" | 12 #include "url/gurl.h" |
12 | 13 |
13 // Encapsulates key parts of a Contextual Search Context, including surrounding | 14 // Encapsulates key parts of a Contextual Search Context, including surrounding |
14 // text. | 15 // text. |
15 struct ContextualSearchContext { | 16 struct ContextualSearchContext { |
16 public: | 17 public: |
| 18 ContextualSearchContext(JNIEnv* env, jobject obj); |
| 19 // Constructor for tests. |
17 ContextualSearchContext(const std::string& selected_text, | 20 ContextualSearchContext(const std::string& selected_text, |
18 const std::string& home_country, | 21 const std::string& home_country, |
19 const GURL& page_url, | 22 const GURL& page_url, |
20 const std::string& encoding); | 23 const std::string& encoding); |
21 ~ContextualSearchContext(); | 24 ~ContextualSearchContext(); |
22 | 25 |
23 const std::string selected_text; | 26 // Calls the destructor. Should be called when this native object is no |
24 const std::string home_country; | 27 // longer needed. |
25 const GURL page_url; | 28 void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj); |
26 const std::string encoding; | |
27 | 29 |
| 30 // Returns the native |ContextualSearchContext| given the Java object. |
| 31 static ContextualSearchContext* FromJavaContextualSearchContext( |
| 32 const base::android::JavaRef<jobject>& j_contextual_search_context); |
| 33 |
| 34 // Returns whether this context can be resolved. |
| 35 // The context can be resolved only after calling SetResolveProperites. |
| 36 bool CanResolve() const; |
| 37 |
| 38 // Returns whether the base page URL may be sent (according to the Java |
| 39 // policy). |
| 40 bool CanSendBasePageUrl() const; |
| 41 |
| 42 // Sets the properties needed to resolve a context. |
| 43 void SetResolveProperties( |
| 44 JNIEnv* env, |
| 45 jobject obj, |
| 46 const base::android::JavaParamRef<jstring>& j_selection, |
| 47 const base::android::JavaParamRef<jstring>& j_home_country, |
| 48 jboolean j_may_send_base_page_url); |
| 49 |
| 50 // Gets the URL of the base page. |
| 51 const GURL GetBasePageUrl() const; |
| 52 // Sets the URL of the base page. |
| 53 void SetBasePageUrl(const GURL& base_page_url); |
| 54 |
| 55 // Gets the encoding of the base page. This is not very important, since |
| 56 // the surrounding text stored here in a base::string16 is implicitly encoded |
| 57 // in UTF-16 (see http://www.chromium.org/developers/chromium-string-usage). |
| 58 const std::string GetBasePageEncoding() const; |
| 59 void SetBasePageEncoding(const std::string& base_page_encoding); |
| 60 |
| 61 // Gets the country code of the home country of the user, or an empty string. |
| 62 const std::string GetHomeCountry() const; |
| 63 |
| 64 // Sets the selection and surroundings. |
| 65 void SetSelectionSurroundings(int start_offset, |
| 66 int end_offset, |
| 67 const base::string16& surrounding_text); |
| 68 |
| 69 // Gets the original selection. |
| 70 const std::string GetOriginalSelectedText() const; |
| 71 |
| 72 // Gets the text surrounding the selection (including the selection). |
| 73 const base::string16 GetSurroundingText() const; |
| 74 |
| 75 // Gets the start offset of the selection within the surrounding text (in |
| 76 // characters). |
| 77 int GetStartOffset() const; |
| 78 // Gets the end offset of the selection within the surrounding text (in |
| 79 // characters). |
| 80 int GetEndOffset() const; |
| 81 |
| 82 private: |
| 83 bool can_resolve; |
| 84 bool can_send_base_page_url; |
| 85 |
| 86 std::string selected_text; |
| 87 std::string home_country; |
| 88 GURL base_page_url; |
| 89 std::string base_page_encoding; |
28 base::string16 surrounding_text; | 90 base::string16 surrounding_text; |
29 int start_offset; | 91 int start_offset; |
30 int end_offset; | 92 int end_offset; |
31 | 93 |
| 94 // The linked Java object. |
| 95 base::android::ScopedJavaGlobalRef<jobject> java_object_; |
| 96 |
32 DISALLOW_COPY_AND_ASSIGN(ContextualSearchContext); | 97 DISALLOW_COPY_AND_ASSIGN(ContextualSearchContext); |
33 }; | 98 }; |
34 | 99 |
| 100 bool RegisterContextualSearchContext(JNIEnv* env); |
| 101 |
35 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_CONTEXT_H_ | 102 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_CONTEXT_H_ |
OLD | NEW |