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