Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(307)

Unified Diff: components/translate/content/browser/data_file_browser_cld_data_provider.cc

Issue 333603002: Modularize Compact Language Detector 2 (CLD2) data sources (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix compiler errors Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/translate/content/browser/data_file_browser_cld_data_provider.cc
diff --git a/components/translate/content/browser/data_file_browser_cld_data_provider.cc b/components/translate/content/browser/data_file_browser_cld_data_provider.cc
new file mode 100644
index 0000000000000000000000000000000000000000..42abf80dd36356fe0294d720595c9d1ee729d9b2
--- /dev/null
+++ b/components/translate/content/browser/data_file_browser_cld_data_provider.cc
@@ -0,0 +1,191 @@
+// Copyright 2014 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 "data_file_browser_cld_data_provider.h"
+
+#include "base/basictypes.h"
+#include "base/files/file.h"
+#include "base/lazy_instance.h"
+#include "base/memory/weak_ptr.h"
+#include "base/path_service.h"
+#include "base/synchronization/lock.h"
+#include "base/task_runner.h"
+#include "chrome/common/chrome_constants.h"
+#include "chrome/common/chrome_paths.h"
+#include "components/translate/content/common/data_file_cld_data_provider_messages.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/render_process_host.h"
+#include "ipc/ipc_message_macros.h"
+#include "ipc/ipc_platform_file.h"
+
+#if defined(CLD2_IS_COMPONENT)
+#include "chrome/browser/component_updater/cld_component_installer.h"
+#endif
+
+namespace {
+ // The data file, cached as long as the process stays alive.
+ // We also track the offset at which the data starts, and its length.
+ base::File* s_cached_file_; // guarded by file_lock_
+ uint64 s_cached_data_offset_; // guarded by file_lock_
+ uint64 s_cached_data_length_; // guarded by file_lock_
+
+ // Guards s_cached_file_
+ base::LazyInstance<base::Lock> s_file_lock_;
+} // namespace
+
+namespace content {
+
+DataFileBrowserCldDataProvider::DataFileBrowserCldDataProvider(
+ content::RenderViewHost* render_view_host)
+ : render_view_host (render_view_host),
+ weak_pointer_factory_(this) {
+}
+
+bool DataFileBrowserCldDataProvider::OnMessageReceived(
+ const IPC::Message& message) {
+ bool handled = true;
+ IPC_BEGIN_MESSAGE_MAP(DataFileBrowserCldDataProvider, message)
+ IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCldDataFile, OnCldDataRequest)
+ IPC_MESSAGE_UNHANDLED(handled = false)
+ IPC_END_MESSAGE_MAP()
+ return handled;
+}
+
+void DataFileBrowserCldDataProvider::OnCldDataRequest() {
+ // Quickly try to read s_cached_file_. If valid, the file handle is
+ // cached and can be used immediately. Else, queue the caching task to the
+ // blocking pool.
+ base::File* handle = NULL;
+ uint64 data_offset = 0;
+ uint64 data_length = 0;
+ {
+ base::AutoLock lock(s_file_lock_.Get());
+ handle = s_cached_file_;
+ data_offset = s_cached_data_offset_;
+ data_length = s_cached_data_length_;
+ }
+
+ if (handle && handle->IsValid()) {
+ // Cached data available. Respond to the request.
+ SendCldDataResponseInternal(handle, data_offset, data_length);
+ return;
+ }
+
+ // Else, we don't have the data file yet. Queue a caching attempt.
+ // The caching attempt happens in the blocking pool because it may involve
+ // arbitrary filesystem access.
+ // After the caching attempt is made, we call MaybeSendCLDDataAvailable
+ // to pass the file handle to the renderer. This only results in an IPC
+ // message if the caching attempt was successful.
+ content::BrowserThread::PostBlockingPoolTaskAndReply(
+ FROM_HERE,
+ base::Bind(&DataFileBrowserCldDataProvider::OnCldDataRequestInternal,
+ weak_pointer_factory_.GetWeakPtr()),
+ base::Bind(&DataFileBrowserCldDataProvider::SendCldDataResponse,
+ weak_pointer_factory_.GetWeakPtr()));
+
+}
+
+void DataFileBrowserCldDataProvider::SendCldDataResponse() {
+ base::File* handle = NULL;
+ uint64 data_offset = 0;
+ uint64 data_length = 0;
+ {
+ base::AutoLock lock(s_file_lock_.Get());
+ handle = s_cached_file_;
+ data_offset = s_cached_data_offset_;
+ data_length = s_cached_data_length_;
+ }
+
+ if (handle && handle->IsValid())
+ SendCldDataResponseInternal(handle, data_offset, data_length);
+}
+
+void DataFileBrowserCldDataProvider::SendCldDataResponseInternal(
+ const base::File* handle,
+ const uint64 data_offset,
+ const uint64 data_length) {
+ // Data available, respond to the request.
+ IPC::PlatformFileForTransit ipc_platform_file =
+ IPC::GetFileHandleForProcess(
+ handle->GetPlatformFile(),
+ render_view_host->GetProcess()->GetHandle(),
+ false);
+ // In general, sending a response from within the code path that is processing
+ // a request is discouraged because there is potential for deadlock (if the
+ // methods are sent synchronously) or loops (if the response can trigger a
+ // new request). Neither of these concerns is relevant in this code, so
+ // sending the response from within the code path of the request handler is
+ // safe.
+ render_view_host->Send(new ChromeViewMsg_CldDataFileAvailable(
+ render_view_host->GetRoutingID(),
+ ipc_platform_file,
+ data_offset,
+ data_length));
+}
+
+void DataFileBrowserCldDataProvider::OnCldDataRequestInternal() {
+ // Because this function involves arbitrary file system access, it must run
+ // on the blocking pool.
+ DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
+ DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+
+ {
+ base::AutoLock lock(s_file_lock_.Get());
+ if (s_cached_file_)
+ return; // Already done, duplicate request
+ }
+
+#if defined(CLD2_IS_COMPONENT)
+ base::FilePath path = component_updater::GetLatestCldDataFile();
+ if (path.empty())
+ return;
+#else // CLD2 data is at a well-known file path
+ base::FilePath path;
+ if (!PathService::Get(chrome::DIR_USER_DATA, &path)) {
+ LOG(WARNING) << "Unable to locate user data directory";
+ return; // Chrome isn't properly installed.
+ }
+ path = path.Append(chrome::kCLDDataFilename);
+#endif
+
+ // If the file exists, we can send an IPC-safe construct back to the
+ // renderer process immediately; otherwise, nothing to do here.
+ if (!base::PathExists(path))
+ return;
+
+ // Attempt to open the file for reading.
+ scoped_ptr<base::File> file(
+ new base::File(path, base::File::FLAG_OPEN | base::File::FLAG_READ));
+ if (!file->IsValid()) {
+ LOG(WARNING) << "CLD data file exists but cannot be opened";
+ return;
+ }
+
+ base::File::Info file_info;
+ if (!file->GetInfo(&file_info)) {
+ LOG(WARNING) << "CLD data file exists but cannot be inspected";
+ return;
+ }
+
+ // For now, our offset and length are simply 0 and the length of the file,
+ // respectively. If we later decide to include the CLD2 data file inside of
+ // a larger binary context, these params can be twiddled appropriately.
+ const uint64 data_offset = 0;
+ const uint64 data_length = file_info.size;
+
+ {
+ base::AutoLock lock(s_file_lock_.Get());
+ if (s_cached_file_) {
+ // Idempotence: Racing another request on the blocking pool, abort.
+ } else {
+ // Else, this request has taken care of it all. Cache all info.
+ s_cached_file_ = file.release();
+ s_cached_data_offset_ = data_offset;
+ s_cached_data_length_ = data_length;
+ }
+ }
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698