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