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