Index: chrome/browser/android/contextualsearch/search_action.h |
diff --git a/chrome/browser/android/contextualsearch/search_action.h b/chrome/browser/android/contextualsearch/search_action.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9c4d593d80748ce2b960ee132390c52716a358c8 |
--- /dev/null |
+++ b/chrome/browser/android/contextualsearch/search_action.h |
@@ -0,0 +1,89 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_SEARCH_ACTION_H_ |
+#define CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_SEARCH_ACTION_H_ |
+ |
+#include <memory> |
+ |
+#include "base/android/jni_android.h" |
+#include "base/gtest_prod_util.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/strings/string_util.h" |
+ |
+struct ContextualSearchContext; |
+ |
+// Represents the native side of a Java Search Action, which knows how to do a |
+// Contextual Search. |
+// This is part of the 2016-refactoring, see go/cs-refactoring-2016. |
+// Gathers text surrounding the selection from the page and makes it accessible |
+// to Java. |
+// TODO(donnd): add capability to "resolve" the best search term for the section |
+// of the page based on a server request or local text analysis. |
+class SearchAction : public base::SupportsWeakPtr<SearchAction> { |
+ public: |
+ // Constructs a new Search Action linked to the given Java object. |
+ SearchAction(JNIEnv* env, jobject obj); |
+ virtual ~SearchAction(); |
+ |
+ // Should be called when this native object is no longer needed (calls the |
+ // destructor). |
+ void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj); |
+ |
+ // Requests the text surrounding the selection for the given WebContents Java |
+ // object. The surrounding text will be made available through a call to |
+ // |OnSurroundingTextResponse|. |
+ void RequestSurroundingText( |
+ JNIEnv* env, |
+ const base::android::JavaParamRef<jobject>& obj, |
+ const base::android::JavaParamRef<jobject>& j_web_contents); |
+ |
+ private: |
+ FRIEND_TEST_ALL_PREFIXES(SearchActionTest, IsValidCharacterTest); |
+ FRIEND_TEST_ALL_PREFIXES(SearchActionTest, FindFocusedWordTest); |
+ FRIEND_TEST_ALL_PREFIXES(SearchActionTest, SampleSurroundingsTest); |
+ |
+ // Analyzes the surrounding text and makes it available to the Java |
+ // SearchAction object in a call to SearchAction#onSurroundingTextResponse. |
+ void OnSurroundingTextResponse(const base::string16& surrounding_text, |
+ int focus_start, |
+ int focus_end); |
+ |
+ // Expands the given focus to find the start and end word boundary. |
+ // Returns a pair whose first member is the start offset and second member |
+ // is the end offset of the word. If the focus is not within a word then |
+ // the focus inputs are returned. |
+ static std::pair<int, int> FindFocusedWord( |
+ const base::string16& surrounding_text, |
+ int focus_start, |
+ int focus_end); |
+ |
+ // Returns a sample of the given surrounding text with length <= the given |
+ // |surrounding_text_sample_limit|. The |focus_start| and |focus_end| |
+ // determine which part of the given text will be sampled with the focus |
+ // being centered as best as possible in the sample. |
+ // Returns a pair with the first member being the sampled text string and |
+ // the second member being the offset of the start of the sample within |
+ // the given input text. |
+ static std::pair<base::string16, int> SampleSurroundings( |
+ const base::string16& surrounding_text, |
+ int focus_start, |
+ int focus_end, |
+ int surrounding_text_sample_limit); |
+ |
+ // Determines if the given character is a valid part of a word to search for. |
+ static bool IsValidCharacter(int char_code); |
+ |
+ // The linked Java object. |
+ base::android::ScopedJavaGlobalRef<jobject> java_object_; |
+ |
+ // The current search context, or null. |
+ std::shared_ptr<ContextualSearchContext> search_context_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(SearchAction); |
+}; |
+ |
+bool RegisterSearchAction(JNIEnv* env); |
+ |
+#endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_SEARCH_ACTION_H_ |