Index: chrome/browser/instant/instant_ntp.cc |
diff --git a/chrome/browser/instant/instant_ntp.cc b/chrome/browser/instant/instant_ntp.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e58a83beb3a4ee360c7190c026a84ce93bfd2c20 |
--- /dev/null |
+++ b/chrome/browser/instant/instant_ntp.cc |
@@ -0,0 +1,259 @@ |
+// Copyright 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/instant/instant_ntp.h" |
+ |
+#include "chrome/browser/content_settings/tab_specific_content_settings.h" |
+#include "chrome/browser/favicon/favicon_tab_helper.h" |
+#include "chrome/browser/instant/instant_controller.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h" |
+#include "chrome/browser/tab_contents/tab_util.h" |
+#include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" |
+#include "chrome/browser/ui/tab_contents/core_tab_helper.h" |
+#include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h" |
+#include "chrome/common/url_constants.h" |
+#include "content/public/browser/navigation_entry.h" |
+#include "content/public/browser/notification_source.h" |
+#include "content/public/browser/render_widget_host_view.h" |
+#include "content/public/browser/web_contents_delegate.h" |
+#include "content/public/browser/web_contents_view.h" |
+ |
+namespace { |
+ |
+int kUserDataKey; |
+ |
+class InstantNTPUserData : public base::SupportsUserData::Data { |
+ public: |
+ explicit InstantNTPUserData(InstantNTP* ntp) : ntp_(ntp) {} |
+ |
+ InstantNTP* ntp() const { return ntp_; } |
+ |
+ private: |
+ ~InstantNTPUserData() {} |
+ |
+ InstantNTP* const ntp_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(InstantNTPUserData); |
+}; |
+ |
+} // namespace |
+ |
+// WebContentsDelegateImpl ----------------------------------------------------- |
+ |
+class InstantNTP::WebContentsDelegateImpl |
+ : public CoreTabHelperDelegate, |
+ public content::WebContentsDelegate { |
+ public: |
+ explicit WebContentsDelegateImpl(InstantNTP* ntp); |
+ |
+ private: |
+ // Overridden from CoreTabHelperDelegate: |
+ virtual void SwapTabContents(content::WebContents* old_contents, |
+ content::WebContents* new_contents) OVERRIDE; |
+ |
+ // Overridden from content::WebContentsDelegate: |
+ virtual bool ShouldSuppressDialogs() OVERRIDE; |
+ virtual bool CanDownload(content::RenderViewHost* render_view_host, |
+ int request_id, |
+ const std::string& request_method) OVERRIDE; |
+ virtual bool OnGoToEntryOffset(int offset) OVERRIDE; |
+ |
+ InstantNTP* const ntp_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(WebContentsDelegateImpl); |
+}; |
+ |
+InstantNTP::WebContentsDelegateImpl::WebContentsDelegateImpl(InstantNTP* ntp) |
+ : ntp_(ntp) { |
+} |
+ |
+void InstantNTP::WebContentsDelegateImpl::SwapTabContents( |
+ content::WebContents* old_contents, |
+ content::WebContents* new_contents) { |
+ // If this is being called, something is swapping in to ntp's |contents_| |
+ // before we've added it to the tab strip. |
+ ntp_->ReplacePreviewContents(old_contents, new_contents); |
+} |
+ |
+bool InstantNTP::WebContentsDelegateImpl::ShouldSuppressDialogs() { |
+ // Any message shown during Instant cancels Instant, so we suppress them. |
+ return true; |
+} |
+ |
+bool InstantNTP::WebContentsDelegateImpl::CanDownload( |
+ content::RenderViewHost* /* render_view_host */, |
+ int /* request_id */, |
+ const std::string& /* request_method */) { |
+ // Downloads are disabled. |
+ return false; |
+} |
+ |
+bool InstantNTP::WebContentsDelegateImpl::OnGoToEntryOffset(int offset) { |
+ return false; |
+} |
+ |
+// InstantNTP ------------------------------------------------------------------ |
+ |
+// static |
+InstantNTP* InstantNTP::FromWebContents( |
+ const content::WebContents* web_contents) { |
+ InstantNTPUserData* data = static_cast<InstantNTPUserData*>( |
+ web_contents->GetUserData(&kUserDataKey)); |
+ return data ? data->ntp() : NULL; |
+} |
+ |
+InstantNTP::InstantNTP(InstantController* controller, |
+ const std::string& instant_url) |
+ : client_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
+ controller_(controller), |
+ delegate_(new WebContentsDelegateImpl( |
+ ALLOW_THIS_IN_INITIALIZER_LIST(this))), |
dhollowa
2013/01/10 18:50:29
nit: indent seems off here.
samarth
2013/01/11 19:43:05
Done.
|
+ instant_url_(instant_url), |
+ supports_instant_(false) { |
+} |
+ |
+InstantNTP::~InstantNTP() { |
+} |
+ |
+void InstantNTP::InitContents(Profile* profile, |
+ const content::WebContents* active_tab) { |
+ content::WebContents::CreateParams create_params( |
+ profile, |
+ tab_util::GetSiteInstanceForNewTab(profile, GURL(instant_url_))); |
+ if (active_tab) |
+ create_params.initial_size = active_tab->GetView()->GetContainerSize(); |
+ |
+ contents_.reset(content::WebContents::Create(create_params)); |
+ SetupPreviewContents(); |
+ |
+ // This HTTP header and value are set on loads that originate from Instant. |
+ const char kInstantHeader[] = "X-Purpose: Instant"; |
+ DVLOG(1) << "LoadNTP: " << instant_url_; |
+ contents_->GetController().LoadURL(GURL(instant_url_), content::Referrer(), |
+ content::PAGE_TRANSITION_GENERATED, kInstantHeader); |
+ contents_->GetController().GetPendingEntry()->SetVirtualURL( |
+ GURL(chrome::kChromeUINewTabURL)); |
+ contents_->WasHidden(); |
+} |
+ |
+content::WebContents* InstantNTP::ReleaseContents() { |
+ CleanupPreviewContents(); |
+ return contents_.release(); |
+} |
+ |
+void InstantNTP::SetMarginSize(int start, int end) { |
+ client_.SetMarginSize(start, end); |
+} |
+ |
+void InstantNTP::SendThemeBackgroundInfo( |
+ const ThemeBackgroundInfo& theme_info) { |
+ client_.SendThemeBackgroundInfo(theme_info); |
+} |
+ |
+void InstantNTP::SendThemeAreaHeight(int height) { |
+ client_.SendThemeAreaHeight(height); |
+} |
+ |
+void InstantNTP::SetDisplayInstantResults(bool display_instant_results) { |
+ client_.SetDisplayInstantResults(display_instant_results); |
+} |
+ |
+void InstantNTP::SetSuggestions( |
+ const std::vector<InstantSuggestion>& suggestions) { |
+ // Nothing to do in an uncommitted NTP. |
+} |
+ |
+void InstantNTP::InstantSupportDetermined(bool supports_instant) { |
+ // If we had already determined that the page supports Instant, nothing to do. |
+ if (supports_instant_) |
+ return; |
+ |
+ // If the page doesn't support Instant, stop communicating with it. |
+ if (!supports_instant) |
+ client_.SetContents(NULL); |
+ |
+ supports_instant_ = supports_instant; |
+ controller_->InstantSupportDetermined(contents(), supports_instant); |
+} |
+ |
+void InstantNTP::ShowInstantPreview(InstantShownReason reason, |
+ int height, |
+ InstantSizeUnits units) { |
+ // Nothing to do in an uncommitted NTP. |
+} |
+ |
+void InstantNTP::StartCapturingKeyStrokes() { |
+ // Nothing to do in an uncommitted NTP. |
+} |
+ |
+void InstantNTP::StopCapturingKeyStrokes() { |
+ // Nothing to do in an uncommitted NTP. |
+} |
+ |
+void InstantNTP::RenderViewGone() { |
+ controller_->InstantNTPRenderViewGone(); |
+} |
+ |
+void InstantNTP::AboutToNavigateMainFrame(const GURL& url) { |
+ // Nothing to do in an uncommitted NTP. |
+} |
+ |
+void InstantNTP::NavigateToURL(const GURL& url, |
+ content::PageTransition transition) { |
+ controller_->NavigateToURL(url, transition); |
+} |
+ |
+void InstantNTP::SetupPreviewContents() { |
+ client_.SetContents(contents()); |
+ contents_->SetUserData(&kUserDataKey, new InstantNTPUserData(this)); |
+ contents_->SetDelegate(delegate_.get()); |
+ |
+ // Set up various tab helpers. The rest will get attached when (if) the |
+ // contents is added to the tab strip. |
+ |
+ // Tab helpers to control popups. |
+ BlockedContentTabHelper::CreateForWebContents(contents()); |
+ BlockedContentTabHelper::FromWebContents(contents())-> |
+ SetAllContentsBlocked(true); |
+ TabSpecificContentSettings::CreateForWebContents(contents()); |
+ TabSpecificContentSettings::FromWebContents(contents())-> |
+ SetPopupsBlocked(true); |
+ |
+ // A tab helper to catch prerender content swapping shenanigans. |
+ CoreTabHelper::CreateForWebContents(contents()); |
+ CoreTabHelper::FromWebContents(contents())->set_delegate(delegate_.get()); |
+ |
+ // Favicons, required by the Task Manager. |
+ FaviconTabHelper::CreateForWebContents(contents()); |
+ |
+ // And some flat-out paranoia. |
+ safe_browsing::SafeBrowsingTabObserver::CreateForWebContents(contents()); |
+} |
+ |
+void InstantNTP::CleanupPreviewContents() { |
+ client_.SetContents(NULL); |
+ contents_->RemoveUserData(&kUserDataKey); |
+ contents_->SetDelegate(NULL); |
+ |
+ // Undo tab helper work done in SetupPreviewContents(). |
+ |
+ BlockedContentTabHelper::FromWebContents(contents())-> |
+ SetAllContentsBlocked(false); |
+ TabSpecificContentSettings::FromWebContents(contents())-> |
+ SetPopupsBlocked(false); |
+ |
+ CoreTabHelper::FromWebContents(contents())->set_delegate(NULL); |
+} |
+ |
+void InstantNTP::ReplacePreviewContents(content::WebContents* old_contents, |
+ content::WebContents* new_contents) { |
+ DCHECK_EQ(old_contents, contents()); |
+ CleanupPreviewContents(); |
+ // We release here without deleting so that the caller still has the |
+ // responsibility for deleting the WebContents. |
+ ignore_result(contents_.release()); |
+ contents_.reset(new_contents); |
+ SetupPreviewContents(); |
+} |