summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/java_bridge_dispatcher_host_manager.cc
blob: 905adcfc5572c86f970eda0a5e9136231436dede (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) 2011 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 "content/browser/renderer_host/java_bridge_dispatcher_host_manager.h"

#include "base/utf_string_conversions.h"
#include "content/browser/renderer_host/java_bridge_dispatcher_host.h"
#include "content/browser/tab_contents/tab_contents.h"

JavaBridgeDispatcherHostManager::JavaBridgeDispatcherHostManager(
    TabContents* tab_contents)
    : TabContentsObserver(tab_contents) {
}

JavaBridgeDispatcherHostManager::~JavaBridgeDispatcherHostManager() {
  DCHECK_EQ(0U, instances_.size());
}

void JavaBridgeDispatcherHostManager::AddNamedObject(const string16& name,
    NPObject* object) {
  // Record this object in a map so that we can add it into RenderViewHosts
  // created later. We don't need to take a reference because each
  // JavaBridgeDispatcherHost will do so. The object can't be deleted until
  // after RemoveNamedObject() is called, at which time we remove the entry
  // from the map.
  DCHECK(!instances_.empty()) << "There must be at least one instance present";
  objects_[name] = object;

  for (InstanceMap::iterator iter = instances_.begin();
      iter != instances_.end(); ++iter) {
    iter->second->AddNamedObject(name, object);
  }
}

void JavaBridgeDispatcherHostManager::RemoveNamedObject(const string16& name) {
  ObjectMap::iterator iter = objects_.find(name);
  if (iter == objects_.end()) {
    return;
  }

  objects_.erase(iter);

  for (InstanceMap::iterator iter = instances_.begin();
      iter != instances_.end(); ++iter) {
    iter->second->RemoveNamedObject(name);
  }
}

void JavaBridgeDispatcherHostManager::RenderViewCreated(
    RenderViewHost* render_view_host) {
  // Creates a JavaBridgeDispatcherHost for the specified RenderViewHost and
  // adds all currently registered named objects to the new instance.
  scoped_refptr<JavaBridgeDispatcherHost> instance =
      new JavaBridgeDispatcherHost(render_view_host);

  for (ObjectMap::const_iterator iter = objects_.begin();
      iter != objects_.end(); ++iter) {
    instance->AddNamedObject(iter->first, iter->second);
  }

  instances_[render_view_host] = instance;
}

void JavaBridgeDispatcherHostManager::RenderViewDeleted(
    RenderViewHost* render_view_host) {
  instances_.erase(render_view_host);
}

void JavaBridgeDispatcherHostManager::TabContentsDestroyed(
    TabContents* tab_contents) {
  // When the tab is shutting down, the TabContents clears its observers before
  // it kills all of its RenderViewHosts, so we won't get a call to
  // RenderViewDeleted() for all RenderViewHosts.
  instances_.clear();
}