summaryrefslogtreecommitdiff
path: root/chromium/components/soda/constants.cc
blob: b7aaf75bbeee3e763c264f5ba24739faba811539 (plain)
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

// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/soda/constants.h"

#include "base/files/file_enumerator.h"
#include "base/notreached.h"
#include "base/path_service.h"
#include "components/component_updater/component_updater_paths.h"

namespace speech {

#ifdef OS_WIN
constexpr base::FilePath::CharType kSodaBinaryRelativePath[] =
    FILE_PATH_LITERAL("SODAFiles/SODA.dll");
#else
constexpr base::FilePath::CharType kSodaBinaryRelativePath[] =
    FILE_PATH_LITERAL("SODAFiles/libsoda.so");
#endif

constexpr base::FilePath::CharType kSodaInstallationRelativePath[] =
    FILE_PATH_LITERAL("SODA");

constexpr base::FilePath::CharType kSodaLanguagePacksRelativePath[] =
    FILE_PATH_LITERAL("SODALanguagePacks");

constexpr base::FilePath::CharType kSodaEnUsInstallationRelativePath[] =
    FILE_PATH_LITERAL("SODALanguagePacks/en-US");

constexpr base::FilePath::CharType kSodaJaJpInstallationRelativePath[] =
    FILE_PATH_LITERAL("SODALanguagePacks/ja-JP");

constexpr base::FilePath::CharType kSodaLanguagePackDirectoryRelativePath[] =
    FILE_PATH_LITERAL("SODAModels");

const base::FilePath GetSodaDirectory() {
  base::FilePath components_dir;
  base::PathService::Get(component_updater::DIR_COMPONENT_USER,
                         &components_dir);

  return components_dir.empty()
             ? base::FilePath()
             : components_dir.Append(kSodaInstallationRelativePath);
}

const base::FilePath GetSodaLanguagePacksDirectory() {
  base::FilePath components_dir;
  base::PathService::Get(component_updater::DIR_COMPONENT_USER,
                         &components_dir);

  return components_dir.empty()
             ? base::FilePath()
             : components_dir.Append(kSodaLanguagePacksRelativePath);
}

const base::FilePath GetLatestSodaDirectory() {
  base::FileEnumerator enumerator(GetSodaDirectory(), false,
                                  base::FileEnumerator::DIRECTORIES);
  base::FilePath latest_version_dir;
  for (base::FilePath version_dir = enumerator.Next(); !version_dir.empty();
       version_dir = enumerator.Next()) {
    latest_version_dir =
        latest_version_dir < version_dir ? version_dir : latest_version_dir;
  }

  return latest_version_dir;
}

const base::FilePath GetSodaBinaryPath() {
  base::FilePath soda_dir = GetLatestSodaDirectory();
  return soda_dir.empty() ? base::FilePath()
                          : soda_dir.Append(kSodaBinaryRelativePath);
}

LanguageCode GetLanguageCode(std::string language) {
  if (language.empty()) {
    return LanguageCode::kNone;
  }

  if (language == "en-US") {
    return LanguageCode::kEnUs;
  }

  if (language == "ja-JP") {
    return LanguageCode::kJaJp;
  }

  NOTREACHED();
  return LanguageCode::kNone;
}

std::vector<base::FilePath> GetSodaLanguagePackDirectories() {
  std::vector<base::FilePath> paths;

  base::FilePath components_dir;
  base::PathService::Get(component_updater::DIR_COMPONENT_USER,
                         &components_dir);

  if (!components_dir.empty()) {
    paths.push_back(components_dir.Append(kSodaEnUsInstallationRelativePath));
    paths.push_back(components_dir.Append(kSodaJaJpInstallationRelativePath));
  }

  return paths;
}

}  // namespace speech