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_NTP_PRERENDERER_H_
6 #define CHROME_BROWSER_UI_SEARCH_INSTANT_NTP_PRERENDERER_H_
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"
24 // InstantNTPPrerenderer maintains a prerendered instance of InstantNTP.
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.
31 // InstantNTPPrerenderer is owned by InstantService.
32 class InstantNTPPrerenderer
33 : public InstantPage::Delegate,
34 public net::NetworkChangeNotifier::NetworkChangeObserver,
35 public InstantServiceObserver {
37 InstantNTPPrerenderer(Profile* profile, InstantService* instant_service,
39 virtual ~InstantNTPPrerenderer();
41 // Preloads |ntp_| with a new InstantNTP.
42 void ReloadInstantNTP();
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;
48 // The NTP WebContents. May be NULL. InstantNTPPrerenderer retains ownership.
49 content::WebContents* GetNTPContents() const;
51 // Invoked to null out |ntp_|.
52 void DeleteNTPContents();
54 // Invoked when the InstantNTP renderer process crashes.
55 void RenderProcessGone();
57 // Invoked when the |ntp_| main frame load completes.
58 void LoadCompletedMainFrame();
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;
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;
72 // Returns true if Javascript is enabled and false otherwise.
73 virtual bool IsJavascriptEnabled() const;
75 // Returns true if the browser is in startup.
76 virtual bool InStartup() const;
78 // Accessors are made protected for testing purposes.
79 virtual InstantNTP* ntp() const;
81 Profile* profile() const {
86 friend class InstantExtendedTest;
87 friend class InstantNTPPrerendererTest;
88 friend class InstantTestBase;
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);
117 // Overridden from net::NetworkChangeNotifier::NetworkChangeObserver:
118 // If the network status changes, resets InstantNTP.
119 virtual void OnNetworkChanged(net::NetworkChangeNotifier::ConnectionType type)
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;
130 // Overridden from InstantServiceObserver:
131 virtual void DefaultSearchProviderChanged() OVERRIDE;
132 virtual void GoogleURLUpdated() OVERRIDE;
134 // Recreates |ntp_| using |instant_url|.
135 void ResetNTP(const std::string& instant_url);
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;
141 // Returns true if we should switch to using the local NTP.
142 bool ShouldSwitchToLocalNTP() const;
146 // Preloaded InstantNTP.
147 scoped_ptr<InstantNTP> ntp_;
149 PrefChangeRegistrar profile_pref_registrar_;
151 DISALLOW_COPY_AND_ASSIGN(InstantNTPPrerenderer);
154 #endif // CHROME_BROWSER_UI_SEARCH_INSTANT_NTP_PRERENDERER_H_