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

Unified Diff: chrome/browser/translate/translate_tab_helper.cc

Issue 187393005: Make it possible to read CLD data from a file (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Final rebase Created 6 years, 9 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
« no previous file with comments | « chrome/browser/translate/translate_tab_helper.h ('k') | chrome/common/chrome_constants.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/translate/translate_tab_helper.cc
diff --git a/chrome/browser/translate/translate_tab_helper.cc b/chrome/browser/translate/translate_tab_helper.cc
index 85d4f2b4fc4ae272dfcc19decc2fb9158f61a1e4..0e5bd18d5e62b5860824c2bb43e83597bbba47e7 100644
--- a/chrome/browser/translate/translate_tab_helper.cc
+++ b/chrome/browser/translate/translate_tab_helper.cc
@@ -4,7 +4,17 @@
#include "chrome/browser/translate/translate_tab_helper.h"
+#if defined(CLD2_DYNAMIC_MODE)
+#include "base/basictypes.h"
+#include "base/lazy_instance.h"
+#endif
#include "base/logging.h"
+#if defined(CLD2_DYNAMIC_MODE)
+#include "base/path_service.h"
+#include "base/platform_file.h"
+#include "base/synchronization/lock.h"
+#include "base/task_runner.h"
+#endif
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/translate/translate_accept_languages_factory.h"
@@ -16,19 +26,42 @@
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/translate/translate_bubble_factory.h"
+#if defined(CLD2_DYNAMIC_MODE)
+#include "chrome/common/chrome_paths.h"
+#endif
#include "chrome/common/pref_names.h"
#include "chrome/common/render_messages.h"
#include "components/translate/core/browser/page_translated_details.h"
#include "components/translate/core/browser/translate_accept_languages.h"
#include "components/translate/core/browser/translate_prefs.h"
#include "components/translate/core/common/language_detection_details.h"
+#if defined(CLD2_DYNAMIC_MODE)
+#include "content/public/browser/browser_thread.h"
+#endif
#include "content/public/browser/notification_service.h"
+#if defined(CLD2_DYNAMIC_MODE)
+#include "content/public/browser/render_process_host.h"
+#include "content/public/browser/render_view_host.h"
+#endif
#include "content/public/browser/web_contents.h"
DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper);
+#if defined(CLD2_DYNAMIC_MODE)
+// Statics defined in the .h file:
+base::PlatformFile TranslateTabHelper::s_cached_platform_file_ =
+ base::kInvalidPlatformFileValue;
+uint64 TranslateTabHelper::s_cached_data_offset_ = 0;
+uint64 TranslateTabHelper::s_cached_data_length_ = 0;
+base::LazyInstance<base::Lock> TranslateTabHelper::s_file_lock_ =
+ LAZY_INSTANCE_INITIALIZER;
+#endif
+
TranslateTabHelper::TranslateTabHelper(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
+#if defined(CLD2_DYNAMIC_MODE)
+ weak_pointer_factory_(this),
+#endif
translate_driver_(&web_contents->GetController()),
translate_manager_(new TranslateManager(this, prefs::kAcceptLanguages)) {}
@@ -138,6 +171,9 @@ bool TranslateTabHelper::OnMessageReceived(const IPC::Message& message) {
IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateLanguageDetermined,
OnLanguageDetermined)
IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated)
+#if defined(CLD2_DYNAMIC_MODE)
+ IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCLDData, OnCLDDataRequested)
+#endif
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
@@ -159,6 +195,145 @@ void TranslateTabHelper::WebContentsDestroyed(
translate_manager_.reset();
}
+#if defined(CLD2_DYNAMIC_MODE)
+void TranslateTabHelper::OnCLDDataRequested() {
+ // Quickly try to read s_cached_platform_file_. If valid, the file handle is
+ // cached and can be used immediately. Else, queue the caching task to the
+ // blocking pool.
+ base::PlatformFile handle = base::kInvalidPlatformFileValue;
+ uint64 data_offset = 0;
+ uint64 data_length = 0;
+ {
+ base::AutoLock lock(s_file_lock_.Get());
+ handle = s_cached_platform_file_;
+ data_offset = s_cached_data_offset_;
+ data_length = s_cached_data_length_;
+ }
+
+ if (handle != base::kInvalidPlatformFileValue) {
+ // Cached data available. Respond to the request.
+ SendCLDDataAvailable(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(&TranslateTabHelper::HandleCLDDataRequest),
+ base::Bind(&TranslateTabHelper::MaybeSendCLDDataAvailable,
+ weak_pointer_factory_.GetWeakPtr()));
+}
+
+void TranslateTabHelper::MaybeSendCLDDataAvailable() {
+ base::PlatformFile handle = base::kInvalidPlatformFileValue;
+ uint64 data_offset = 0;
+ uint64 data_length = 0;
+ {
+ base::AutoLock lock(s_file_lock_.Get());
+ handle = s_cached_platform_file_;
+ data_offset = s_cached_data_offset_;
+ data_length = s_cached_data_length_;
+ }
+
+ if (handle != base::kInvalidPlatformFileValue)
+ SendCLDDataAvailable(handle, data_offset, data_length);
+}
+
+void TranslateTabHelper::SendCLDDataAvailable(const base::PlatformFile handle,
+ const uint64 data_offset,
+ const uint64 data_length) {
+ // Data available, respond to the request.
+ IPC::PlatformFileForTransit ipc_platform_file =
+ IPC::GetFileHandleForProcess(
+ handle,
+ GetWebContents()->GetRenderViewHost()->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.
+ Send(new ChromeViewMsg_CLDDataAvailable(
+ GetWebContents()->GetRenderViewHost()->GetRoutingID(),
+ ipc_platform_file, data_offset, data_length));
+}
+
+void TranslateTabHelper::HandleCLDDataRequest() {
+ // 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_platform_file_ != base::kInvalidPlatformFileValue)
+ return; // Already done, duplicate request
+ }
+
+ 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.
+ }
+
+ // If the file exists, we can send an IPC-safe construct back to the
+ // renderer process immediately.
+ path = path.Append(chrome::kCLDDataFilename);
+ if (!base::PathExists(path))
+ return;
+
+ // Attempt to open the file for reading.
+ bool created = false;
+ base::PlatformFileError error;
+ const base::PlatformFile file = base::CreatePlatformFile(
+ path,
+ base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
+ &created, &error);
+ DCHECK(!created);
+ if (error != base::PLATFORM_FILE_OK) {
+ LOG(WARNING) << "CLD data file exists but cannot be opened";
+ return;
+ }
+
+ base::PlatformFileInfo file_info;
+ if (!base::GetPlatformFileInfo(file, &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;
+
+ bool racing = false;
+ {
+ base::AutoLock lock(s_file_lock_.Get());
+ if (s_cached_platform_file_ != base::kInvalidPlatformFileValue) {
+ // Idempotence: Racing another request on the blocking pool, abort.
+ racing = true;
+ } else {
+ // Else, this request has taken care of it all. Cache all info.
+ s_cached_platform_file_ = file;
+ s_cached_data_offset_ = data_offset;
+ s_cached_data_length_ = data_length;
+ }
+ }
+
+ if (racing) {
+ // Other thread wins, give up the redundant file handle.
+ base::ClosePlatformFile(file);
+ }
+}
+#endif // defined(CLD2_DYNAMIC_MODE)
+
void TranslateTabHelper::OnLanguageDetermined(
const LanguageDetectionDetails& details,
bool page_needs_translation) {
« no previous file with comments | « chrome/browser/translate/translate_tab_helper.h ('k') | chrome/common/chrome_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698