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 "ipc/ipc_message_macros.h" | |
13 #include "ipc/ipc_platform_file.h" | |
14 #include "third_party/cld_2/src/public/compact_lang_det.h" | |
15 | |
16 namespace content { | |
17 // The mmap for the CLD2 data must be held forever once it is available in the | |
18 // process. This is declared static in the translate_helper.h. | |
19 base::LazyInstance<DataFileRendererCldDataProvider::CLDMmapWrapper>::Leaky | |
20 DataFileRendererCldDataProvider::s_cld_mmap_ = LAZY_INSTANCE_INITIALIZER; | |
21 } // namespace | |
Takashi Toyoshima
2014/06/19 10:20:35
remove line 21 - 23
Andrew Hayden (chromium.org)
2014/06/19 13:40:20
Whoops. Done.
| |
22 | |
23 namespace content { | |
24 | |
25 DataFileRendererCldDataProvider::DataFileRendererCldDataProvider( | |
26 content::RenderViewObserver* render_view_observer) | |
27 : render_view_observer (render_view_observer) { | |
28 } | |
29 | |
30 bool DataFileRendererCldDataProvider::OnMessageReceived( | |
31 const IPC::Message& message) { | |
32 bool handled = true; | |
33 IPC_BEGIN_MESSAGE_MAP(DataFileRendererCldDataProvider, message) | |
34 IPC_MESSAGE_HANDLER(ChromeViewMsg_CldDataFileAvailable, OnCldDataAvailable) | |
35 IPC_MESSAGE_UNHANDLED(handled = false) | |
36 IPC_END_MESSAGE_MAP() | |
37 return handled; | |
38 } | |
39 | |
40 void DataFileRendererCldDataProvider::SendCldDataRequest() { | |
41 // Else, send the IPC message to the browser process requesting the data... | |
42 render_view_observer->Send(new ChromeViewHostMsg_NeedCldDataFile( | |
43 render_view_observer->routing_id())); | |
44 } | |
45 | |
46 bool DataFileRendererCldDataProvider::IsCldDataAvailable() { | |
47 // This neatly removes the need for code that depends on the generalized | |
48 // RendererCldDataProvider to #ifdef on CLD2_DYNAMIC_MODE | |
49 return CLD2::isDataLoaded(); // ground truth, independent of our state. | |
50 } | |
51 | |
52 void DataFileRendererCldDataProvider::SetCldAvailableCallback( | |
53 base::Callback<void(void)> callback) { | |
54 cld_available_callback = callback; | |
55 } | |
56 | |
57 void DataFileRendererCldDataProvider::OnCldDataAvailable( | |
58 const IPC::PlatformFileForTransit ipc_file_handle, | |
59 const uint64 data_offset, | |
60 const uint64 data_length) { | |
61 LoadCldData(IPC::PlatformFileForTransitToFile(ipc_file_handle), | |
62 data_offset, | |
63 data_length); | |
64 } | |
65 | |
66 void DataFileRendererCldDataProvider::LoadCldData( | |
67 base::File file, | |
68 const uint64 data_offset, | |
69 const uint64 data_length) { | |
70 | |
71 // Terminate immediately if data is already loaded. | |
72 if (IsCldDataAvailable()) | |
73 return; | |
74 | |
75 if (!file.IsValid()) { | |
76 LOG(ERROR) << "Can't find the CLD data file."; | |
77 return; | |
78 } | |
79 | |
80 // mmap the file | |
81 s_cld_mmap_.Get().value = new base::MemoryMappedFile(); | |
82 bool initialized = s_cld_mmap_.Get().value->Initialize(file.Pass()); | |
83 if (!initialized) { | |
84 LOG(ERROR) << "mmap initialization failed"; | |
85 delete s_cld_mmap_.Get().value; | |
86 s_cld_mmap_.Get().value = NULL; | |
87 return; | |
88 } | |
89 | |
90 // Sanity checks | |
91 uint64 max_int32 = std::numeric_limits<int32>::max(); | |
92 if (data_length + data_offset > s_cld_mmap_.Get().value->length() | |
93 || data_length > max_int32) { // max signed 32 bit integer | |
94 LOG(ERROR) << "Illegal mmap config: data_offset=" | |
95 << data_offset << ", data_length=" << data_length | |
96 << ", mmap->length()=" << s_cld_mmap_.Get().value->length(); | |
97 delete s_cld_mmap_.Get().value; | |
98 s_cld_mmap_.Get().value = NULL; | |
99 return; | |
100 } | |
101 | |
102 // Initialize the CLD subsystem... and it's all done! | |
103 const uint8* data_ptr = s_cld_mmap_.Get().value->data() + data_offset; | |
104 CLD2::loadDataFromRawAddress(data_ptr, data_length); | |
105 DCHECK(CLD2::isDataLoaded()) << "Failed to load CLD data from mmap"; | |
106 if (!cld_available_callback.is_null()) { | |
107 cld_available_callback.Run(); | |
108 } | |
109 } | |
110 | |
111 } // namespace content | |
OLD | NEW |