File: instant_search_prerenderer.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (109 lines) | stat: -rw-r--r-- 3,581 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_SEARCH_INSTANT_SEARCH_PRERENDERER_H_
#define CHROME_BROWSER_UI_SEARCH_INSTANT_SEARCH_PRERENDERER_H_

#include <memory>

#include "base/macros.h"
#include "base/strings/string16.h"
#include "chrome/common/search/instant_types.h"

class GURL;
class Profile;
struct AutocompleteMatch;

namespace chrome {
struct NavigateParams;
}

namespace content {
class SessionStorageNamespace;
class WebContents;
}

namespace gfx {
class Size;
}

namespace prerender {
class PrerenderHandle;
}

// InstantSearchPrerenderer is responsible for prerendering the default search
// provider Instant search base page URL to prefetch high-confidence search
// suggestions. InstantSearchPrerenderer manages the prerender handle associated
// with the prerendered contents.
class InstantSearchPrerenderer {
 public:
  InstantSearchPrerenderer(Profile* profile, const GURL& prerender_url);
  ~InstantSearchPrerenderer();

  // Returns the InstantSearchPrerenderer instance for the given |profile|.
  static InstantSearchPrerenderer* GetForProfile(Profile* profile);

  // Prerender the |prerender_url_| contents. |session_storage_namespace| is
  // used to identify the namespace of the active tab at the time the prerender
  // is generated. The |size| gives the initial size for the target
  // prerender. InstantSearchPrerenderer will run at most one prerender at a
  // time, so launching a prerender will cancel the previous prerenders (if
  // any).
  void Init(content::SessionStorageNamespace* session_storage_namespace,
            const gfx::Size& size);

  // Cancels the current request.
  void Cancel();

  // Tells the Instant search base page to prerender |suggestion|.
  void Prerender(const InstantSuggestion& suggestion);

  // Tells the Instant search base page to render the search results for the
  // given |query|.
  void Commit(const base::string16& query,
              const EmbeddedSearchRequestParams& params);

  // Returns true if the prerendered page can be used to process the search for
  // the given |source|.
  bool CanCommitQuery(content::WebContents* source,
                      const base::string16& query) const;

  // Returns true and updates |params->target_contents| if a prerendered page
  // exists for |url| and is swapped in.
  bool UsePrerenderedPage(const GURL& url, chrome::NavigateParams* params);

  const GURL& prerender_url() const { return prerender_url_; }

  // Returns the last prefetched search query.
  const base::string16& get_last_query() const {
    return last_instant_suggestion_.text;
  }

  // Returns true when prerendering is allowed for the given |source| and
  // |match|.
  bool IsAllowed(const AutocompleteMatch& match,
                 content::WebContents* source) const;

 private:
  friend class InstantSearchPrerendererTest;

  content::WebContents* prerender_contents() const;

  // Returns true if the |query| matches the last prefetched search query or if
  // the 'reuse_instant_search_base_page' flag is enabled in the field trials.
  bool QueryMatchesPrefetch(const base::string16& query) const;

  Profile* const profile_;

  // Instant search base page URL.
  const GURL prerender_url_;

  std::unique_ptr<prerender::PrerenderHandle> prerender_handle_;

  InstantSuggestion last_instant_suggestion_;

  DISALLOW_COPY_AND_ASSIGN(InstantSearchPrerenderer);
};

#endif  // CHROME_BROWSER_UI_SEARCH_INSTANT_SEARCH_PRERENDERER_H_