[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors. All rights reserved. |
[email protected] | fd2b9ce | 2010-08-11 04:03:57 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 03bb710 | 2013-03-17 22:44:47 | [diff] [blame] | 5 | #include "chrome/browser/ui/search/instant_controller.h" |
[email protected] | fd2b9ce | 2010-08-11 04:03:57 | [diff] [blame] | 6 | |
[email protected] | 7e03e81 | 2010-11-15 23:01:01 | [diff] [blame] | 7 | #include "base/metrics/histogram.h" |
[email protected] | 2b3ded9 | 2013-06-19 05:48:43 | [diff] [blame] | 8 | #include "base/prefs/pref_service.h" |
[email protected] | f92d6588 | 2013-06-10 22:02:36 | [diff] [blame] | 9 | #include "base/strings/stringprintf.h" |
[email protected] | 1a7ff74 | 2013-07-12 00:26:23 | [diff] [blame^] | 10 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 2b3ded9 | 2013-06-19 05:48:43 | [diff] [blame] | 11 | #include "chrome/browser/content_settings/content_settings_provider.h" |
| 12 | #include "chrome/browser/content_settings/host_content_settings_map.h" |
[email protected] | ba6680f | 2010-11-01 20:35:08 | [diff] [blame] | 13 | #include "chrome/browser/platform_util.h" |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 14 | #include "chrome/browser/profiles/profile.h" |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 15 | #include "chrome/browser/search/instant_service.h" |
| 16 | #include "chrome/browser/search/instant_service_factory.h" |
| 17 | #include "chrome/browser/search/search.h" |
[email protected] | c55364c | 2013-03-06 00:24:43 | [diff] [blame] | 18 | #include "chrome/browser/search_engines/search_terms_data.h" |
[email protected] | 8e5c89a | 2011-06-07 18:13:33 | [diff] [blame] | 19 | #include "chrome/browser/search_engines/template_url_service.h" |
| 20 | #include "chrome/browser/search_engines/template_url_service_factory.h" |
[email protected] | 4418dbb | 2012-10-25 03:21:54 | [diff] [blame] | 21 | #include "chrome/browser/ui/browser_instant_controller.h" |
[email protected] | 03bb710 | 2013-03-17 22:44:47 | [diff] [blame] | 22 | #include "chrome/browser/ui/search/instant_ntp.h" |
[email protected] | 03bb710 | 2013-03-17 22:44:47 | [diff] [blame] | 23 | #include "chrome/browser/ui/search/instant_tab.h" |
[email protected] | 23f02b2 | 2012-10-26 06:08:27 | [diff] [blame] | 24 | #include "chrome/browser/ui/search/search_tab_helper.h" |
[email protected] | fd2b9ce | 2010-08-11 04:03:57 | [diff] [blame] | 25 | #include "chrome/common/chrome_switches.h" |
[email protected] | 2b3ded9 | 2013-06-19 05:48:43 | [diff] [blame] | 26 | #include "chrome/common/content_settings_types.h" |
| 27 | #include "chrome/common/pref_names.h" |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 28 | #include "chrome/common/url_constants.h" |
[email protected] | 40a7e41 | 2013-04-29 18:13:01 | [diff] [blame] | 29 | #include "components/sessions/serialized_navigation_entry.h" |
[email protected] | c55e3b8 | 2012-08-09 15:27:05 | [diff] [blame] | 30 | #include "content/public/browser/navigation_entry.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 31 | #include "content/public/browser/notification_service.h" |
[email protected] | d928589a | 2013-03-14 13:12:39 | [diff] [blame] | 32 | #include "content/public/browser/render_process_host.h" |
[email protected] | 5626b089 | 2012-02-20 14:46:58 | [diff] [blame] | 33 | #include "content/public/browser/render_widget_host_view.h" |
[email protected] | 0987a41 | 2013-03-15 12:21:14 | [diff] [blame] | 34 | #include "content/public/browser/user_metrics.h" |
[email protected] | 3d6a895 | 2012-12-14 03:18:07 | [diff] [blame] | 35 | #include "content/public/browser/web_contents.h" |
[email protected] | f3615f0 | 2013-02-26 06:09:06 | [diff] [blame] | 36 | #include "content/public/browser/web_contents_view.h" |
[email protected] | a7c22e5 | 2012-10-12 19:19:05 | [diff] [blame] | 37 | #include "net/base/escape.h" |
[email protected] | 5aaa17d | 2013-05-06 22:44:34 | [diff] [blame] | 38 | #include "net/base/network_change_notifier.h" |
[email protected] | fd2b9ce | 2010-08-11 04:03:57 | [diff] [blame] | 39 | |
[email protected] | 6eb8ea9 | 2011-08-22 21:01:41 | [diff] [blame] | 40 | #if defined(TOOLKIT_VIEWS) |
[email protected] | c13be0d | 2011-11-22 02:09:58 | [diff] [blame] | 41 | #include "ui/views/widget/widget.h" |
[email protected] | 6eb8ea9 | 2011-08-22 21:01:41 | [diff] [blame] | 42 | #endif |
| 43 | |
[email protected] | c55e3b8 | 2012-08-09 15:27:05 | [diff] [blame] | 44 | namespace { |
| 45 | |
[email protected] | e6af47b | 2013-05-06 06:26:46 | [diff] [blame] | 46 | // For reporting Instant navigations. |
[email protected] | 18956a5 | 2013-04-26 21:37:05 | [diff] [blame] | 47 | enum InstantNavigation { |
| 48 | INSTANT_NAVIGATION_LOCAL_CLICK = 0, |
| 49 | INSTANT_NAVIGATION_LOCAL_SUBMIT = 1, |
| 50 | INSTANT_NAVIGATION_ONLINE_CLICK = 2, |
| 51 | INSTANT_NAVIGATION_ONLINE_SUBMIT = 3, |
[email protected] | e6af47b | 2013-05-06 06:26:46 | [diff] [blame] | 52 | INSTANT_NAVIGATION_NONEXTENDED = 4, |
| 53 | INSTANT_NAVIGATION_MAX = 5 |
[email protected] | 18956a5 | 2013-04-26 21:37:05 | [diff] [blame] | 54 | }; |
| 55 | |
[email protected] | e6af47b | 2013-05-06 06:26:46 | [diff] [blame] | 56 | void RecordNavigationHistogram(bool is_local, bool is_click, bool is_extended) { |
[email protected] | 18956a5 | 2013-04-26 21:37:05 | [diff] [blame] | 57 | InstantNavigation navigation; |
[email protected] | e6af47b | 2013-05-06 06:26:46 | [diff] [blame] | 58 | if (!is_extended) { |
| 59 | navigation = INSTANT_NAVIGATION_NONEXTENDED; |
| 60 | } else if (is_local) { |
| 61 | navigation = is_click ? INSTANT_NAVIGATION_LOCAL_CLICK : |
| 62 | INSTANT_NAVIGATION_LOCAL_SUBMIT; |
[email protected] | 18956a5 | 2013-04-26 21:37:05 | [diff] [blame] | 63 | } else { |
[email protected] | e6af47b | 2013-05-06 06:26:46 | [diff] [blame] | 64 | navigation = is_click ? INSTANT_NAVIGATION_ONLINE_CLICK : |
| 65 | INSTANT_NAVIGATION_ONLINE_SUBMIT; |
[email protected] | 18956a5 | 2013-04-26 21:37:05 | [diff] [blame] | 66 | } |
| 67 | UMA_HISTOGRAM_ENUMERATION("InstantExtended.InstantNavigation", |
| 68 | navigation, |
| 69 | INSTANT_NAVIGATION_MAX); |
| 70 | } |
| 71 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 72 | bool IsContentsFrom(const InstantPage* page, |
| 73 | const content::WebContents* contents) { |
| 74 | return page && (page->contents() == contents); |
| 75 | } |
| 76 | |
[email protected] | 0b68426 | 2013-02-20 02:18:21 | [diff] [blame] | 77 | // Adds a transient NavigationEntry to the supplied |contents|'s |
| 78 | // NavigationController if the page's URL has not already been updated with the |
| 79 | // supplied |search_terms|. Sets the |search_terms| on the transient entry for |
| 80 | // search terms extraction to work correctly. |
| 81 | void EnsureSearchTermsAreSet(content::WebContents* contents, |
| 82 | const string16& search_terms) { |
[email protected] | 988cf72 | 2013-03-08 00:42:00 | [diff] [blame] | 83 | content::NavigationController* controller = &contents->GetController(); |
[email protected] | 0b68426 | 2013-02-20 02:18:21 | [diff] [blame] | 84 | |
| 85 | // If search terms are already correct or there is already a transient entry |
| 86 | // (there shouldn't be), bail out early. |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 87 | if (chrome::GetSearchTerms(contents) == search_terms || |
[email protected] | 988cf72 | 2013-03-08 00:42:00 | [diff] [blame] | 88 | controller->GetTransientEntry()) |
[email protected] | 0b68426 | 2013-02-20 02:18:21 | [diff] [blame] | 89 | return; |
| 90 | |
[email protected] | 988cf72 | 2013-03-08 00:42:00 | [diff] [blame] | 91 | const content::NavigationEntry* active_entry = controller->GetActiveEntry(); |
| 92 | content::NavigationEntry* transient = controller->CreateNavigationEntry( |
| 93 | active_entry->GetURL(), |
| 94 | active_entry->GetReferrer(), |
| 95 | active_entry->GetTransitionType(), |
| 96 | false, |
| 97 | std::string(), |
| 98 | contents->GetBrowserContext()); |
[email protected] | 40a7e41 | 2013-04-29 18:13:01 | [diff] [blame] | 99 | transient->SetExtraData(sessions::kSearchTermsKey, search_terms); |
[email protected] | 988cf72 | 2013-03-08 00:42:00 | [diff] [blame] | 100 | controller->SetTransientEntry(transient); |
[email protected] | 0b68426 | 2013-02-20 02:18:21 | [diff] [blame] | 101 | |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 102 | SearchTabHelper::FromWebContents(contents)->NavigationEntryUpdated(); |
[email protected] | 0b68426 | 2013-02-20 02:18:21 | [diff] [blame] | 103 | } |
| 104 | |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 105 | template <typename T> |
| 106 | void DeletePageSoon(scoped_ptr<T> page) { |
[email protected] | 30d75a0 | 2013-05-24 23:05:14 | [diff] [blame] | 107 | if (page->contents()) { |
| 108 | base::MessageLoop::current()->DeleteSoon( |
| 109 | FROM_HERE, page->ReleaseContents().release()); |
| 110 | } |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 111 | |
[email protected] | 3264d16 | 2013-05-08 22:02:11 | [diff] [blame] | 112 | base::MessageLoop::current()->DeleteSoon(FROM_HERE, page.release()); |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 113 | } |
| 114 | |
[email protected] | b67d0a4 | 2012-09-04 20:57:35 | [diff] [blame] | 115 | } // namespace |
| 116 | |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 117 | InstantController::InstantController(BrowserInstantController* browser, |
[email protected] | 411c9c0 | 2013-02-07 04:55:59 | [diff] [blame] | 118 | bool extended_enabled) |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 119 | : browser_(browser), |
| 120 | extended_enabled_(extended_enabled), |
[email protected] | 6a6811a | 2012-12-12 04:18:16 | [diff] [blame] | 121 | omnibox_focus_state_(OMNIBOX_FOCUS_NONE), |
[email protected] | 8d4c276d | 2013-05-29 19:39:52 | [diff] [blame] | 122 | omnibox_focus_change_reason_(OMNIBOX_FOCUS_CHANGE_EXPLICIT), |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 123 | omnibox_bounds_(-1, -1, 0, 0) { |
[email protected] | 9c349e8 | 2013-03-12 00:52:53 | [diff] [blame] | 124 | |
| 125 | // When the InstantController lives, the InstantService should live. |
| 126 | // InstantService sets up profile-level facilities such as the ThemeSource for |
| 127 | // the NTP. |
[email protected] | 4facb8b | 2013-05-23 21:01:45 | [diff] [blame] | 128 | // However, in some tests, browser_ may be null. |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 129 | if (browser_) { |
| 130 | InstantService* instant_service = GetInstantService(); |
| 131 | instant_service->AddObserver(this); |
| 132 | } |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | 6b723f8 | 2010-10-05 20:14:27 | [diff] [blame] | 135 | InstantController::~InstantController() { |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 136 | if (browser_) { |
| 137 | InstantService* instant_service = GetInstantService(); |
| 138 | instant_service->RemoveObserver(this); |
| 139 | } |
[email protected] | 03bb953d | 2010-09-14 21:38:30 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 142 | scoped_ptr<content::WebContents> InstantController::ReleaseNTPContents() { |
[email protected] | 4facb8b | 2013-05-23 21:01:45 | [diff] [blame] | 143 | if (!extended_enabled() || !browser_->profile() || |
[email protected] | 51742d9 | 2013-06-21 03:58:14 | [diff] [blame] | 144 | browser_->profile()->IsOffTheRecord() || |
| 145 | !chrome::ShouldShowInstantNTP()) |
[email protected] | 4b4c013 | 2013-06-12 17:58:55 | [diff] [blame] | 146 | return scoped_ptr<content::WebContents>(); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 147 | |
| 148 | LOG_INSTANT_DEBUG_EVENT(this, "ReleaseNTPContents"); |
| 149 | |
[email protected] | 01a07a1d | 2013-04-19 23:02:14 | [diff] [blame] | 150 | if (ShouldSwitchToLocalNTP()) |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 151 | ResetNTP(GetLocalInstantURL()); |
[email protected] | 453b6eb7 | 2013-03-24 03:13:37 | [diff] [blame] | 152 | |
[email protected] | b82d265 | 2013-03-29 19:44:44 | [diff] [blame] | 153 | scoped_ptr<content::WebContents> ntp_contents = ntp_->ReleaseContents(); |
[email protected] | 453b6eb7 | 2013-03-24 03:13:37 | [diff] [blame] | 154 | |
[email protected] | eab7c1bc | 2013-05-07 03:05:28 | [diff] [blame] | 155 | // Preload a new Instant NTP. |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 156 | ResetNTP(GetInstantURL()); |
[email protected] | eab7c1bc | 2013-05-07 03:05:28 | [diff] [blame] | 157 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 158 | return ntp_contents.Pass(); |
| 159 | } |
| 160 | |
[email protected] | fcde79a | 2013-02-28 02:25:09 | [diff] [blame] | 161 | void InstantController::SetOmniboxBounds(const gfx::Rect& bounds) { |
[email protected] | 4facb8b | 2013-05-23 21:01:45 | [diff] [blame] | 162 | if (!extended_enabled() || omnibox_bounds_ == bounds) |
[email protected] | ec4aad54 | 2012-12-14 01:11:04 | [diff] [blame] | 163 | return; |
| 164 | |
[email protected] | fcde79a | 2013-02-28 02:25:09 | [diff] [blame] | 165 | omnibox_bounds_ = bounds; |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 166 | if (ntp_) |
[email protected] | 215ebc4 | 2013-06-22 00:24:50 | [diff] [blame] | 167 | ntp_->sender()->SetOmniboxBounds(omnibox_bounds_); |
[email protected] | ec4aad54 | 2012-12-14 01:11:04 | [diff] [blame] | 168 | if (instant_tab_) |
[email protected] | 215ebc4 | 2013-06-22 00:24:50 | [diff] [blame] | 169 | instant_tab_->sender()->SetOmniboxBounds(omnibox_bounds_); |
[email protected] | ec4aad54 | 2012-12-14 01:11:04 | [diff] [blame] | 170 | } |
| 171 | |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 172 | void InstantController::OnDefaultSearchProviderChanged() { |
[email protected] | 4facb8b | 2013-05-23 21:01:45 | [diff] [blame] | 173 | if (ntp_ && extended_enabled()) { |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 174 | ntp_.reset(); |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 175 | ResetNTP(GetInstantURL()); |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 176 | } |
[email protected] | bdb3038 | 2013-04-12 00:39:34 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | 3473ae0 | 2013-06-07 00:28:08 | [diff] [blame] | 179 | void InstantController::ToggleVoiceSearch() { |
| 180 | if (instant_tab_) |
[email protected] | 215ebc4 | 2013-06-22 00:24:50 | [diff] [blame] | 181 | instant_tab_->sender()->ToggleVoiceSearch(); |
[email protected] | 3473ae0 | 2013-06-07 00:28:08 | [diff] [blame] | 182 | } |
| 183 | |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 184 | void InstantController::InstantPageLoadFailed(content::WebContents* contents) { |
[email protected] | 4facb8b | 2013-05-23 21:01:45 | [diff] [blame] | 185 | if (!chrome::ShouldPreferRemoteNTPOnStartup() || !extended_enabled()) { |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 186 | // We only need to fall back on errors if we're showing the online page |
| 187 | // at startup, as otherwise we fall back correctly when trying to show |
| 188 | // a page that hasn't yet indicated that it supports the InstantExtended |
| 189 | // API. |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | if (IsContentsFrom(instant_tab(), contents)) { |
| 194 | // Verify we're not already on a local page and that the URL precisely |
| 195 | // equals the instant_url (minus the query params, as those will be filled |
| 196 | // in by template values). This check is necessary to make sure we don't |
| 197 | // inadvertently redirect to the local NTP if someone, say, reloads a SRP |
| 198 | // while offline, as a committed results page still counts as an instant |
| 199 | // url. We also check to make sure there's no forward history, as if |
| 200 | // someone hits the back button a lot when offline and returns to a NTP |
| 201 | // we don't want to redirect and nuke their forward history stack. |
| 202 | const GURL& current_url = contents->GetURL(); |
| 203 | if (instant_tab_->IsLocal() || |
| 204 | !chrome::MatchesOriginAndPath(GURL(GetInstantURL()), current_url) || |
| 205 | !current_url.ref().empty() || |
| 206 | contents->GetController().CanGoForward()) |
| 207 | return; |
| 208 | LOG_INSTANT_DEBUG_EVENT(this, "InstantPageLoadFailed: instant_tab"); |
| 209 | RedirectToLocalNTP(contents); |
| 210 | } else if (IsContentsFrom(ntp(), contents)) { |
| 211 | LOG_INSTANT_DEBUG_EVENT(this, "InstantPageLoadFailed: ntp"); |
| 212 | bool is_local = ntp_->IsLocal(); |
| 213 | DeletePageSoon(ntp_.Pass()); |
| 214 | if (!is_local) |
| 215 | ResetNTP(GetLocalInstantURL()); |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 216 | } else { |
| 217 | NOTREACHED(); |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
[email protected] | 364c850 | 2013-06-06 05:28:16 | [diff] [blame] | 221 | content::WebContents* InstantController::GetNTPContents() const { |
| 222 | return ntp_ ? ntp_->contents() : NULL; |
| 223 | } |
| 224 | |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 225 | bool InstantController::SubmitQuery(const string16& search_terms) { |
| 226 | if (extended_enabled() && instant_tab_ && instant_tab_->supports_instant() && |
| 227 | search_mode_.is_origin_search()) { |
| 228 | // Use |instant_tab_| to run the query if we're already on a search results |
| 229 | // page. (NOTE: in particular, we do not send the query to NTPs.) |
[email protected] | 215ebc4 | 2013-06-22 00:24:50 | [diff] [blame] | 230 | instant_tab_->sender()->Submit(search_terms); |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 231 | instant_tab_->contents()->GetView()->Focus(); |
| 232 | EnsureSearchTermsAreSet(instant_tab_->contents(), search_terms); |
| 233 | return true; |
| 234 | } |
| 235 | return false; |
| 236 | } |
| 237 | |
[email protected] | 6a6811a | 2012-12-12 04:18:16 | [diff] [blame] | 238 | void InstantController::OmniboxFocusChanged( |
| 239 | OmniboxFocusState state, |
| 240 | OmniboxFocusChangeReason reason, |
| 241 | gfx::NativeView view_gaining_focus) { |
[email protected] | b97059d | 2013-01-26 00:06:40 | [diff] [blame] | 242 | LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( |
| 243 | "OmniboxFocusChanged: %d to %d for reason %d", omnibox_focus_state_, |
| 244 | state, reason)); |
[email protected] | 484ae591 | 2010-09-29 19:16:14 | [diff] [blame] | 245 | |
[email protected] | 6a6811a | 2012-12-12 04:18:16 | [diff] [blame] | 246 | omnibox_focus_state_ = state; |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 247 | if (!extended_enabled() || !instant_tab_) |
[email protected] | c55e3b8 | 2012-08-09 15:27:05 | [diff] [blame] | 248 | return; |
[email protected] | 484ae591 | 2010-09-29 19:16:14 | [diff] [blame] | 249 | |
[email protected] | 7d4c1daa | 2013-07-03 16:16:19 | [diff] [blame] | 250 | content::NotificationService::current()->Notify( |
| 251 | chrome::NOTIFICATION_OMNIBOX_FOCUS_CHANGED, |
| 252 | content::Source<InstantController>(this), |
| 253 | content::NotificationService::NoDetails()); |
| 254 | |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 255 | instant_tab_->sender()->FocusChanged(omnibox_focus_state_, reason); |
| 256 | // Don't send oninputstart/oninputend updates in response to focus changes |
| 257 | // if there's a navigation in progress. This prevents Chrome from sending |
| 258 | // a spurious oninputend when the user accepts a match in the omnibox. |
| 259 | if (instant_tab_->contents()->GetController().GetPendingEntry() == NULL) |
| 260 | instant_tab_->sender()->SetInputInProgress(IsInputInProgress()); |
[email protected] | f2557bd | 2011-06-01 02:33:07 | [diff] [blame] | 261 | } |
| 262 | |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 263 | void InstantController::SearchModeChanged(const SearchMode& old_mode, |
| 264 | const SearchMode& new_mode) { |
[email protected] | 4facb8b | 2013-05-23 21:01:45 | [diff] [blame] | 265 | if (!extended_enabled()) |
[email protected] | 249a035 | 2012-11-26 21:06:19 | [diff] [blame] | 266 | return; |
| 267 | |
[email protected] | b97059d | 2013-01-26 00:06:40 | [diff] [blame] | 268 | LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( |
| 269 | "SearchModeChanged: [origin:mode] %d:%d to %d:%d", old_mode.origin, |
| 270 | old_mode.mode, new_mode.origin, new_mode.mode)); |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 271 | |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 272 | search_mode_ = new_mode; |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 273 | ResetInstantTab(); |
[email protected] | 0d0b4a4 | 2013-06-14 00:46:26 | [diff] [blame] | 274 | |
[email protected] | 274b42c | 2013-06-18 11:54:52 | [diff] [blame] | 275 | if (instant_tab_ && old_mode.is_ntp() != new_mode.is_ntp()) |
[email protected] | 215ebc4 | 2013-06-22 00:24:50 | [diff] [blame] | 276 | instant_tab_->sender()->SetInputInProgress(IsInputInProgress()); |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 277 | } |
| 278 | |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 279 | void InstantController::ActiveTabChanged() { |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 280 | if (!extended_enabled()) |
[email protected] | 249a035 | 2012-11-26 21:06:19 | [diff] [blame] | 281 | return; |
| 282 | |
[email protected] | b97059d | 2013-01-26 00:06:40 | [diff] [blame] | 283 | LOG_INSTANT_DEBUG_EVENT(this, "ActiveTabChanged"); |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 284 | ResetInstantTab(); |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 285 | } |
| 286 | |
[email protected] | 3d6a895 | 2012-12-14 03:18:07 | [diff] [blame] | 287 | void InstantController::TabDeactivated(content::WebContents* contents) { |
[email protected] | ebc8046 | 2013-07-08 18:47:14 | [diff] [blame] | 288 | // If user is deactivating an NTP tab, log the number of mouseovers for this |
| 289 | // NTP session. |
| 290 | if (chrome::IsInstantNTP(contents)) |
| 291 | InstantNTP::EmitMouseoverCount(contents); |
[email protected] | a0b8466 | 2010-10-04 23:22:04 | [diff] [blame] | 292 | } |
| 293 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 294 | void InstantController::ThemeInfoChanged( |
| 295 | const ThemeBackgroundInfo& theme_info) { |
[email protected] | 4facb8b | 2013-05-23 21:01:45 | [diff] [blame] | 296 | if (!extended_enabled()) |
[email protected] | 249a035 | 2012-11-26 21:06:19 | [diff] [blame] | 297 | return; |
| 298 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 299 | if (ntp_) |
[email protected] | 215ebc4 | 2013-06-22 00:24:50 | [diff] [blame] | 300 | ntp_->sender()->SendThemeBackgroundInfo(theme_info); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 301 | if (instant_tab_) |
[email protected] | 215ebc4 | 2013-06-22 00:24:50 | [diff] [blame] | 302 | instant_tab_->sender()->SendThemeBackgroundInfo(theme_info); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 303 | } |
| 304 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 305 | void InstantController::LogDebugEvent(const std::string& info) const { |
| 306 | DVLOG(1) << info; |
| 307 | |
| 308 | debug_events_.push_front(std::make_pair( |
| 309 | base::Time::Now().ToInternalValue(), info)); |
| 310 | static const size_t kMaxDebugEventSize = 2000; |
| 311 | if (debug_events_.size() > kMaxDebugEventSize) |
| 312 | debug_events_.pop_back(); |
| 313 | } |
| 314 | |
[email protected] | 66be5181 | 2013-03-05 22:28:09 | [diff] [blame] | 315 | void InstantController::ClearDebugEvents() { |
| 316 | debug_events_.clear(); |
| 317 | } |
| 318 | |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 319 | void InstantController::MostVisitedItemsChanged( |
| 320 | const std::vector<InstantMostVisitedItem>& items) { |
[email protected] | 723eaf48 | 2013-06-22 20:28:11 | [diff] [blame] | 321 | if (ntp_) |
[email protected] | 215ebc4 | 2013-06-22 00:24:50 | [diff] [blame] | 322 | ntp_->sender()->SendMostVisitedItems(items); |
[email protected] | 723eaf48 | 2013-06-22 20:28:11 | [diff] [blame] | 323 | if (instant_tab_) |
[email protected] | 215ebc4 | 2013-06-22 00:24:50 | [diff] [blame] | 324 | instant_tab_->sender()->SendMostVisitedItems(items); |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 325 | |
| 326 | content::NotificationService::current()->Notify( |
| 327 | chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS, |
| 328 | content::Source<InstantController>(this), |
| 329 | content::NotificationService::NoDetails()); |
[email protected] | dfee2f6 | 2013-02-22 06:46:06 | [diff] [blame] | 330 | } |
| 331 | |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 332 | void InstantController::DeleteMostVisitedItem(const GURL& url) { |
| 333 | DCHECK(!url.is_empty()); |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 334 | InstantService* instant_service = GetInstantService(); |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 335 | if (!instant_service) |
[email protected] | dfee2f6 | 2013-02-22 06:46:06 | [diff] [blame] | 336 | return; |
| 337 | |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 338 | instant_service->DeleteMostVisitedItem(url); |
| 339 | } |
| 340 | |
| 341 | void InstantController::UndoMostVisitedDeletion(const GURL& url) { |
| 342 | DCHECK(!url.is_empty()); |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 343 | InstantService* instant_service = GetInstantService(); |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 344 | if (!instant_service) |
| 345 | return; |
| 346 | |
| 347 | instant_service->UndoMostVisitedDeletion(url); |
[email protected] | dfee2f6 | 2013-02-22 06:46:06 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void InstantController::UndoAllMostVisitedDeletions() { |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 351 | InstantService* instant_service = GetInstantService(); |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 352 | if (!instant_service) |
[email protected] | dfee2f6 | 2013-02-22 06:46:06 | [diff] [blame] | 353 | return; |
| 354 | |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 355 | instant_service->UndoAllMostVisitedDeletions(); |
[email protected] | dfee2f6 | 2013-02-22 06:46:06 | [diff] [blame] | 356 | } |
| 357 | |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 358 | Profile* InstantController::profile() const { |
| 359 | return browser_->profile(); |
| 360 | } |
| 361 | |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 362 | InstantTab* InstantController::instant_tab() const { |
| 363 | return instant_tab_.get(); |
| 364 | } |
| 365 | |
| 366 | InstantNTP* InstantController::ntp() const { |
| 367 | return ntp_.get(); |
| 368 | } |
| 369 | |
[email protected] | f2bb4ef | 2013-07-01 19:54:13 | [diff] [blame] | 370 | void InstantController::OnNetworkChanged( |
| 371 | net::NetworkChangeNotifier::ConnectionType type) { |
| 372 | // Not interested in events conveying change to offline |
| 373 | if (type == net::NetworkChangeNotifier::CONNECTION_NONE) |
| 374 | return; |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 375 | if (!extended_enabled_) |
[email protected] | f2bb4ef | 2013-07-01 19:54:13 | [diff] [blame] | 376 | return; |
| 377 | if (!ntp_ || ntp_->IsLocal()) |
| 378 | ResetNTP(GetInstantURL()); |
| 379 | } |
| 380 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 381 | // TODO(shishir): We assume that the WebContent's current RenderViewHost is the |
| 382 | // RenderViewHost being created which is not always true. Fix this. |
| 383 | void InstantController::InstantPageRenderViewCreated( |
| 384 | const content::WebContents* contents) { |
[email protected] | 4facb8b | 2013-05-23 21:01:45 | [diff] [blame] | 385 | if (!extended_enabled()) |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 386 | return; |
| 387 | |
| 388 | // Update theme info so that the page picks it up. |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 389 | InstantService* instant_service = GetInstantService(); |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 390 | if (instant_service) { |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 391 | instant_service->UpdateThemeInfo(); |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 392 | instant_service->UpdateMostVisitedItemsInfo(); |
| 393 | } |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 394 | |
| 395 | // Ensure the searchbox API has the correct initial state. |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 396 | if (IsContentsFrom(ntp(), contents)) { |
[email protected] | 215ebc4 | 2013-06-22 00:24:50 | [diff] [blame] | 397 | ntp_->sender()->SetOmniboxBounds(omnibox_bounds_); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 398 | ntp_->InitializeFonts(); |
[email protected] | 43132fbd | 2013-06-26 02:18:03 | [diff] [blame] | 399 | ntp_->InitializePromos(); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 400 | } else { |
| 401 | NOTREACHED(); |
| 402 | } |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 403 | } |
| 404 | |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 405 | void InstantController::InstantSupportChanged( |
| 406 | InstantSupportState instant_support) { |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 407 | // Handle INSTANT_SUPPORT_YES here because InstantPage is not hooked up to the |
| 408 | // active tab. Search model changed listener in InstantPage will handle other |
| 409 | // cases. |
| 410 | if (instant_support != INSTANT_SUPPORT_YES) |
| 411 | return; |
| 412 | |
| 413 | ResetInstantTab(); |
| 414 | } |
| 415 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 416 | void InstantController::InstantSupportDetermined( |
| 417 | const content::WebContents* contents, |
| 418 | bool supports_instant) { |
| 419 | if (IsContentsFrom(instant_tab(), contents)) { |
| 420 | if (!supports_instant) |
[email protected] | 3264d16 | 2013-05-08 22:02:11 | [diff] [blame] | 421 | base::MessageLoop::current()->DeleteSoon(FROM_HERE, |
| 422 | instant_tab_.release()); |
[email protected] | ab3e6667 | 2013-03-20 23:32:16 | [diff] [blame] | 423 | |
| 424 | content::NotificationService::current()->Notify( |
| 425 | chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED, |
| 426 | content::Source<InstantController>(this), |
| 427 | content::NotificationService::NoDetails()); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 428 | } else if (IsContentsFrom(ntp(), contents)) { |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 429 | if (!supports_instant) { |
| 430 | bool is_local = ntp_->IsLocal(); |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 431 | DeletePageSoon(ntp_.Pass()); |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 432 | // Preload a local NTP in place of the broken online one. |
| 433 | if (!is_local) |
| 434 | ResetNTP(GetLocalInstantURL()); |
| 435 | } |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 436 | |
| 437 | content::NotificationService::current()->Notify( |
| 438 | chrome::NOTIFICATION_INSTANT_NTP_SUPPORT_DETERMINED, |
| 439 | content::Source<InstantController>(this), |
| 440 | content::NotificationService::NoDetails()); |
| 441 | |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 442 | } else { |
| 443 | NOTREACHED(); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
[email protected] | 58d5cfe | 2013-07-10 02:40:52 | [diff] [blame] | 447 | void InstantController::InstantPageRenderProcessGone( |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 448 | const content::WebContents* contents) { |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 449 | if (IsContentsFrom(ntp(), contents)) { |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 450 | DeletePageSoon(ntp_.Pass()); |
| 451 | } else { |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 452 | NOTREACHED(); |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 453 | } |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | void InstantController::InstantPageAboutToNavigateMainFrame( |
| 457 | const content::WebContents* contents, |
| 458 | const GURL& url) { |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 459 | if (IsContentsFrom(instant_tab(), contents)) { |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 460 | // The Instant tab navigated. Send it the data it needs to display |
| 461 | // properly. |
| 462 | UpdateInfoForInstantTab(); |
| 463 | } else { |
| 464 | NOTREACHED(); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 465 | } |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 466 | } |
| 467 | |
[email protected] | ca98bd3 | 2013-04-09 05:16:05 | [diff] [blame] | 468 | void InstantController::FocusOmnibox(const content::WebContents* contents, |
| 469 | OmniboxFocusState state) { |
[email protected] | 4facb8b | 2013-05-23 21:01:45 | [diff] [blame] | 470 | if (!extended_enabled()) |
[email protected] | 10aec59 | 2013-03-06 20:19:00 | [diff] [blame] | 471 | return; |
| 472 | |
| 473 | DCHECK(IsContentsFrom(instant_tab(), contents)); |
[email protected] | 10aec59 | 2013-03-06 20:19:00 | [diff] [blame] | 474 | |
[email protected] | ca98bd3 | 2013-04-09 05:16:05 | [diff] [blame] | 475 | // Do not add a default case in the switch block for the following reasons: |
| 476 | // (1) Explicitly handle the new states. If new states are added in the |
| 477 | // OmniboxFocusState, the compiler will warn the developer to handle the new |
| 478 | // states. |
| 479 | // (2) An attacker may control the renderer and sends the browser process a |
| 480 | // malformed IPC. This function responds to the invalid |state| values by |
| 481 | // doing nothing instead of crashing the browser process (intentional no-op). |
| 482 | switch (state) { |
| 483 | case OMNIBOX_FOCUS_VISIBLE: |
[email protected] | 2ff3ed4 | 2013-05-30 03:23:41 | [diff] [blame] | 484 | // TODO(samarth): re-enable this once setValue() correctly handles |
| 485 | // URL-shaped queries. |
| 486 | // browser_->FocusOmnibox(true); |
[email protected] | ca98bd3 | 2013-04-09 05:16:05 | [diff] [blame] | 487 | break; |
| 488 | case OMNIBOX_FOCUS_INVISIBLE: |
| 489 | browser_->FocusOmnibox(false); |
| 490 | break; |
| 491 | case OMNIBOX_FOCUS_NONE: |
| 492 | if (omnibox_focus_state_ != OMNIBOX_FOCUS_INVISIBLE) |
| 493 | contents->GetView()->Focus(); |
| 494 | break; |
| 495 | } |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | void InstantController::NavigateToURL(const content::WebContents* contents, |
| 499 | const GURL& url, |
[email protected] | 3c3acca | 2013-02-26 03:07:07 | [diff] [blame] | 500 | content::PageTransition transition, |
[email protected] | 63bd6864 | 2013-05-13 23:02:55 | [diff] [blame] | 501 | WindowOpenDisposition disposition, |
| 502 | bool is_search_type) { |
[email protected] | 3c3acca | 2013-02-26 03:07:07 | [diff] [blame] | 503 | LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( |
| 504 | "NavigateToURL: url='%s'", url.spec().c_str())); |
| 505 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 506 | // TODO(samarth): handle case where contents are no longer "active" (e.g. user |
| 507 | // has switched tabs). |
[email protected] | 4facb8b | 2013-05-23 21:01:45 | [diff] [blame] | 508 | if (!extended_enabled()) |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 509 | return; |
[email protected] | 0987a41 | 2013-03-15 12:21:14 | [diff] [blame] | 510 | |
| 511 | if (transition == content::PAGE_TRANSITION_AUTO_BOOKMARK) { |
| 512 | content::RecordAction( |
| 513 | content::UserMetricsAction("InstantExtended.MostVisitedClicked")); |
[email protected] | e6af47b | 2013-05-06 06:26:46 | [diff] [blame] | 514 | } else { |
[email protected] | 63bd6864 | 2013-05-13 23:02:55 | [diff] [blame] | 515 | // Exclude navigation by Most Visited click and searches. |
| 516 | if (!is_search_type) |
| 517 | RecordNavigationHistogram(UsingLocalPage(), true, true); |
[email protected] | 0987a41 | 2013-03-15 12:21:14 | [diff] [blame] | 518 | } |
[email protected] | 3c3acca | 2013-02-26 03:07:07 | [diff] [blame] | 519 | browser_->OpenURL(url, transition, disposition); |
[email protected] | 30889c2 | 2013-02-04 21:27:56 | [diff] [blame] | 520 | } |
| 521 | |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 522 | std::string InstantController::GetLocalInstantURL() const { |
| 523 | return chrome::GetLocalInstantURL(profile()).spec(); |
[email protected] | e3033eb | 2012-12-13 23:46:08 | [diff] [blame] | 524 | } |
| 525 | |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 526 | std::string InstantController::GetInstantURL() const { |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 527 | if (extended_enabled() && net::NetworkChangeNotifier::IsOffline()) |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 528 | return GetLocalInstantURL(); |
| 529 | |
| 530 | const GURL instant_url = chrome::GetInstantURL(profile(), |
| 531 | omnibox_bounds_.x()); |
| 532 | if (instant_url.is_valid()) |
| 533 | return instant_url.spec(); |
| 534 | |
| 535 | // Only extended mode has a local fallback. |
[email protected] | 4facb8b | 2013-05-23 21:01:45 | [diff] [blame] | 536 | return extended_enabled() ? GetLocalInstantURL() : std::string(); |
| 537 | } |
| 538 | |
| 539 | bool InstantController::extended_enabled() const { |
| 540 | return extended_enabled_; |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | bool InstantController::PageIsCurrent(const InstantPage* page) const { |
| 544 | |
| 545 | const std::string& instant_url = GetInstantURL(); |
| 546 | if (instant_url.empty() || |
| 547 | !chrome::MatchesOriginAndPath(GURL(page->instant_url()), |
| 548 | GURL(instant_url))) |
[email protected] | e7a80bd | 2013-01-25 06:53:41 | [diff] [blame] | 549 | return false; |
[email protected] | ed6e37b | 2012-12-16 06:36:51 | [diff] [blame] | 550 | |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 551 | return page->supports_instant(); |
[email protected] | e7a80bd | 2013-01-25 06:53:41 | [diff] [blame] | 552 | } |
| 553 | |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 554 | void InstantController::ResetNTP(const std::string& instant_url) { |
[email protected] | 51742d9 | 2013-06-21 03:58:14 | [diff] [blame] | 555 | // Never load the Instant NTP if it is disabled. |
| 556 | if (!chrome::ShouldShowInstantNTP()) |
| 557 | return; |
| 558 | |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 559 | // Instant NTP is only used in extended mode so we should always have a |
| 560 | // non-empty URL to use. |
| 561 | DCHECK(!instant_url.empty()); |
[email protected] | 215ebc4 | 2013-06-22 00:24:50 | [diff] [blame] | 562 | ntp_.reset(new InstantNTP(this, instant_url, |
| 563 | browser_->profile()->IsOffTheRecord())); |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 564 | ntp_->InitContents(profile(), browser_->GetActiveWebContents(), |
| 565 | base::Bind(&InstantController::ReloadStaleNTP, |
| 566 | base::Unretained(this))); |
[email protected] | b97059d | 2013-01-26 00:06:40 | [diff] [blame] | 567 | LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 568 | "ResetNTP: instant_url='%s'", instant_url.c_str())); |
[email protected] | 08a9f97 | 2012-08-27 23:23:15 | [diff] [blame] | 569 | } |
| 570 | |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 571 | void InstantController::ReloadStaleNTP() { |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 572 | if (extended_enabled()) |
| 573 | ResetNTP(GetInstantURL()); |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | bool InstantController::ShouldSwitchToLocalNTP() const { |
[email protected] | 2b3ded9 | 2013-06-19 05:48:43 | [diff] [blame] | 577 | if (!ntp()) |
| 578 | return true; |
| 579 | |
| 580 | // Assume users with Javascript disabled do not want the online experience. |
| 581 | if (!IsJavascriptEnabled()) |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 582 | return true; |
| 583 | |
| 584 | // Already a local page. Not calling IsLocal() because we want to distinguish |
| 585 | // between the Google-specific and generic local NTP. |
[email protected] | 2b3ded9 | 2013-06-19 05:48:43 | [diff] [blame] | 586 | if (extended_enabled() && ntp()->instant_url() == GetLocalInstantURL()) |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 587 | return false; |
| 588 | |
| 589 | if (PageIsCurrent(ntp())) |
| 590 | return false; |
| 591 | |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 592 | // The preloaded NTP does not support instant yet. If we're not in startup, |
| 593 | // always fall back to the local NTP. If we are in startup, use the local NTP |
| 594 | // (unless the finch flag to use the remote NTP is set). |
[email protected] | 2b3ded9 | 2013-06-19 05:48:43 | [diff] [blame] | 595 | return !(InStartup() && chrome::ShouldPreferRemoteNTPOnStartup()); |
[email protected] | cc45906 | 2013-05-02 08:05:25 | [diff] [blame] | 596 | } |
| 597 | |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 598 | void InstantController::ResetInstantTab() { |
[email protected] | 215ebc4 | 2013-06-22 00:24:50 | [diff] [blame] | 599 | if (!search_mode_.is_origin_default()) { |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 600 | content::WebContents* active_tab = browser_->GetActiveWebContents(); |
| 601 | if (!instant_tab_ || active_tab != instant_tab_->contents()) { |
[email protected] | 215ebc4 | 2013-06-22 00:24:50 | [diff] [blame] | 602 | instant_tab_.reset( |
| 603 | new InstantTab(this, browser_->profile()->IsOffTheRecord())); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 604 | instant_tab_->Init(active_tab); |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 605 | UpdateInfoForInstantTab(); |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 606 | } |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 607 | } else { |
| 608 | instant_tab_.reset(); |
| 609 | } |
[email protected] | 0a38747 | 2010-10-07 00:18:20 | [diff] [blame] | 610 | } |
| 611 | |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 612 | void InstantController::UpdateInfoForInstantTab() { |
| 613 | if (instant_tab_) { |
[email protected] | 215ebc4 | 2013-06-22 00:24:50 | [diff] [blame] | 614 | instant_tab_->sender()->SetOmniboxBounds(omnibox_bounds_); |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 615 | |
| 616 | // Update theme details. |
| 617 | InstantService* instant_service = GetInstantService(); |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 618 | if (instant_service) { |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 619 | instant_service->UpdateThemeInfo(); |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 620 | instant_service->UpdateMostVisitedItemsInfo(); |
| 621 | } |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 622 | |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 623 | instant_tab_->InitializeFonts(); |
[email protected] | 43132fbd | 2013-06-26 02:18:03 | [diff] [blame] | 624 | instant_tab_->InitializePromos(); |
[email protected] | 215ebc4 | 2013-06-22 00:24:50 | [diff] [blame] | 625 | instant_tab_->sender()->FocusChanged(omnibox_focus_state_, |
| 626 | omnibox_focus_change_reason_); |
| 627 | instant_tab_->sender()->SetInputInProgress(IsInputInProgress()); |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
[email protected] | 0d0b4a4 | 2013-06-14 00:46:26 | [diff] [blame] | 631 | bool InstantController::IsInputInProgress() const { |
[email protected] | 274b42c | 2013-06-18 11:54:52 | [diff] [blame] | 632 | return !search_mode_.is_ntp() && |
[email protected] | 0d0b4a4 | 2013-06-14 00:46:26 | [diff] [blame] | 633 | omnibox_focus_state_ == OMNIBOX_FOCUS_VISIBLE; |
| 634 | } |
| 635 | |
[email protected] | 18956a5 | 2013-04-26 21:37:05 | [diff] [blame] | 636 | bool InstantController::UsingLocalPage() const { |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 637 | return instant_tab_ && instant_tab_->IsLocal(); |
[email protected] | 18956a5 | 2013-04-26 21:37:05 | [diff] [blame] | 638 | } |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 639 | |
| 640 | void InstantController::RedirectToLocalNTP(content::WebContents* contents) { |
| 641 | contents->GetController().LoadURL( |
| 642 | chrome::GetLocalInstantURL(browser_->profile()), |
| 643 | content::Referrer(), |
| 644 | content::PAGE_TRANSITION_SERVER_REDIRECT, |
| 645 | std::string()); // No extra headers. |
| 646 | // TODO(dcblack): Remove extraneous history entry caused by 404s. |
| 647 | // Note that the base case of a 204 being returned doesn't push a history |
| 648 | // entry. |
| 649 | } |
[email protected] | 2485c812 | 2013-05-07 03:17:52 | [diff] [blame] | 650 | |
[email protected] | 2b3ded9 | 2013-06-19 05:48:43 | [diff] [blame] | 651 | bool InstantController::IsJavascriptEnabled() const { |
| 652 | GURL instant_url(GetInstantURL()); |
| 653 | GURL origin(instant_url.GetOrigin()); |
| 654 | ContentSetting js_setting = profile()->GetHostContentSettingsMap()-> |
| 655 | GetContentSetting(origin, origin, CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 656 | NO_RESOURCE_IDENTIFIER); |
| 657 | // Javascript can be disabled either in content settings or via a WebKit |
| 658 | // preference, so check both. Disabling it through the Settings page affects |
| 659 | // content settings. I'm not sure how to disable the WebKit preference, but |
| 660 | // it's theoretically possible some users have it off. |
| 661 | bool js_content_enabled = |
| 662 | js_setting == CONTENT_SETTING_DEFAULT || |
| 663 | js_setting == CONTENT_SETTING_ALLOW; |
| 664 | bool js_webkit_enabled = profile()->GetPrefs()->GetBoolean( |
| 665 | prefs::kWebKitJavascriptEnabled); |
| 666 | return js_content_enabled && js_webkit_enabled; |
| 667 | } |
| 668 | |
| 669 | bool InstantController::InStartup() const { |
| 670 | // TODO(shishir): This is not completely reliable. Find a better way to detect |
| 671 | // startup time. |
| 672 | return !browser_->GetActiveWebContents(); |
| 673 | } |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 674 | |
| 675 | InstantService* InstantController::GetInstantService() const { |
| 676 | return InstantServiceFactory::GetForProfile(profile()); |
| 677 | } |