OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // NOT DEAD CODE! | 5 // NOT DEAD CODE! |
6 // This code isn't dead, even if it isn't currently being used. Please refer to: | 6 // This code isn't dead, even if it isn't currently being used. Please refer to: |
7 // https://www.chromium.org/developers/how-tos/compact-language-detector-cld-dat
a-source-configuration | 7 // https://www.chromium.org/developers/how-tos/compact-language-detector-cld-dat
a-source-configuration |
8 | 8 |
9 #include "components/translate/content/browser/data_file_browser_cld_data_provid
er.h" | 9 #include "components/translate/content/browser/data_file_browser_cld_data_provid
er.h" |
10 | 10 |
11 #include "base/basictypes.h" | |
12 #include "base/files/file.h" | 11 #include "base/files/file.h" |
13 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
14 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
15 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
16 #include "base/logging.h" | 15 #include "base/logging.h" |
17 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
18 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
19 #include "base/task_runner.h" | 18 #include "base/task_runner.h" |
20 #include "components/translate/content/common/cld_data_source.h" | 19 #include "components/translate/content/common/cld_data_source.h" |
21 #include "components/translate/content/common/data_file_cld_data_provider_messag
es.h" | 20 #include "components/translate/content/common/data_file_cld_data_provider_messag
es.h" |
22 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
23 #include "content/public/browser/render_frame_host.h" | 22 #include "content/public/browser/render_frame_host.h" |
24 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
25 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
26 #include "ipc/ipc_message.h" | 25 #include "ipc/ipc_message.h" |
27 #include "ipc/ipc_message_macros.h" | 26 #include "ipc/ipc_message_macros.h" |
28 #include "ipc/ipc_platform_file.h" | 27 #include "ipc/ipc_platform_file.h" |
29 | 28 |
30 namespace { | 29 namespace { |
31 // The data file, cached as long as the process stays alive. | 30 // The data file, cached as long as the process stays alive. |
32 // We also track the offset at which the data starts, and its length. | 31 // We also track the offset at which the data starts, and its length. |
33 base::LazyInstance<base::FilePath> g_cached_filepath; // guarded by g_file_lock | 32 base::LazyInstance<base::FilePath> g_cached_filepath; // guarded by g_file_lock |
34 base::File* g_cached_file = NULL; // guarded by g_file_lock_ | 33 base::File* g_cached_file = NULL; // guarded by g_file_lock_ |
35 uint64 g_cached_data_offset = 0; // guarded by g_file_lock_ | 34 uint64_t g_cached_data_offset = 0; // guarded by g_file_lock_ |
36 uint64 g_cached_data_length = 0; // guarded by g_file_lock_ | 35 uint64_t g_cached_data_length = 0; // guarded by g_file_lock_ |
37 | 36 |
38 // Guards g_cached_filepath | 37 // Guards g_cached_filepath |
39 base::LazyInstance<base::Lock> g_file_lock_; | 38 base::LazyInstance<base::Lock> g_file_lock_; |
40 } // namespace | 39 } // namespace |
41 | 40 |
42 namespace translate { | 41 namespace translate { |
43 | 42 |
44 void SetCldDataFilePath(const base::FilePath& path) { | 43 void SetCldDataFilePath(const base::FilePath& path) { |
45 VLOG(1) << "Setting CLD data file path to: " << path.value(); | 44 VLOG(1) << "Setting CLD data file path to: " << path.value(); |
46 base::AutoLock lock(g_file_lock_.Get()); | 45 base::AutoLock lock(g_file_lock_.Get()); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 IPC_END_MESSAGE_MAP() | 79 IPC_END_MESSAGE_MAP() |
81 return handled; | 80 return handled; |
82 } | 81 } |
83 | 82 |
84 void DataFileBrowserCldDataProvider::OnCldDataRequest() { | 83 void DataFileBrowserCldDataProvider::OnCldDataRequest() { |
85 // Quickly try to read g_cached_file. If valid, the file handle is | 84 // Quickly try to read g_cached_file. If valid, the file handle is |
86 // cached and can be used immediately. Else, queue the caching task to the | 85 // cached and can be used immediately. Else, queue the caching task to the |
87 // blocking pool. | 86 // blocking pool. |
88 VLOG(1) << "Received request for CLD data file."; | 87 VLOG(1) << "Received request for CLD data file."; |
89 base::File* handle = NULL; | 88 base::File* handle = NULL; |
90 uint64 data_offset = 0; | 89 uint64_t data_offset = 0; |
91 uint64 data_length = 0; | 90 uint64_t data_length = 0; |
92 { | 91 { |
93 base::AutoLock lock(g_file_lock_.Get()); | 92 base::AutoLock lock(g_file_lock_.Get()); |
94 handle = g_cached_file; | 93 handle = g_cached_file; |
95 data_offset = g_cached_data_offset; | 94 data_offset = g_cached_data_offset; |
96 data_length = g_cached_data_length; | 95 data_length = g_cached_data_length; |
97 } | 96 } |
98 | 97 |
99 if (handle && handle->IsValid()) { | 98 if (handle && handle->IsValid()) { |
100 // Cached data available. Respond to the request. | 99 // Cached data available. Respond to the request. |
101 VLOG(1) << "CLD data file is already cached, replying immediately."; | 100 VLOG(1) << "CLD data file is already cached, replying immediately."; |
(...skipping 16 matching lines...) Expand all Loading... |
118 VLOG(1) << "CLD data file not yet cached, deferring lookup"; | 117 VLOG(1) << "CLD data file not yet cached, deferring lookup"; |
119 content::BrowserThread::PostBlockingPoolTaskAndReply( | 118 content::BrowserThread::PostBlockingPoolTaskAndReply( |
120 FROM_HERE, | 119 FROM_HERE, |
121 base::Bind(&DataFileBrowserCldDataProvider::OnCldDataRequestInternal), | 120 base::Bind(&DataFileBrowserCldDataProvider::OnCldDataRequestInternal), |
122 base::Bind(&DataFileBrowserCldDataProvider::SendCldDataResponse, | 121 base::Bind(&DataFileBrowserCldDataProvider::SendCldDataResponse, |
123 weak_pointer_factory_.get()->GetWeakPtr())); | 122 weak_pointer_factory_.get()->GetWeakPtr())); |
124 } | 123 } |
125 | 124 |
126 void DataFileBrowserCldDataProvider::SendCldDataResponse() { | 125 void DataFileBrowserCldDataProvider::SendCldDataResponse() { |
127 base::File* handle = NULL; | 126 base::File* handle = NULL; |
128 uint64 data_offset = 0; | 127 uint64_t data_offset = 0; |
129 uint64 data_length = 0; | 128 uint64_t data_length = 0; |
130 { | 129 { |
131 base::AutoLock lock(g_file_lock_.Get()); | 130 base::AutoLock lock(g_file_lock_.Get()); |
132 handle = g_cached_file; | 131 handle = g_cached_file; |
133 data_offset = g_cached_data_offset; | 132 data_offset = g_cached_data_offset; |
134 data_length = g_cached_data_length; | 133 data_length = g_cached_data_length; |
135 } | 134 } |
136 | 135 |
137 if (handle && handle->IsValid()) | 136 if (handle && handle->IsValid()) |
138 SendCldDataResponseInternal(handle, data_offset, data_length); | 137 SendCldDataResponseInternal(handle, data_offset, data_length); |
139 } | 138 } |
140 | 139 |
141 void DataFileBrowserCldDataProvider::SendCldDataResponseInternal( | 140 void DataFileBrowserCldDataProvider::SendCldDataResponseInternal( |
142 const base::File* handle, | 141 const base::File* handle, |
143 const uint64 data_offset, | 142 const uint64_t data_offset, |
144 const uint64 data_length) { | 143 const uint64_t data_length) { |
145 VLOG(1) << "Sending CLD data file response."; | 144 VLOG(1) << "Sending CLD data file response."; |
146 | 145 |
147 content::RenderFrameHost* render_frame_host = | 146 content::RenderFrameHost* render_frame_host = |
148 web_contents_->GetMainFrame(); | 147 web_contents_->GetMainFrame(); |
149 if (render_frame_host == NULL) { | 148 if (render_frame_host == NULL) { |
150 // Render frame destroyed, no need to bother. | 149 // Render frame destroyed, no need to bother. |
151 VLOG(1) << "Lost render frame host, giving up"; | 150 VLOG(1) << "Lost render frame host, giving up"; |
152 return; | 151 return; |
153 } | 152 } |
154 | 153 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 | 216 |
218 base::File::Info file_info; | 217 base::File::Info file_info; |
219 if (!file->GetInfo(&file_info)) { | 218 if (!file->GetInfo(&file_info)) { |
220 LOG(WARNING) << "CLD data file exists but cannot be inspected"; | 219 LOG(WARNING) << "CLD data file exists but cannot be inspected"; |
221 return; | 220 return; |
222 } | 221 } |
223 | 222 |
224 // For now, our offset and length are simply 0 and the length of the file, | 223 // For now, our offset and length are simply 0 and the length of the file, |
225 // respectively. If we later decide to include the CLD2 data file inside of | 224 // respectively. If we later decide to include the CLD2 data file inside of |
226 // a larger binary context, these params can be twiddled appropriately. | 225 // a larger binary context, these params can be twiddled appropriately. |
227 const uint64 data_offset = 0; | 226 const uint64_t data_offset = 0; |
228 const uint64 data_length = file_info.size; | 227 const uint64_t data_length = file_info.size; |
229 | 228 |
230 { | 229 { |
231 base::AutoLock lock(g_file_lock_.Get()); | 230 base::AutoLock lock(g_file_lock_.Get()); |
232 if (g_cached_file) { | 231 if (g_cached_file) { |
233 // Idempotence: Racing another request on the blocking pool, abort. | 232 // Idempotence: Racing another request on the blocking pool, abort. |
234 VLOG(1) << "Another thread finished caching first, aborting."; | 233 VLOG(1) << "Another thread finished caching first, aborting."; |
235 } else { | 234 } else { |
236 // Else, this request has taken care of it all. Cache all info. | 235 // Else, this request has taken care of it all. Cache all info. |
237 VLOG(1) << "Caching CLD data file information."; | 236 VLOG(1) << "Caching CLD data file information."; |
238 g_cached_file = file.release(); | 237 g_cached_file = file.release(); |
239 g_cached_data_offset = data_offset; | 238 g_cached_data_offset = data_offset; |
240 g_cached_data_length = data_length; | 239 g_cached_data_length = data_length; |
241 } | 240 } |
242 } | 241 } |
243 } | 242 } |
244 | 243 |
245 } // namespace translate | 244 } // namespace translate |
OLD | NEW |