summaryrefslogtreecommitdiffstats
path: root/chrome/browser/tab_render_watcher.cc
blob: 160199f4d02288eb04b2ab4b75311a8885858486 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) 2012 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/tab_render_watcher.h"

#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"

using content::RenderWidgetHost;
using content::WebContents;

TabRenderWatcher::TabRenderWatcher(WebContents* tab, Delegate* delegate)
    : loaded_(false),
      web_contents_(tab),
      delegate_(delegate) {
  registrar_.Add(this,
      content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB,
      content::Source<WebContents>(web_contents_));
  registrar_.Add(this,
      content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
      content::Source<WebContents>(web_contents_));
}

void TabRenderWatcher::Observe(int type,
                               const content::NotificationSource& source,
                               const content::NotificationDetails& details) {
  switch (type) {
    case content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB: {
      RenderWidgetHost* rwh = content::Details<RenderWidgetHost>(details).ptr();
      registrar_.Add(this,
          content::NOTIFICATION_RENDER_WIDGET_HOST_DID_PAINT,
          content::Source<RenderWidgetHost>(rwh));
      delegate_->OnRenderHostCreated(
          content::Details<content::RenderViewHost>(details).ptr());
      break;
    }
    case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME:
      if (!loaded_) {
        loaded_ = true;
        delegate_->OnTabMainFrameLoaded();
      }
      break;
    case content::NOTIFICATION_RENDER_WIDGET_HOST_DID_PAINT:
      if (loaded_)
        delegate_->OnTabMainFrameRender();
      break;
    default:
      NOTREACHED() << "Unknown notification " << type;
  }
}