| 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 typedef base::Callback<void(const ResolvedSearchTerm&)> |
| 31 int, | 32 SearchTermResolutionCallback; |
| 32 const std::string&, | |
| 33 const std::string&, | |
| 34 const std::string&, | |
| 35 bool, | |
| 36 int, | |
| 37 int)> SearchTermResolutionCallback; | |
| 38 typedef base::Callback< | 33 typedef base::Callback< |
| 39 void(const std::string&, const std::string&)> SurroundingTextCallback; | 34 void(const std::string&, const std::string&)> SurroundingTextCallback; |
| 40 typedef base::Callback< | 35 typedef base::Callback< |
| 41 void(const base::string16&, int, int)> | 36 void(const base::string16&, int, int)> |
| 42 HandleSurroundingsCallback; | 37 HandleSurroundingsCallback; |
| 43 typedef base::Callback< | 38 typedef base::Callback< |
| 44 void(const std::string&, const base::string16&, size_t, size_t)> | 39 void(const std::string&, const base::string16&, size_t, size_t)> |
| 45 IcingCallback; | 40 IcingCallback; |
| 46 | 41 |
| 47 // ID used in creating URLFetcher for Contextual Search results. | 42 // ID used in creating URLFetcher for Contextual Search results. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 70 // Gathers surrounding text and saves it locally for a future query. | 65 // Gathers surrounding text and saves it locally for a future query. |
| 71 void GatherAndSaveSurroundingText(const std::string& selection, | 66 void GatherAndSaveSurroundingText(const std::string& selection, |
| 72 bool use_resolved_search_term, | 67 bool use_resolved_search_term, |
| 73 content::ContentViewCore* content_view_core, | 68 content::ContentViewCore* content_view_core, |
| 74 bool may_send_base_page_url); | 69 bool may_send_base_page_url); |
| 75 | 70 |
| 76 // Continues making a Search Term Resolution request, once the surrounding | 71 // Continues making a Search Term Resolution request, once the surrounding |
| 77 // text has been gathered. | 72 // text has been gathered. |
| 78 void ContinueSearchTermResolutionRequest(); | 73 void ContinueSearchTermResolutionRequest(); |
| 79 | 74 |
| 75 // Gets the target language for translation purposes (cached). |
| 76 std::string GetTargetLanguage(); |
| 77 |
| 80 // For testing. | 78 // For testing. |
| 81 void set_context_for_testing(ContextualSearchContext* context) { | 79 void set_context_for_testing(ContextualSearchContext* context) { |
| 82 context_.reset(context); | 80 context_.reset(context); |
| 83 } | 81 } |
| 84 | 82 |
| 85 private: | 83 private: |
| 86 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 84 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
| 87 SurroundingTextHighMaximum); | 85 SurroundingTextHighMaximum); |
| 88 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 86 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
| 89 SurroundingTextLowMaximum); | 87 SurroundingTextLowMaximum); |
| 90 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 88 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
| 91 SurroundingTextNoBeforeText); | 89 SurroundingTextNoBeforeText); |
| 92 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 90 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
| 93 ExtractMentionsStartEnd); | 91 ExtractMentionsStartEnd); |
| 94 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 92 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
| 95 SurroundingTextForIcing); | 93 SurroundingTextForIcing); |
| 96 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 94 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
| 97 SurroundingTextForIcingNegativeLimit); | 95 SurroundingTextForIcingNegativeLimit); |
| 98 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, | 96 FRIEND_TEST_ALL_PREFIXES(ContextualSearchDelegateTest, |
| 99 DecodeSearchTermsFromJsonResponse); | 97 DecodeSearchTermFromJsonResponse); |
| 100 | 98 |
| 101 // net::URLFetcherDelegate: | 99 // net::URLFetcherDelegate: |
| 102 void OnURLFetchComplete(const net::URLFetcher* source) override; | 100 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 103 | 101 |
| 104 // Builds the search term resolution request URL from the current context. | 102 // Builds the search term resolution request URL from the current context. |
| 105 GURL BuildRequestUrl(); | 103 GURL BuildRequestUrl(); |
| 106 | 104 |
| 107 // Uses the TemplateURL service to construct a search term resolution URL from | 105 // Uses the TemplateURL service to construct a search term resolution URL from |
| 108 // the given parameters. | 106 // the given parameters. |
| 109 std::string GetSearchTermResolutionUrlString( | 107 std::string GetSearchTermResolutionUrlString( |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 | 142 |
| 145 // Checks if we can send the URL for this user. Several conditions are checked | 143 // Checks if we can send the URL for this user. Several conditions are checked |
| 146 // to make sure it's OK to send the URL. These fall into two categories: | 144 // to make sure it's OK to send the URL. These fall into two categories: |
| 147 // 1) check if it's allowed by our policy, and 2) ensure that the user is | 145 // 1) check if it's allowed by our policy, and 2) ensure that the user is |
| 148 // already sending their URL browsing activity to Google. | 146 // already sending their URL browsing activity to Google. |
| 149 bool CanSendPageURL(const GURL& current_page_url, | 147 bool CanSendPageURL(const GURL& current_page_url, |
| 150 Profile* profile, | 148 Profile* profile, |
| 151 TemplateURLService* template_url_service); | 149 TemplateURLService* template_url_service); |
| 152 | 150 |
| 153 // Decodes the given json response string and extracts parameters. | 151 // Decodes the given json response string and extracts parameters. |
| 154 void DecodeSearchTermsFromJsonResponse(const std::string& response, | 152 void DecodeSearchTermFromJsonResponse(const std::string& response, |
| 155 std::string* search_term, | 153 std::string* search_term, |
| 156 std::string* display_text, | 154 std::string* display_text, |
| 157 std::string* alternate_term, | 155 std::string* alternate_term, |
| 158 std::string* prevent_preload, | 156 std::string* prevent_preload, |
| 159 int* mention_start, | 157 int* mention_start, |
| 160 int* mention_end); | 158 int* mention_end, |
| 159 std::string* lang); |
| 161 | 160 |
| 162 void ExtractMentionsStartEnd(const base::ListValue& mentions_list, | 161 void ExtractMentionsStartEnd(const base::ListValue& mentions_list, |
| 163 int* startResult, | 162 int* startResult, |
| 164 int* endResult); | 163 int* endResult); |
| 165 | 164 |
| 166 // Returns the surrounding size to use for the search term resolution | 165 // Returns the surrounding size to use for the search term resolution |
| 167 // request. | 166 // request. |
| 168 int GetSearchTermSurroundingSize(); | 167 int GetSearchTermSurroundingSize(); |
| 169 | 168 |
| 170 // Returns the size of the surroundings to be sent to Icing. | 169 // Returns the size of the surroundings to be sent to Icing. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 201 | 200 |
| 202 // The callback for notifications of surrounding text being available. | 201 // The callback for notifications of surrounding text being available. |
| 203 SurroundingTextCallback surrounding_callback_; | 202 SurroundingTextCallback surrounding_callback_; |
| 204 | 203 |
| 205 // The callback for notifications of Icing selection being available. | 204 // The callback for notifications of Icing selection being available. |
| 206 IcingCallback icing_callback_; | 205 IcingCallback icing_callback_; |
| 207 | 206 |
| 208 // Used to hold the context until an upcoming search term request is started. | 207 // Used to hold the context until an upcoming search term request is started. |
| 209 scoped_ptr<ContextualSearchContext> context_; | 208 scoped_ptr<ContextualSearchContext> context_; |
| 210 | 209 |
| 210 // Cached Target Language string, to determine when we need to trigger |
| 211 // translation. |
| 212 std::string target_language_; |
| 213 |
| 211 DISALLOW_COPY_AND_ASSIGN(ContextualSearchDelegate); | 214 DISALLOW_COPY_AND_ASSIGN(ContextualSearchDelegate); |
| 212 }; | 215 }; |
| 213 | 216 |
| 214 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ | 217 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ |
| OLD | NEW |