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

content / public / browser / web_contents_observer.cc [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/public/browser/web_contents_observer.h"

#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/navigation_details.h"

namespace content {

WebContentsObserver::WebContentsObserver(WebContents* web_contents) {
  Observe(web_contents);
}

WebContentsObserver::WebContentsObserver() = default;

WebContentsObserver::~WebContentsObserver() {
  if (web_contents_) {
    static_cast<WebContentsImpl*>(web_contents_)->RemoveObserver(this);
  }
  CHECK(!IsInObserverList());
}

WebContents* WebContentsObserver::web_contents() const {
  return web_contents_;
}

void WebContentsObserver::Observe(WebContents* web_contents) {
  if (web_contents == web_contents_) {
    // Early exit to avoid infinite loops if we're in the middle of a callback.
    return;
  }
  if (web_contents_)
    static_cast<WebContentsImpl*>(web_contents_)->RemoveObserver(this);
  web_contents_ = web_contents;
  if (web_contents_) {
    static_cast<WebContentsImpl*>(web_contents_)->AddObserver(this);
  }
}

bool WebContentsObserver::OnMessageReceived(
    const IPC::Message& message,
    RenderFrameHost* render_frame_host) {
  return false;
}

void WebContentsObserver::ResetWebContents() {
  static_cast<WebContentsImpl*>(web_contents_)->RemoveObserver(this);
  web_contents_ = nullptr;
}

}  // namespace content