summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/extensions/api/media_perception_private/media_perception_api_delegate_chromeos.cc
blob: 69ddd3fbc6abf9ab7d623fa2a282ba1bb6175657 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// 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 "chrome/browser/extensions/api/media_perception_private/media_perception_api_delegate_chromeos.h"

#include <string>
#include <utility>

#include "base/bind.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/component_updater/cros_component_installer_chromeos.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/chromeos/delegate_to_browser_gpu_service_accelerator_factory.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/video_capture_service.h"
#include "mojo/public/cpp/bindings/strong_binding.h"
#include "services/video_capture/public/mojom/device_factory.mojom.h"

namespace extensions {

namespace {

constexpr char kLightComponentName[] = "rtanalytics-light";
constexpr char kFullComponentName[] = "rtanalytics-full";

std::string GetComponentNameForComponentType(
    const extensions::api::media_perception_private::ComponentType& type) {
  switch (type) {
    case extensions::api::media_perception_private::COMPONENT_TYPE_LIGHT:
      return kLightComponentName;
    case extensions::api::media_perception_private::COMPONENT_TYPE_FULL:
      return kFullComponentName;
    case extensions::api::media_perception_private::COMPONENT_TYPE_NONE:
      LOG(ERROR) << "No component type requested.";
      return "";
  }
  NOTREACHED() << "Reached component type not in switch.";
  return "";
}

api::media_perception_private::ComponentInstallationError
GetComponentInstallationErrorForCrOSComponentManagerError(
    const component_updater::CrOSComponentManager::Error error) {
  switch (error) {
    case component_updater::CrOSComponentManager::Error::ERROR_MAX:
    case component_updater::CrOSComponentManager::Error::NONE:
      return api::media_perception_private::COMPONENT_INSTALLATION_ERROR_NONE;
    case component_updater::CrOSComponentManager::Error::UNKNOWN_COMPONENT:
      return api::media_perception_private::
          COMPONENT_INSTALLATION_ERROR_UNKNOWN_COMPONENT;
    case component_updater::CrOSComponentManager::Error::INSTALL_FAILURE:
      return api::media_perception_private::
          COMPONENT_INSTALLATION_ERROR_INSTALL_FAILURE;
    case component_updater::CrOSComponentManager::Error::MOUNT_FAILURE:
      return api::media_perception_private::
          COMPONENT_INSTALLATION_ERROR_MOUNT_FAILURE;
    case component_updater::CrOSComponentManager::Error::
        COMPATIBILITY_CHECK_FAILED:
      return api::media_perception_private::
          COMPONENT_INSTALLATION_ERROR_COMPATIBILITY_CHECK_FAILED;
    case component_updater::CrOSComponentManager::Error::NOT_FOUND:
      return api::media_perception_private::
          COMPONENT_INSTALLATION_ERROR_NOT_FOUND;
  }
  NOTREACHED() << "Reached component error type not in switch.";
  return api::media_perception_private::COMPONENT_INSTALLATION_ERROR_NONE;
}

void OnLoadComponent(
    MediaPerceptionAPIDelegate::LoadCrOSComponentCallback load_callback,
    component_updater::CrOSComponentManager::Error error,
    const base::FilePath& mount_point) {
  std::move(load_callback)
      .Run(GetComponentInstallationErrorForCrOSComponentManagerError(error),
           mount_point);
}

}  // namespace

MediaPerceptionAPIDelegateChromeOS::MediaPerceptionAPIDelegateChromeOS() =
    default;

MediaPerceptionAPIDelegateChromeOS::~MediaPerceptionAPIDelegateChromeOS() {}

void MediaPerceptionAPIDelegateChromeOS::LoadCrOSComponent(
    const extensions::api::media_perception_private::ComponentType& type,
    LoadCrOSComponentCallback load_callback) {
  g_browser_process->platform_part()->cros_component_manager()->Load(
      GetComponentNameForComponentType(type),
      component_updater::CrOSComponentManager::MountPolicy::kMount,
      component_updater::CrOSComponentManager::UpdatePolicy::kDontForce,
      base::BindOnce(OnLoadComponent, std::move(load_callback)));
}

void MediaPerceptionAPIDelegateChromeOS::BindVideoSourceProvider(
    mojo::PendingReceiver<video_capture::mojom::VideoSourceProvider> receiver) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  video_capture::mojom::AcceleratorFactoryPtr accelerator_factory;
  mojo::MakeStrongBinding(
      std::make_unique<
          content::DelegateToBrowserGpuServiceAcceleratorFactory>(),
      mojo::MakeRequest(&accelerator_factory));

  auto& service = content::GetVideoCaptureService();
  service.InjectGpuDependencies(std::move(accelerator_factory));
  service.ConnectToVideoSourceProvider(std::move(receiver));
}

void MediaPerceptionAPIDelegateChromeOS::SetMediaPerceptionRequestHandler(
    MediaPerceptionRequestHandler handler) {
  handler_ = std::move(handler);
}

void MediaPerceptionAPIDelegateChromeOS::ForwardMediaPerceptionRequest(
    chromeos::media_perception::mojom::MediaPerceptionRequest request,
    content::RenderFrameHost* render_frame_host) {
  if (!handler_) {
    DLOG(ERROR) << "Got request but the handler is not set.";
    return;
  }
  handler_.Run(std::move(request));
}

}  // namespace extensions