Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2012 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 #include "chrome/browser/instant/instant_ntp.h" | |
| 6 | |
| 7 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | |
| 8 #include "chrome/browser/favicon/favicon_tab_helper.h" | |
| 9 #include "chrome/browser/instant/instant_controller.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h" | |
| 12 #include "chrome/browser/tab_contents/tab_util.h" | |
| 13 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" | |
| 14 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" | |
| 15 #include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h" | |
| 16 #include "chrome/common/url_constants.h" | |
| 17 #include "content/public/browser/navigation_entry.h" | |
| 18 #include "content/public/browser/notification_source.h" | |
| 19 #include "content/public/browser/render_widget_host_view.h" | |
| 20 #include "content/public/browser/web_contents_delegate.h" | |
| 21 #include "content/public/browser/web_contents_view.h" | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 int kUserDataKey; | |
| 26 | |
| 27 class InstantNTPUserData : public base::SupportsUserData::Data { | |
| 28 public: | |
| 29 explicit InstantNTPUserData(InstantNTP* ntp) : ntp_(ntp) {} | |
| 30 | |
| 31 InstantNTP* ntp() const { return ntp_; } | |
| 32 | |
| 33 private: | |
| 34 ~InstantNTPUserData() {} | |
| 35 | |
| 36 InstantNTP* const ntp_; | |
| 37 | |
| 38 DISALLOW_COPY_AND_ASSIGN(InstantNTPUserData); | |
| 39 }; | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 // WebContentsDelegateImpl ----------------------------------------------------- | |
| 44 | |
| 45 class InstantNTP::WebContentsDelegateImpl | |
| 46 : public CoreTabHelperDelegate, | |
| 47 public content::WebContentsDelegate { | |
| 48 public: | |
| 49 explicit WebContentsDelegateImpl(InstantNTP* ntp); | |
| 50 | |
| 51 private: | |
| 52 // Overridden from CoreTabHelperDelegate: | |
| 53 virtual void SwapTabContents(content::WebContents* old_contents, | |
| 54 content::WebContents* new_contents) OVERRIDE; | |
| 55 | |
| 56 // Overridden from content::WebContentsDelegate: | |
| 57 virtual bool ShouldSuppressDialogs() OVERRIDE; | |
| 58 virtual bool CanDownload(content::RenderViewHost* render_view_host, | |
| 59 int request_id, | |
| 60 const std::string& request_method) OVERRIDE; | |
| 61 virtual bool OnGoToEntryOffset(int offset) OVERRIDE; | |
| 62 | |
| 63 InstantNTP* const ntp_; | |
| 64 | |
| 65 DISALLOW_COPY_AND_ASSIGN(WebContentsDelegateImpl); | |
| 66 }; | |
| 67 | |
| 68 InstantNTP::WebContentsDelegateImpl::WebContentsDelegateImpl(InstantNTP* ntp) | |
| 69 : ntp_(ntp) { | |
| 70 } | |
| 71 | |
| 72 void InstantNTP::WebContentsDelegateImpl::SwapTabContents( | |
| 73 content::WebContents* old_contents, | |
| 74 content::WebContents* new_contents) { | |
| 75 // If this is being called, something is swapping in to ntp's |contents_| | |
| 76 // before we've added it to the tab strip. | |
| 77 ntp_->ReplacePreviewContents(old_contents, new_contents); | |
| 78 } | |
| 79 | |
| 80 bool InstantNTP::WebContentsDelegateImpl::ShouldSuppressDialogs() { | |
| 81 // Any message shown during Instant cancels Instant, so we suppress them. | |
| 82 return true; | |
| 83 } | |
| 84 | |
| 85 bool InstantNTP::WebContentsDelegateImpl::CanDownload( | |
| 86 content::RenderViewHost* /* render_view_host */, | |
| 87 int /* request_id */, | |
| 88 const std::string& /* request_method */) { | |
| 89 // Downloads are disabled. | |
| 90 return false; | |
| 91 } | |
| 92 | |
| 93 bool InstantNTP::WebContentsDelegateImpl::OnGoToEntryOffset(int offset) { | |
| 94 return false; | |
| 95 } | |
| 96 | |
| 97 // InstantNTP ------------------------------------------------------------------ | |
| 98 | |
| 99 // static | |
| 100 InstantNTP* InstantNTP::FromWebContents( | |
| 101 const content::WebContents* web_contents) { | |
| 102 InstantNTPUserData* data = static_cast<InstantNTPUserData*>( | |
| 103 web_contents->GetUserData(&kUserDataKey)); | |
| 104 return data ? data->ntp() : NULL; | |
| 105 } | |
| 106 | |
| 107 InstantNTP::InstantNTP(InstantController* controller, | |
| 108 const std::string& instant_url) | |
| 109 : client_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
| 110 controller_(controller), | |
| 111 delegate_(new WebContentsDelegateImpl( | |
| 112 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.
| |
| 113 instant_url_(instant_url), | |
| 114 supports_instant_(false) { | |
| 115 } | |
| 116 | |
| 117 InstantNTP::~InstantNTP() { | |
| 118 } | |
| 119 | |
| 120 void InstantNTP::InitContents(Profile* profile, | |
| 121 const content::WebContents* active_tab) { | |
| 122 content::WebContents::CreateParams create_params( | |
| 123 profile, | |
| 124 tab_util::GetSiteInstanceForNewTab(profile, GURL(instant_url_))); | |
| 125 if (active_tab) | |
| 126 create_params.initial_size = active_tab->GetView()->GetContainerSize(); | |
| 127 | |
| 128 contents_.reset(content::WebContents::Create(create_params)); | |
| 129 SetupPreviewContents(); | |
| 130 | |
| 131 // This HTTP header and value are set on loads that originate from Instant. | |
| 132 const char kInstantHeader[] = "X-Purpose: Instant"; | |
| 133 DVLOG(1) << "LoadNTP: " << instant_url_; | |
| 134 contents_->GetController().LoadURL(GURL(instant_url_), content::Referrer(), | |
| 135 content::PAGE_TRANSITION_GENERATED, kInstantHeader); | |
| 136 contents_->GetController().GetPendingEntry()->SetVirtualURL( | |
| 137 GURL(chrome::kChromeUINewTabURL)); | |
| 138 contents_->WasHidden(); | |
| 139 } | |
| 140 | |
| 141 content::WebContents* InstantNTP::ReleaseContents() { | |
| 142 CleanupPreviewContents(); | |
| 143 return contents_.release(); | |
| 144 } | |
| 145 | |
| 146 void InstantNTP::SetMarginSize(int start, int end) { | |
| 147 client_.SetMarginSize(start, end); | |
| 148 } | |
| 149 | |
| 150 void InstantNTP::SendThemeBackgroundInfo( | |
| 151 const ThemeBackgroundInfo& theme_info) { | |
| 152 client_.SendThemeBackgroundInfo(theme_info); | |
| 153 } | |
| 154 | |
| 155 void InstantNTP::SendThemeAreaHeight(int height) { | |
| 156 client_.SendThemeAreaHeight(height); | |
| 157 } | |
| 158 | |
| 159 void InstantNTP::SetDisplayInstantResults(bool display_instant_results) { | |
| 160 client_.SetDisplayInstantResults(display_instant_results); | |
| 161 } | |
| 162 | |
| 163 void InstantNTP::SetSuggestions( | |
| 164 const std::vector<InstantSuggestion>& suggestions) { | |
| 165 // Nothing to do in an uncommitted NTP. | |
| 166 } | |
| 167 | |
| 168 void InstantNTP::InstantSupportDetermined(bool supports_instant) { | |
| 169 // If we had already determined that the page supports Instant, nothing to do. | |
| 170 if (supports_instant_) | |
| 171 return; | |
| 172 | |
| 173 // If the page doesn't support Instant, stop communicating with it. | |
| 174 if (!supports_instant) | |
| 175 client_.SetContents(NULL); | |
| 176 | |
| 177 supports_instant_ = supports_instant; | |
| 178 controller_->InstantSupportDetermined(contents(), supports_instant); | |
| 179 } | |
| 180 | |
| 181 void InstantNTP::ShowInstantPreview(InstantShownReason reason, | |
| 182 int height, | |
| 183 InstantSizeUnits units) { | |
| 184 // Nothing to do in an uncommitted NTP. | |
| 185 } | |
| 186 | |
| 187 void InstantNTP::StartCapturingKeyStrokes() { | |
| 188 // Nothing to do in an uncommitted NTP. | |
| 189 } | |
| 190 | |
| 191 void InstantNTP::StopCapturingKeyStrokes() { | |
| 192 // Nothing to do in an uncommitted NTP. | |
| 193 } | |
| 194 | |
| 195 void InstantNTP::RenderViewGone() { | |
| 196 controller_->InstantNTPRenderViewGone(); | |
| 197 } | |
| 198 | |
| 199 void InstantNTP::AboutToNavigateMainFrame(const GURL& url) { | |
| 200 // Nothing to do in an uncommitted NTP. | |
| 201 } | |
| 202 | |
| 203 void InstantNTP::NavigateToURL(const GURL& url, | |
| 204 content::PageTransition transition) { | |
| 205 controller_->NavigateToURL(url, transition); | |
| 206 } | |
| 207 | |
| 208 void InstantNTP::SetupPreviewContents() { | |
| 209 client_.SetContents(contents()); | |
| 210 contents_->SetUserData(&kUserDataKey, new InstantNTPUserData(this)); | |
| 211 contents_->SetDelegate(delegate_.get()); | |
| 212 | |
| 213 // Set up various tab helpers. The rest will get attached when (if) the | |
| 214 // contents is added to the tab strip. | |
| 215 | |
| 216 // Tab helpers to control popups. | |
| 217 BlockedContentTabHelper::CreateForWebContents(contents()); | |
| 218 BlockedContentTabHelper::FromWebContents(contents())-> | |
| 219 SetAllContentsBlocked(true); | |
| 220 TabSpecificContentSettings::CreateForWebContents(contents()); | |
| 221 TabSpecificContentSettings::FromWebContents(contents())-> | |
| 222 SetPopupsBlocked(true); | |
| 223 | |
| 224 // A tab helper to catch prerender content swapping shenanigans. | |
| 225 CoreTabHelper::CreateForWebContents(contents()); | |
| 226 CoreTabHelper::FromWebContents(contents())->set_delegate(delegate_.get()); | |
| 227 | |
| 228 // Favicons, required by the Task Manager. | |
| 229 FaviconTabHelper::CreateForWebContents(contents()); | |
| 230 | |
| 231 // And some flat-out paranoia. | |
| 232 safe_browsing::SafeBrowsingTabObserver::CreateForWebContents(contents()); | |
| 233 } | |
| 234 | |
| 235 void InstantNTP::CleanupPreviewContents() { | |
| 236 client_.SetContents(NULL); | |
| 237 contents_->RemoveUserData(&kUserDataKey); | |
| 238 contents_->SetDelegate(NULL); | |
| 239 | |
| 240 // Undo tab helper work done in SetupPreviewContents(). | |
| 241 | |
| 242 BlockedContentTabHelper::FromWebContents(contents())-> | |
| 243 SetAllContentsBlocked(false); | |
| 244 TabSpecificContentSettings::FromWebContents(contents())-> | |
| 245 SetPopupsBlocked(false); | |
| 246 | |
| 247 CoreTabHelper::FromWebContents(contents())->set_delegate(NULL); | |
| 248 } | |
| 249 | |
| 250 void InstantNTP::ReplacePreviewContents(content::WebContents* old_contents, | |
| 251 content::WebContents* new_contents) { | |
| 252 DCHECK_EQ(old_contents, contents()); | |
| 253 CleanupPreviewContents(); | |
| 254 // We release here without deleting so that the caller still has the | |
| 255 // responsibility for deleting the WebContents. | |
| 256 ignore_result(contents_.release()); | |
| 257 contents_.reset(new_contents); | |
| 258 SetupPreviewContents(); | |
| 259 } | |
| OLD | NEW |