File: chrome_extensions_browser_interface_binders.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (282 lines) | stat: -rw-r--r-- 12,642 bytes parent folder | download | duplicates (4)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/chrome_extensions_browser_interface_binders.h"

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "build/branding_buildflags.h"
#include "build/chromeos_buildflags.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "extensions/browser/api/mime_handler_private/mime_handler_private.h"
#include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
#include "extensions/common/api/mime_handler.mojom.h"
#include "extensions/common/extension.h"
#include "extensions/common/permissions/api_permission.h"
#include "extensions/common/permissions/permissions_data.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/webui/camera_app_ui/camera_app_ui.h"
#include "build/config/chromebox_for_meetings/buildflags.h"
#include "chrome/browser/ash/remote_apps/remote_apps_manager.h"
#include "chrome/browser/ash/remote_apps/remote_apps_manager_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/speech/extension_api/tts_engine_extension_observer_chromeos.h"
#include "chrome/browser/speech/extension_api/tts_engine_extension_observer_chromeos_factory.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chromeos/ash/components/enhanced_network_tts/enhanced_network_tts_impl.h"
#include "chromeos/ash/components/enhanced_network_tts/mojom/enhanced_network_tts.mojom.h"
#include "chromeos/ash/components/language_packs/language_packs_impl.h"
#include "chromeos/ash/components/language_packs/public/mojom/language_packs.mojom.h"
#include "chromeos/components/remote_apps/mojom/remote_apps.mojom.h"
#include "chromeos/services/chromebox_for_meetings/public/cpp/appid_util.h"
#include "chromeos/services/chromebox_for_meetings/public/cpp/service_connection.h"
#include "chromeos/services/chromebox_for_meetings/public/mojom/cfm_service_manager.mojom.h"
#include "chromeos/services/chromebox_for_meetings/public/mojom/xu_camera.mojom.h"
#include "chromeos/services/media_perception/public/mojom/media_perception.mojom.h"
#include "chromeos/services/tts/public/mojom/tts_service.mojom.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/api/media_perception_private/media_perception_api_delegate.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "ui/accessibility/accessibility_features.h"

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
#include "chromeos/ash/services/ime/public/mojom/input_engine.mojom.h"
#include "chromeos/services/machine_learning/public/cpp/service_connection.h"  // nogncheck
#include "ui/base/ime/ash/extension_ime_util.h"
#include "ui/base/ime/ash/input_method_manager.h"
#endif

#if BUILDFLAG(PLATFORM_CFM)
#include "chrome/browser/ash/chromebox_for_meetings/meet_browser/meet_browser_service.h"
#include "chrome/browser/ash/chromebox_for_meetings/xu_camera/xu_camera_service.h"
#include "chromeos/ash/components/chromebox_for_meetings/features.h"
#endif
#endif  // BUILDFLAG(IS_CHROMEOS)

namespace extensions {

namespace {
#if BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
// Resolves InputEngineManager receiver in InputMethodManager.
void BindInputEngineManager(
    content::RenderFrameHost* render_frame_host,
    mojo::PendingReceiver<ash::ime::mojom::InputEngineManager> receiver) {
  ash::input_method::InputMethodManager::Get()->ConnectInputEngineManager(
      std::move(receiver));
}

void BindMachineLearningService(
    content::RenderFrameHost* render_frame_host,
    mojo::PendingReceiver<
        chromeos::machine_learning::mojom::MachineLearningService> receiver) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  chromeos::machine_learning::ServiceConnection::GetInstance()
      ->BindMachineLearningService(std::move(receiver));
}
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)

void BindLanguagePacks(
    content::RenderFrameHost* render_frame_host,
    mojo::PendingReceiver<ash::language::mojom::LanguagePacks> receiver) {
  ash::language_packs::LanguagePacksImpl::GetInstance().BindReceiver(
      std::move(receiver));
}

void BindGoogleTtsStream(
    content::RenderFrameHost* render_frame_host,
    mojo::PendingReceiver<chromeos::tts::mojom::GoogleTtsStream> receiver) {
  TtsEngineExtensionObserverChromeOSFactory::GetForProfile(
      Profile::FromBrowserContext(render_frame_host->GetBrowserContext()))
      ->BindGoogleTtsStream(std::move(receiver));
}

void BindEnhancedNetworkTts(
    content::RenderFrameHost* render_frame_host,
    mojo::PendingReceiver<ash::enhanced_network_tts::mojom::EnhancedNetworkTts>
        receiver) {
  ash::enhanced_network_tts::EnhancedNetworkTtsImpl::GetInstance()
      .BindReceiverAndURLFactory(
          std::move(receiver),
          Profile::FromBrowserContext(render_frame_host->GetBrowserContext())
              ->GetURLLoaderFactory());
}

void BindRemoteAppsFactory(
    content::RenderFrameHost* render_frame_host,
    mojo::PendingReceiver<chromeos::remote_apps::mojom::RemoteAppsFactory>
        pending_receiver) {
  // |remote_apps_manager| will be null for sessions that are not regular user
  // sessions or managed guest sessions. This is checked in
  // |RemoteAppsImpl::IsMojoPrivateApiAllowed()|.
  ash::RemoteAppsManager* remote_apps_manager =
      ash::RemoteAppsManagerFactory::GetForProfile(
          Profile::FromBrowserContext(render_frame_host->GetBrowserContext()));
  DCHECK(remote_apps_manager);
  remote_apps_manager->BindFactoryInterface(std::move(pending_receiver));
}

void BindCfmServiceContext(
    content::RenderFrameHost* render_frame_host,
    mojo::PendingReceiver<chromeos::cfm::mojom::CfmServiceContext> receiver) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  chromeos::cfm::ServiceConnection::GetInstance()->BindServiceContext(
      std::move(receiver));
#if BUILDFLAG(PLATFORM_CFM)
  ash::cfm::MeetBrowserService::Get()->SetMeetGlobalRenderFrameToken(
      render_frame_host->GetGlobalFrameToken());
#endif  // BUILDFLAG(PLATFORM_CFM)
}

#endif  // BUILDFLAG(IS_CHROMEOS)

void BindMimeHandlerService(
    content::RenderFrameHost* frame_host,
    mojo::PendingReceiver<mime_handler::MimeHandlerService> receiver) {
  auto* guest_view = MimeHandlerViewGuest::FromRenderFrameHost(frame_host);
  if (!guest_view) {
    return;
  }
  MimeHandlerServiceImpl::Create(guest_view->GetStreamWeakPtr(),
                                 std::move(receiver));
}

void BindBeforeUnloadControl(
    content::RenderFrameHost* frame_host,
    mojo::PendingReceiver<mime_handler::BeforeUnloadControl> receiver) {
  auto* guest_view = MimeHandlerViewGuest::FromRenderFrameHost(frame_host);
  if (!guest_view) {
    return;
  }
  guest_view->FuseBeforeUnloadControl(std::move(receiver));
}

}  // namespace

void PopulateChromeFrameBindersForExtension(
    mojo::BinderMapWithContext<content::RenderFrameHost*>* binder_map,
    content::RenderFrameHost* render_frame_host,
    const Extension* extension) {
  DCHECK(extension);

#if BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  // Register InputEngineManager for official Google ChromeOS 1P Input only.
  if (extension->id() == ash::extension_ime_util::kXkbExtensionId) {
    binder_map->Add<ash::ime::mojom::InputEngineManager>(
        base::BindRepeating(&BindInputEngineManager));
    binder_map->Add<ash::language::mojom::LanguagePacks>(
        base::BindRepeating(&BindLanguagePacks));
    binder_map->Add<chromeos::machine_learning::mojom::MachineLearningService>(
        base::BindRepeating(&BindMachineLearningService));
  }
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)

// The experimentation framework used to manage the
// `ash::cfm::features::kMojoServices` feature flag requires
// Chrome to restart before updates are applied. Meet Devices have
// a variable uptime ranging from a week or more and set by the
// admin. Additionally its kiosked process is not tied to a chromium
// release and can be dynamically updated during Chrome runtime.
// Unfortunately this makes it difficult to fully predict when the
// flag will be applied to all devices across the fleet.
// As such we proactively support the case for devices that may be
// in a different state than expected from the kiosked process.
// TODO(b/341493979): Deprecate after CfM LaCrOS migration is completed.
#if BUILDFLAG(PLATFORM_CFM)
  if (chromeos::cfm::IsChromeboxForMeetingsHashedAppId(
          extension->hashed_id().value())) {
    binder_map->Add<ash::cfm::mojom::XuCamera>(base::BindRepeating(
        [](content::RenderFrameHost* frame_host,
           mojo::PendingReceiver<ash::cfm::mojom::XuCamera> receiver) {
          if (base::FeatureList::IsEnabled(ash::cfm::features::kXuControls)) {
            ash::cfm::XuCameraService::Get()->BindServiceContext(
                std::move(receiver), frame_host->GetGlobalId());
          } else {
            receiver.ResetWithReason(
                static_cast<uint32_t>(
                    chromeos::cfm::mojom::DisconnectReason::kFinchDisabledCode),
                chromeos::cfm::mojom::DisconnectReason::kFinchDisabledMessage);
          }
        }));
  }
#endif  // BUILDFLAG(PLATFORM_CFM)

  if (extension->permissions_data()->HasAPIPermission(
          mojom::APIPermissionID::kMediaPerceptionPrivate)) {
    extensions::ExtensionsAPIClient* client =
        extensions::ExtensionsAPIClient::Get();
    extensions::MediaPerceptionAPIDelegate* delegate = nullptr;
    if (client) {
      delegate = client->GetMediaPerceptionAPIDelegate();
    }
    if (delegate) {
      // Note that it is safe to use base::Unretained here because |delegate| is
      // owned by the |client|, which is instantiated by the
      // ChromeExtensionsBrowserClient, which in turn is owned and lives as long
      // as the BrowserProcessImpl.
      binder_map->Add<chromeos::media_perception::mojom::MediaPerception>(
          base::BindRepeating(&extensions::MediaPerceptionAPIDelegate::
                                  ForwardMediaPerceptionReceiver,
                              base::Unretained(delegate)));
    }
  }

  if (extension->id() == extension_misc::kGoogleSpeechSynthesisExtensionId) {
    binder_map->Add<chromeos::tts::mojom::GoogleTtsStream>(
        base::BindRepeating(&BindGoogleTtsStream));
    binder_map->Add<ash::language::mojom::LanguagePacks>(
        base::BindRepeating(&BindLanguagePacks));
  }

  // Limit the binding to EnhancedNetworkTts Extension.
  if (extension->id() == extension_misc::kEnhancedNetworkTtsExtensionId) {
    binder_map->Add<ash::enhanced_network_tts::mojom::EnhancedNetworkTts>(
        base::BindRepeating(&BindEnhancedNetworkTts));
  }

  if (ash::RemoteAppsImpl::IsMojoPrivateApiAllowed(render_frame_host,
                                                   extension)) {
    binder_map->Add<chromeos::remote_apps::mojom::RemoteAppsFactory>(
        base::BindRepeating(&BindRemoteAppsFactory));
  }

  // Only allow specific extensions to bind CfmServiceContext
  if (chromeos::cfm::IsChromeboxForMeetingsHashedAppId(
          extension->hashed_id().value())) {
    binder_map->Add<chromeos::cfm::mojom::CfmServiceContext>(
        base::BindRepeating(&BindCfmServiceContext));

#if !BUILDFLAG(PLATFORM_CFM)
    // On first launch some older devices may be running on none-CfM
    // images. For those devices reject all requests until they are
    // rebooted to the CfM image variant for their device.
    // This applies to LaCrOS and none CfM Ash builds
    // TODO(crbug.com/341493979): Deprecate after CfM LaCrOS migration.
    binder_map->Add<ash::cfm::mojom::XuCamera>(base::BindRepeating(
        [](content::RenderFrameHost* frame_host,
           mojo::PendingReceiver<ash::cfm::mojom::XuCamera> receiver) {
          receiver.ResetWithReason(
              static_cast<uint32_t>(chromeos::cfm::mojom::DisconnectReason::
                                        kServiceUnavailableCode),
              chromeos::cfm::mojom::DisconnectReason::
                  kServiceUnavailableMessage);
        }));
#endif  // BUILDFLAG(PLATFORM_CFM)
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

  binder_map->Add<mime_handler::MimeHandlerService>(
      base::BindRepeating(&BindMimeHandlerService));
  binder_map->Add<mime_handler::BeforeUnloadControl>(
      base::BindRepeating(&BindBeforeUnloadControl));
}

}  // namespace extensions