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 "components/translate/content/browser/data_file_browser_cld_data_provid
er.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/files/file_util.h" |
10 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
13 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
14 #include "base/task_runner.h" | 15 #include "base/task_runner.h" |
| 16 #include "components/translate/content/common/cld_data_source.h" |
15 #include "components/translate/content/common/data_file_cld_data_provider_messag
es.h" | 17 #include "components/translate/content/common/data_file_cld_data_provider_messag
es.h" |
16 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
18 #include "content/public/browser/render_view_host.h" | 20 #include "content/public/browser/render_view_host.h" |
19 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
20 #include "ipc/ipc_message.h" | 22 #include "ipc/ipc_message.h" |
21 #include "ipc/ipc_message_macros.h" | 23 #include "ipc/ipc_message_macros.h" |
22 #include "ipc/ipc_platform_file.h" | 24 #include "ipc/ipc_platform_file.h" |
23 | 25 |
24 namespace { | 26 namespace { |
25 // The data file, cached as long as the process stays alive. | 27 // 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. | 28 // We also track the offset at which the data starts, and its length. |
27 base::FilePath g_cached_filepath; // guarded by g_file_lock_ | 29 base::FilePath g_cached_filepath; // guarded by g_file_lock_ |
28 base::File* g_cached_file = NULL; // guarded by g_file_lock_ | 30 base::File* g_cached_file = NULL; // guarded by g_file_lock_ |
29 uint64 g_cached_data_offset = -1; // guarded by g_file_lock_ | 31 uint64 g_cached_data_offset = 0; // guarded by g_file_lock_ |
30 uint64 g_cached_data_length = -1; // guarded by g_file_lock_ | 32 uint64 g_cached_data_length = 0; // guarded by g_file_lock_ |
31 | 33 |
32 // Guards g_cached_filepath | 34 // Guards g_cached_filepath |
33 base::LazyInstance<base::Lock> g_file_lock_; | 35 base::LazyInstance<base::Lock> g_file_lock_; |
34 } // namespace | 36 } // namespace |
35 | 37 |
36 namespace translate { | 38 namespace translate { |
37 | 39 |
38 // Implementation of the static factory method from BrowserCldDataProvider, | |
39 // hooking up this specific implementation for all of Chromium. | |
40 BrowserCldDataProvider* CreateBrowserCldDataProviderFor( | |
41 content::WebContents* web_contents) { | |
42 VLOG(1) << "Creating DataFileBrowserCldDataProvider"; | |
43 return new DataFileBrowserCldDataProvider(web_contents); | |
44 } | |
45 | |
46 void SetCldDataFilePath(const base::FilePath& path) { | 40 void SetCldDataFilePath(const base::FilePath& path) { |
47 VLOG(1) << "Setting CLD data file path to: " << path.value(); | 41 VLOG(1) << "Setting CLD data file path to: " << path.value(); |
48 base::AutoLock lock(g_file_lock_.Get()); | 42 base::AutoLock lock(g_file_lock_.Get()); |
49 if (g_cached_filepath == path) | 43 if (g_cached_filepath == path) |
50 return; // no change necessary | 44 return; // no change necessary |
51 g_cached_filepath = path; | 45 g_cached_filepath = path; |
52 // For sanity, clean these other values up just in case. | 46 // For sanity, clean these other values up just in case. |
53 g_cached_file = NULL; | 47 g_cached_file = NULL; |
54 g_cached_data_length = -1; | 48 g_cached_data_length = 0; |
55 g_cached_data_offset = -1; | 49 g_cached_data_offset = 0; |
56 } | 50 } |
57 | 51 |
58 base::FilePath GetCldDataFilePath() { | 52 base::FilePath GetCldDataFilePath() { |
59 base::AutoLock lock(g_file_lock_.Get()); | 53 base::AutoLock lock(g_file_lock_.Get()); |
| 54 if (g_cached_filepath.empty()) { |
| 55 g_cached_filepath = translate::CldDataSource::Get()->GetCldDataFilePath(); |
| 56 } |
60 return g_cached_filepath; | 57 return g_cached_filepath; |
61 } | 58 } |
62 | 59 |
63 DataFileBrowserCldDataProvider::DataFileBrowserCldDataProvider( | 60 DataFileBrowserCldDataProvider::DataFileBrowserCldDataProvider( |
64 content::WebContents* web_contents) | 61 content::WebContents* web_contents) |
65 : web_contents_(web_contents), weak_pointer_factory_() { | 62 : web_contents_(web_contents), weak_pointer_factory_() { |
66 } | 63 } |
67 | 64 |
68 DataFileBrowserCldDataProvider::~DataFileBrowserCldDataProvider() { | 65 DataFileBrowserCldDataProvider::~DataFileBrowserCldDataProvider() { |
| 66 // web_contents_ outlives this object |
69 } | 67 } |
70 | 68 |
71 bool DataFileBrowserCldDataProvider::OnMessageReceived( | 69 bool DataFileBrowserCldDataProvider::OnMessageReceived( |
72 const IPC::Message& message) { | 70 const IPC::Message& message) { |
73 bool handled = true; | 71 bool handled = true; |
74 IPC_BEGIN_MESSAGE_MAP(DataFileBrowserCldDataProvider, message) | 72 IPC_BEGIN_MESSAGE_MAP(DataFileBrowserCldDataProvider, message) |
75 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCldDataFile, OnCldDataRequest) | 73 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCldDataFile, OnCldDataRequest) |
76 IPC_MESSAGE_UNHANDLED(handled = false) | 74 IPC_MESSAGE_UNHANDLED(handled = false) |
77 IPC_END_MESSAGE_MAP() | 75 IPC_END_MESSAGE_MAP() |
78 return handled; | 76 return handled; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 | 149 |
152 content::RenderProcessHost* render_process_host = | 150 content::RenderProcessHost* render_process_host = |
153 render_view_host->GetProcess(); | 151 render_view_host->GetProcess(); |
154 if (render_process_host == NULL) { | 152 if (render_process_host == NULL) { |
155 // Render process destroyed, render view not yet dead. No need to bother. | 153 // Render process destroyed, render view not yet dead. No need to bother. |
156 VLOG(1) << "Lost render process, giving up"; | 154 VLOG(1) << "Lost render process, giving up"; |
157 return; | 155 return; |
158 } | 156 } |
159 | 157 |
160 // Data available, respond to the request. | 158 // Data available, respond to the request. |
161 const int render_process_handle = render_process_host->GetHandle(); | 159 base::ProcessHandle render_process_handle = render_process_host->GetHandle(); |
162 IPC::PlatformFileForTransit ipc_platform_file = | 160 IPC::PlatformFileForTransit ipc_platform_file = |
163 IPC::GetFileHandleForProcess(handle->GetPlatformFile(), | 161 IPC::GetFileHandleForProcess(handle->GetPlatformFile(), |
164 render_process_handle, false); | 162 render_process_handle, false); |
165 | 163 |
166 // In general, sending a response from within the code path that is processing | 164 // In general, sending a response from within the code path that is processing |
167 // a request is discouraged because there is potential for deadlock (if the | 165 // a request is discouraged because there is potential for deadlock (if the |
168 // methods are sent synchronously) or loops (if the response can trigger a | 166 // methods are sent synchronously) or loops (if the response can trigger a |
169 // new request). Neither of these concerns is relevant in this code, so | 167 // new request). Neither of these concerns is relevant in this code, so |
170 // sending the response from within the code path of the request handler is | 168 // sending the response from within the code path of the request handler is |
171 // safe. | 169 // safe. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 // Else, this request has taken care of it all. Cache all info. | 231 // Else, this request has taken care of it all. Cache all info. |
234 VLOG(1) << "Caching CLD data file information."; | 232 VLOG(1) << "Caching CLD data file information."; |
235 g_cached_file = file.release(); | 233 g_cached_file = file.release(); |
236 g_cached_data_offset = data_offset; | 234 g_cached_data_offset = data_offset; |
237 g_cached_data_length = data_length; | 235 g_cached_data_length = data_length; |
238 } | 236 } |
239 } | 237 } |
240 } | 238 } |
241 | 239 |
242 } // namespace translate | 240 } // namespace translate |
OLD | NEW |