File: ime_service_connector.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 (238 lines) | stat: -rw-r--r-- 8,608 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2019 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/input_method/ime_service_connector.h"

#include <memory>
#include <utility>

#include "ash/constants/ash_features.h"
#include "base/feature_list.h"
#include "base/files/file_util.h"
#include "chromeos/ash/services/ime/constants.h"
#include "chromeos/ash/services/ime/public/mojom/ime_service.mojom.h"
#include "chromeos/ash/services/ime/public/mojom/input_method_user_data.mojom.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "content/public/browser/service_process_host.h"
#include "net/base/load_flags.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/mojom/network_context.mojom.h"

namespace ash {
namespace input_method {

namespace {

constexpr net::NetworkTrafficAnnotationTag traffic_annotation =
    net::DefineNetworkTrafficAnnotation("ime_url_downloader", R"(
    semantics {
      sender: "IME Service Downloader"
      description:
        "When user selects a new input method in ChromeOS, it may request a"
        "corresponding language module downloaded if it does not exist."
      trigger: "User switches to an input method without language module."
      data:
        "The language module download URL. No user identifier is sent."
      destination: GOOGLE_OWNED_SERVICE
    }
    policy {
      cookies_allowed: NO
      policy_exception_justification:
        "Not implemented, considered not useful."
    })");

bool IsDownloadPathValid(const base::FilePath& file_path) {
  // Only non-empty, relative path which doesn't reference a parent is allowed.
  if (file_path.empty() || file_path.IsAbsolute() ||
      file_path.ReferencesParent()) {
    return false;
  }

  // Target path must be restricted in the provided path.
  base::FilePath parent(ime::kInputMethodsDirName);
  parent = parent.Append(ime::kLanguageDataDirName);
  return parent.IsParent(file_path);
}

bool IsDownloadURLValid(const GURL& url) {
  return url.SchemeIs(url::kHttpsScheme) &&
         (url.DomainIs("dl.google.com") || url.DomainIs("edgedl.me.gvt1.com"));
}

bool ShouldUseUpdatedDownloadLogic() {
  return base::FeatureList::IsEnabled(features::kImeDownloaderUpdate);
}

std::unique_ptr<network::SimpleURLLoader> CreateUrlLoader(const GURL& url) {
  auto resource_request = std::make_unique<network::ResourceRequest>();
  resource_request->url = url;
  resource_request->load_flags =
      net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE;
  // Disable cookies for this request.
  resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;

  auto url_loader = network::SimpleURLLoader::Create(
      std::move(resource_request), traffic_annotation);
  // TODO(https://crbug.com/971954): Allow the client to specify the timeout.
  url_loader->SetTimeoutDuration(base::Minutes(10));
  return url_loader;
}

}  // namespace

ImeServiceConnector::ImeServiceConnector(Profile* profile)
    : profile_(profile), url_loader_factory_(profile->GetURLLoaderFactory()) {
  profile_observation_.Observe(profile);
}

ImeServiceConnector::~ImeServiceConnector() = default;

void ImeServiceConnector::DownloadImeFileTo(
    const GURL& url,
    const base::FilePath& file_path,
    DownloadImeFileToCallback callback) {
  // Validate url and file_path, return an empty file path if not.
  if (!IsDownloadURLValid(url) || !IsDownloadPathValid(file_path)) {
    base::FilePath empty_path;
    std::move(callback).Run(empty_path);
    return;
  }

  if (ShouldUseUpdatedDownloadLogic()) {
    base::FilePath full_path = profile_->GetPath().Append(file_path);
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(&ImeServiceConnector::MaybeTriggerDownload,
                                  weak_ptr_factory_.GetWeakPtr(), url,
                                  full_path, std::move(callback)));
    return;
  }

  // For now, we don't allow the client to download multi files at same time.
  // Downloading request will be aborted and return empty before the current
  // 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_) {
    base::FilePath empty_path;
    std::move(callback).Run(empty_path);
    return;
  }

  // Download the language module into a preconfigured ime folder of current
  // user's home which is allowed in IME service's sandbox.
  base::FilePath full_path = profile_->GetPath().Append(file_path);
  url_loader_ = CreateUrlLoader(url);
  url_loader_->DownloadToFile(
      url_loader_factory_.get(),
      base::BindOnce(&ImeServiceConnector::OnFileDownloadComplete,
                     base::Unretained(this), std::move(callback)),
      full_path);
}

void ImeServiceConnector::OnProfileWillBeDestroyed(Profile* profile) {
  profile_observation_.Reset();
  profile_ = nullptr;
}

void ImeServiceConnector::SetupImeService(
    mojo::PendingReceiver<ime::mojom::InputEngineManager> receiver) {
  if (!remote_service_) {
    content::ServiceProcessHost::Launch(
        remote_service_.BindNewPipeAndPassReceiver(),
        content::ServiceProcessHost::Options()
            .WithDisplayName(IDS_IME_SERVICE_DISPLAY_NAME)
            .Pass());
    remote_service_.reset_on_disconnect();

    platform_access_receiver_.reset();
    remote_service_->SetPlatformAccessProvider(
        platform_access_receiver_.BindNewPipeAndPassRemote());
  }

  remote_service_->BindInputEngineManager(std::move(receiver));
}

void ImeServiceConnector::BindInputMethodUserDataService(
    mojo::PendingReceiver<ime::mojom::InputMethodUserDataService> receiver) {
  if (!remote_service_) {
    content::ServiceProcessHost::Launch(
        remote_service_.BindNewPipeAndPassReceiver(),
        content::ServiceProcessHost::Options()
            .WithDisplayName(IDS_IME_SERVICE_DISPLAY_NAME)
            .Pass());
    remote_service_.reset_on_disconnect();

    platform_access_receiver_.reset();
    remote_service_->SetPlatformAccessProvider(
        platform_access_receiver_.BindNewPipeAndPassRemote());
  }

  remote_service_->BindInputMethodUserDataService(std::move(receiver));
}

void ImeServiceConnector::OnFileDownloadComplete(
    DownloadImeFileToCallback client_callback,
    base::FilePath path) {
  std::move(client_callback).Run(path);
  url_loader_.reset();
  return;
}

void ImeServiceConnector::MaybeTriggerDownload(
    GURL url,
    base::FilePath file_path,
    DownloadImeFileToCallback callback) {
  // Do not trigger a new download if one is already in progress for the same
  // url. Store the callback given and run it when the current download
  // finishes.
  if (url_loader_ && active_request_url_ && active_request_url_ == url.spec()) {
    download_callbacks_.emplace_back(std::move(callback));
    return;
  }

  // If the currently active request does not match the url being requested,
  // then revert to the previous logic of dropping new requests while the
  // current request is in progress.
  if (url_loader_) {
    base::FilePath empty_path;
    std::move(callback).Run(empty_path);
    return;
  }

  // Reset the download context before triggering a new download request.
  active_request_url_ = url.spec();
  download_callbacks_.clear();
  download_callbacks_.emplace_back(std::move(callback));
  url_loader_ = CreateUrlLoader(url);
  url_loader_->DownloadToFile(
      url_loader_factory_.get(),
      base::BindOnce(&ImeServiceConnector::HandleDownloadResponse,
                     weak_ptr_factory_.GetWeakPtr()),
      file_path);
}

void ImeServiceConnector::HandleDownloadResponse(base::FilePath file_path) {
  // Notify any download callbacks registered for the current request.
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(&ImeServiceConnector::NotifyAllDownloadListeners,
                     weak_ptr_factory_.GetWeakPtr(), file_path));
}

void ImeServiceConnector::NotifyAllDownloadListeners(base::FilePath file_path) {
  while (!download_callbacks_.empty()) {
    std::move(download_callbacks_.back()).Run(file_path);
    download_callbacks_.pop_back();
  }

  // Clear the currently active request info.
  url_loader_.reset();
  active_request_url_ = std::nullopt;
}

}  // namespace input_method
}  // namespace ash