instant_search_prerenderer.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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
14class GURL;
15class Profile;
16struct AutocompleteMatch;
17
18namespace chrome {
19struct NavigateParams;
20}
21
22namespace content {
23class WebContents;
24}
25
26namespace gfx {
27class Size;
28}
29
30namespace prerender {
31class 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.
38class InstantSearchPrerenderer {
39 public:
40  InstantSearchPrerenderer(Profile* profile, const GURL& url);
41  ~InstantSearchPrerenderer();
42
43  // Prerender the |prerender_url_| contents. The
44  // |session_storage_namespace_map| is used to identify the namespace of the
45  // active tab at the time the prerender is generated. The |size| gives the
46  // initial size for the target prerender. InstantSearchPrerenderer will run at
47  // most one prerender at a time, so launching a prerender will cancel the
48  // previous prerenders (if any).
49  void Init(
50      const content::SessionStorageNamespaceMap& session_storage_namespace_map,
51      const gfx::Size& size);
52
53  // Cancels the current request.
54  void Cancel();
55
56  // Tells the Instant search base page to prerender |suggestion|.
57  void Prerender(const InstantSuggestion& suggestion);
58
59  // Tells the Instant search base page to render the prefetched search results.
60  void Commit(const string16& query);
61
62  // Returns true if the prerendered page can be used to process the search for
63  // the given |source|.
64  bool CanCommitQuery(content::WebContents* source,
65                      const string16& query) const;
66
67  // Returns true and updates |params->target_contents| if a prerendered page
68  // exists for |url| and is swapped in.
69  bool UsePrerenderedPage(const GURL& url, chrome::NavigateParams* params);
70
71  // Returns the last prefetched search query.
72  const string16& get_last_query() const {
73    return last_instant_suggestion_.text;
74  }
75
76  // Returns true when prerendering is allowed for the given |source| and
77  // |match|.
78  bool IsAllowed(const AutocompleteMatch& match,
79                 content::WebContents* source) const;
80
81 private:
82  friend class InstantSearchPrerendererTest;
83
84  content::WebContents* prerender_contents() const;
85
86  Profile* const profile_;
87
88  // Instant search base page URL.
89  const GURL prerender_url_;
90
91  scoped_ptr<prerender::PrerenderHandle> prerender_handle_;
92
93  InstantSuggestion last_instant_suggestion_;
94
95  DISALLOW_COPY_AND_ASSIGN(InstantSearchPrerenderer);
96};
97
98#endif  // CHROME_BROWSER_UI_SEARCH_INSTANT_SEARCH_PRERENDERER_H_
99