Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / search / instant_page.h
bloba054cb48386d2f94ba6e1ccb55de2fa08edc3d6b
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/memory/scoped_ptr.h"
14 #include "base/strings/string16.h"
15 #include "chrome/browser/ui/search/instant_ipc_sender.h"
16 #include "chrome/browser/ui/search/search_model_observer.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "content/public/common/page_transition_types.h"
20 class GURL;
21 class Profile;
23 namespace content {
24 struct FrameNavigateParams;
25 struct LoadCommittedDetails;
26 class WebContents;
29 namespace gfx {
30 class Rect;
33 // InstantPage is used to exchange messages with a page that implements the
34 // Instant/Embedded Search API (http://dev.chromium.org/embeddedsearch).
35 // InstantPage is not used directly but via one of its derived classes,
36 // InstantNTP and InstantTab.
37 class InstantPage : public content::WebContentsObserver,
38 public SearchModelObserver {
39 public:
40 // InstantPage calls its delegate in response to messages received from the
41 // page. Each method is called with the |contents| corresponding to the page
42 // we are observing.
43 class Delegate {
44 public:
45 // Called upon determination of Instant API support. Either in response to
46 // the page loading or because we received some other message.
47 virtual void InstantSupportDetermined(const content::WebContents* contents,
48 bool supports_instant) = 0;
50 // Called when the page is about to navigate to |url|.
51 virtual void InstantPageAboutToNavigateMainFrame(
52 const content::WebContents* contents,
53 const GURL& url) = 0;
55 protected:
56 virtual ~Delegate();
59 virtual ~InstantPage();
61 // The WebContents corresponding to the page we're talking to. May be NULL.
62 content::WebContents* contents() const { return web_contents(); }
64 // Used to send IPC messages to the page.
65 InstantIPCSender* sender() const { return ipc_sender_.get(); }
67 // Returns the Instant URL that was loaded for this page. Returns the empty
68 // string if no URL was explicitly loaded as is the case for InstantTab.
69 virtual const std::string& instant_url() const;
71 // Returns true if the page is known to support the Instant API. This starts
72 // out false, and is set to true whenever we get any message from the page.
73 // Once true, it never becomes false (the page isn't expected to drop API
74 // support suddenly).
75 virtual bool supports_instant() const;
77 // Returns true if the page is the local NTP (i.e. its URL is
78 // chrome::kChromeSearchLocalNTPURL).
79 virtual bool IsLocal() const;
81 protected:
82 InstantPage(Delegate* delegate, const std::string& instant_url,
83 Profile* profile, bool is_incognito);
85 // Sets |web_contents| as the page to communicate with. |web_contents| may be
86 // NULL, which effectively stops all communication.
87 void SetContents(content::WebContents* web_contents);
89 Delegate* delegate() const { return delegate_; }
91 Profile* profile() const { return profile_; }
93 // These functions are called before processing messages received from the
94 // page. By default, all messages are handled, but any derived classes may
95 // choose to ignore some or all of the received messages by overriding these
96 // methods.
97 virtual bool ShouldProcessAboutToNavigateMainFrame();
99 private:
100 FRIEND_TEST_ALL_PREFIXES(InstantPageTest, IsLocal);
101 FRIEND_TEST_ALL_PREFIXES(InstantPageTest,
102 DetermineIfPageSupportsInstant_Local);
103 FRIEND_TEST_ALL_PREFIXES(InstantPageTest,
104 DetermineIfPageSupportsInstant_NonLocal);
105 FRIEND_TEST_ALL_PREFIXES(InstantPageTest,
106 PageURLDoesntBelongToInstantRenderer);
107 FRIEND_TEST_ALL_PREFIXES(InstantPageTest, PageSupportsInstant);
108 FRIEND_TEST_ALL_PREFIXES(InstantPageTest,
109 AppropriateMessagesSentToIncognitoPages);
111 // Overridden from content::WebContentsObserver:
112 virtual void DidCommitProvisionalLoadForFrame(
113 int64 frame_id,
114 const base::string16& frame_unique_name,
115 bool is_main_frame,
116 const GURL& url,
117 content::PageTransition transition_type,
118 content::RenderViewHost* render_view_host) OVERRIDE;
120 // Overridden from SearchModelObserver:
121 virtual void ModelChanged(const SearchModel::State& old_state,
122 const SearchModel::State& new_state) OVERRIDE;
124 // Update the status of Instant support.
125 void InstantSupportDetermined(bool supports_instant);
127 void ClearContents();
129 // TODO(kmadhusu): Remove |profile_| from here and update InstantNTP to get
130 // |profile| from InstantNTPPrerenderer.
131 Profile* profile_;
133 Delegate* const delegate_;
134 scoped_ptr<InstantIPCSender> ipc_sender_;
135 const std::string instant_url_;
136 const bool is_incognito_;
138 DISALLOW_COPY_AND_ASSIGN(InstantPage);
141 #endif // CHROME_BROWSER_UI_SEARCH_INSTANT_PAGE_H_