forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape_detection_service.cc
105 lines (92 loc) · 3.65 KB
/
shape_detection_service.cc
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2017 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 "services/shape_detection/shape_detection_service.h"
#include <string>
#include <utility>
#include "base/bind.h"
#include "base/macros.h"
#include "services/service_manager/public/cpp/service_context.h"
#if defined(OS_WIN)
#include "services/shape_detection/barcode_detection_provider_impl.h"
#include "services/shape_detection/face_detection_provider_win.h"
#elif defined(OS_MACOSX)
#include <dlfcn.h>
#include "services/shape_detection/barcode_detection_provider_mac.h"
#include "services/shape_detection/face_detection_provider_mac.h"
#else
#include "services/shape_detection/barcode_detection_provider_impl.h"
#include "services/shape_detection/face_detection_provider_impl.h"
#endif
#include "services/shape_detection/text_detection_impl.h"
#if defined(OS_ANDROID)
#include "base/android/jni_android.h"
#include "jni/InterfaceRegistrar_jni.h"
#endif
namespace shape_detection {
std::unique_ptr<service_manager::Service> ShapeDetectionService::Create() {
return std::make_unique<ShapeDetectionService>();
}
ShapeDetectionService::ShapeDetectionService() {
#if defined(OS_MACOSX)
if (__builtin_available(macOS 10.13, *)) {
vision_framework_ =
dlopen("/System/Library/Frameworks/Vision.framework/Vision", RTLD_LAZY);
}
#endif
}
ShapeDetectionService::~ShapeDetectionService() {
#if defined(OS_MACOSX)
if (__builtin_available(macOS 10.13, *)) {
if (vision_framework_)
dlclose(vision_framework_);
}
#endif
}
void ShapeDetectionService::OnStart() {
ref_factory_.reset(new service_manager::ServiceContextRefFactory(
context()->CreateQuitClosure()));
#if defined(OS_ANDROID)
registry_.AddInterface(
GetJavaInterfaces()
->CreateInterfaceFactory<mojom::BarcodeDetectionProvider>());
registry_.AddInterface(
GetJavaInterfaces()
->CreateInterfaceFactory<mojom::FaceDetectionProvider>());
registry_.AddInterface(
GetJavaInterfaces()->CreateInterfaceFactory<mojom::TextDetection>());
#elif defined(OS_WIN)
registry_.AddInterface(base::Bind(&BarcodeDetectionProviderImpl::Create));
registry_.AddInterface(base::Bind(&TextDetectionImpl::Create));
registry_.AddInterface(base::Bind(&FaceDetectionProviderWin::Create));
#elif defined(OS_MACOSX)
registry_.AddInterface(base::Bind(&BarcodeDetectionProviderMac::Create));
registry_.AddInterface(base::Bind(&TextDetectionImpl::Create));
registry_.AddInterface(base::Bind(&FaceDetectionProviderMac::Create));
#else
registry_.AddInterface(base::Bind(&BarcodeDetectionProviderImpl::Create));
registry_.AddInterface(base::Bind(&FaceDetectionProviderImpl::Create));
registry_.AddInterface(base::Bind(&TextDetectionImpl::Create));
#endif
}
void ShapeDetectionService::OnBindInterface(
const service_manager::BindSourceInfo& source_info,
const std::string& interface_name,
mojo::ScopedMessagePipeHandle interface_pipe) {
registry_.BindInterface(interface_name, std::move(interface_pipe));
}
#if defined(OS_ANDROID)
service_manager::InterfaceProvider* ShapeDetectionService::GetJavaInterfaces() {
if (!java_interface_provider_) {
service_manager::mojom::InterfaceProviderPtr provider;
Java_InterfaceRegistrar_createInterfaceRegistryForContext(
base::android::AttachCurrentThread(),
mojo::MakeRequest(&provider).PassMessagePipe().release().value());
java_interface_provider_ =
std::make_unique<service_manager::InterfaceProvider>();
java_interface_provider_->Bind(std::move(provider));
}
return java_interface_provider_.get();
}
#endif
} // namespace shape_detection