summaryrefslogtreecommitdiffstats
path: root/extensions/browser/mojo/service_registration.cc
blob: c6367f070f9a169e1f47783b2aa58cb78c503ac0 (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
// Copyright 2015 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/service_registration.h"

#include <string>

#include "base/bind.h"
#include "base/command_line.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/site_instance.h"
#include "content/public/common/service_registry.h"
#include "extensions/browser/api/serial/serial_service_factory.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/mojo/keep_alive_impl.h"
#include "extensions/browser/process_map.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension_api.h"
#include "extensions/common/switches.h"

#if defined(ENABLE_WIFI_DISPLAY)
#include "extensions/browser/api/display_source/wifi_display/wifi_display_session_service_impl.h"
#endif

namespace extensions {
namespace {

bool ExtensionHasPermission(const Extension* extension,
                            content::RenderProcessHost* render_process_host,
                            const std::string& permission_name) {
  Feature::Context context =
      ProcessMap::Get(render_process_host->GetBrowserContext())
          ->GetMostLikelyContextType(extension, render_process_host->GetID());

  return ExtensionAPI::GetSharedInstance()
      ->IsAvailable(permission_name, extension, context, extension->url())
      .is_available();
}

}  // namespace

void RegisterServicesForFrame(content::RenderFrameHost* render_frame_host,
                              const Extension* extension) {
  DCHECK(extension);

  content::ServiceRegistry* service_registry =
      render_frame_host->GetServiceRegistry();
  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kEnableMojoSerialService)) {
    if (ExtensionHasPermission(extension, render_frame_host->GetProcess(),
                               "serial")) {
      service_registry->AddService(base::Bind(&BindToSerialServiceRequest));
    }
  }
  service_registry->AddService(base::Bind(
      KeepAliveImpl::Create,
      render_frame_host->GetProcess()->GetBrowserContext(), extension));

#if defined(ENABLE_WIFI_DISPLAY)
  if (ExtensionHasPermission(extension, render_frame_host->GetProcess(),
                             "displaySource")) {
    service_registry->AddService(
        base::Bind(WiFiDisplaySessionServiceImpl::BindToRequest,
                   render_frame_host->GetProcess()->GetBrowserContext()));
  }
#endif
}

}  // namespace extensions