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
|
// Copyright 2021 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/ash/extensions/speech/speech_recognition_private_api.h"
#include <optional>
#include <string>
#include "chrome/browser/ash/extensions/speech/speech_recognition_private_manager.h"
#include "chrome/browser/ash/extensions/speech/speech_recognition_private_manager_factory.h"
#include "chrome/browser/speech/speech_recognition_constants.h"
#include "chrome/common/extensions/api/speech_recognition_private.h"
#include "content/public/browser/browser_context.h"
namespace extensions {
ExtensionFunction::ResponseAction SpeechRecognitionPrivateStartFunction::Run() {
// Extract arguments.
std::optional<api::speech_recognition_private::Start::Params> params =
api::speech_recognition_private::Start::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
const api::speech_recognition_private::StartOptions* options =
¶ms->options;
DCHECK(options);
std::optional<int> client_id;
std::optional<std::string> locale;
std::optional<bool> interim_results;
if (options->client_id)
client_id = *options->client_id;
if (options->locale)
locale = *options->locale;
if (options->interim_results)
interim_results = *options->interim_results;
// Get the manager for this context and ask it to handle this API call.
SpeechRecognitionPrivateManager* manager =
SpeechRecognitionPrivateManagerFactory::GetForBrowserContext(
browser_context());
const std::string key = manager->CreateKey(extension_id(), client_id);
manager->HandleStart(
key, locale, interim_results,
base::BindOnce(&SpeechRecognitionPrivateStartFunction::OnStart, this));
return RespondLater();
}
void SpeechRecognitionPrivateStartFunction::OnStart(
speech::SpeechRecognitionType type,
std::optional<std::string> error) {
if (error.has_value()) {
Respond(Error(error.value()));
return;
}
Respond(WithArguments(api::speech_recognition_private::ToString(
speech::SpeechRecognitionTypeToApiType(type))));
}
ExtensionFunction::ResponseAction SpeechRecognitionPrivateStopFunction::Run() {
// Extract arguments.
std::optional<api::speech_recognition_private::Stop::Params> params =
api::speech_recognition_private::Stop::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
const api::speech_recognition_private::StopOptions* options =
¶ms->options;
DCHECK(options);
std::optional<int> client_id;
if (options->client_id)
client_id = *options->client_id;
// Get the manager for this context and ask it to handle this API call.
SpeechRecognitionPrivateManager* manager =
SpeechRecognitionPrivateManagerFactory::GetForBrowserContext(
browser_context());
const std::string key = manager->CreateKey(extension_id(), client_id);
manager->HandleStop(
key, base::BindOnce(&SpeechRecognitionPrivateStopFunction::OnStop, this));
return RespondLater();
}
void SpeechRecognitionPrivateStopFunction::OnStop(
std::optional<std::string> error) {
if (error.has_value()) {
Respond(Error(error.value()));
return;
}
Respond(NoArguments());
}
} // namespace extensions
|