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