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 #include "data_file_browser_cld_data_provider.h" | 5 #include "data_file_browser_cld_data_provider.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
15 #include "base/task_runner.h" | 15 #include "base/task_runner.h" |
16 #include "components/translate/content/common/data_file_cld_data_provider_messag
es.h" | 16 #include "components/translate/content/common/data_file_cld_data_provider_messag
es.h" |
17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/render_process_host.h" | 18 #include "content/public/browser/render_process_host.h" |
19 #include "content/public/browser/render_view_host.h" | 19 #include "content/public/browser/render_view_host.h" |
| 20 #include "content/public/browser/web_contents.h" |
20 #include "ipc/ipc_message.h" | 21 #include "ipc/ipc_message.h" |
21 #include "ipc/ipc_message_macros.h" | 22 #include "ipc/ipc_message_macros.h" |
22 #include "ipc/ipc_platform_file.h" | 23 #include "ipc/ipc_platform_file.h" |
23 | 24 |
24 namespace { | 25 namespace { |
25 // The data file, cached as long as the process stays alive. | 26 // 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 // We also track the offset at which the data starts, and its length. |
27 base::FilePath g_cached_filepath; // guarded by g_file_lock_ | 28 base::FilePath g_cached_filepath; // guarded by g_file_lock_ |
28 base::File* g_cached_file = NULL; // guarded by g_file_lock_ | 29 base::File* g_cached_file = NULL; // guarded by g_file_lock_ |
29 uint64 g_cached_data_offset = -1; // guarded by g_file_lock_ | 30 uint64 g_cached_data_offset = -1; // guarded by g_file_lock_ |
30 uint64 g_cached_data_length = -1; // guarded by g_file_lock_ | 31 uint64 g_cached_data_length = -1; // guarded by g_file_lock_ |
31 | 32 |
32 // Guards g_cached_filepath | 33 // Guards g_cached_filepath |
33 base::LazyInstance<base::Lock> g_file_lock_; | 34 base::LazyInstance<base::Lock> g_file_lock_; |
34 } // namespace | 35 } // namespace |
35 | 36 |
36 namespace translate { | 37 namespace translate { |
37 | 38 |
38 // Implementation of the static factory method from BrowserCldDataProvider, | 39 // Implementation of the static factory method from BrowserCldDataProvider, |
39 // hooking up this specific implementation for all of Chromium. | 40 // hooking up this specific implementation for all of Chromium. |
40 BrowserCldDataProvider* CreateBrowserCldDataProviderFor( | 41 BrowserCldDataProvider* CreateBrowserCldDataProviderFor( |
41 content::RenderViewHost* render_view_host) { | 42 content::WebContents* web_contents) { |
42 return new DataFileBrowserCldDataProvider(render_view_host); | 43 return new DataFileBrowserCldDataProvider(web_contents); |
43 } | 44 } |
44 | 45 |
45 void DataFileBrowserCldDataProvider::SetCldDataFilePath( | 46 void DataFileBrowserCldDataProvider::SetCldDataFilePath( |
46 const base::FilePath& path) { | 47 const base::FilePath& path) { |
47 VLOG(1) << "Setting CLD data file path to: " << path.value(); | 48 VLOG(1) << "Setting CLD data file path to: " << path.value(); |
48 base::AutoLock lock(g_file_lock_.Get()); | 49 base::AutoLock lock(g_file_lock_.Get()); |
49 if (g_cached_filepath == path) | 50 if (g_cached_filepath == path) |
50 return; // no change necessary | 51 return; // no change necessary |
51 g_cached_filepath = path; | 52 g_cached_filepath = path; |
52 // For sanity, clean these other values up just in case. | 53 // For sanity, clean these other values up just in case. |
53 g_cached_file = NULL; | 54 g_cached_file = NULL; |
54 g_cached_data_length = -1; | 55 g_cached_data_length = -1; |
55 g_cached_data_offset = -1; | 56 g_cached_data_offset = -1; |
56 } | 57 } |
57 | 58 |
58 base::FilePath DataFileBrowserCldDataProvider::GetCldDataFilePath() { | 59 base::FilePath DataFileBrowserCldDataProvider::GetCldDataFilePath() { |
59 base::AutoLock lock(g_file_lock_.Get()); | 60 base::AutoLock lock(g_file_lock_.Get()); |
60 return g_cached_filepath; | 61 return g_cached_filepath; |
61 } | 62 } |
62 | 63 |
63 DataFileBrowserCldDataProvider::DataFileBrowserCldDataProvider( | 64 DataFileBrowserCldDataProvider::DataFileBrowserCldDataProvider( |
64 content::RenderViewHost* render_view_host) | 65 content::WebContents* web_contents) |
65 : render_view_host_(render_view_host), weak_pointer_factory_() { | 66 : web_contents_(web_contents), weak_pointer_factory_() { |
66 } | 67 } |
67 | 68 |
68 DataFileBrowserCldDataProvider::~DataFileBrowserCldDataProvider() { | 69 DataFileBrowserCldDataProvider::~DataFileBrowserCldDataProvider() { |
69 } | 70 } |
70 | 71 |
71 bool DataFileBrowserCldDataProvider::OnMessageReceived( | 72 bool DataFileBrowserCldDataProvider::OnMessageReceived( |
72 const IPC::Message& message) { | 73 const IPC::Message& message) { |
73 bool handled = true; | 74 bool handled = true; |
74 IPC_BEGIN_MESSAGE_MAP(DataFileBrowserCldDataProvider, message) | 75 IPC_BEGIN_MESSAGE_MAP(DataFileBrowserCldDataProvider, message) |
75 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCldDataFile, OnCldDataRequest) | 76 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCldDataFile, OnCldDataRequest) |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 134 |
134 if (handle && handle->IsValid()) | 135 if (handle && handle->IsValid()) |
135 SendCldDataResponseInternal(handle, data_offset, data_length); | 136 SendCldDataResponseInternal(handle, data_offset, data_length); |
136 } | 137 } |
137 | 138 |
138 void DataFileBrowserCldDataProvider::SendCldDataResponseInternal( | 139 void DataFileBrowserCldDataProvider::SendCldDataResponseInternal( |
139 const base::File* handle, | 140 const base::File* handle, |
140 const uint64 data_offset, | 141 const uint64 data_offset, |
141 const uint64 data_length) { | 142 const uint64 data_length) { |
142 VLOG(1) << "Sending CLD data file response."; | 143 VLOG(1) << "Sending CLD data file response."; |
| 144 |
| 145 content::RenderViewHost* render_view_host = |
| 146 web_contents_->GetRenderViewHost(); |
| 147 if (render_view_host == NULL) { |
| 148 // Render view destroyed, no need to bother. |
| 149 VLOG(1) << "Lost render view host, giving up"; |
| 150 return; |
| 151 } |
| 152 |
| 153 content::RenderProcessHost* render_process_host = |
| 154 render_view_host->GetProcess(); |
| 155 if (render_process_host == NULL) { |
| 156 // Render process destroyed, render view not yet dead. No need to bother. |
| 157 VLOG(1) << "Lost render process, giving up"; |
| 158 return; |
| 159 } |
| 160 |
143 // Data available, respond to the request. | 161 // Data available, respond to the request. |
| 162 const int render_process_handle = render_process_host->GetHandle(); |
144 IPC::PlatformFileForTransit ipc_platform_file = | 163 IPC::PlatformFileForTransit ipc_platform_file = |
145 IPC::GetFileHandleForProcess(handle->GetPlatformFile(), | 164 IPC::GetFileHandleForProcess(handle->GetPlatformFile(), |
146 render_view_host_->GetProcess()->GetHandle(), | 165 render_process_handle, false); |
147 false); | 166 |
148 // In general, sending a response from within the code path that is processing | 167 // 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 | 168 // 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 | 169 // 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 | 170 // 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 | 171 // sending the response from within the code path of the request handler is |
153 // safe. | 172 // safe. |
154 render_view_host_->Send( | 173 render_view_host->Send( |
155 new ChromeViewMsg_CldDataFileAvailable(render_view_host_->GetRoutingID(), | 174 new ChromeViewMsg_CldDataFileAvailable(render_view_host->GetRoutingID(), |
156 ipc_platform_file, | 175 ipc_platform_file, |
157 data_offset, | 176 data_offset, |
158 data_length)); | 177 data_length)); |
159 } | 178 } |
160 | 179 |
161 void DataFileBrowserCldDataProvider::OnCldDataRequestInternal() { | 180 void DataFileBrowserCldDataProvider::OnCldDataRequestInternal() { |
162 // Because this function involves arbitrary file system access, it must run | 181 // Because this function involves arbitrary file system access, it must run |
163 // on the blocking pool. | 182 // on the blocking pool. |
164 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 183 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
165 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 184 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 // Else, this request has taken care of it all. Cache all info. | 234 // Else, this request has taken care of it all. Cache all info. |
216 VLOG(1) << "Caching CLD data file information."; | 235 VLOG(1) << "Caching CLD data file information."; |
217 g_cached_file = file.release(); | 236 g_cached_file = file.release(); |
218 g_cached_data_offset = data_offset; | 237 g_cached_data_offset = data_offset; |
219 g_cached_data_length = data_length; | 238 g_cached_data_length = data_length; |
220 } | 239 } |
221 } | 240 } |
222 } | 241 } |
223 | 242 |
224 } // namespace translate | 243 } // namespace translate |
OLD | NEW |