• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PAGE_H_
6 #define CHROME_BROWSER_UI_SEARCH_INSTANT_PAGE_H_
7 
8 #include <vector>
9 
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"
19 
20 class GURL;
21 class Profile;
22 
23 namespace content {
24 struct FrameNavigateParams;
25 struct LoadCommittedDetails;
26 class WebContents;
27 }
28 
29 namespace gfx {
30 class Rect;
31 }
32 
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;
49 
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;
54 
55     // Called when the page fails to load for whatever reason.
56     virtual void InstantPageLoadFailed(content::WebContents* contents) = 0;
57 
58    protected:
59     virtual ~Delegate();
60   };
61 
62   virtual ~InstantPage();
63 
64   // The WebContents corresponding to the page we're talking to. May be NULL.
contents()65   content::WebContents* contents() const { return web_contents(); }
66 
67   // Used to send IPC messages to the page.
sender()68   InstantIPCSender* sender() const { return ipc_sender_.get(); }
69 
70   // Returns the Instant URL that was loaded for this page. Returns the empty
71   // string if no URL was explicitly loaded as is the case for InstantTab.
72   virtual const std::string& instant_url() const;
73 
74   // Returns true if the page is known to support the Instant API. This starts
75   // out false, and is set to true whenever we get any message from the page.
76   // Once true, it never becomes false (the page isn't expected to drop API
77   // support suddenly).
78   virtual bool supports_instant() const;
79 
80   // Returns true if the page is the local NTP (i.e. its URL is
81   // chrome::kChromeSearchLocalNTPURL).
82   virtual bool IsLocal() const;
83 
84  protected:
85   InstantPage(Delegate* delegate, const std::string& instant_url,
86               Profile* profile, bool is_incognito);
87 
88   // Sets |web_contents| as the page to communicate with. |web_contents| may be
89   // NULL, which effectively stops all communication.
90   void SetContents(content::WebContents* web_contents);
91 
delegate()92   Delegate* delegate() const { return delegate_; }
93 
profile()94   Profile* profile() const { return profile_; }
95 
96   // These functions are called before processing messages received from the
97   // page. By default, all messages are handled, but any derived classes may
98   // choose to ignore some or all of the received messages by overriding these
99   // methods.
100   virtual bool ShouldProcessAboutToNavigateMainFrame();
101 
102  private:
103   FRIEND_TEST_ALL_PREFIXES(InstantPageTest, IsLocal);
104   FRIEND_TEST_ALL_PREFIXES(InstantPageTest,
105                            DetermineIfPageSupportsInstant_Local);
106   FRIEND_TEST_ALL_PREFIXES(InstantPageTest,
107                            DetermineIfPageSupportsInstant_NonLocal);
108   FRIEND_TEST_ALL_PREFIXES(InstantPageTest,
109                            PageURLDoesntBelongToInstantRenderer);
110   FRIEND_TEST_ALL_PREFIXES(InstantPageTest, PageSupportsInstant);
111   FRIEND_TEST_ALL_PREFIXES(InstantPageTest,
112                            AppropriateMessagesSentToIncognitoPages);
113 
114   // Overridden from content::WebContentsObserver:
115   virtual void DidCommitProvisionalLoadForFrame(
116       int64 frame_id,
117       const base::string16& frame_unique_name,
118       bool is_main_frame,
119       const GURL& url,
120       content::PageTransition transition_type,
121       content::RenderViewHost* render_view_host) OVERRIDE;
122   virtual void DidNavigateMainFrame(
123       const content::LoadCommittedDetails& details,
124       const content::FrameNavigateParams& params) OVERRIDE;
125   virtual void DidFailProvisionalLoad(
126       int64 frame_id,
127       const base::string16& frame_unique_name,
128       bool is_main_frame,
129       const GURL& validated_url,
130       int error_code,
131       const base::string16& error_description,
132       content::RenderViewHost* render_view_host) OVERRIDE;
133 
134   // Overridden from SearchModelObserver:
135   virtual void ModelChanged(const SearchModel::State& old_state,
136                             const SearchModel::State& new_state) OVERRIDE;
137 
138   // Update the status of Instant support.
139   void InstantSupportDetermined(bool supports_instant);
140 
141   void ClearContents();
142 
143   // TODO(kmadhusu): Remove |profile_| from here and update InstantNTP to get
144   // |profile| from InstantNTPPrerenderer.
145   Profile* profile_;
146 
147   Delegate* const delegate_;
148   scoped_ptr<InstantIPCSender> ipc_sender_;
149   const std::string instant_url_;
150   const bool is_incognito_;
151 
152   DISALLOW_COPY_AND_ASSIGN(InstantPage);
153 };
154 
155 #endif  // CHROME_BROWSER_UI_SEARCH_INSTANT_PAGE_H_
156