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/ui/search/instant_page.h" 15 #include "content/public/browser/web_contents.h" 16 #include "net/base/network_change_notifier.h" 17 18 class InstantExtendedTest; 19 class InstantNTP; 20 class InstantService; 21 class InstantTestBase; 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: 36 InstantNTPPrerenderer(Profile* profile, PrefService* prefs); 37 virtual ~InstantNTPPrerenderer(); 38 39 // Preloads |ntp_| with a new InstantNTP. 40 void PreloadInstantNTP(); 41 42 // Releases and returns the InstantNTP WebContents. May be NULL. Loads a new 43 // WebContents for the InstantNTP. 44 scoped_ptr<content::WebContents> ReleaseNTPContents() WARN_UNUSED_RESULT; 45 46 // The NTP WebContents. May be NULL. InstantNTPPrerenderer retains ownership. 47 content::WebContents* GetNTPContents() const; 48 49 // Invoked to null out |ntp_|. 50 void DeleteNTPContents(); 51 52 // Invoked when the InstantNTP renderer process crashes. 53 void RenderProcessGone(); 54 55 // Invoked when the |ntp_| main frame load completes. 56 void LoadCompletedMainFrame(); 57 58 protected: 59 // Returns the local Instant URL. (Just a convenience wrapper to get the local 60 // Instant URL from InstantService.) 61 virtual std::string GetLocalInstantURL() const; 62 63 // Returns the correct Instant URL to use from the following possibilities: 64 // o The default search engine's Instant URL. 65 // o The local page (see GetLocalInstantURL()) 66 // Returns an empty string if no valid Instant URL is available (this is only 67 // possible in non-extended mode where we don't have a local page fall-back). 68 virtual std::string GetInstantURL() const; 69 70 // Returns true if Javascript is enabled and false otherwise. 71 virtual bool IsJavascriptEnabled() const; 72 73 // Returns true if the browser is in startup. 74 virtual bool InStartup() const; 75 76 // Accessors are made protected for testing purposes. 77 virtual InstantNTP* ntp() const; 78 79 Profile* profile() const { 80 return profile_; 81 } 82 83 private: 84 friend class InstantExtendedTest; 85 friend class InstantNTPPrerendererTest; 86 friend class InstantTestBase; 87 88 FRIEND_TEST_ALL_PREFIXES(InstantExtendedNetworkTest, 89 NTPReactsToNetworkChanges); 90 FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest, 91 PrefersRemoteNTPOnStartup); 92 FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest, 93 SwitchesToLocalNTPIfNoInstantSupport); 94 FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest, 95 SwitchesToLocalNTPIfPathBad); 96 FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest, 97 DoesNotSwitchToLocalNTPIfOnCurrentNTP); 98 FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest, 99 DoesNotSwitchToLocalNTPIfOnLocalNTP); 100 FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest, 101 SwitchesToLocalNTPIfJSDisabled); 102 FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest, 103 SwitchesToLocalNTPIfNoNTPReady); 104 FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest, 105 IsJavascriptEnabled); 106 FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest, 107 IsJavascriptEnabledChecksContentSettings); 108 FRIEND_TEST_ALL_PREFIXES(InstantNTPPrerendererTest, 109 IsJavascriptEnabledChecksPrefs); 110 FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest, MANUAL_ShowsGoogleNTP); 111 FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest, 112 MANUAL_SearchesFromFakebox); 113 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation); 114 115 // Overridden from net::NetworkChangeNotifier::NetworkChangeObserver: 116 // If the network status changes, resets InstantNTP. 117 virtual void OnNetworkChanged(net::NetworkChangeNotifier::ConnectionType type) 118 OVERRIDE; 119 120 // Overridden from InstantPage::Delegate: 121 virtual void InstantSupportDetermined(const content::WebContents* contents, 122 bool supports_instant) OVERRIDE; 123 virtual void InstantPageAboutToNavigateMainFrame( 124 const content::WebContents* contents, 125 const GURL& url) OVERRIDE; 126 virtual void FocusOmnibox(const content::WebContents* contents, 127 OmniboxFocusState state) OVERRIDE; 128 virtual void NavigateToURL(const content::WebContents* contents, 129 const GURL& url, 130 content::PageTransition transition, 131 WindowOpenDisposition disposition, 132 bool is_search_type) OVERRIDE; 133 virtual void PasteIntoOmnibox(const content::WebContents* contents, 134 const string16& text) OVERRIDE; 135 virtual void DeleteMostVisitedItem(const GURL& url) OVERRIDE; 136 virtual void UndoMostVisitedDeletion(const GURL& url) OVERRIDE; 137 virtual void UndoAllMostVisitedDeletions() OVERRIDE; 138 virtual void InstantPageLoadFailed(content::WebContents* contents) OVERRIDE; 139 140 // Called when the default search provider changes. Resets InstantNTP. 141 void OnDefaultSearchProviderChanged(const std::string& pref_name); 142 143 // Recreates |ntp_| using |instant_url|. 144 void ResetNTP(const std::string& instant_url); 145 146 // Resets |ntp_| with a new InstantNTP. Called when |ntp_| is stale or when a 147 // pref is changed. 148 void ReloadStaleNTP(); 149 150 // Returns true if |ntp_| has an up-to-date Instant URL and supports Instant. 151 // Note that local URLs will not pass this check. 152 bool PageIsCurrent() const; 153 154 // Returns true if we should switch to using the local NTP. 155 bool ShouldSwitchToLocalNTP() const; 156 157 Profile* profile_; 158 159 // Preloaded InstantNTP. 160 scoped_ptr<InstantNTP> ntp_; 161 162 PrefChangeRegistrar profile_pref_registrar_; 163 164 DISALLOW_COPY_AND_ASSIGN(InstantNTPPrerenderer); 165 }; 166 167 #endif // CHROME_BROWSER_UI_SEARCH_INSTANT_NTP_PRERENDERER_H_ 168