Home | History | Annotate | Download | only in search
      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/strings/string16.h"
     14 #include "chrome/browser/ui/search/search_model_observer.h"
     15 #include "content/public/browser/web_contents_observer.h"
     16 #include "content/public/common/page_transition_types.h"
     17 
     18 class GURL;
     19 class Profile;
     20 
     21 namespace content {
     22 struct FrameNavigateParams;
     23 struct LoadCommittedDetails;
     24 class WebContents;
     25 }
     26 
     27 namespace gfx {
     28 class Rect;
     29 }
     30 
     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;
     47 
     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;
     52 
     53    protected:
     54     virtual ~Delegate();
     55   };
     56 
     57   virtual ~InstantPage();
     58 
     59   // The WebContents corresponding to the page we're talking to. May be NULL.
     60   content::WebContents* contents() const { return web_contents(); }
     61 
     62   // Returns the Instant URL that was loaded for this page. Returns the empty
     63   // string if no URL was explicitly loaded as is the case for InstantTab.
     64   virtual const std::string& instant_url() const;
     65 
     66   // Returns true if the page is known to support the Instant API. This starts
     67   // out false, and is set to true whenever we get any message from the page.
     68   // Once true, it never becomes false (the page isn't expected to drop API
     69   // support suddenly).
     70   virtual bool supports_instant() const;
     71 
     72   // Returns true if the page is the local NTP (i.e. its URL is
     73   // chrome::kChromeSearchLocalNTPURL).
     74   virtual bool IsLocal() const;
     75 
     76  protected:
     77   InstantPage(Delegate* delegate, const std::string& instant_url,
     78               Profile* profile, bool is_incognito);
     79 
     80   // Sets |web_contents| as the page to communicate with. |web_contents| may be
     81   // NULL, which effectively stops all communication.
     82   void SetContents(content::WebContents* web_contents);
     83 
     84   Delegate* delegate() const { return delegate_; }
     85 
     86   Profile* profile() const { return profile_; }
     87 
     88   // These functions are called before processing messages received from the
     89   // page. By default, all messages are handled, but any derived classes may
     90   // choose to ignore some or all of the received messages by overriding these
     91   // methods.
     92   virtual bool ShouldProcessAboutToNavigateMainFrame();
     93 
     94  private:
     95   FRIEND_TEST_ALL_PREFIXES(InstantPageTest, IsLocal);
     96   FRIEND_TEST_ALL_PREFIXES(InstantPageTest,
     97                            DetermineIfPageSupportsInstant_Local);
     98   FRIEND_TEST_ALL_PREFIXES(InstantPageTest,
     99                            DetermineIfPageSupportsInstant_NonLocal);
    100   FRIEND_TEST_ALL_PREFIXES(InstantPageTest,
    101                            PageURLDoesntBelongToInstantRenderer);
    102   FRIEND_TEST_ALL_PREFIXES(InstantPageTest, PageSupportsInstant);
    103 
    104   // Overridden from content::WebContentsObserver:
    105   virtual void DidCommitProvisionalLoadForFrame(
    106       int64 frame_id,
    107       const base::string16& frame_unique_name,
    108       bool is_main_frame,
    109       const GURL& url,
    110       content::PageTransition transition_type,
    111       content::RenderViewHost* render_view_host) OVERRIDE;
    112 
    113   // Overridden from SearchModelObserver:
    114   virtual void ModelChanged(const SearchModel::State& old_state,
    115                             const SearchModel::State& new_state) OVERRIDE;
    116 
    117   // Update the status of Instant support.
    118   void InstantSupportDetermined(bool supports_instant);
    119 
    120   void ClearContents();
    121 
    122   // TODO(kmadhusu): Remove |profile_| from here and update InstantNTP to get
    123   // |profile| from InstantNTPPrerenderer.
    124   Profile* profile_;
    125 
    126   Delegate* const delegate_;
    127   const std::string instant_url_;
    128   const bool is_incognito_;
    129 
    130   DISALLOW_COPY_AND_ASSIGN(InstantPage);
    131 };
    132 
    133 #endif  // CHROME_BROWSER_UI_SEARCH_INSTANT_PAGE_H_
    134