ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / extensions / browser / mojo / service_registration.cc
blob6b1c3d808de162f1640002e927011875a9416115
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/browser/mojo/service_registration.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "content/public/browser/render_frame_host.h"
12 #include "content/public/browser/render_process_host.h"
13 #include "content/public/browser/site_instance.h"
14 #include "content/public/common/service_registry.h"
15 #include "extensions/browser/api/serial/serial_service_factory.h"
16 #include "extensions/browser/extension_registry.h"
17 #include "extensions/browser/extensions_browser_client.h"
18 #include "extensions/browser/mojo/keep_alive_impl.h"
19 #include "extensions/browser/process_map.h"
20 #include "extensions/common/constants.h"
21 #include "extensions/common/extension_api.h"
22 #include "extensions/common/switches.h"
24 namespace extensions {
25 namespace {
27 const Extension* GetExtension(content::RenderFrameHost* render_frame_host) {
28 content::SiteInstance* site_instance = render_frame_host->GetSiteInstance();
29 GURL url = render_frame_host->GetLastCommittedURL();
30 if (!url.is_empty()) {
31 if (site_instance->GetSiteURL().GetOrigin() != url.GetOrigin())
32 return nullptr;
33 } else {
34 url = site_instance->GetSiteURL();
36 content::BrowserContext* browser_context = site_instance->GetBrowserContext();
37 if (!url.SchemeIs(kExtensionScheme))
38 return nullptr;
40 return ExtensionRegistry::Get(browser_context)
41 ->enabled_extensions()
42 .GetExtensionOrAppByURL(url);
45 bool ExtensionHasPermission(const Extension* extension,
46 content::RenderProcessHost* render_process_host,
47 const std::string& permission_name) {
48 Feature::Context context =
49 ProcessMap::Get(render_process_host->GetBrowserContext())
50 ->GetMostLikelyContextType(extension, render_process_host->GetID());
52 return ExtensionAPI::GetSharedInstance()
53 ->IsAvailable(permission_name, extension, context, extension->url())
54 .is_available();
57 } // namespace
59 void RegisterCoreExtensionServices(
60 content::RenderFrameHost* render_frame_host) {
61 const Extension* extension = GetExtension(render_frame_host);
62 if (!extension)
63 return;
65 content::ServiceRegistry* service_registry =
66 render_frame_host->GetServiceRegistry();
67 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
68 switches::kEnableMojoSerialService)) {
69 if (ExtensionHasPermission(extension, render_frame_host->GetProcess(),
70 "serial")) {
71 service_registry->AddService(base::Bind(&BindToSerialServiceRequest));
74 service_registry->AddService(base::Bind(
75 KeepAliveImpl::Create,
76 render_frame_host->GetProcess()->GetBrowserContext(), extension));
79 } // namespace extensions