Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(335)

Unified Diff: chrome/browser/android/contextualsearch/search_action.h

Issue 2211353002: [TTS] Gather surrounding text on Tap before any UX. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Simplified the results for the native accessors to the context. Removed print statements. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..129401a3579beecc9df7171eeff80451d8fd2c72
--- /dev/null
+++ b/chrome/browser/android/contextualsearch/search_action.h
@@ -0,0 +1,103 @@
+// 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 (crbug.com/624609,
+// 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);
+
+ // Finds and returns the focused word within the current surrounding text.
+ // RequestSurroundingText must be called first and the associated response
+ // received.
+ std::string FindFocusedWord();
+
+ // Returns a sample of the current surrounding text of the given
+ // |sample_length| or shorter if insufficient sample text is available.
+ // RequestSurroundingText must be called first and the associated response
+ // received.
+ std::string GetSampleText(int sample_length);
+
+ 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.
+ // TODO(donnd): consider changing the return value to a simple string.
+ 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.
+ // TODO(donnd): consider changing the return value to a simple string.
+ 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_

Powered by Google App Engine
This is Rietveld 408576698