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
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/quick_answers/translation_result_loader.h"
#include <string_view>
#include <utility>
#include "base/json/json_writer.h"
#include "base/strings/escape.h"
#include "base/strings/utf_string_conversions.h"
#include "chromeos/components/quick_answers/quick_answers_model.h"
#include "chromeos/components/quick_answers/utils/quick_answers_utils.h"
#include "chromeos/services/assistant/public/shared/constants.h"
#include "google_apis/google_api_keys.h"
#include "net/base/url_util.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "ui/base/resource/resource_bundle.h"
#include "url/gurl.h"
namespace quick_answers {
namespace {
using base::Value;
// The JSON we generate looks like this:
// {
// "q": [
// "test input"
// ],
// "source": "en",
// "target": "zh"
// }
constexpr char kTranslationAPIUrl[] =
"https://translation.googleapis.com/language/translate/v2";
constexpr char kApiKeyName[] = "key";
constexpr std::string_view kQueryKey = "q";
constexpr std::string_view kSourceLanguageKey = "source";
constexpr std::string_view kTargetLanguageKey = "target";
std::string BuildTranslationRequestBody(const IntentInfo& intent_info) {
Value::Dict payload;
Value::List query;
query.Append(intent_info.intent_text);
payload.Set(kQueryKey, std::move(query));
payload.Set(kSourceLanguageKey, intent_info.source_language);
payload.Set(kTargetLanguageKey, intent_info.device_language);
std::string request_payload_str;
base::JSONWriter::Write(payload, &request_payload_str);
return request_payload_str;
}
} // namespace
TranslationResultLoader::TranslationResultLoader(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
ResultLoaderDelegate* delegate)
: ResultLoader(url_loader_factory, delegate) {}
TranslationResultLoader::~TranslationResultLoader() = default;
void TranslationResultLoader::BuildRequest(
const PreprocessedOutput& preprocessed_output,
BuildRequestCallback callback) const {
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = net::AppendQueryParameter(
GURL(kTranslationAPIUrl), kApiKeyName, google_apis::GetAPIKey());
resource_request->method = net::HttpRequestHeaders::kPostMethod;
resource_request->headers.SetHeader(net::HttpRequestHeaders::kAccept,
"application/json");
std::move(callback).Run(
std::move(resource_request),
BuildTranslationRequestBody(preprocessed_output.intent_info));
}
void TranslationResultLoader::ProcessResponse(
const PreprocessedOutput& preprocessed_output,
std::unique_ptr<std::string> response_body,
ResponseParserCallback complete_callback) {
if (translation_response_parser_) {
DCHECK(false) << "translation_response_parser_ must be nullptr";
std::move(complete_callback).Run(nullptr);
return;
}
translation_response_parser_ =
std::make_unique<TranslationResponseParser>(base::BindOnce(
&TranslationResultLoader::ProcessParsedResponse,
weak_ptr_factory_.GetWeakPtr(), preprocessed_output.intent_info,
std::move(complete_callback)));
translation_response_parser_->ProcessResponse(std::move(response_body));
}
void TranslationResultLoader::ProcessParsedResponse(
IntentInfo intent_info,
ResponseParserCallback complete_callback,
std::unique_ptr<TranslationResult> translation_result) {
translation_response_parser_.reset();
if (!translation_result || translation_result->translated_text.empty()) {
std::move(complete_callback).Run(nullptr);
return;
}
translation_result->text_to_translate = intent_info.intent_text;
translation_result->source_locale = intent_info.source_language;
translation_result->target_locale = intent_info.device_language;
std::unique_ptr<QuickAnswer> quick_answer = std::make_unique<QuickAnswer>();
quick_answer->result_type = ResultType::kTranslationResult;
quick_answer->title.push_back(std::make_unique<QuickAnswerText>(
BuildTranslationTitleText(intent_info)));
quick_answer->first_answer_row.push_back(
std::make_unique<QuickAnswerResultText>(
translation_result->translated_text));
std::unique_ptr<QuickAnswersSession> session =
std::make_unique<QuickAnswersSession>();
session->structured_result = std::make_unique<StructuredResult>();
session->structured_result->translation_result =
std::move(translation_result);
session->quick_answer = std::move(quick_answer);
std::move(complete_callback).Run(std::move(session));
}
} // namespace quick_answers
|