| Index: chrome/browser/ui/webui/print_preview/hidden_web_contents.cc
|
| diff --git a/chrome/browser/ui/webui/print_preview/hidden_web_contents.cc b/chrome/browser/ui/webui/print_preview/hidden_web_contents.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..246b22bf73e35f80141f287182855a62e147d5b4
|
| --- /dev/null
|
| +++ b/chrome/browser/ui/webui/print_preview/hidden_web_contents.cc
|
| @@ -0,0 +1,324 @@
|
| +// Copyright (c) 2014 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/ui/webui/print_preview/hidden_web_contents.h"
|
| +
|
| +#include <string>
|
| +
|
| +#include "chrome/browser/chrome_notification_types.h"
|
| +#include "chrome/browser/profiles/profile.h"
|
| +#include "chrome/browser/ui/tab_helpers.h"
|
| +#include "chrome/browser/ui/web_contents_sizer.h"
|
| +#include "chrome/common/prerender_messages.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "content/public/browser/notification_service.h"
|
| +#include "content/public/browser/render_frame_host.h"
|
| +#include "content/public/browser/render_process_host.h"
|
| +#include "content/public/browser/render_view_host.h"
|
| +#include "content/public/browser/session_storage_namespace.h"
|
| +#include "content/public/browser/web_contents.h"
|
| +#include "content/public/browser/web_contents_delegate.h"
|
| +
|
| +using content::OpenURLParams;
|
| +using content::RenderViewHost;
|
| +using content::SessionStorageNamespace;
|
| +using content::WebContents;
|
| +
|
| +class HiddenWebContents::WebContentsDelegateImpl
|
| + : public content::WebContentsDelegate {
|
| + public:
|
| + explicit WebContentsDelegateImpl(HiddenWebContents* hidden_web_contents)
|
| + : hidden_web_contents_(hidden_web_contents) {
|
| + }
|
| +
|
| + // content::WebContentsDelegate implementation:
|
| + WebContents* OpenURLFromTab(WebContents* source,
|
| + const OpenURLParams& params) override {
|
| + // |OpenURLFromTab| is typically called when a frame performs a navigation
|
| + // that requires the browser to perform the transition instead of WebKit.
|
| + // Examples include rendering a site that redirects to an app URL,
|
| + // or if --enable-strict-site-isolation is specified and the rendered
|
| + // frame redirects to a different origin.
|
| + hidden_web_contents_->Destroy();
|
| + return NULL;
|
| + }
|
| +
|
| + void CloseContents(content::WebContents* contents) override {
|
| + hidden_web_contents_->Destroy();
|
| + }
|
| +
|
| + void CanDownload(RenderViewHost* render_view_host,
|
| + const GURL& url,
|
| + const std::string& request_method,
|
| + const base::Callback<void(bool)>& callback) override {
|
| + hidden_web_contents_->Destroy();
|
| + // Cancel the download.
|
| + callback.Run(false);
|
| + }
|
| +
|
| + bool ShouldCreateWebContents(
|
| + WebContents* web_contents,
|
| + int route_id,
|
| + int main_frame_route_id,
|
| + WindowContainerType window_container_type,
|
| + const base::string16& frame_name,
|
| + const GURL& target_url,
|
| + const std::string& partition_id,
|
| + SessionStorageNamespace* session_storage_namespace) override {
|
| + // Since we don't want to permit child windows that would have a
|
| + // window.opener property, terminate rendering.
|
| + hidden_web_contents_->Destroy();
|
| + // Cancel the popup.
|
| + return false;
|
| + }
|
| +
|
| + bool OnGoToEntryOffset(int offset) override {
|
| + // This isn't allowed because the history merge operation
|
| + // does not work if there are renderer issued challenges.
|
| + // TODO(cbentzel): Cancel in this case? May not need to do
|
| + // since render-issued offset navigations are not guaranteed,
|
| + // but indicates that the page cares about the history.
|
| + return false;
|
| + }
|
| +
|
| + bool ShouldSuppressDialogs(WebContents* source) override {
|
| + // We still want to show the user the message when they navigate to this
|
| + // page, so cancel this render.
|
| + hidden_web_contents_->Destroy();
|
| + // Always suppress JavaScript messages if they're triggered by a page being
|
| + // rendered.
|
| + return true;
|
| + }
|
| +
|
| + void RegisterProtocolHandler(WebContents* web_contents,
|
| + const std::string& protocol,
|
| + const GURL& url,
|
| + bool user_gesture) override {
|
| + hidden_web_contents_->Destroy();
|
| + }
|
| +
|
| + gfx::Size GetSizeForNewRenderView(WebContents* web_contents) const override {
|
| + // Have to set the size of the RenderView on initialization to be sure it is
|
| + // set before the RenderView is hidden on all platforms (esp. Android).
|
| + return hidden_web_contents_->size_;
|
| + }
|
| +
|
| + private:
|
| + HiddenWebContents* hidden_web_contents_;
|
| +};
|
| +
|
| +void HiddenWebContents::Observer::OnFinishedLoad(
|
| + HiddenWebContents* contents) {
|
| +}
|
| +
|
| +HiddenWebContents::Observer::Observer() {
|
| +}
|
| +
|
| +HiddenWebContents::Observer::~Observer() {
|
| +}
|
| +
|
| +HiddenWebContents::HiddenWebContents(
|
| + Profile* profile,
|
| + const GURL& url)
|
| + : rendering_has_started_(false),
|
| + session_storage_namespace_id_(-1),
|
| + url_(url),
|
| + profile_(profile),
|
| + has_stopped_loading_(false),
|
| + has_finished_loading_(false),
|
| + rendering_has_been_cancelled_(false) {
|
| +}
|
| +
|
| +// static
|
| +bool HiddenWebContents::IsValidUrl(const GURL& url) {
|
| + return url.SchemeIs("chrome-distiller");
|
| +}
|
| +
|
| +void HiddenWebContents::StartRendering(
|
| + const gfx::Size& size,
|
| + SessionStorageNamespace* session_storage_namespace) {
|
| + DCHECK(profile_ != NULL);
|
| + DCHECK(!size.IsEmpty());
|
| + DCHECK(!rendering_has_started_);
|
| + DCHECK(web_contents_.get() == NULL);
|
| + DCHECK(size_.IsEmpty());
|
| +
|
| + if (!HiddenWebContents::IsValidUrl(url_)) {
|
| + Destroy();
|
| + return;
|
| + }
|
| +
|
| + session_storage_namespace_id_ = session_storage_namespace->id();
|
| + size_ = size;
|
| +
|
| + rendering_has_started_ = true;
|
| +
|
| + web_contents_.reset(CreateWebContents(session_storage_namespace));
|
| + TabHelpers::AttachTabHelpers(web_contents_.get());
|
| + content::WebContentsObserver::Observe(web_contents_.get());
|
| +
|
| + web_contents_delegate_.reset(new WebContentsDelegateImpl(this));
|
| + web_contents_.get()->SetDelegate(web_contents_delegate_.get());
|
| +
|
| + // Set the size of the hidden WebContents.
|
| + ResizeWebContents(web_contents_.get(), size_);
|
| +
|
| + // Close ourselves when the application is shutting down.
|
| + notification_registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
|
| + content::NotificationService::AllSources());
|
| +
|
| + // Register to inform new RenderViews that we're rendering.
|
| + notification_registrar_.Add(
|
| + this, content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
|
| + content::Source<WebContents>(web_contents_.get()));
|
| +
|
| + content::NavigationController::LoadURLParams load_url_params(url_);
|
| + load_url_params.transition_type = ui::PAGE_TRANSITION_LINK;
|
| + web_contents_.get()->GetController().LoadURLWithParams(load_url_params);
|
| +}
|
| +
|
| +HiddenWebContents::~HiddenWebContents() {
|
| + if (web_contents_.get()) {
|
| + web_contents_->SetDelegate(NULL);
|
| + content::WebContentsObserver::Observe(NULL);
|
| + }
|
| +}
|
| +
|
| +void HiddenWebContents::AddObserver(Observer* observer) {
|
| + observer_list_.AddObserver(observer);
|
| +}
|
| +
|
| +void HiddenWebContents::RemoveObserver(Observer* observer) {
|
| + observer_list_.RemoveObserver(observer);
|
| +}
|
| +
|
| +void HiddenWebContents::Observe(int type,
|
| + const content::NotificationSource& source,
|
| + const content::NotificationDetails& details) {
|
| + switch (type) {
|
| + // TODO(davidben): Try to remove this in favor of relying on
|
| + // FINAL_STATUS_PROFILE_DESTROYED.
|
| + case chrome::NOTIFICATION_APP_TERMINATING:
|
| + Destroy();
|
| + return;
|
| +
|
| + case content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED: {
|
| + if (web_contents_.get()) {
|
| + DCHECK_EQ(content::Source<WebContents>(source).ptr(),
|
| + web_contents_.get());
|
| +
|
| + // Make sure the size of the RenderViewHost has been passed to the new
|
| + // RenderView. Otherwise, the size may not be sent until the
|
| + // RenderViewReady event makes it from the render process to the UI
|
| + // thread of the browser process. When the RenderView receives its
|
| + // size, is also sets itself to be visible, which would then break the
|
| + // visibility API.
|
| + content::Details<RenderViewHost> new_render_view_host(details);
|
| + new_render_view_host->WasResized();
|
| + web_contents_->WasHidden();
|
| + }
|
| + break;
|
| + }
|
| +
|
| + default:
|
| + NOTREACHED() << "Unexpected notification sent.";
|
| + break;
|
| + }
|
| +}
|
| +
|
| +WebContents* HiddenWebContents::CreateWebContents(
|
| + SessionStorageNamespace* session_storage_namespace) {
|
| + // TODO(ajwong): Remove the temporary map once prerendering is aware of
|
| + // multiple session storage namespaces per tab.
|
| + content::SessionStorageNamespaceMap session_storage_namespace_map;
|
| + session_storage_namespace_map[std::string()] = session_storage_namespace;
|
| + return WebContents::CreateWithSessionStorage(
|
| + WebContents::CreateParams(profile_), session_storage_namespace_map);
|
| +}
|
| +
|
| +void HiddenWebContents::NotifyFail() {
|
| + FOR_EACH_OBSERVER(Observer, observer_list_, OnFail(this));
|
| + observer_list_.Clear();
|
| +}
|
| +
|
| +void HiddenWebContents::NotifyFinishedLoad() {
|
| + FOR_EACH_OBSERVER(Observer, observer_list_, OnFinishedLoad(this));
|
| +}
|
| +
|
| +void HiddenWebContents::RenderProcessGone(base::TerminationStatus status) {
|
| + Destroy();
|
| +}
|
| +
|
| +void HiddenWebContents::RenderFrameCreated(
|
| + content::RenderFrameHost* render_frame_host) {
|
| + // When a new RenderFrame is created for a hidden rendering WebContents, tell
|
| + // the new RenderFrame it's being used for prerendering before any
|
| + // navigations occur. Note that this is always triggered before the first
|
| + // navigation, so there's no need to send the message just after the
|
| + // WebContents is created.
|
| + render_frame_host->Send(new PrerenderMsg_SetIsPrerendering(
|
| + render_frame_host->GetRoutingID(), true));
|
| +}
|
| +
|
| +void HiddenWebContents::DidStopLoading() {
|
| + has_stopped_loading_ = true;
|
| +}
|
| +
|
| +void HiddenWebContents::DocumentLoadedInFrame(
|
| + content::RenderFrameHost* render_frame_host) {
|
| +}
|
| +
|
| +void HiddenWebContents::DidStartProvisionalLoadForFrame(
|
| + content::RenderFrameHost* render_frame_host,
|
| + const GURL& validated_url,
|
| + bool is_error_page,
|
| + bool is_iframe_srcdoc) {
|
| + if (!render_frame_host->GetParent()) {
|
| + // Usually, this event fires if the user clicks or enters a new URL.
|
| + // Neither of these can happen in the case of an hidden renderer.
|
| + // So the cause is: Some JavaScript caused a new URL to be loaded. In that
|
| + // case, the spinner would start again in the browser, so we must reset
|
| + // has_stopped_loading_ so that the spinner won't be stopped.
|
| + has_stopped_loading_ = false;
|
| + has_finished_loading_ = false;
|
| + }
|
| +}
|
| +
|
| +void HiddenWebContents::DidFinishLoad(
|
| + content::RenderFrameHost* render_frame_host,
|
| + const GURL& validated_url) {
|
| + if (!render_frame_host->GetParent())
|
| + has_finished_loading_ = true;
|
| + NotifyFinishedLoad();
|
| +}
|
| +
|
| +void HiddenWebContents::DidNavigateMainFrame(
|
| + const content::LoadCommittedDetails& details,
|
| + const content::FrameNavigateParams& params) {
|
| + // If the render made a second navigation entry, abort the render. This
|
| + // avoids having to correctly implement a complex history merging case (this
|
| + // interacts with location.replace) and correctly synchronize with the
|
| + // renderer. The final status may be monitored to see we need to revisit this
|
| + // decision. This does not affect client redirects as those do not push new
|
| + // history entries. (Calls to location.replace, navigations before onload, and
|
| + // <meta http-equiv=refresh> with timeouts under 1 second do not create
|
| + // entries in Blink.)
|
| + if (web_contents_->GetController().GetEntryCount() > 1)
|
| + Destroy();
|
| +}
|
| +
|
| +void HiddenWebContents::DidGetRedirectForResourceRequest(
|
| + content::RenderFrameHost* render_frame_host,
|
| + const content::ResourceRedirectDetails& details) {
|
| + // Redirects are unsupported for hidden renderers.
|
| + Destroy();
|
| +}
|
| +
|
| +void HiddenWebContents::Destroy() {
|
| + if (rendering_has_been_cancelled_)
|
| + return;
|
| +
|
| + rendering_has_been_cancelled_ = true;
|
| + NotifyFail();
|
| +}
|
|
|