OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ |
6 #define CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ | 6 #define CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/gtest_prod_util.h" | 9 #include "base/gtest_prod_util.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/android/contextualsearch/contextual_search_context.h" | 13 #include "chrome/browser/android/contextualsearch/contextual_search_context.h" |
| 14 #include "chrome/browser/android/contextualsearch/resolved_search_term.h" |
14 #include "content/public/browser/android/content_view_core.h" | 15 #include "content/public/browser/android/content_view_core.h" |
15 #include "net/url_request/url_fetcher_delegate.h" | 16 #include "net/url_request/url_fetcher_delegate.h" |
16 | 17 |
17 namespace net { | 18 namespace net { |
18 class URLRequestContextGetter; | 19 class URLRequestContextGetter; |
19 } // namespace net | 20 } // namespace net |
20 | 21 |
21 class Profile; | 22 class Profile; |
22 class TemplateURLService; | 23 class TemplateURLService; |
23 | 24 |
24 // Handles tasks for the ContextualSearchManager in a separable and testable | 25 // Handles tasks for the ContextualSearchManager in a separable and testable |
25 // way, without the complication of being connected to a Java object. | 26 // way, without the complication of being connected to a Java object. |
26 class ContextualSearchDelegate | 27 class ContextualSearchDelegate |
27 : public net::URLFetcherDelegate, | 28 : public net::URLFetcherDelegate, |
28 public base::SupportsWeakPtr<ContextualSearchDelegate> { | 29 public base::SupportsWeakPtr<ContextualSearchDelegate> { |
29 public: | 30 public: |
30 typedef base::Callback<void(bool, | |
31 int, | |
32 const std::string&, | |
33 const std::string&, | |
34 const std::string&, | |
35 bool, | |
36 int, | |
37 int)> SearchTermResolutionCallback; | |
38 typedef base::Callback<void(const std::string&)> SurroundingTextCallback; | 31 typedef base::Callback<void(const std::string&)> SurroundingTextCallback; |
| 32 typedef base::Callback<void(const ResolvedSearchTerm&)> |
| 33 SearchTermResolutionCallback; |
39 typedef base::Callback< | 34 typedef base::Callback< |
40 void(const base::string16&, int, int)> | 35 void(const base::string16&, int, int)> |
41 HandleSurroundingsCallback; | 36 HandleSurroundingsCallback; |
42 typedef base::Callback< | 37 typedef base::Callback< |
43 void(const std::string&, const base::string16&, size_t, size_t)> | 38 void(const std::string&, const base::string16&, size_t, size_t)> |
44 IcingCallback; | 39 IcingCallback; |
45 | 40 |
46 // ID used in creating URLFetcher for Contextual Search results. | 41 // ID used in creating URLFetcher for Contextual Search results. |
47 static const int kContextualSearchURLFetcherID; | 42 static const int kContextualSearchURLFetcherID; |
48 | 43 |
(...skipping 20 matching lines...) Expand all Loading... |
69 // Gathers surrounding text and saves it locally for a future query. | 64 // Gathers surrounding text and saves it locally for a future query. |
70 void GatherAndSaveSurroundingText(const std::string& selection, | 65 void GatherAndSaveSurroundingText(const std::string& selection, |
71 bool use_resolved_search_term, | 66 bool use_resolved_search_term, |
72 content::ContentViewCore* content_view_core, | 67 content::ContentViewCore* content_view_core, |
73 bool may_send_base_page_url); | 68 bool may_send_base_page_url); |
74 | 69 |
75 // Continues making a Search Term Resolution request, once the surrounding | 70 // Continues making a Search Term Resolution request, once the surrounding |
76 // text has been gathered. | 71 // text has been gathered. |
77 void ContinueSearchTermResolutionRequest(); | 72 void ContinueSearchTermResolutionRequest(); |
78 | 73 |
| 74 // Gets the target language for translation purposes for this user. |
| 75 std::string GetTargetLanguage(); |
| 76 |
| 77 // Returns the accept languages preference string. |
| 78 std::string GetAcceptLanguages(); |
| 79 |
79 // For testing. | 80 // For testing. |
80 void set_context_for_testing(ContextualSearchContext* context) { | 81 void set_context_for_testing(ContextualSearchContext* context) { |
81 context_.reset(context); | 82 context_.reset(context); |
82 } | 83 } |
83 | 84 |
84 private: | 85 private: |
85 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 86 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
86 SurroundingTextHighMaximum); | 87 SurroundingTextHighMaximum); |
87 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 88 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
88 SurroundingTextLowMaximum); | 89 SurroundingTextLowMaximum); |
89 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 90 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
90 SurroundingTextNoBeforeText); | 91 SurroundingTextNoBeforeText); |
91 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 92 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
92 SurroundingTextNoAfterText); | 93 SurroundingTextNoAfterText); |
93 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 94 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
94 ExtractMentionsStartEnd); | 95 ExtractMentionsStartEnd); |
95 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 96 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
96 SurroundingTextForIcing); | 97 SurroundingTextForIcing); |
97 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 98 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
98 SurroundingTextForIcingNegativeLimit); | 99 SurroundingTextForIcingNegativeLimit); |
99 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 100 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
100 DecodeSearchTermsFromJsonResponse); | 101 DecodeSearchTermFromJsonResponse); |
101 | 102 |
102 // net::URLFetcherDelegate: | 103 // net::URLFetcherDelegate: |
103 void OnURLFetchComplete(const net::URLFetcher* source) override; | 104 void OnURLFetchComplete(const net::URLFetcher* source) override; |
104 | 105 |
105 // Builds the search term resolution request URL from the current context. | 106 // Builds the search term resolution request URL from the current context. |
106 GURL BuildRequestUrl(); | 107 GURL BuildRequestUrl(); |
107 | 108 |
108 // Uses the TemplateURL service to construct a search term resolution URL from | 109 // Uses the TemplateURL service to construct a search term resolution URL from |
109 // the given parameters. | 110 // the given parameters. |
110 std::string GetSearchTermResolutionUrlString( | 111 std::string GetSearchTermResolutionUrlString( |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 | 146 |
146 // Checks if we can send the URL for this user. Several conditions are checked | 147 // Checks if we can send the URL for this user. Several conditions are checked |
147 // to make sure it's OK to send the URL. These fall into two categories: | 148 // to make sure it's OK to send the URL. These fall into two categories: |
148 // 1) check if it's allowed by our policy, and 2) ensure that the user is | 149 // 1) check if it's allowed by our policy, and 2) ensure that the user is |
149 // already sending their URL browsing activity to Google. | 150 // already sending their URL browsing activity to Google. |
150 bool CanSendPageURL(const GURL& current_page_url, | 151 bool CanSendPageURL(const GURL& current_page_url, |
151 Profile* profile, | 152 Profile* profile, |
152 TemplateURLService* template_url_service); | 153 TemplateURLService* template_url_service); |
153 | 154 |
154 // Decodes the given json response string and extracts parameters. | 155 // Decodes the given json response string and extracts parameters. |
155 void DecodeSearchTermsFromJsonResponse(const std::string& response, | 156 void DecodeSearchTermFromJsonResponse(const std::string& response, |
156 std::string* search_term, | 157 std::string* search_term, |
157 std::string* display_text, | 158 std::string* display_text, |
158 std::string* alternate_term, | 159 std::string* alternate_term, |
159 std::string* prevent_preload, | 160 std::string* prevent_preload, |
160 int* mention_start, | 161 int* mention_start, |
161 int* mention_end); | 162 int* mention_end, |
| 163 std::string* context_language); |
162 | 164 |
163 void ExtractMentionsStartEnd(const base::ListValue& mentions_list, | 165 void ExtractMentionsStartEnd(const base::ListValue& mentions_list, |
164 int* startResult, | 166 int* startResult, |
165 int* endResult); | 167 int* endResult); |
166 | 168 |
167 // Returns the surrounding size to use for the search term resolution | 169 // Returns the surrounding size to use for the search term resolution |
168 // request. | 170 // request. |
169 int GetSearchTermSurroundingSize(); | 171 int GetSearchTermSurroundingSize(); |
170 | 172 |
171 // Returns the size of the surroundings to be sent to Icing. | 173 // Returns the size of the surroundings to be sent to Icing. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 // The callback for notifications of Icing selection being available. | 208 // The callback for notifications of Icing selection being available. |
207 IcingCallback icing_callback_; | 209 IcingCallback icing_callback_; |
208 | 210 |
209 // Used to hold the context until an upcoming search term request is started. | 211 // Used to hold the context until an upcoming search term request is started. |
210 scoped_ptr<ContextualSearchContext> context_; | 212 scoped_ptr<ContextualSearchContext> context_; |
211 | 213 |
212 DISALLOW_COPY_AND_ASSIGN(ContextualSearchDelegate); | 214 DISALLOW_COPY_AND_ASSIGN(ContextualSearchDelegate); |
213 }; | 215 }; |
214 | 216 |
215 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ | 217 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ |
OLD | NEW |