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

Side by Side Diff: components/translate/content/browser/data_file_browser_cld_data_provider.cc

Issue 333603002: Modularize Compact Language Detector 2 (CLD2) data sources (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: git cl format Created 6 years, 6 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_browser_cld_data_provider.h"
6
7 #include "base/basictypes.h"
8 #include "base/files/file.h"
9 #include "base/files/file_path.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/path_service.h"
14 #include "base/synchronization/lock.h"
15 #include "base/task_runner.h"
16 #include "components/translate/content/common/data_file_cld_data_provider_messag es.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/render_view_host.h"
20 #include "ipc/ipc_message.h"
21 #include "ipc/ipc_message_macros.h"
22 #include "ipc/ipc_platform_file.h"
23
24 namespace {
25 // The data file, cached as long as the process stays alive.
26 // We also track the offset at which the data starts, and its length.
27 base::FilePath g_cached_filepath; // guarded by file_lock_
28 base::File* g_cached_file = NULL; // guarded by file_lock_
29 uint64 g_cached_data_offset = -1; // guarded by file_lock_
30 uint64 g_cached_data_length = -1; // guarded by file_lock_
31
32 // Guards g_cached_file
33 base::LazyInstance<base::Lock> s_file_lock_;
Takashi Toyoshima 2014/06/24 02:09:47 g_file_lock? comments should be updated. ditto on
34 } // namespace
35
36 namespace translate {
37
38 // Implementation of the static factory method from BrowserCldDataProvider,
39 // hooking up this specific implementation for all of Chromium.
40 BrowserCldDataProvider* CreateBrowserCldDataProviderFor(
41 content::RenderViewHost* render_view_host) {
42 return new DataFileBrowserCldDataProvider(render_view_host);
43 }
44
45 void DataFileBrowserCldDataProvider::SetCldDataFilePath(
46 const base::FilePath& path) {
47 VLOG(1) << "Setting CLD data file path to: " << path.value();
48 base::AutoLock lock(s_file_lock_.Get());
49 if (g_cached_filepath == path)
50 return; // no change necessary
51 g_cached_filepath = path;
52 // For sanity, clean these other values up just in case.
53 g_cached_file = NULL;
54 g_cached_data_length = -1;
55 g_cached_data_offset = -1;
56 }
57
58 base::FilePath DataFileBrowserCldDataProvider::GetCldDataFilePath() {
59 base::AutoLock lock(s_file_lock_.Get());
60 return g_cached_filepath;
61 }
62
63 DataFileBrowserCldDataProvider::DataFileBrowserCldDataProvider(
64 content::RenderViewHost* render_view_host)
65 : render_view_host_(render_view_host), weak_pointer_factory_() {
66 }
67
68 DataFileBrowserCldDataProvider::~DataFileBrowserCldDataProvider() {
69 }
70
71 bool DataFileBrowserCldDataProvider::OnMessageReceived(
72 const IPC::Message& message) {
73 bool handled = true;
74 IPC_BEGIN_MESSAGE_MAP(DataFileBrowserCldDataProvider, message)
75 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCldDataFile, OnCldDataRequest)
76 IPC_MESSAGE_UNHANDLED(handled = false)
77 IPC_END_MESSAGE_MAP()
78 return handled;
79 }
80
81 void DataFileBrowserCldDataProvider::OnCldDataRequest() {
82 // Quickly try to read g_cached_file. If valid, the file handle is
83 // cached and can be used immediately. Else, queue the caching task to the
84 // blocking pool.
85 VLOG(1) << "Received request for CLD data file.";
86 base::File* handle = NULL;
87 uint64 data_offset = 0;
88 uint64 data_length = 0;
89 {
90 base::AutoLock lock(s_file_lock_.Get());
91 handle = g_cached_file;
92 data_offset = g_cached_data_offset;
93 data_length = g_cached_data_length;
94 }
95
96 if (handle && handle->IsValid()) {
97 // Cached data available. Respond to the request.
98 VLOG(1) << "CLD data file is already cached, replying immediately.";
99 SendCldDataResponseInternal(handle, data_offset, data_length);
100 return;
101 }
102
103 if (weak_pointer_factory_.get() == NULL) {
104 weak_pointer_factory_.reset(
105 new base::WeakPtrFactory<DataFileBrowserCldDataProvider>(this));
106 weak_pointer_factory_.get()->GetWeakPtr().get();
107 }
108
109 // Else, we don't have the data file yet. Queue a caching attempt.
110 // The caching attempt happens in the blocking pool because it may involve
111 // arbitrary filesystem access.
112 // After the caching attempt is made, we call MaybeSendCLDDataAvailable
113 // to pass the file handle to the renderer. This only results in an IPC
114 // message if the caching attempt was successful.
115 VLOG(1) << "CLD data file not yet cached, deferring lookup";
116 content::BrowserThread::PostBlockingPoolTaskAndReply(
117 FROM_HERE,
118 base::Bind(&DataFileBrowserCldDataProvider::OnCldDataRequestInternal),
119 base::Bind(&DataFileBrowserCldDataProvider::SendCldDataResponse,
120 weak_pointer_factory_.get()->GetWeakPtr()));
121 }
122
123 void DataFileBrowserCldDataProvider::SendCldDataResponse() {
124 base::File* handle = NULL;
125 uint64 data_offset = 0;
126 uint64 data_length = 0;
127 {
128 base::AutoLock lock(s_file_lock_.Get());
129 handle = g_cached_file;
130 data_offset = g_cached_data_offset;
131 data_length = g_cached_data_length;
132 }
133
134 if (handle && handle->IsValid())
135 SendCldDataResponseInternal(handle, data_offset, data_length);
136 }
137
138 void DataFileBrowserCldDataProvider::SendCldDataResponseInternal(
139 const base::File* handle,
140 const uint64 data_offset,
141 const uint64 data_length) {
142 VLOG(1) << "Sending CLD data file response.";
143 // Data available, respond to the request.
144 IPC::PlatformFileForTransit ipc_platform_file =
145 IPC::GetFileHandleForProcess(handle->GetPlatformFile(),
146 render_view_host_->GetProcess()->GetHandle(),
147 false);
148 // In general, sending a response from within the code path that is processing
149 // a request is discouraged because there is potential for deadlock (if the
150 // methods are sent synchronously) or loops (if the response can trigger a
151 // new request). Neither of these concerns is relevant in this code, so
152 // sending the response from within the code path of the request handler is
153 // safe.
154 render_view_host_->Send(
155 new ChromeViewMsg_CldDataFileAvailable(render_view_host_->GetRoutingID(),
156 ipc_platform_file,
157 data_offset,
158 data_length));
159 }
160
161 void DataFileBrowserCldDataProvider::OnCldDataRequestInternal() {
162 // Because this function involves arbitrary file system access, it must run
163 // on the blocking pool.
164 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
165 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
166 VLOG(1) << "CLD data file caching attempt starting.";
167
168 {
169 base::AutoLock lock(s_file_lock_.Get());
170 if (g_cached_file) {
171 VLOG(1) << "CLD data file is already cached, aborting caching attempt";
172 return; // Already done, duplicate request
173 }
174 }
175
176 const base::FilePath path = GetCldDataFilePath();
177 if (path.empty()) {
178 VLOG(1) << "CLD data file does not yet have a known location.";
179 return;
180 }
181
182 // If the file exists, we can send an IPC-safe construct back to the
183 // renderer process immediately; otherwise, nothing to do here.
184 if (!base::PathExists(path)) {
185 VLOG(1) << "CLD data file does not exist.";
186 return;
187 }
188
189 // Attempt to open the file for reading.
190 scoped_ptr<base::File> file(
191 new base::File(path, base::File::FLAG_OPEN | base::File::FLAG_READ));
192 if (!file->IsValid()) {
193 LOG(WARNING) << "CLD data file exists but cannot be opened";
194 return;
195 }
196
197 base::File::Info file_info;
198 if (!file->GetInfo(&file_info)) {
199 LOG(WARNING) << "CLD data file exists but cannot be inspected";
200 return;
201 }
202
203 // For now, our offset and length are simply 0 and the length of the file,
204 // respectively. If we later decide to include the CLD2 data file inside of
205 // a larger binary context, these params can be twiddled appropriately.
206 const uint64 data_offset = 0;
207 const uint64 data_length = file_info.size;
208
209 {
210 base::AutoLock lock(s_file_lock_.Get());
211 if (g_cached_file) {
212 // Idempotence: Racing another request on the blocking pool, abort.
213 VLOG(1) << "Another thread finished caching first, aborting.";
214 } else {
215 // Else, this request has taken care of it all. Cache all info.
216 VLOG(1) << "Caching CLD data file information.";
217 g_cached_file = file.release();
218 g_cached_data_offset = data_offset;
219 g_cached_data_length = data_length;
220 }
221 }
222 }
223
224 } // namespace translate
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698