OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_SEARCH_ACTION_H_ |
| 6 #define CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_SEARCH_ACTION_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "base/android/jni_android.h" |
| 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/strings/string_util.h" |
| 14 |
| 15 struct ContextualSearchContext; |
| 16 |
| 17 // Represents the native side of a Java Search Action, which knows how to do a |
| 18 // Contextual Search. |
| 19 // This is part of the 2016-refactoring (crbug.com/624609, |
| 20 // go/cs-refactoring-2016). |
| 21 // Gathers text surrounding the selection from the page and makes it accessible |
| 22 // to Java. |
| 23 // TODO(donnd): add capability to "resolve" the best search term for the section |
| 24 // of the page based on a server request or local text analysis. |
| 25 class SearchAction : public base::SupportsWeakPtr<SearchAction> { |
| 26 public: |
| 27 // Constructs a new Search Action linked to the given Java object. |
| 28 SearchAction(JNIEnv* env, jobject obj); |
| 29 virtual ~SearchAction(); |
| 30 |
| 31 // Should be called when this native object is no longer needed (calls the |
| 32 // destructor). |
| 33 void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj); |
| 34 |
| 35 // Requests the text surrounding the selection for the given WebContents Java |
| 36 // object. The surrounding text will be made available through a call to |
| 37 // |OnSurroundingTextResponse|. |
| 38 void RequestSurroundingText( |
| 39 JNIEnv* env, |
| 40 const base::android::JavaParamRef<jobject>& obj, |
| 41 const base::android::JavaParamRef<jobject>& j_web_contents); |
| 42 |
| 43 // Finds and returns the focused word within the current surrounding text. |
| 44 // RequestSurroundingText must be called first and the associated response |
| 45 // received. |
| 46 std::string FindFocusedWord(); |
| 47 |
| 48 // Returns a sample of the current surrounding text of the given |
| 49 // |sample_length| or shorter if insufficient sample text is available. |
| 50 // RequestSurroundingText must be called first and the associated response |
| 51 // received. |
| 52 std::string GetSampleText(int sample_length); |
| 53 |
| 54 private: |
| 55 friend class SearchActionTest; |
| 56 FRIEND_TEST_ALL_PREFIXES(SearchActionTest, IsValidCharacterTest); |
| 57 FRIEND_TEST_ALL_PREFIXES(SearchActionTest, FindFocusedWordTest); |
| 58 FRIEND_TEST_ALL_PREFIXES(SearchActionTest, SampleSurroundingsTest); |
| 59 |
| 60 // Analyzes the surrounding text and makes it available to the Java |
| 61 // SearchAction object in a call to SearchAction#onSurroundingTextResponse. |
| 62 void OnSurroundingTextResponse(const base::string16& surrounding_text, |
| 63 int focus_start, |
| 64 int focus_end); |
| 65 |
| 66 // Determines if the given character is a valid part of a word to search for. |
| 67 bool IsValidCharacter(int char_code); |
| 68 |
| 69 // Testing support |
| 70 SearchAction(); |
| 71 void SetContext(std::string surrounding_text, int focus_start, int focus_end); |
| 72 |
| 73 // The linked Java object. |
| 74 base::android::ScopedJavaGlobalRef<jobject> java_object_; |
| 75 |
| 76 // The current search context, or null. |
| 77 std::shared_ptr<ContextualSearchContext> search_context_; |
| 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(SearchAction); |
| 80 }; |
| 81 |
| 82 bool RegisterSearchAction(JNIEnv* env); |
| 83 |
| 84 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_SEARCH_ACTION_H_ |
OLD | NEW |