Commit ae5783e5 authored by Leo Zhang's avatar Leo Zhang Committed by Commit Bot

Create CrOS IME constants

Move all the ime-related paths to constants, and share these paths
across the different components.

Bug: 837156
Change-Id: I067d6b03b81df7cfce8240558c796ab7d1a3edb4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1715678Reviewed-by: default avatarShu Chen <shuchen@chromium.org>
Reviewed-by: default avatarYuichiro Hanada <yhanada@chromium.org>
Commit-Queue: Leo Zhang <googleo@chromium.org>
Auto-Submit: Leo Zhang <googleo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#682225}
parent cc246eef
......@@ -128,6 +128,7 @@ source_set("chromeos") {
"//chromeos/services/cros_healthd/public/cpp",
"//chromeos/services/cros_healthd/public/mojom",
"//chromeos/services/device_sync/public/cpp",
"//chromeos/services/ime:constants",
"//chromeos/services/ime/public/mojom",
"//chromeos/services/machine_learning/public/cpp",
"//chromeos/services/machine_learning/public/mojom",
......
......@@ -9,6 +9,7 @@
#include "base/files/file_util.h"
#include "base/logging.h"
#include "chromeos/services/ime/constants.h"
#include "chromeos/services/ime/public/mojom/constants.mojom.h"
#include "content/public/browser/system_connector.h"
......@@ -42,16 +43,23 @@ constexpr net::NetworkTrafficAnnotationTag traffic_annotation =
"Not implemented, considered not useful."
})");
bool IsImePathInvalid(const base::FilePath& file_path) {
bool IsDownloadPathValid(const base::FilePath& file_path) {
// Only non-empty, relative path which doesn't reference a parent is allowed.
return file_path.empty() || file_path.IsAbsolute() ||
file_path.ReferencesParent();
if (file_path.empty() || file_path.IsAbsolute() ||
file_path.ReferencesParent())
return false;
// Target path must be restricted in the provided path.
base::FilePath parent(chromeos::ime::kInputMethodsDirName);
parent = parent.Append(chromeos::ime::kLanguageDataDirName);
return parent.IsParent(file_path);
}
bool IsURLInvalid(const GURL& url) {
// TODO(https://crbug.com/837156): Use URL whitelist instead of the general
bool IsDownloadURLValid(const GURL& url) {
// TODO(https://crbug.com/837156): Whitelist all URLs instead of some general
// checks below.
return !url.DomainIs("dl.google.com") || !url.SchemeIs(url::kHttpsScheme);
return url.SchemeIs(url::kHttpsScheme) &&
url.DomainIs(chromeos::ime::kGoogleKeyboardDownloadDomain);
}
} // namespace
......@@ -73,7 +81,8 @@ void ImeServiceConnector::DownloadImeFileTo(
// downloading task exits.
// TODO(https://crbug.com/971954): Support multi downloads.
// Validate url and file_path, return an empty file path if not.
if (url_loader_ || IsURLInvalid(url) || IsImePathInvalid(file_path)) {
if (url_loader_ || !IsDownloadURLValid(url) ||
!IsDownloadPathValid(file_path)) {
base::FilePath empty_path;
std::move(callback).Run(empty_path);
return;
......
......@@ -6,6 +6,19 @@ import("//chromeos/services/ime/public/features.gni")
assert(is_chromeos, "Non-ChromeOS builds cannot depend on //chromeos")
component("constants") {
output_name = "chromeos_ime_constants"
defines = [ "IS_CHROMEOS_IME_CONSTANTS_IMPL" ]
deps = [
"//base",
"//build:branding_buildflags",
]
sources = [
"constants.cc",
"constants.h",
]
}
source_set("lib") {
sources = [
"ime_service.cc",
......@@ -15,6 +28,7 @@ source_set("lib") {
]
deps = [
":constants",
"//base",
"//chromeos/services/ime/public/cpp:buildflags",
"//chromeos/services/ime/public/cpp:rulebased",
......@@ -41,6 +55,7 @@ source_set("sandbox_hook") {
]
deps = [
":constants",
"//base",
"//chromeos/services/ime/public/cpp:buildflags",
"//sandbox/linux:sandbox_services",
......
googleo@chromium.org
shuchen@chromium.org
per-file ime_sandbox_hook.*=file://sandbox/linux/OWNERS
# COMPONENTS: UI>Input>Text>IME
// Copyright 2019 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 "chromeos/services/ime/constants.h"
#include "build/branding_buildflags.h"
#define FPL FILE_PATH_LITERAL
#define IME_DIR_STRING "input_methods"
namespace chromeos {
namespace ime {
const base::FilePath::CharType kInputMethodsDirName[] =
FILE_PATH_LITERAL(IME_DIR_STRING);
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
const base::FilePath::CharType kBundledInputMethodsDirPath[] =
FILE_PATH_LITERAL("/usr/share/chromeos-assets/input_methods/input_tools");
const base::FilePath::CharType kUserInputMethodsDirPath[] =
FILE_PATH_LITERAL("/home/chronos/user/" IME_DIR_STRING);
const base::FilePath::CharType kSharedInputMethodsDirPath[] =
FILE_PATH_LITERAL("/home/chronos/" IME_DIR_STRING);
const base::FilePath::CharType kLanguageDataDirName[] =
FILE_PATH_LITERAL("google");
#else
// IME service does not support third-party IME yet, so the paths below kind
// of act like a placeholder. In the future, put some well-designed paths here.
const base::FilePath::CharType kBundledInputMethodsDirPath[] =
FILE_PATH_LITERAL("/tmp/" IME_DIR_STRING);
const base::FilePath::CharType kUserInputMethodsDirPath[] =
FILE_PATH_LITERAL("/tmp/" IME_DIR_STRING);
const base::FilePath::CharType kSharedInputMethodsDirPath[] =
FILE_PATH_LITERAL("/tmp/" IME_DIR_STRING);
const base::FilePath::CharType kLanguageDataDirName[] =
FILE_PATH_LITERAL("data");
#endif
const char kGoogleKeyboardDownloadDomain[] = "dl.google.com";
} // namespace ime
} // namespace chromeos
// Copyright 2019 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.
#ifndef CHROMEOS_SERVICES_IME_CONSTANTS_H_
#define CHROMEOS_SERVICES_IME_CONSTANTS_H_
#include "base/component_export.h"
#include "base/files/file_path.h"
namespace chromeos {
namespace ime {
// The path where ChromeOS default input methods is installed, consisting of
// IME manifest and some bundled language dictionaries.
COMPONENT_EXPORT(CHROMEOS_IME_CONSTANTS)
extern const base::FilePath::CharType kBundledInputMethodsDirPath[];
// The path of the active user's own input methods data, including user's
// private dictionary or downloaded language dictionaries.
COMPONENT_EXPORT(CHROMEOS_IME_CONSTANTS)
extern const base::FilePath::CharType kUserInputMethodsDirPath[];
// The path of downloaded IME language dictionaries which shared by all users.
// It aims to reduce storage and improve responsiveness by reusing static
// dictionary for all users. This feature is disabled by default. When
// `CrosImeSharedDataEnabled` is on and the IME attempt to load some language
// dictionary which is missing on the device, the IME service will try to
// download it to the shared path. Because the language dictionary will be
// accessible by all users, it will prevent duplicate downloads of the same
// language dictionary.
COMPONENT_EXPORT(CHROMEOS_IME_CONSTANTS)
extern const base::FilePath::CharType kSharedInputMethodsDirPath[];
// The name of the directory inside the profile where IME data are stored in.
COMPONENT_EXPORT(CHROMEOS_IME_CONSTANTS)
extern const base::FilePath::CharType kInputMethodsDirName[];
// The name of the directory inside the input methods directory where language
// dictionaries are downloaded to.
COMPONENT_EXPORT(CHROMEOS_IME_CONSTANTS)
extern const base::FilePath::CharType kLanguageDataDirName[];
// The domain of Google Keyboard language dictionary download URL.
COMPONENT_EXPORT(CHROMEOS_IME_CONSTANTS)
extern const char kGoogleKeyboardDownloadDomain[];
} // namespace ime
} // namespace chromeos
#endif // CHROMEOS_SERVICES_IME_CONSTANTS_H_
......@@ -9,6 +9,7 @@
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "build/buildflag.h"
#include "chromeos/services/ime/constants.h"
#include "chromeos/services/ime/public/cpp/buildflags.h"
#include "sandbox/linux/syscall_broker/broker_command.h"
#include "sandbox/linux/syscall_broker/broker_file_permission.h"
......@@ -20,27 +21,6 @@ namespace chromeos {
namespace ime {
namespace {
// The path of input tools relative folder, which contains some 'pre-bundled'
// static language dictionaries.
const char kInputToolsBundleFolder[] = "input_methods/input_tools";
// The path of the user's own language dictionaries, including private and
// public static language dictionaries.
const char kUserHomePath[] = "/home/chronos/user/ime/";
// The path of downloadable IME language dictionaries shared by all users.
// This feature is to reduce storage by sharing single dictionary between all
// users. This feature is optional.
// When `CrosImeSharedDataEnabled`, if the input decoder find some language
// dictionary is missing from the device, the IME service will download the
// dictionary to the shared path. Then the data can be accessible by others to
// avoid another download of the same dictionary.
const char kSharedHomePath[] = "/home/chronos/ime/";
bool CreateFolderIfNotExist(const char* dir) {
base::FilePath path = base::FilePath(dir);
return base::CreateDirectory(path);
}
// Whether IME instance shares a same language data path with each other.
inline constexpr bool CrosImeSharedDataEnabled() {
#if BUILDFLAG(ENABLE_CROS_IME_SHARED_DATA)
......@@ -50,11 +30,6 @@ inline constexpr bool CrosImeSharedDataEnabled() {
#endif
}
// This is where input method's application bundle will be put.
base::FilePath GetChromeOSAssetFolder() {
return base::FilePath("/usr/share/chromeos-assets");
}
void AddSharedLibraryAndDepsPath(
std::vector<BrokerFilePermission>* permissions) {
// Where IME decoder shared library and its dependencies will live.
......@@ -74,11 +49,8 @@ void AddSharedLibraryAndDepsPath(
}
void AddBundleFolder(std::vector<BrokerFilePermission>* permissions) {
// This is the IME application bundle folder, where pre-installed dictionaries
// are put, which decoder needs to read.
base::FilePath bundle_dir = GetChromeOSAssetFolder()
.AppendASCII(kInputToolsBundleFolder)
.AsEndingWithSeparator();
base::FilePath bundle_dir =
base::FilePath(kBundledInputMethodsDirPath).AsEndingWithSeparator();
permissions->push_back(
BrokerFilePermission::ReadOnlyRecursive(bundle_dir.value()));
}
......@@ -89,16 +61,21 @@ void AddSharedDataFolderIfEnabled(
return;
// Without access to shared home folder, IME servcie will download all
// missing dictionaries to `kUserHomePath` of the current user.
CHECK(CreateFolderIfNotExist(kSharedHomePath));
// missing dictionaries to `kUserInputMethodsDirPath` of the current user.
base::FilePath shared_path =
base::FilePath(kSharedInputMethodsDirPath).AsEndingWithSeparator();
if (base::CreateDirectory(shared_path)) {
permissions->push_back(
BrokerFilePermission::ReadWriteCreateRecursive(kSharedHomePath));
BrokerFilePermission::ReadWriteCreateRecursive(shared_path.value()));
}
}
void AddUserDataFolder(std::vector<BrokerFilePermission>* permissions) {
// When failed to access user profile folder, decoder still can work, but
// user dictionary can not be saved.
bool success = CreateFolderIfNotExist(kUserHomePath);
base::FilePath user_path =
base::FilePath(kUserInputMethodsDirPath).AsEndingWithSeparator();
bool success = base::CreateDirectory(user_path);
if (!success) {
LOG(WARNING) << "Unable to create IME folder under user profile folder";
return;
......@@ -106,7 +83,7 @@ void AddUserDataFolder(std::vector<BrokerFilePermission>* permissions) {
// Push this path, otherwise process will crash directly when IME decoder
// tries to access this folder.
permissions->push_back(
BrokerFilePermission::ReadWriteCreateRecursive(kUserHomePath));
BrokerFilePermission::ReadWriteCreateRecursive(user_path.value()));
}
std::vector<BrokerFilePermission> GetImeFilePermissions() {
......
......@@ -11,6 +11,7 @@
#include "base/bind.h"
#include "build/buildflag.h"
#include "chromeos/services/ime/constants.h"
#include "chromeos/services/ime/public/cpp/buildflags.h"
#if BUILDFLAG(ENABLE_CROS_IME_DECODER)
......@@ -107,7 +108,7 @@ void ImeService::SimpleDownloadFinished(SimpleDownloadCallback callback,
}
const char* ImeService::GetImeBundleDir() {
return "";
return kBundledInputMethodsDirPath;
}
const char* ImeService::GetImeGlobalDir() {
......@@ -116,7 +117,7 @@ const char* ImeService::GetImeGlobalDir() {
}
const char* ImeService::GetImeUserHomeDir() {
return "";
return kUserInputMethodsDirPath;
}
int ImeService::SimpleDownloadToFile(const char* url,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment