OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 IOS_CHROME_BROWSER_UI_CONTEXTUAL_SEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ |
| 6 #define IOS_CHROME_BROWSER_UI_CONTEXTUAL_SEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "base/callback.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/time/time.h" |
| 13 #include "ios/chrome/browser/ui/contextual_search/contextual_search_context.h" |
| 14 #include "net/url_request/url_fetcher_delegate.h" |
| 15 |
| 16 class TemplateURLService; |
| 17 |
| 18 namespace ios { |
| 19 class ChromeBrowserState; |
| 20 } |
| 21 |
| 22 // Handles tasks for the ContextualSearchManager in a separable and testable |
| 23 // way, without the complication of being connected to a Java object. |
| 24 class ContextualSearchDelegate |
| 25 : public net::URLFetcherDelegate, |
| 26 public base::SupportsWeakPtr<ContextualSearchDelegate> { |
| 27 public: |
| 28 typedef struct { |
| 29 bool is_invalid; |
| 30 int response_code; |
| 31 std::string search_term; |
| 32 std::string display_text; |
| 33 std::string alternate_term; |
| 34 bool prevent_preload; |
| 35 int start_offset; |
| 36 int end_offset; |
| 37 } SearchResolution; |
| 38 |
| 39 typedef base::Callback<void(SearchResolution)> SearchTermResolutionCallback; |
| 40 |
| 41 // ID used in creating URLFetcher for Contextual Search results. |
| 42 static const int kContextualSearchURLFetcherID; |
| 43 |
| 44 // Constructs a delegate for the given user browser state that will always |
| 45 // call back to the given callbacks when search term resolution or surrounding |
| 46 // text responses are available. |
| 47 ContextualSearchDelegate( |
| 48 ios::ChromeBrowserState* browser_state, |
| 49 const SearchTermResolutionCallback& search_term_callback); |
| 50 ~ContextualSearchDelegate() override; |
| 51 |
| 52 // Requests the search term using |context| and parameters that specify the |
| 53 // surrounding text. if |context| represents a local text selection, then no |
| 54 // server request is made and the selection contents are returned as the |
| 55 // relevant text. |
| 56 // When the response is available the callback specified in the constructor |
| 57 // is run. |
| 58 // This method is asynchronous and may delay the request to avoid sending |
| 59 // too many requests to the server. If this function is called multiple times |
| 60 // during that delay, only the latest request is executed. Each new call |
| 61 // cancels previous requests. |
| 62 void PostSearchTermRequest(std::shared_ptr<ContextualSearchContext> context); |
| 63 |
| 64 // This method will immediately start the pending request previously posted by |
| 65 // |PostSearchTermRequest|. |
| 66 void StartPendingSearchTermRequest(); |
| 67 |
| 68 // Cancels the current search term network request. |
| 69 void CancelSearchTermRequest(); |
| 70 |
| 71 // Get the URL for the search to be run for the query in |resolution|. |
| 72 GURL GetURLForResolvedSearch(SearchResolution resolution, |
| 73 bool should_prefetch); |
| 74 |
| 75 private: |
| 76 // net::URLFetcherDelegate: |
| 77 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 78 |
| 79 // Builds the request URL. |
| 80 GURL BuildRequestUrl(); |
| 81 |
| 82 // Copy search context term as SearchResolution and calls the |
| 83 // search_term_callback_. |
| 84 void RequestLocalSearchTerm(); |
| 85 |
| 86 // Initiates a network request to get the relevant query context. |
| 87 void RequestServerSearchTerm(); |
| 88 |
| 89 // Uses the TemplateURL service to construct a search term resolution URL from |
| 90 // the given parameters. |
| 91 std::string GetSearchTermResolutionUrlString( |
| 92 const std::string& selected_text, |
| 93 const std::string& base_page_url, |
| 94 const bool use_resolved_search_term); |
| 95 |
| 96 // Populates the discourse context and adds it to the HTTP header of the |
| 97 // search term resolution request. |
| 98 void SetDiscourseContextAndAddToHeader( |
| 99 const ContextualSearchContext& context); |
| 100 |
| 101 // The current request in progress, or NULL. |
| 102 std::unique_ptr<net::URLFetcher> search_term_fetcher_; |
| 103 |
| 104 // Holds the TemplateURLService. Not owned. |
| 105 TemplateURLService* template_url_service_; |
| 106 |
| 107 // The browser state associated with the Contextual Search Manager. |
| 108 // This reference is not owned by us. |
| 109 ios::ChromeBrowserState* browser_state_; |
| 110 |
| 111 // The callback for notifications of completed URL fetches. |
| 112 SearchTermResolutionCallback search_term_callback_; |
| 113 |
| 114 // Used to hold the context until an upcoming search term request is started. |
| 115 std::shared_ptr<ContextualSearchContext> context_; |
| 116 |
| 117 base::Time last_request_startup_time_; |
| 118 bool request_pending_; |
| 119 |
| 120 // Used to ensure |StartRequestSearchTerm()| is not run when this object is |
| 121 // destroyed. |
| 122 base::WeakPtrFactory<ContextualSearchDelegate> weak_ptr_factory_; |
| 123 |
| 124 DISALLOW_COPY_AND_ASSIGN(ContextualSearchDelegate); |
| 125 }; |
| 126 |
| 127 #endif // IOS_CHROME_BROWSER_UI_CONTEXTUAL_SEARCH_CONTEXTUAL_SEARCH_DELEGATE_H_ |
OLD | NEW |