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

Unified Diff: chrome/renderer/translate/translate_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: Address Marcus' comments 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
Index: chrome/renderer/translate/translate_helper.cc
diff --git a/chrome/renderer/translate/translate_helper.cc b/chrome/renderer/translate/translate_helper.cc
index ccd2279a294de5603fb918002707f27b268f5817..bcb35fd0ae34ac66936fd0a24f0c5383c6ff2440 100644
--- a/chrome/renderer/translate/translate_helper.cc
+++ b/chrome/renderer/translate/translate_helper.cc
@@ -6,6 +6,7 @@
#include "base/bind.h"
#include "base/compiler_specific.h"
+#include "base/files/memory_mapped_file.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string16.h"
@@ -19,6 +20,12 @@
#include "components/translate/core/common/translate_util.h"
#include "components/translate/language_detection/language_detection_util.h"
#include "content/public/renderer/render_view.h"
+#include "extensions/common/constants.h"
+#include "ipc/ipc_platform_file.h"
+#if defined(CLD2_DYNAMIC_MODE)
+#include "content/public/common/url_constants.h"
+#include "third_party/cld_2/src/public/compact_lang_det.h"
+#endif
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebElement.h"
#include "third_party/WebKit/public/web/WebFrame.h"
@@ -64,6 +71,10 @@ const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'";
} // namespace
+// The mmap for the CLD2 data must be held forever once it is available in the
+// process. This is declared static in the translate_helper.h.
+base::MemoryMappedFile* TranslateHelper::s_cld2_data_file_mmap_ = NULL;
+
////////////////////////////////////////////////////////////////////////////////
// TranslateHelper, public:
//
@@ -71,12 +82,63 @@ TranslateHelper::TranslateHelper(content::RenderView* render_view)
: content::RenderViewObserver(render_view),
page_id_(-1),
translation_pending_(false),
- weak_method_factory_(this) {
+ weak_method_factory_(this)
+#if defined(CLD2_DYNAMIC_MODE)
+ ,cld2_data_file_polling_started_(false),
+ cld2_data_file_polling_canceled_(false),
+ deferred_page_capture_(false),
+ deferred_page_id_(-1),
+ deferred_contents_(ASCIIToUTF16(""))
+#endif
+ {
}
TranslateHelper::~TranslateHelper() {
CancelPendingTranslation();
+#if defined(CLD2_DYNAMIC_MODE)
+ CancelCLD2DataFilePolling();
+#endif
+}
+
+void TranslateHelper::PrepareForUrl(const GURL& url) {
+#if defined(CLD2_DYNAMIC_MODE)
+ deferred_page_capture_ = false;
+ deferred_contents_.clear();
+ if (cld2_data_file_polling_started_)
+ return;
+
+ // TODO(andrewhayden): Refactor translate_manager.cc's IsTranslatableURL to
+ // components/translate/core/common/translate_util.cc, and ignore any URL
+ // that fails that check. This will require moving unit tests and rewiring
+ // other function calls as well, so for now replicate the logic here.
+ if (url.is_empty())
+ return;
+ if (url.SchemeIs(content::kChromeUIScheme))
+ return;
+ if (url.SchemeIs(content::kChromeDevToolsScheme))
+ return;
+ if (url.SchemeIs(content::kFtpScheme))
+ return;
+#if defined(OS_CHROMEOS)
+ if (url.SchemeIs(extensions::kExtensionScheme)
+ && url.DomainIs(file_manager::kFileManagerAppId))
bulach 2014/03/25 09:29:26 nit: && in the previous line
Andrew Hayden (chromium.org) 2014/03/26 15:34:11 Done.
+ return;
+#endif
+
+ // Start polling for CLD data.
+ cld2_data_file_polling_started_ = true;
+ TranslateHelper::SendCLD2DataFileRequest(0, 1000);
+#endif
+}
+
+#if defined(CLD2_DYNAMIC_MODE)
+void TranslateHelper::DeferPageCaptured(const int page_id,
+ const base::string16& contents) {
+ deferred_page_capture_ = true;
+ deferred_page_id_ = page_id;
+ deferred_contents_ = contents;
}
+#endif
void TranslateHelper::PageCaptured(int page_id,
const base::string16& contents) {
@@ -92,6 +154,17 @@ void TranslateHelper::PageCaptured(int page_id,
WebFrame* main_frame = GetMainFrame();
if (!main_frame || render_view()->GetPageId() != page_id)
return;
+
+ // TODO(andrewhayden): UMA insertion point here: Track if data is available.
+ // TODO(andrewhayden): Retry insertion point here, retry till data available.
+#if defined(CLD2_DYNAMIC_MODE)
+ if (!CLD2::isDataLoaded()) {
+ // We're in dynamic mode and CLD data isn't loaded. Retry when CLD data
+ // is loaded, if ever.
+ TranslateHelper::DeferPageCaptured(page_id, contents);
+ return;
+ }
+#endif
page_id_ = page_id;
WebDocument document = main_frame->document();
std::string content_language = document.contentLanguage().utf8();
@@ -136,6 +209,9 @@ void TranslateHelper::CancelPendingTranslation() {
translation_pending_ = false;
source_lang_.clear();
target_lang_.clear();
+#if defined(CLD2_DYNAMIC_MODE)
+ CancelCLD2DataFilePolling();
+#endif
}
////////////////////////////////////////////////////////////////////////////////
@@ -310,6 +386,9 @@ bool TranslateHelper::OnMessageReceived(const IPC::Message& message) {
IPC_BEGIN_MESSAGE_MAP(TranslateHelper, message)
IPC_MESSAGE_HANDLER(ChromeViewMsg_TranslatePage, OnTranslatePage)
IPC_MESSAGE_HANDLER(ChromeViewMsg_RevertTranslation, OnRevertTranslation)
+#if defined(CLD2_DYNAMIC_MODE)
+ IPC_MESSAGE_HANDLER(ChromeViewMsg_CLDDataAvailable, OnCLDDataAvailable);
+#endif
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
@@ -499,3 +578,93 @@ WebFrame* TranslateHelper::GetMainFrame() {
return web_view->mainFrame();
}
+
+#if defined(CLD2_DYNAMIC_MODE)
+void TranslateHelper::CancelCLD2DataFilePolling() {
+ cld2_data_file_polling_canceled_ = true;
+}
+
+void TranslateHelper::SendCLD2DataFileRequest(int delay_millis,
+ int next_delay_millis) {
+ // Terminate immediately if told to stop polling
palmer 2014/03/24 19:01:47 Nit: Punctuate.
Andrew Hayden (chromium.org) 2014/03/26 15:34:11 Done.
+ if (cld2_data_file_polling_canceled_)
+ return;
+
+ // Terminate immediately if data is already loaded.
+ if (CLD2::isDataLoaded())
+ return;
+
+ // Else, send the IPC message to the browser process requesting the data...
+ Send(new ChromeViewHostMsg_NeedCLDData(routing_id()));
+
+ // ... and enqueue another delayed task to call again. This will start a
+ // chain of polling that will last until the pointer stops being null,
palmer 2014/03/24 19:01:47 Nit: NULL
Andrew Hayden (chromium.org) 2014/03/26 15:34:11 Done.
+ // which is the right thing to do.
+ // NB: In the great majority of cases, the data file will be available and
+ // the very first delayed task will be a no-op that terminates the chain.
+ // It's only while downloading the file that we expect this to chain for a
Andrew Hayden (chromium.org) 2014/03/26 15:34:11 Grammar police, don't use pronouns in comments.
+ // nontrivial amount of time.
+ // Use a weak pointer to avoid keeping this helper object around forever.
+ base::MessageLoop::current()->PostDelayedTask(
+ FROM_HERE,
+ base::Bind(&TranslateHelper::SendCLD2DataFileRequest,
+ weak_method_factory_.GetWeakPtr(),
+ next_delay_millis, next_delay_millis),
+ base::TimeDelta::FromMilliseconds(delay_millis));
+}
+
+void TranslateHelper::OnCLDDataAvailable(
+ IPC::PlatformFileForTransit ipc_file_handle) {
+ LoadCLDDData(ipc_file_handle);
+ if (deferred_page_capture_ && CLD2::isDataLoaded()) {
+ // Data is available now, and there was previously a deferred request to
+ // start translation. Finish that request up and clear state.
+ // The real situation this translates to is as follows:
+ // The user loaded a page before the CLD data was available - either
+ // because the browser process took too long to respond to our CLD data
+ // request, or because the data truly hasn't been available;
+ // The data is now available, so get that language check done and post the
+ // message back to the browser ASAP.
+ deferred_page_capture_ = false; // Don't do this a second time.
+ PageCaptured(deferred_page_id_, deferred_contents_);
+ deferred_page_id_ = -1; // Clean up for sanity
palmer 2014/03/24 19:01:47 Nit: punctuation, but really these comments are su
Andrew Hayden (chromium.org) 2014/03/26 15:34:11 Deleted.
+ deferred_contents_.clear(); // Clean up for sanity
+ }
+}
+
+void TranslateHelper::LoadCLDDData(
+ IPC::PlatformFileForTransit ipc_file_handle) {
+ // Terminate immediately if told to stop polling
palmer 2014/03/24 19:01:47 Nit: Punctuate.
Andrew Hayden (chromium.org) 2014/03/26 15:34:11 Done.
+ if (cld2_data_file_polling_canceled_)
+ return;
+
+ // Terminate immediately if data is already loaded.
+ if (CLD2::isDataLoaded())
+ return;
+
+ // Grab the file handle
+ base::PlatformFile platform_file =
+ IPC::PlatformFileForTransitToPlatformFile(ipc_file_handle);
+ DCHECK(platform_file != base::kInvalidPlatformFileValue)
palmer 2014/03/24 19:01:47 This should be a production, run-time check, not a
Andrew Hayden (chromium.org) 2014/03/26 15:34:11 Done.
+ << "Can't find the CLD data file!";
+ base::File basic_file(platform_file);
+
+ // mmap the file
+ static base::MemoryMappedFile* mmap = new base::MemoryMappedFile();
+ bool initialized = mmap->Initialize(basic_file.Pass());
+ DCHECK(initialized) << "Failed to initialize CLD data mmap";
+ if (!initialized) {
+ delete mmap;
+ mmap = NULL;
+ return;
+ }
+
+ // Retain the reference forever, so the data doesn't get unmapped by the
+ // mmap destructor.
+ s_cld2_data_file_mmap_ = mmap;
+
+ // Initialize the CLD subsystem... and it's all done!
+ CLD2::loadDataFromRawAddress(mmap->data(), mmap->length());
+ DCHECK(CLD2::isDataLoaded()) << "Failed to load CLD data from mmap";
+}
+#endif

Powered by Google App Engine
This is Rietveld 408576698