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

Side by Side 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: Changed passing the text sample to java -- now it's by Java request only (currently unused). 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 unified diff | Download patch
OLDNEW
(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 base::android::ScopedJavaLocalRef<jstring> GetFocusedWord(
47 JNIEnv* env,
48 const base::android::JavaParamRef<jobject>& obj);
49
50 // Returns a sample of the current surrounding text of the given
51 // |sample_length| or
52 // shorter if insufficient sample text is available.
53 // RequestSurroundingText must be called first and the associated response
54 // received.
55 base::android::ScopedJavaLocalRef<jstring> GetSampleText(
56 JNIEnv* env,
57 const base::android::JavaParamRef<jobject>& obj,
58 int sample_length);
59
60 private:
61 FRIEND_TEST_ALL_PREFIXES(SearchActionTest, IsValidCharacterTest);
62 FRIEND_TEST_ALL_PREFIXES(SearchActionTest, FindFocusedWordTest);
63 FRIEND_TEST_ALL_PREFIXES(SearchActionTest, SampleSurroundingsTest);
64
65 // Analyzes the surrounding text and makes it available to the Java
66 // SearchAction object in a call to SearchAction#onSurroundingTextResponse.
67 void OnSurroundingTextResponse(const base::string16& surrounding_text,
68 int focus_start,
69 int focus_end);
70
71 // Expands the given focus to find the start and end word boundary.
72 // Returns a pair whose first member is the start offset and second member
73 // is the end offset of the word. If the focus is not within a word then
74 // the focus inputs are returned.
75 static std::pair<int, int> FindFocusedWord(
76 const base::string16& surrounding_text,
77 int focus_start,
78 int focus_end);
79
80 // Returns a sample of the given surrounding text with length <= the given
81 // |surrounding_text_sample_limit|. The |focus_start| and |focus_end|
82 // determine which part of the given text will be sampled with the focus
83 // being centered as best as possible in the sample.
84 // Returns a pair with the first member being the sampled text string and
85 // the second member being the offset of the start of the sample within
86 // the given input text.
87 static std::pair<base::string16, int> SampleSurroundings(
88 const base::string16& surrounding_text,
89 int focus_start,
90 int focus_end,
91 int surrounding_text_sample_limit);
92
93 // Determines if the given character is a valid part of a word to search for.
94 static bool IsValidCharacter(int char_code);
95
96 // The linked Java object.
97 base::android::ScopedJavaGlobalRef<jobject> java_object_;
98
99 // The current search context, or null.
100 std::shared_ptr<ContextualSearchContext> search_context_;
101
102 DISALLOW_COPY_AND_ASSIGN(SearchAction);
103 };
104
105 bool RegisterSearchAction(JNIEnv* env);
106
107 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_SEARCH_ACTION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698