OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "data_file_renderer_cld_data_provider.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/files/file.h" |
| 9 #include "base/files/memory_mapped_file.h" |
| 10 #include "base/lazy_instance.h" |
| 11 #include "components/translate/content/common/data_file_cld_data_provider_messag
es.h" |
| 12 #include "content/public/renderer/render_view_observer.h" |
| 13 #include "ipc/ipc_message.h" |
| 14 #include "ipc/ipc_message_macros.h" |
| 15 #include "ipc/ipc_platform_file.h" |
| 16 #include "third_party/cld_2/src/public/compact_lang_det.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 // A struct that contains the pointer to the CLD mmap. Used so that we can |
| 21 // leverage LazyInstance:Leaky to properly scope the lifetime of the mmap. |
| 22 struct CLDMmapWrapper { |
| 23 CLDMmapWrapper() { value = NULL; } |
| 24 base::MemoryMappedFile* value; |
| 25 }; |
| 26 base::LazyInstance<CLDMmapWrapper>::Leaky g_cld_mmap = |
| 27 LAZY_INSTANCE_INITIALIZER; |
| 28 |
| 29 } // namespace |
| 30 |
| 31 namespace translate { |
| 32 |
| 33 // Implementation of the static factory method from RendererCldDataProvider, |
| 34 // hooking up this specific implementation for all of Chromium. |
| 35 RendererCldDataProvider* CreateRendererCldDataProviderFor( |
| 36 content::RenderViewObserver* render_view_observer) { |
| 37 return new DataFileRendererCldDataProvider(render_view_observer); |
| 38 } |
| 39 |
| 40 DataFileRendererCldDataProvider::DataFileRendererCldDataProvider( |
| 41 content::RenderViewObserver* render_view_observer) |
| 42 : render_view_observer_(render_view_observer) { |
| 43 } |
| 44 |
| 45 DataFileRendererCldDataProvider::~DataFileRendererCldDataProvider() { |
| 46 } |
| 47 |
| 48 bool DataFileRendererCldDataProvider::OnMessageReceived( |
| 49 const IPC::Message& message) { |
| 50 bool handled = true; |
| 51 IPC_BEGIN_MESSAGE_MAP(DataFileRendererCldDataProvider, message) |
| 52 IPC_MESSAGE_HANDLER(ChromeViewMsg_CldDataFileAvailable, OnCldDataAvailable) |
| 53 IPC_MESSAGE_UNHANDLED(handled = false) |
| 54 IPC_END_MESSAGE_MAP() |
| 55 return handled; |
| 56 } |
| 57 |
| 58 void DataFileRendererCldDataProvider::SendCldDataRequest() { |
| 59 // Else, send the IPC message to the browser process requesting the data... |
| 60 render_view_observer_->Send(new ChromeViewHostMsg_NeedCldDataFile( |
| 61 render_view_observer_->routing_id())); |
| 62 } |
| 63 |
| 64 bool DataFileRendererCldDataProvider::IsCldDataAvailable() { |
| 65 // This neatly removes the need for code that depends on the generalized |
| 66 // RendererCldDataProvider to #ifdef on CLD2_DYNAMIC_MODE |
| 67 return CLD2::isDataLoaded(); // ground truth, independent of our state. |
| 68 } |
| 69 |
| 70 void DataFileRendererCldDataProvider::SetCldAvailableCallback( |
| 71 base::Callback<void(void)> callback) { |
| 72 cld_available_callback_ = callback; |
| 73 } |
| 74 |
| 75 void DataFileRendererCldDataProvider::OnCldDataAvailable( |
| 76 const IPC::PlatformFileForTransit ipc_file_handle, |
| 77 const uint64 data_offset, |
| 78 const uint64 data_length) { |
| 79 LoadCldData(IPC::PlatformFileForTransitToFile(ipc_file_handle), |
| 80 data_offset, |
| 81 data_length); |
| 82 } |
| 83 |
| 84 void DataFileRendererCldDataProvider::LoadCldData(base::File file, |
| 85 const uint64 data_offset, |
| 86 const uint64 data_length) { |
| 87 // Terminate immediately if data is already loaded. |
| 88 if (IsCldDataAvailable()) |
| 89 return; |
| 90 |
| 91 if (!file.IsValid()) { |
| 92 LOG(ERROR) << "Can't find the CLD data file."; |
| 93 return; |
| 94 } |
| 95 |
| 96 // mmap the file |
| 97 g_cld_mmap.Get().value = new base::MemoryMappedFile(); |
| 98 bool initialized = g_cld_mmap.Get().value->Initialize(file.Pass()); |
| 99 if (!initialized) { |
| 100 LOG(ERROR) << "mmap initialization failed"; |
| 101 delete g_cld_mmap.Get().value; |
| 102 g_cld_mmap.Get().value = NULL; |
| 103 return; |
| 104 } |
| 105 |
| 106 // Sanity checks |
| 107 uint64 max_int32 = std::numeric_limits<int32>::max(); |
| 108 if (data_length + data_offset > g_cld_mmap.Get().value->length() || |
| 109 data_length > max_int32) { // max signed 32 bit integer |
| 110 LOG(ERROR) << "Illegal mmap config: data_offset=" << data_offset |
| 111 << ", data_length=" << data_length |
| 112 << ", mmap->length()=" << g_cld_mmap.Get().value->length(); |
| 113 delete g_cld_mmap.Get().value; |
| 114 g_cld_mmap.Get().value = NULL; |
| 115 return; |
| 116 } |
| 117 |
| 118 // Initialize the CLD subsystem... and it's all done! |
| 119 const uint8* data_ptr = g_cld_mmap.Get().value->data() + data_offset; |
| 120 CLD2::loadDataFromRawAddress(data_ptr, data_length); |
| 121 DCHECK(CLD2::isDataLoaded()) << "Failed to load CLD data from mmap"; |
| 122 if (!cld_available_callback_.is_null()) { |
| 123 cld_available_callback_.Run(); |
| 124 } |
| 125 } |
| 126 |
| 127 } // namespace translate |
OLD | NEW |