- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / renderer / prerender / prerender_helper.cc
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_view.h"
12 #include "third_party/WebKit/public/web/WebFrame.h"
13 #include "third_party/WebKit/public/web/WebView.h"
14
15 using content::DocumentState;
16
17 namespace {
18
19 // Updates the visibility state of the RenderView.  Must be called whenever
20 // prerendering starts or finishes and the page is about to be show.  At both
21 // those times, the RenderView is hidden.
22 void UpdateVisibilityState(content::RenderView* render_view) {
23   if (render_view->GetWebView()) {
24     render_view->GetWebView()->setVisibilityState(
25         render_view->GetVisibilityState(), false);
26   }
27 }
28
29 }  // namespace
30
31 namespace prerender {
32
33 PrerenderHelper::PrerenderHelper(content::RenderView* render_view)
34     : content::RenderViewObserver(render_view),
35       content::RenderViewObserverTracker<PrerenderHelper>(render_view) {
36   UpdateVisibilityState(render_view);
37 }
38
39 PrerenderHelper::~PrerenderHelper() {
40 }
41
42 // static.
43 bool PrerenderHelper::IsPrerendering(const content::RenderView* render_view) {
44   return PrerenderHelper::Get(render_view) != NULL;
45 }
46
47 bool PrerenderHelper::OnMessageReceived(
48     const IPC::Message& message) {
49   IPC_BEGIN_MESSAGE_MAP(PrerenderHelper, message)
50     IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering, OnSetIsPrerendering)
51   IPC_END_MESSAGE_MAP()
52   // Return false on ViewMsg_SetIsPrerendering so other observers can see the
53   // message.
54   return false;
55 }
56
57 void PrerenderHelper::OnSetIsPrerendering(bool is_prerendering) {
58   // Immediately after construction, |this| may receive the message that
59   // triggered its creation.  If so, ignore it.
60   if (is_prerendering)
61     return;
62
63   content::RenderView* view = render_view();
64   // |this| must be deleted so PrerenderHelper::IsPrerendering returns false
65   // when the visibility state is updated, so the visibility state string will
66   // not be "prerendered".
67   delete this;
68
69   UpdateVisibilityState(view);
70 }
71
72 }  // namespace prerender