Chromium Code Reviews| 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 bae7a5984c1eafd9be8b712c9d2192326930bed0..86aeb0c561c8f4ba20ef67bf8f76766aa3f36edb 100644 |
| --- a/chrome/browser/translate/translate_tab_helper.cc |
| +++ b/chrome/browser/translate/translate_tab_helper.cc |
| @@ -4,6 +4,8 @@ |
| #include "chrome/browser/translate/translate_tab_helper.h" |
| +#include "base/path_service.h" |
| +#include "base/platform_file.h" |
| #include "chrome/browser/chrome_notification_types.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/translate/translate_accept_languages_factory.h" |
| @@ -15,6 +17,7 @@ |
| #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" |
| +#include "chrome/common/chrome_paths.h" |
| #include "chrome/common/pref_names.h" |
| #include "chrome/common/render_messages.h" |
| #include "components/translate/core/browser/page_translated_details.h" |
| @@ -22,6 +25,8 @@ |
| #include "components/translate/core/browser/translate_prefs.h" |
| #include "components/translate/core/common/language_detection_details.h" |
| #include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "content/public/browser/render_view_host.h" |
| #include "content/public/browser/web_contents.h" |
| DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper); |
| @@ -112,6 +117,7 @@ bool TranslateTabHelper::OnMessageReceived(const IPC::Message& message) { |
| IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateLanguageDetermined, |
| OnLanguageDetermined) |
| IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated) |
| + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCLDData, OnCLDDataRequested) |
| IPC_MESSAGE_UNHANDLED(handled = false) |
| IPC_END_MESSAGE_MAP() |
| @@ -133,6 +139,59 @@ void TranslateTabHelper::WebContentsDestroyed( |
| translate_manager_.reset(); |
| } |
| +// TODO(andrewhayden): It's annoying that during download of the data file, |
| +// multiple renderers may be calling this method repeatedly, but there's not |
| +// an obviously-better choice from a complexity standpoint: we have no idea |
| +// if or when the file will become available, and the check is cheap. |
| +void TranslateTabHelper::OnCLDDataRequested() { |
| + static base::PlatformFile cached_platform_file = 0; |
| + const int routing_id = GetWebContents()->GetRenderViewHost()->GetRoutingID(); |
| + |
| + // If we have cached the platform file, we reuse it. Else, we cache it now. |
| + if (cached_platform_file == 0) { |
| + // First, grab a reference to the user data directory |
| + base::FilePath path; |
| + if (!PathService::Get(chrome::DIR_USER_DATA, &path)) { |
| + NOTREACHED(); |
|
jochen (gone - plz use gerrit)
2014/03/12 13:08:27
i don't think this if () { NOTREACHED(); } adds mu
Andrew Hayden (chromium.org)
2014/03/13 16:39:48
Done.
|
| + } |
| + |
| + // Now derive the name of the CLD data file that we are looking for. |
| + path = path.Append(chrome::kCLDDataFilename); |
| + |
| + // If the file exists, we can send an IPC-safe construct back to the |
| + // renderer process immediately. If not (e.g., the file is being downloaded |
| + // right now), then we will do nothing. It is up to the renderer process |
| + // to call us again later. |
| + if (!base::PathExists(path)) { |
| + return; |
|
jochen (gone - plz use gerrit)
2014/03/12 13:08:27
no { }
Andrew Hayden (chromium.org)
2014/03/13 16:39:48
Done.
|
| + } |
| + |
| + // Attempt to open the file for reading. |
| + int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; |
| + bool created = false; |
| + base::PlatformFileError error; |
| + cached_platform_file = base::CreatePlatformFile( |
| + path, flags, &created, &error); |
| + DCHECK(created == false); |
|
jochen (gone - plz use gerrit)
2014/03/12 13:08:27
!created
Andrew Hayden (chromium.org)
2014/03/13 16:39:48
Done.
|
| + if (error != base::PLATFORM_FILE_OK) { |
| + cached_platform_file = 0; |
| + DCHECK(false) << "Failed to open CLD data file, error code=" << error; |
|
jochen (gone - plz use gerrit)
2014/03/12 13:08:27
is it really necessary to crash here?
Andrew Hayden (chromium.org)
2014/03/13 16:39:48
No, it isn't. I just put a comment in the code ins
|
| + return; |
| + } |
| + } |
| + |
| + // Get a handle to the renderer process for IPC. |
| + base::ProcessHandle rendererProcessHandle = |
| + GetWebContents()->GetRenderViewHost()->GetProcess()->GetHandle(); |
| + // Prepare a file handle for transit to that renderer process. |
| + IPC::PlatformFileForTransit ipcPlatformFile = IPC::GetFileHandleForProcess( |
| + cached_platform_file, |
| + rendererProcessHandle, |
| + false); |
| + // Send the file handle to the renderer via IPC. We're done! |
| + Send(new ChromeViewMsg_CLDDataAvailable(routing_id, ipcPlatformFile)); |
| +} |
| + |
| void TranslateTabHelper::OnLanguageDetermined( |
| const LanguageDetectionDetails& details, |
| bool page_needs_translation) { |