1 // Copyright (c) 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/renderer/prerender/prerender_helper.h" 6 7 #include "base/metrics/field_trial.h" 8 #include "base/metrics/histogram.h" 9 #include "chrome/common/prerender_messages.h" 10 #include "content/public/renderer/document_state.h" 11 #include "content/public/renderer/render_frame.h" 12 #include "content/public/renderer/render_view.h" 13 #include "third_party/WebKit/public/web/WebFrame.h" 14 #include "third_party/WebKit/public/web/WebView.h" 15 16 using content::DocumentState; 17 18 namespace { 19 20 // Updates the visibility state of the RenderFrame. Must be called whenever 21 // prerendering starts or finishes and the page is about to be show. At both 22 // those times, the RenderFrame is hidden. 23 void UpdateVisibilityState(content::RenderFrame* render_frame) { 24 // TODO(jam): until the prerendering code works on frames instead of views, we 25 // have to do this awkward check. 26 content::RenderView* render_view = render_frame->GetRenderView(); 27 if (render_view->GetMainRenderFrame() == render_frame) { 28 render_view->GetWebView()->setVisibilityState( 29 render_view->GetVisibilityState(), false); 30 } 31 } 32 33 } // namespace 34 35 namespace prerender { 36 37 PrerenderHelper::PrerenderHelper(content::RenderFrame* render_frame) 38 : content::RenderFrameObserver(render_frame), 39 content::RenderFrameObserverTracker<PrerenderHelper>(render_frame) { 40 UpdateVisibilityState(render_frame); 41 } 42 43 PrerenderHelper::~PrerenderHelper() { 44 } 45 46 // static. 47 bool PrerenderHelper::IsPrerendering(const content::RenderFrame* render_frame) { 48 return PrerenderHelper::Get(render_frame) != NULL; 49 } 50 51 bool PrerenderHelper::OnMessageReceived( 52 const IPC::Message& message) { 53 IPC_BEGIN_MESSAGE_MAP(PrerenderHelper, message) 54 IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering, OnSetIsPrerendering) 55 IPC_END_MESSAGE_MAP() 56 // Return false on ViewMsg_SetIsPrerendering so other observers can see the 57 // message. 58 return false; 59 } 60 61 void PrerenderHelper::OnSetIsPrerendering(bool is_prerendering) { 62 // Immediately after construction, |this| may receive the message that 63 // triggered its creation. If so, ignore it. 64 if (is_prerendering) 65 return; 66 67 content::RenderFrame* frame = render_frame(); 68 // |this| must be deleted so PrerenderHelper::IsPrerendering returns false 69 // when the visibility state is updated, so the visibility state string will 70 // not be "prerendered". 71 delete this; 72 73 UpdateVisibilityState(frame); 74 } 75 76 } // namespace prerender 77