File: arc_tts_service.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (157 lines) | stat: -rw-r--r-- 5,325 bytes parent folder | download | duplicates (6)
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
// Copyright 2016 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/arc/tts/arc_tts_service.h"

#include <utility>

#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "chrome/browser/speech/tts_chromeos.h"
#include "chromeos/ash/experiences/arc/arc_browser_context_keyed_service_factory_base.h"
#include "chromeos/ash/experiences/arc/session/arc_bridge_service.h"
#include "content/public/browser/tts_controller.h"
#include "third_party/icu/source/common/unicode/uloc.h"

namespace arc {
namespace {

// Singleton factory for ArcTtsService.
class ArcTtsServiceFactory
    : public internal::ArcBrowserContextKeyedServiceFactoryBase<
          ArcTtsService,
          ArcTtsServiceFactory> {
 public:
  // Factory name used by ArcBrowserContextKeyedServiceFactoryBase.
  static constexpr const char* kName = "ArcTtsServiceFactory";

  static ArcTtsServiceFactory* GetInstance() {
    return base::Singleton<ArcTtsServiceFactory>::get();
  }

 private:
  friend base::DefaultSingletonTraits<ArcTtsServiceFactory>;
  ArcTtsServiceFactory() = default;
  ~ArcTtsServiceFactory() override = default;
};

std::string CanonicalizeLocale(std::string locale) {
  UErrorCode error = U_ZERO_ERROR;

  // We only ever expect a maximum of a three-letter language code, a separator,
  // followed by a three-letter country code.
  static constexpr int kBufferSize = 8;
  std::string canonical_locale;
  int actual_size = uloc_canonicalize(
      locale.c_str(), base::WriteInto(&canonical_locale, kBufferSize),
      kBufferSize, &error);

  if (actual_size == 0 || error != U_ZERO_ERROR)
    return locale;

  canonical_locale.resize(actual_size);
  return canonical_locale;
}

}  // namespace

// static
ArcTtsService* ArcTtsService::GetForBrowserContext(
    content::BrowserContext* context) {
  return ArcTtsServiceFactory::GetForBrowserContext(context);
}

// static
ArcTtsService* ArcTtsService::GetForBrowserContextForTesting(
    content::BrowserContext* context) {
  return ArcTtsServiceFactory::GetForBrowserContextForTesting(context);
}

ArcTtsService::ArcTtsService(content::BrowserContext* context,
                             ArcBridgeService* bridge_service)
    : arc_bridge_service_(bridge_service), tts_controller_(nullptr) {
  arc_bridge_service_->tts()->SetHost(this);
}

ArcTtsService::~ArcTtsService() {
  arc_bridge_service_->tts()->SetHost(nullptr);
}

void ArcTtsService::OnTtsEvent(uint32_t id,
                               mojom::TtsEventType event_type,
                               uint32_t char_index,
                               uint32_t length,
                               const std::string& error_msg) {
  if (!tts_controller_) {
    // GetInstance() returns a base::Singleton<> object which always outlives
    // |this| object.
    tts_controller_ = content::TtsController::GetInstance();
    if (!tts_controller_) {
      LOG(WARNING) << "TtsController is not available.";
      return;
    }
  }

  content::TtsEventType chrome_event_type;
  switch (event_type) {
    case mojom::TtsEventType::START:
      chrome_event_type = content::TTS_EVENT_START;
      break;
    case mojom::TtsEventType::END:
      chrome_event_type = content::TTS_EVENT_END;
      break;
    case mojom::TtsEventType::INTERRUPTED:
      chrome_event_type = content::TTS_EVENT_INTERRUPTED;
      break;
    case mojom::TtsEventType::ERROR:
      chrome_event_type = content::TTS_EVENT_ERROR;
      break;
    case mojom::TtsEventType::WORD:
      chrome_event_type = content::TTS_EVENT_WORD;
      TtsPlatformImplChromeOs::GetInstance()->ReceivedWordEvent();
  }
  tts_controller_->OnTtsEvent(id, chrome_event_type, char_index, length,
                              error_msg);
}

void ArcTtsService::OnVoicesChanged(std::vector<mojom::TtsVoicePtr> voices) {
  std::vector<content::VoiceData> chrome_voices;
  for (const auto& voice : voices) {
    chrome_voices.emplace_back();
    content::VoiceData& chrome_voice = chrome_voices.back();
    chrome_voice.native = true;
    chrome_voice.native_voice_identifier = base::NumberToString(voice->id);
    chrome_voice.name = std::move(voice->name);

    // Normalizes using ICU; in particular, turns language codes from three to
    // two letter codes. Then, replaces _ with - (expected by Chrome's tts
    // controller).
    chrome_voice.lang = CanonicalizeLocale(std::move(voice->locale));
    for (size_t i = 0; i < chrome_voice.lang.size(); i++) {
      if (chrome_voice.lang[i] == '_')
        chrome_voice.lang[i] = '-';
    }

    chrome_voice.remote = voice->is_network_connection_required;
    chrome_voice.events.insert(content::TTS_EVENT_START);
    chrome_voice.events.insert(content::TTS_EVENT_END);
    chrome_voice.events.insert(content::TTS_EVENT_INTERRUPTED);
    chrome_voice.events.insert(content::TTS_EVENT_ERROR);
  }

  TtsPlatformImplChromeOs* impl = TtsPlatformImplChromeOs::GetInstance();
  DCHECK(impl);
  impl->SetVoices(std::move(chrome_voices));

  content::TtsController::GetInstance()->VoicesChanged();
}

// static
void ArcTtsService::EnsureFactoryBuilt() {
  ArcTtsServiceFactory::GetInstance();
}

}  // namespace arc