summaryrefslogtreecommitdiffstats
path: root/content/browser/browser_plugin/test_guest_manager_delegate.cc
blob: 11d3b34af69550984af0c6fe2f75a2940e425f3e (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
77
78
79
80
81
82
83
84
85
86
// Copyright 2014 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/browser_plugin/test_guest_manager_delegate.h"

#include "base/logging.h"
#include "base/memory/singleton.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"

namespace content {

TestGuestManagerDelegate::TestGuestManagerDelegate()
    : next_instance_id_(0) {
}

TestGuestManagerDelegate::~TestGuestManagerDelegate() {
}

// static.
TestGuestManagerDelegate* TestGuestManagerDelegate::GetInstance() {
  return Singleton<TestGuestManagerDelegate>::get();
}

int TestGuestManagerDelegate::GetNextInstanceID() {
  return ++next_instance_id_;
}

void TestGuestManagerDelegate::AddGuest(
    int guest_instance_id,
    WebContents* guest_web_contents) {
  DCHECK(guest_web_contents_by_instance_id_.find(guest_instance_id) ==
         guest_web_contents_by_instance_id_.end());
  guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents;
}

void TestGuestManagerDelegate::RemoveGuest(
    int guest_instance_id) {
  GuestInstanceMap::iterator it =
      guest_web_contents_by_instance_id_.find(guest_instance_id);
  DCHECK(it != guest_web_contents_by_instance_id_.end());
  guest_web_contents_by_instance_id_.erase(it);
}

void TestGuestManagerDelegate::MaybeGetGuestByInstanceIDOrKill(
    int guest_instance_id,
    int embedder_render_process_id,
    const GuestByInstanceIDCallback& callback) {
  GuestInstanceMap::const_iterator it =
      guest_web_contents_by_instance_id_.find(guest_instance_id);
  if (it == guest_web_contents_by_instance_id_.end()) {
    callback.Run(NULL);
    return;
  }
  callback.Run(it->second);
}

SiteInstance* TestGuestManagerDelegate::GetGuestSiteInstance(
  const GURL& guest_site) {
  for (GuestInstanceMap::const_iterator it =
       guest_web_contents_by_instance_id_.begin();
       it != guest_web_contents_by_instance_id_.end(); ++it) {
    if (it->second->GetSiteInstance()->GetSiteURL() == guest_site)
      return it->second->GetSiteInstance();
  }
  return NULL;
}

bool TestGuestManagerDelegate::ForEachGuest(
    WebContents* embedder_web_contents,
    const GuestCallback& callback) {
  for (GuestInstanceMap::iterator it =
           guest_web_contents_by_instance_id_.begin();
       it != guest_web_contents_by_instance_id_.end(); ++it) {
    WebContents* guest = it->second;
    if (embedder_web_contents != guest->GetEmbedderWebContents())
      continue;

    if (callback.Run(guest))
      return true;
  }
  return false;
}

}  // namespace content