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

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

Powered by Google App Engine
This is Rietveld 408576698