Home | History | Annotate | Download | only in search
      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 prefetched search results.
     63   void Commit(const string16& query);
     64 
     65   // Returns true if the prerendered page can be used to process the search for
     66   // the given |source|.
     67   bool CanCommitQuery(content::WebContents* source,
     68                       const string16& query) const;
     69 
     70   // Returns true and updates |params->target_contents| if a prerendered page
     71   // exists for |url| and is swapped in.
     72   bool UsePrerenderedPage(const GURL& url, chrome::NavigateParams* params);
     73 
     74   // Returns the last prefetched search query.
     75   const string16& get_last_query() const {
     76     return last_instant_suggestion_.text;
     77   }
     78 
     79   // Returns true when prerendering is allowed for the given |source| and
     80   // |match|.
     81   bool IsAllowed(const AutocompleteMatch& match,
     82                  content::WebContents* source) const;
     83 
     84  private:
     85   friend class InstantSearchPrerendererTest;
     86 
     87   content::WebContents* prerender_contents() const;
     88 
     89   Profile* const profile_;
     90 
     91   // Instant search base page URL.
     92   const GURL prerender_url_;
     93 
     94   scoped_ptr<prerender::PrerenderHandle> prerender_handle_;
     95 
     96   InstantSuggestion last_instant_suggestion_;
     97 
     98   DISALLOW_COPY_AND_ASSIGN(InstantSearchPrerenderer);
     99 };
    100 
    101 #endif  // CHROME_BROWSER_UI_SEARCH_INSTANT_SEARCH_PRERENDERER_H_
    102