Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / chrome / renderer / prerender / prerender_helper.cc
blob797c05bb5cc35df851d1dd83894ae8b7c96e8d7e
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.
5 #include "chrome/renderer/prerender/prerender_helper.h"
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"
16 using content::DocumentState;
18 namespace {
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);
33 } // namespace
35 namespace prerender {
37 PrerenderHelper::PrerenderHelper(content::RenderFrame* render_frame)
38 : content::RenderFrameObserver(render_frame),
39 content::RenderFrameObserverTracker<PrerenderHelper>(render_frame) {
40 UpdateVisibilityState(render_frame);
43 PrerenderHelper::~PrerenderHelper() {
46 // static.
47 bool PrerenderHelper::IsPrerendering(const content::RenderFrame* render_frame) {
48 return PrerenderHelper::Get(render_frame) != NULL;
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;
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;
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;
73 UpdateVisibilityState(frame);
76 } // namespace prerender