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

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: Takashi's comments and virtual destructors 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/memory/weak_ptr.h"
12 #include "base/path_service.h"
13 #include "base/synchronization/lock.h"
14 #include "base/task_runner.h"
15 #include "components/translate/content/common/data_file_cld_data_provider_messag es.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/render_process_host.h"
18 #include "content/public/browser/render_view_host.h"
19 #include "ipc/ipc_message.h"
20 #include "ipc/ipc_message_macros.h"
21 #include "ipc/ipc_platform_file.h"
22
23 namespace {
24 // The data file, cached as long as the process stays alive.
25 // We also track the offset at which the data starts, and its length.
26 base::FilePath s_cached_filepath_; // guarded by file_lock_
27 base::File* s_cached_file_ = NULL; // guarded by file_lock_
28 uint64 s_cached_data_offset_ = -1; // guarded by file_lock_
29 uint64 s_cached_data_length_ = -1; // guarded by file_lock_
30
31 // Guards s_cached_file_
32 base::LazyInstance<base::Lock> s_file_lock_;
33
34 } // namespace
35
36 namespace content {
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 SetCldDataFilePath(
46 const base::FilePath& path) {
droger 2014/06/19 16:13:19 This fits in one line.
Andrew Hayden (chromium.org) 2014/06/19 19:49:30 Done.
47 base::AutoLock lock(s_file_lock_.Get());
48 if (s_cached_filepath_ == path)
49 return; // no change necessary
50 s_cached_filepath_ = path;
51 // For sanity, clean these other values up just in case.
52 s_cached_file_ = NULL;
53 s_cached_data_length_ = -1;
54 s_cached_data_offset_ = -1;
55 }
56
57 base::FilePath GetCldDataFilePath() {
58 base::AutoLock lock(s_file_lock_.Get());
59 return s_cached_filepath_;
60 }
61
62
63 DataFileBrowserCldDataProvider::DataFileBrowserCldDataProvider(
64 content::RenderViewHost* render_view_host)
65 : render_view_host (render_view_host),
66 weak_pointer_factory_(this) {
67 }
68
69 DataFileBrowserCldDataProvider::~DataFileBrowserCldDataProvider() {
70 }
71
72 bool DataFileBrowserCldDataProvider::OnMessageReceived(
73 const IPC::Message& message) {
74 bool handled = true;
75 IPC_BEGIN_MESSAGE_MAP(DataFileBrowserCldDataProvider, message)
76 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCldDataFile, OnCldDataRequest)
77 IPC_MESSAGE_UNHANDLED(handled = false)
78 IPC_END_MESSAGE_MAP()
79 return handled;
80 }
81
82 void DataFileBrowserCldDataProvider::OnCldDataRequest() {
83 // Quickly try to read s_cached_file_. If valid, the file handle is
84 // cached and can be used immediately. Else, queue the caching task to the
85 // blocking pool.
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 = s_cached_file_;
92 data_offset = s_cached_data_offset_;
93 data_length = s_cached_data_length_;
94 }
95
96 if (handle && handle->IsValid()) {
97 // Cached data available. Respond to the request.
98 SendCldDataResponseInternal(handle, data_offset, data_length);
99 return;
100 }
101
102 // Else, we don't have the data file yet. Queue a caching attempt.
103 // The caching attempt happens in the blocking pool because it may involve
104 // arbitrary filesystem access.
105 // After the caching attempt is made, we call MaybeSendCLDDataAvailable
106 // to pass the file handle to the renderer. This only results in an IPC
107 // message if the caching attempt was successful.
108 content::BrowserThread::PostBlockingPoolTaskAndReply(
109 FROM_HERE,
110 base::Bind(&DataFileBrowserCldDataProvider::OnCldDataRequestInternal,
111 weak_pointer_factory_.GetWeakPtr()),
112 base::Bind(&DataFileBrowserCldDataProvider::SendCldDataResponse,
113 weak_pointer_factory_.GetWeakPtr()));
114
115 }
116
117 void DataFileBrowserCldDataProvider::SendCldDataResponse() {
118 base::File* handle = NULL;
119 uint64 data_offset = 0;
120 uint64 data_length = 0;
121 {
122 base::AutoLock lock(s_file_lock_.Get());
123 handle = s_cached_file_;
124 data_offset = s_cached_data_offset_;
125 data_length = s_cached_data_length_;
126 }
127
128 if (handle && handle->IsValid())
129 SendCldDataResponseInternal(handle, data_offset, data_length);
130 }
131
132 void DataFileBrowserCldDataProvider::SendCldDataResponseInternal(
133 const base::File* handle,
134 const uint64 data_offset,
135 const uint64 data_length) {
136 // Data available, respond to the request.
137 IPC::PlatformFileForTransit ipc_platform_file =
138 IPC::GetFileHandleForProcess(
139 handle->GetPlatformFile(),
140 render_view_host->GetProcess()->GetHandle(),
141 false);
142 // In general, sending a response from within the code path that is processing
143 // a request is discouraged because there is potential for deadlock (if the
144 // methods are sent synchronously) or loops (if the response can trigger a
145 // new request). Neither of these concerns is relevant in this code, so
146 // sending the response from within the code path of the request handler is
147 // safe.
148 render_view_host->Send(new ChromeViewMsg_CldDataFileAvailable(
149 render_view_host->GetRoutingID(),
150 ipc_platform_file,
151 data_offset,
152 data_length));
153 }
154
155 void DataFileBrowserCldDataProvider::OnCldDataRequestInternal() {
156 // Because this function involves arbitrary file system access, it must run
157 // on the blocking pool.
158 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
159 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
160
161 {
162 base::AutoLock lock(s_file_lock_.Get());
163 if (s_cached_file_)
164 return; // Already done, duplicate request
165 }
166
167 const base::FilePath path = GetCldDataFilePath();
168 if (path.empty())
169 return;
170
171 // If the file exists, we can send an IPC-safe construct back to the
172 // renderer process immediately; otherwise, nothing to do here.
173 if (!base::PathExists(path))
174 return;
175
176 // Attempt to open the file for reading.
177 scoped_ptr<base::File> file(
178 new base::File(path, base::File::FLAG_OPEN | base::File::FLAG_READ));
179 if (!file->IsValid()) {
180 LOG(WARNING) << "CLD data file exists but cannot be opened";
181 return;
182 }
183
184 base::File::Info file_info;
185 if (!file->GetInfo(&file_info)) {
186 LOG(WARNING) << "CLD data file exists but cannot be inspected";
187 return;
188 }
189
190 // For now, our offset and length are simply 0 and the length of the file,
191 // respectively. If we later decide to include the CLD2 data file inside of
192 // a larger binary context, these params can be twiddled appropriately.
193 const uint64 data_offset = 0;
194 const uint64 data_length = file_info.size;
195
196 {
197 base::AutoLock lock(s_file_lock_.Get());
198 if (s_cached_file_) {
199 // Idempotence: Racing another request on the blocking pool, abort.
200 } else {
201 // Else, this request has taken care of it all. Cache all info.
202 s_cached_file_ = file.release();
203 s_cached_data_offset_ = data_offset;
204 s_cached_data_length_ = data_length;
205 }
206 }
207 }
208
209 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698