Jing Wang | 961b8af | 2020-10-26 12:40:35 +1100 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ml/grammar_checker_impl.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "ml/grammar_proto_mojom_conversion.h" |
| 11 | #include "ml/request_metrics.h" |
| 12 | |
| 13 | namespace ml { |
| 14 | namespace { |
| 15 | |
| 16 | using ::chromeos::machine_learning::mojom::GrammarChecker; |
| 17 | using ::chromeos::machine_learning::mojom::GrammarCheckerCandidatePtr; |
| 18 | using ::chromeos::machine_learning::mojom::GrammarCheckerQueryPtr; |
| 19 | using ::chromeos::machine_learning::mojom::GrammarCheckerResult; |
| 20 | |
| 21 | } // namespace |
| 22 | |
| 23 | bool GrammarCheckerImpl::Create( |
| 24 | mojo::PendingReceiver<GrammarChecker> receiver) { |
| 25 | auto checker_impl = new GrammarCheckerImpl(std::move(receiver)); |
| 26 | |
| 27 | // Set the disconnection handler to strongly bind `checker_impl` to delete |
| 28 | // `checker_impl` when the connection is gone. |
| 29 | checker_impl->receiver_.set_disconnect_handler(base::Bind( |
| 30 | [](const GrammarCheckerImpl* const checker_impl) { delete checker_impl; }, |
| 31 | base::Unretained(checker_impl))); |
| 32 | |
| 33 | return checker_impl->successfully_loaded_; |
| 34 | } |
| 35 | |
| 36 | GrammarCheckerImpl::GrammarCheckerImpl( |
| 37 | mojo::PendingReceiver<GrammarChecker> receiver) |
| 38 | : library_(ml::GrammarLibrary::GetInstance()), |
| 39 | receiver_(this, std::move(receiver)) { |
| 40 | DCHECK(library_->GetStatus() == ml::GrammarLibrary::Status::kOk) |
| 41 | << "GrammarCheckerImpl should be created only if GrammarLibrary is " |
| 42 | "initialized successfully."; |
| 43 | |
| 44 | checker_ = library_->CreateGrammarChecker(); |
| 45 | |
| 46 | successfully_loaded_ = library_->LoadGrammarChecker(checker_); |
| 47 | } |
| 48 | |
| 49 | GrammarCheckerImpl::~GrammarCheckerImpl() { |
| 50 | library_->DestroyGrammarChecker(checker_); |
| 51 | } |
| 52 | |
| 53 | void GrammarCheckerImpl::Check(GrammarCheckerQueryPtr query, |
| 54 | CheckCallback callback) { |
| 55 | RequestMetrics request_metrics("GrammarChecker", "Check"); |
| 56 | request_metrics.StartRecordingPerformanceMetrics(); |
| 57 | |
| 58 | chrome_knowledge::GrammarCheckerResult result_proto; |
| 59 | |
| 60 | if (library_->CheckGrammar(checker_, |
| 61 | GrammarCheckerQueryToProto(std::move(query)), |
| 62 | &result_proto)) { |
| 63 | // Check succeeded, run callback on the result. |
| 64 | std::move(callback).Run(GrammarCheckerResultFromProto(result_proto)); |
| 65 | request_metrics.FinishRecordingPerformanceMetrics(); |
| 66 | request_metrics.RecordRequestEvent(GrammarCheckerResult::Status::OK); |
| 67 | } else { |
| 68 | // Check failed, run callback on empty result and status = ERROR. |
| 69 | std::move(callback).Run( |
| 70 | GrammarCheckerResult::New(GrammarCheckerResult::Status::ERROR, |
| 71 | std::vector<GrammarCheckerCandidatePtr>())); |
| 72 | request_metrics.RecordRequestEvent(GrammarCheckerResult::Status::ERROR); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | } // namespace ml |