Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / search / instant_page.h
blob1ff7bc394f5ee6d21633e816daf5dd804be6203e
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.
5 #ifndef CHROME_BROWSER_UI_SEARCH_INSTANT_PAGE_H_
6 #define CHROME_BROWSER_UI_SEARCH_INSTANT_PAGE_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/strings/string16.h"
14 #include "chrome/browser/ui/search/search_model_observer.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "ui/base/page_transition_types.h"
18 class GURL;
19 class Profile;
21 namespace content {
22 struct FrameNavigateParams;
23 struct LoadCommittedDetails;
24 class WebContents;
27 namespace gfx {
28 class Rect;
31 // InstantPage is used to exchange messages with a page that implements the
32 // Instant/Embedded Search API (http://dev.chromium.org/embeddedsearch).
33 // InstantPage is not used directly but via one of its derived classes,
34 // InstantNTP and InstantTab.
35 class InstantPage : public content::WebContentsObserver,
36 public SearchModelObserver {
37 public:
38 // InstantPage calls its delegate in response to messages received from the
39 // page. Each method is called with the |contents| corresponding to the page
40 // we are observing.
41 class Delegate {
42 public:
43 // Called upon determination of Instant API support. Either in response to
44 // the page loading or because we received some other message.
45 virtual void InstantSupportDetermined(const content::WebContents* contents,
46 bool supports_instant) = 0;
48 // Called when the page is about to navigate to |url|.
49 virtual void InstantPageAboutToNavigateMainFrame(
50 const content::WebContents* contents,
51 const GURL& url) = 0;
53 protected:
54 virtual ~Delegate();
57 ~InstantPage() override;
59 // Returns the Instant URL that was loaded for this page. Returns the empty
60 // string if no URL was explicitly loaded as is the case for InstantTab.
61 virtual const std::string& instant_url() const;
63 // Returns true if the page is known to support the Instant API. This starts
64 // out false, and is set to true whenever we get any message from the page.
65 // Once true, it never becomes false (the page isn't expected to drop API
66 // support suddenly).
67 virtual bool supports_instant() const;
69 // Returns true if the page is the local NTP (i.e. its URL is
70 // chrome::kChromeSearchLocalNTPURL).
71 virtual bool IsLocal() const;
73 protected:
74 InstantPage(Delegate* delegate,
75 const std::string& instant_url,
76 Profile* profile);
78 // Sets |web_contents| as the page to communicate with. |web_contents| may be
79 // NULL, which effectively stops all communication.
80 void SetContents(content::WebContents* web_contents);
82 Delegate* delegate() const { return delegate_; }
84 Profile* profile() const { return profile_; }
86 // These functions are called before processing messages received from the
87 // page. By default, all messages are handled, but any derived classes may
88 // choose to ignore some or all of the received messages by overriding these
89 // methods.
90 virtual bool ShouldProcessAboutToNavigateMainFrame();
92 private:
93 FRIEND_TEST_ALL_PREFIXES(InstantPageTest, IsLocal);
94 FRIEND_TEST_ALL_PREFIXES(InstantPageTest,
95 DetermineIfPageSupportsInstant_Local);
96 FRIEND_TEST_ALL_PREFIXES(InstantPageTest,
97 DetermineIfPageSupportsInstant_NonLocal);
98 FRIEND_TEST_ALL_PREFIXES(InstantPageTest,
99 PageURLDoesntBelongToInstantRenderer);
100 FRIEND_TEST_ALL_PREFIXES(InstantPageTest, PageSupportsInstant);
102 // Overridden from content::WebContentsObserver:
103 void DidCommitProvisionalLoadForFrame(
104 content::RenderFrameHost* render_frame_host,
105 const GURL& url,
106 ui::PageTransition transition_type) override;
108 // Overridden from SearchModelObserver:
109 void ModelChanged(const SearchModel::State& old_state,
110 const SearchModel::State& new_state) override;
112 // Update the status of Instant support.
113 void InstantSupportDetermined(bool supports_instant);
115 void ClearContents();
117 // TODO(kmadhusu): Remove |profile_| from here and update InstantNTP to get
118 // |profile| from InstantNTPPrerenderer.
119 Profile* profile_;
121 Delegate* const delegate_;
122 const std::string instant_url_;
124 DISALLOW_COPY_AND_ASSIGN(InstantPage);
127 #endif // CHROME_BROWSER_UI_SEARCH_INSTANT_PAGE_H_