1 // Copyright 2013 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_UI_SEARCH_INSTANT_SEARCH_PRERENDERER_H_ 6 #define CHROME_BROWSER_UI_SEARCH_INSTANT_SEARCH_PRERENDERER_H_ 7 8 #include "base/basictypes.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/strings/string16.h" 11 #include "chrome/common/instant_types.h" 12 #include "content/public/browser/navigation_controller.h" 13 14 class GURL; 15 class Profile; 16 struct AutocompleteMatch; 17 18 namespace chrome { 19 struct NavigateParams; 20 } 21 22 namespace content { 23 class WebContents; 24 } 25 26 namespace gfx { 27 class Size; 28 } 29 30 namespace prerender { 31 class PrerenderHandle; 32 } 33 34 // InstantSearchPrerenderer is responsible for prerendering the default search 35 // provider Instant search base page URL to prefetch high-confidence search 36 // suggestions. InstantSearchPrerenderer manages the prerender handle associated 37 // with the prerendered contents. 38 class InstantSearchPrerenderer { 39 public: 40 InstantSearchPrerenderer(Profile* profile, const GURL& url); 41 ~InstantSearchPrerenderer(); 42 43 // Returns the InstantSearchPrerenderer instance for the given |profile|. 44 static InstantSearchPrerenderer* GetForProfile(Profile* profile); 45 46 // Prerender the |prerender_url_| contents. The 47 // |session_storage_namespace_map| is used to identify the namespace of the 48 // active tab at the time the prerender is generated. The |size| gives the 49 // initial size for the target prerender. InstantSearchPrerenderer will run at 50 // most one prerender at a time, so launching a prerender will cancel the 51 // previous prerenders (if any). 52 void Init( 53 const content::SessionStorageNamespaceMap& session_storage_namespace_map, 54 const gfx::Size& size); 55 56 // Cancels the current request. 57 void Cancel(); 58 59 // Tells the Instant search base page to prerender |suggestion|. 60 void Prerender(const InstantSuggestion& suggestion); 61 62 // Tells the Instant search base page to render the search results for the 63 // given |query|. 64 void Commit(const base::string16& query); 65 66 // Returns true if the prerendered page can be used to process the search for 67 // the given |source|. 68 bool CanCommitQuery(content::WebContents* source, 69 const base::string16& query) const; 70 71 // Returns true and updates |params->target_contents| if a prerendered page 72 // exists for |url| and is swapped in. 73 bool UsePrerenderedPage(const GURL& url, chrome::NavigateParams* params); 74 75 // Returns the last prefetched search query. 76 const base::string16& get_last_query() const { 77 return last_instant_suggestion_.text; 78 } 79 80 // Returns true when prerendering is allowed for the given |source| and 81 // |match|. 82 bool IsAllowed(const AutocompleteMatch& match, 83 content::WebContents* source) const; 84 85 private: 86 friend class InstantSearchPrerendererTest; 87 88 content::WebContents* prerender_contents() const; 89 90 // Returns true if the |query| matches the last prefetched search query or if 91 // the 'reuse_instant_search_base_page' flag is enabled in the field trials. 92 bool QueryMatchesPrefetch(const base::string16& query) const; 93 94 Profile* const profile_; 95 96 // Instant search base page URL. 97 const GURL prerender_url_; 98 99 scoped_ptr<prerender::PrerenderHandle> prerender_handle_; 100 101 InstantSuggestion last_instant_suggestion_; 102 103 DISALLOW_COPY_AND_ASSIGN(InstantSearchPrerenderer); 104 }; 105 106 #endif // CHROME_BROWSER_UI_SEARCH_INSTANT_SEARCH_PRERENDERER_H_ 107