summaryrefslogtreecommitdiffstats
path: root/extensions/browser/mojo/keep_alive_impl.cc
blob: 8f175822132f96cf29c581c85e7c8677ae7f0a17 (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
// 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 "extensions/browser/mojo/keep_alive_impl.h"

#include <utility>

#include "base/bind.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/process_manager.h"

namespace extensions {

// static
void KeepAliveImpl::Create(content::BrowserContext* context,
                           const Extension* extension,
                           mojo::InterfaceRequest<KeepAlive> request) {
  new KeepAliveImpl(context, extension, std::move(request));
}

KeepAliveImpl::KeepAliveImpl(content::BrowserContext* context,
                             const Extension* extension,
                             mojo::InterfaceRequest<KeepAlive> request)
    : context_(context),
      extension_(extension),
      extension_registry_observer_(this),
      binding_(this, std::move(request)) {
  ProcessManager::Get(context_)->IncrementLazyKeepaliveCount(extension_);
  binding_.set_connection_error_handler(
      base::Bind(&KeepAliveImpl::OnDisconnected, base::Unretained(this)));
  extension_registry_observer_.Add(ExtensionRegistry::Get(context_));
}

KeepAliveImpl::~KeepAliveImpl() = default;

void KeepAliveImpl::OnExtensionUnloaded(
    content::BrowserContext* browser_context,
    const Extension* extension,
    UnloadedExtensionInfo::Reason reason) {
  if (browser_context == context_ && extension == extension_)
    delete this;
}

void KeepAliveImpl::OnShutdown(ExtensionRegistry* registry) {
  delete this;
}

void KeepAliveImpl::OnDisconnected() {
  ProcessManager::Get(context_)->DecrementLazyKeepaliveCount(extension_);
}

}  // namespace extensions