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, see go/cs-refactoring-2016. |
| 20 // Gathers text surrounding the selection from the page and makes it accessible |
| 21 // to Java. |
| 22 // TODO(donnd): add capability to "resolve" the best search term for the section |
| 23 // of the page based on a server request or local text analysis. |
| 24 class SearchAction : public base::SupportsWeakPtr<SearchAction> { |
| 25 public: |
| 26 // Constructs a new Search Action linked to the given Java object. |
| 27 SearchAction(JNIEnv* env, jobject obj); |
| 28 virtual ~SearchAction(); |
| 29 |
| 30 // Should be called when this native object is no longer needed (calls the |
| 31 // destructor). |
| 32 void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj); |
| 33 |
| 34 // Requests the text surrounding the selection for the given WebContents Java |
| 35 // object. The surrounding text will be made available through a call to |
| 36 // |OnSurroundingTextResponse|. |
| 37 void RequestSurroundingText( |
| 38 JNIEnv* env, |
| 39 const base::android::JavaParamRef<jobject>& obj, |
| 40 const base::android::JavaParamRef<jobject>& j_web_contents); |
| 41 |
| 42 private: |
| 43 FRIEND_TEST_ALL_PREFIXES(SearchActionTest, IsValidCharacterTest); |
| 44 FRIEND_TEST_ALL_PREFIXES(SearchActionTest, FindFocusedWordTest); |
| 45 FRIEND_TEST_ALL_PREFIXES(SearchActionTest, SampleSurroundingsTest); |
| 46 |
| 47 // Analyzes the surrounding text and makes it available to the Java |
| 48 // SearchAction object in a call to SearchAction#onSurroundingTextResponse. |
| 49 void OnSurroundingTextResponse(const base::string16& surrounding_text, |
| 50 int focus_start, |
| 51 int focus_end); |
| 52 |
| 53 // Expands the given focus to find the start and end word boundary. |
| 54 // Returns a pair whose first member is the start offset and second member |
| 55 // is the end offset of the word. If the focus is not within a word then |
| 56 // the focus inputs are returned. |
| 57 static std::pair<int, int> FindFocusedWord( |
| 58 const base::string16& surrounding_text, |
| 59 int focus_start, |
| 60 int focus_end); |
| 61 |
| 62 // Returns a sample of the given surrounding text with length <= the given |
| 63 // |surrounding_text_sample_limit|. The |focus_start| and |focus_end| |
| 64 // determine which part of the given text will be sampled with the focus |
| 65 // being centered as best as possible in the sample. |
| 66 // Returns a pair with the first member being the sampled text string and |
| 67 // the second member being the offset of the start of the sample within |
| 68 // the given input text. |
| 69 static std::pair<base::string16, int> SampleSurroundings( |
| 70 const base::string16& surrounding_text, |
| 71 int focus_start, |
| 72 int focus_end, |
| 73 int surrounding_text_sample_limit); |
| 74 |
| 75 // Determines if the given character is a valid part of a word to search for. |
| 76 static bool IsValidCharacter(int char_code); |
| 77 |
| 78 // The linked Java object. |
| 79 base::android::ScopedJavaGlobalRef<jobject> java_object_; |
| 80 |
| 81 // The current search context, or null. |
| 82 std::shared_ptr<ContextualSearchContext> search_context_; |
| 83 |
| 84 DISALLOW_COPY_AND_ASSIGN(SearchAction); |
| 85 }; |
| 86 |
| 87 bool RegisterSearchAction(JNIEnv* env); |
| 88 |
| 89 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_SEARCH_ACTION_H_ |
OLD | NEW |