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_NTP_PRERENDERER_H_
      6 #define CHROME_BROWSER_UI_SEARCH_INSTANT_NTP_PRERENDERER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/compiler_specific.h"
     11 #include "base/gtest_prod_util.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/prefs/pref_change_registrar.h"
     14 #include "chrome/browser/search/instant_service_observer.h"
     15 #include "chrome/browser/ui/search/instant_page.h"
     16 #include "content/public/browser/web_contents.h"
     17 #include "net/base/network_change_notifier.h"
     18 
     19 class InstantNTP;
     20 class InstantService;
     21 class PrefService;
     22 class Profile;
     23 
     24 // InstantNTPPrerenderer maintains a prerendered instance of InstantNTP.
     25 //
     26 // An InstantNTP instance is a preloaded search page that will be swapped-in the
     27 // next time when the user navigates to the New Tab Page. It is never shown to
     28 // the user in an uncommitted state. It is backed by a WebContents and that is
     29 // owned by InstantNTP.
     30 //
     31 // InstantNTPPrerenderer is owned by InstantService.
     32 class InstantNTPPrerenderer
     33     : public InstantPage::Delegate,
     34       public net::NetworkChangeNotifier::NetworkChangeObserver,
     35       public InstantServiceObserver {
     36  public:
     37   InstantNTPPrerenderer(Profile* profile, InstantService* instant_service,
     38       PrefService* prefs);
     39   virtual ~InstantNTPPrerenderer();
     40 
     41   // Preloads |ntp_| with a new InstantNTP.
     42   void ReloadInstantNTP();
     43 
     44   // Releases and returns the InstantNTP WebContents. May be NULL. Loads a new
     45   // WebContents for the InstantNTP.
     46   scoped_ptr<content::WebContents> ReleaseNTPContents() WARN_UNUSED_RESULT;
     47 
     48   // The NTP WebContents. May be NULL. InstantNTPPrerenderer retains ownership.
     49   content::WebContents* GetNTPContents() const;
     50 
     51   // Invoked to null out |ntp_|.
     52   void DeleteNTPContents();
     53 
     54   // Invoked when the InstantNTP renderer process crashes.
     55   void RenderProcessGone();
     56 
     57   // Invoked when the |ntp_| main frame load completes.
     58   void LoadCompletedMainFrame();
     59 
     60  protected:
     61   // Returns the local Instant URL. (Just a convenience wrapper to get the local
     62   // Instant URL from InstantService.)
     63   virtual std::string GetLocalInstantURL() const;
     64 
     65   // Returns the correct Instant URL to use from the following possibilities:
     66   //     o The default search engine's Instant URL.
     67   //     o The local page (see GetLocalInstantURL())
     68   // Returns an empty string if no valid Instant URL is available (this is only
     69   // possible in non-extended mode where we don't have a local page fall-back).
     70   virtual std::string GetInstantURL() const;
     71 
     72   // Returns true if Javascript is enabled and false otherwise.
     73   virtual bool IsJavascriptEnabled() const;
     74 
     75   // Returns true if the browser is in startup.
     76   virtual bool InStartup() const;
     77 
     78   // Accessors are made protected for testing purposes.
     79   virtual InstantNTP* ntp() const;
     80 
     81   Profile* profile() const {
     82     return profile_;
     83   }
     84 
     85  private:
     86   friend class InstantExtendedTest;
     87   friend class InstantNTPPrerendererTest;
     88   friend class InstantTestBase;
     89 
     90   FRIEND_TEST_ALL_PREFIXES(InstantExtendedNetworkTest,
     91                            NTPReactsToNetworkChanges);
     92   FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest,
     93                            PrefersRemoteNTPOnStartup);
     94   FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest,
     95                            SwitchesToLocalNTPIfNoInstantSupport);
     96   FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest,
     97                            SwitchesToLocalNTPIfPathBad);
     98   FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest,
     99                            DoesNotSwitchToLocalNTPIfOnCurrentNTP);
    100   FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest,
    101                            DoesNotSwitchToLocalNTPIfOnLocalNTP);
    102   FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest,
    103                            SwitchesToLocalNTPIfJSDisabled);
    104   FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest,
    105                            SwitchesToLocalNTPIfNoNTPReady);
    106   FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest,
    107                            IsJavascriptEnabled);
    108   FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest,
    109                            IsJavascriptEnabledChecksContentSettings);
    110   FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest,
    111                            IsJavascriptEnabledChecksPrefs);
    112   FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest, MANUAL_ShowsGoogleNTP);
    113   FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest,
    114                            MANUAL_SearchesFromFakebox);
    115   FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
    116 
    117   // Overridden from net::NetworkChangeNotifier::NetworkChangeObserver:
    118   // If the network status changes, resets InstantNTP.
    119   virtual void OnNetworkChanged(net::NetworkChangeNotifier::ConnectionType type)
    120       OVERRIDE;
    121 
    122   // Overridden from InstantPage::Delegate:
    123   virtual void InstantSupportDetermined(const content::WebContents* contents,
    124                                         bool supports_instant) OVERRIDE;
    125   virtual void InstantPageAboutToNavigateMainFrame(
    126       const content::WebContents* contents,
    127       const GURL& url) OVERRIDE;
    128   virtual void InstantPageLoadFailed(content::WebContents* contents) OVERRIDE;
    129 
    130   // Overridden from InstantServiceObserver:
    131   virtual void DefaultSearchProviderChanged() OVERRIDE;
    132   virtual void GoogleURLUpdated() OVERRIDE;
    133 
    134   // Recreates |ntp_| using |instant_url|.
    135   void ResetNTP(const std::string& instant_url);
    136 
    137   // Returns true if |ntp_| has an up-to-date Instant URL and supports Instant.
    138   // Note that local URLs will not pass this check.
    139   bool PageIsCurrent() const;
    140 
    141   // Returns true if we should switch to using the local NTP.
    142   bool ShouldSwitchToLocalNTP() const;
    143 
    144   Profile* profile_;
    145 
    146   // Preloaded InstantNTP.
    147   scoped_ptr<InstantNTP> ntp_;
    148 
    149   PrefChangeRegistrar profile_pref_registrar_;
    150 
    151   DISALLOW_COPY_AND_ASSIGN(InstantNTPPrerenderer);
    152 };
    153 
    154 #endif  // CHROME_BROWSER_UI_SEARCH_INSTANT_NTP_PRERENDERER_H_
    155