| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/pepper_flash_settings_manager.h" | 5 #include "chrome/browser/pepper_flash_settings_manager.h" |
| 6 | 6 |
| 7 #include <map> |
| 8 #include <utility> |
| 7 #include <vector> | 9 #include <vector> |
| 8 | 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/compiler_specific.h" |
| 13 #include "base/sequenced_task_runner_helpers.h" |
| 14 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/plugin_prefs.h" | 15 #include "chrome/browser/plugin_prefs.h" |
| 16 #include "chrome/browser/profiles/profile.h" |
| 10 #include "content/browser/plugin_service_impl.h" | 17 #include "content/browser/plugin_service_impl.h" |
| 18 #include "content/browser/ppapi_plugin_process_host.h" |
| 19 #include "content/public/browser/browser_context.h" |
| 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/common/content_constants.h" |
| 22 #include "ipc/ipc_channel.h" |
| 23 #include "ppapi/proxy/ppapi_messages.h" |
| 11 #include "webkit/plugins/plugin_constants.h" | 24 #include "webkit/plugins/plugin_constants.h" |
| 12 #include "webkit/plugins/webplugininfo.h" | 25 #include "webkit/plugins/webplugininfo.h" |
| 13 | 26 |
| 27 using content::BrowserThread; |
| 28 |
| 29 class PepperFlashSettingsManager::Core |
| 30 : public PpapiPluginProcessHost::BrokerClient, |
| 31 public IPC::Channel::Listener, |
| 32 public base::RefCountedThreadSafe<Core, BrowserThread::DeleteOnIOThread> { |
| 33 public: |
| 34 Core(PepperFlashSettingsManager* manager, |
| 35 content::BrowserContext* browser_context); |
| 36 |
| 37 // Stops sending notifications to |manager_| and sets it to NULL. |
| 38 void Detach(); |
| 39 |
| 40 void DeauthorizeContentLicenses(uint32 request_id); |
| 41 |
| 42 // PpapiPluginProcessHost::BrokerClient implementation. |
| 43 virtual void GetPpapiChannelInfo(base::ProcessHandle* renderer_handle, |
| 44 int* renderer_id) OVERRIDE; |
| 45 virtual void OnPpapiChannelOpened(const IPC::ChannelHandle& channel_handle, |
| 46 int plugin_child_id) OVERRIDE; |
| 47 virtual bool OffTheRecord() OVERRIDE; |
| 48 |
| 49 // IPC::Channel::Listener implementation. |
| 50 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 51 virtual void OnChannelError() OVERRIDE; |
| 52 |
| 53 private: |
| 54 friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; |
| 55 friend class base::DeleteHelper<Core>; |
| 56 |
| 57 enum RequestType { |
| 58 INVALID_REQUEST_TYPE = 0, |
| 59 DEAUTHORIZE_CONTENT_LICENSES |
| 60 }; |
| 61 |
| 62 struct PendingRequest { |
| 63 PendingRequest() : id(0), type(INVALID_REQUEST_TYPE) {} |
| 64 |
| 65 uint32 id; |
| 66 RequestType type; |
| 67 }; |
| 68 |
| 69 virtual ~Core(); |
| 70 |
| 71 void Initialize(); |
| 72 void ConnectToChannel(const IPC::ChannelHandle& handle); |
| 73 |
| 74 void DeauthorizeContentLicensesOnIOThread(uint32 request_id); |
| 75 void NotifyErrorFromIOThread(); |
| 76 |
| 77 void NotifyDeauthorizeContentLicensesCompleted(uint32 request_id, |
| 78 bool success); |
| 79 void NotifyError( |
| 80 const std::vector<std::pair<uint32, RequestType> >& notifications); |
| 81 |
| 82 // Message handlers. |
| 83 void OnDeauthorizeContentLicensesResult(uint32 request_id, bool success); |
| 84 |
| 85 // Used only on the UI thread. |
| 86 PepperFlashSettingsManager* manager_; |
| 87 |
| 88 // Used only on the I/O thread. |
| 89 FilePath plugin_data_path_; |
| 90 |
| 91 // The channel is NULL until we have opened a connection to the broker |
| 92 // process. Used only on the I/O thread. |
| 93 scoped_ptr<IPC::Channel> channel_; |
| 94 |
| 95 // Used only on the I/O thread. |
| 96 bool initialized_; |
| 97 |
| 98 // Requests that need to be sent once the channel to the broker process is |
| 99 // established. Used only on the I/O thread. |
| 100 std::vector<PendingRequest> pending_requests_; |
| 101 // Requests that have been sent but haven't got replied. Used only on the |
| 102 // I/O thread. |
| 103 std::map<uint32, RequestType> pending_responses_; |
| 104 |
| 105 // Path for the current profile. Must be retrieved on the UI thread from the |
| 106 // browser context when we start so we can use it later on the I/O thread. |
| 107 FilePath browser_context_path_; |
| 108 |
| 109 scoped_refptr<PluginPrefs> plugin_prefs_; |
| 110 }; |
| 111 |
| 112 PepperFlashSettingsManager::Core::Core(PepperFlashSettingsManager* manager, |
| 113 content::BrowserContext* browser_context) |
| 114 : manager_(manager), |
| 115 initialized_(false), |
| 116 browser_context_path_(browser_context->GetPath()), |
| 117 plugin_prefs_(PluginPrefs::GetForProfile( |
| 118 Profile::FromBrowserContext(browser_context))) { |
| 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 120 |
| 121 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 122 base::Bind(&Core::Initialize, this)); |
| 123 } |
| 124 |
| 125 PepperFlashSettingsManager::Core::~Core() { |
| 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 127 } |
| 128 |
| 129 void PepperFlashSettingsManager::Core::Detach() { |
| 130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 131 |
| 132 manager_ = NULL; |
| 133 } |
| 134 |
| 135 void PepperFlashSettingsManager::Core::DeauthorizeContentLicenses( |
| 136 uint32 request_id) { |
| 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 138 |
| 139 BrowserThread::PostTask( |
| 140 BrowserThread::IO, FROM_HERE, |
| 141 base::Bind(&Core::DeauthorizeContentLicensesOnIOThread, this, |
| 142 request_id)); |
| 143 } |
| 144 |
| 145 void PepperFlashSettingsManager::Core::GetPpapiChannelInfo( |
| 146 base::ProcessHandle* renderer_handle, |
| 147 int* renderer_id) { |
| 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 149 |
| 150 *renderer_handle = base::kNullProcessHandle; |
| 151 *renderer_id = 0; |
| 152 } |
| 153 |
| 154 void PepperFlashSettingsManager::Core::OnPpapiChannelOpened( |
| 155 const IPC::ChannelHandle& channel_handle, |
| 156 int /* plugin_child_id */) { |
| 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 158 DCHECK(!initialized_); |
| 159 |
| 160 if (!channel_handle.name.empty()) { |
| 161 ConnectToChannel(channel_handle); |
| 162 } else { |
| 163 LOG(ERROR) << "Couldn't open plugin channel"; |
| 164 NotifyErrorFromIOThread(); |
| 165 } |
| 166 |
| 167 // Balance the AddRef() call in Initialize(). |
| 168 Release(); |
| 169 } |
| 170 |
| 171 bool PepperFlashSettingsManager::Core::OffTheRecord() { |
| 172 return false; |
| 173 } |
| 174 |
| 175 bool PepperFlashSettingsManager::Core::OnMessageReceived( |
| 176 const IPC::Message& message) { |
| 177 IPC_BEGIN_MESSAGE_MAP(Core, message) |
| 178 IPC_MESSAGE_HANDLER(PpapiHostMsg_DeauthorizeContentLicensesResult, |
| 179 OnDeauthorizeContentLicensesResult) |
| 180 IPC_MESSAGE_UNHANDLED_ERROR() |
| 181 IPC_END_MESSAGE_MAP() |
| 182 |
| 183 return true; |
| 184 } |
| 185 |
| 186 void PepperFlashSettingsManager::Core::OnChannelError() { |
| 187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 188 |
| 189 NotifyErrorFromIOThread(); |
| 190 } |
| 191 |
| 192 void PepperFlashSettingsManager::Core::Initialize() { |
| 193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 194 DCHECK(!initialized_); |
| 195 |
| 196 webkit::WebPluginInfo plugin_info; |
| 197 if (!PepperFlashSettingsManager::IsPepperFlashInUse(plugin_prefs_.get(), |
| 198 &plugin_info)) { |
| 199 NotifyErrorFromIOThread(); |
| 200 return; |
| 201 } |
| 202 |
| 203 // Balanced in OnPpapiChannelOpened(). We need to keep this object around |
| 204 // until then. |
| 205 AddRef(); |
| 206 |
| 207 FilePath profile_path = |
| 208 browser_context_path_.Append(content::kPepperDataDirname); |
| 209 #if defined(OS_WIN) |
| 210 plugin_data_path_ = profile_path.Append(plugin_info.name); |
| 211 #else |
| 212 plugin_data_path_ = profile_path.Append(UTF16ToUTF8(plugin_info.name)); |
| 213 #endif |
| 214 |
| 215 PluginServiceImpl* plugin_service = PluginServiceImpl::GetInstance(); |
| 216 plugin_service->OpenChannelToPpapiBroker(plugin_info.path, this); |
| 217 } |
| 218 |
| 219 void PepperFlashSettingsManager::Core::ConnectToChannel( |
| 220 const IPC::ChannelHandle& handle) { |
| 221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 222 DCHECK(!initialized_); |
| 223 DCHECK(!channel_.get()); |
| 224 |
| 225 channel_.reset(new IPC::Channel(handle, IPC::Channel::MODE_CLIENT, this)); |
| 226 if (!channel_->Connect()) { |
| 227 LOG(ERROR) << "Couldn't connect to plugin"; |
| 228 NotifyErrorFromIOThread(); |
| 229 return; |
| 230 } |
| 231 |
| 232 initialized_ = true; |
| 233 |
| 234 std::vector<PendingRequest> temp_pending_requests; |
| 235 temp_pending_requests.swap(pending_requests_); |
| 236 for (std::vector<PendingRequest>::iterator iter = |
| 237 temp_pending_requests.begin(); |
| 238 iter != temp_pending_requests.end(); ++iter) { |
| 239 switch (iter->type) { |
| 240 case DEAUTHORIZE_CONTENT_LICENSES: |
| 241 DeauthorizeContentLicensesOnIOThread(iter->id); |
| 242 break; |
| 243 default: |
| 244 NOTREACHED(); |
| 245 break; |
| 246 } |
| 247 } |
| 248 } |
| 249 |
| 250 void PepperFlashSettingsManager::Core::DeauthorizeContentLicensesOnIOThread( |
| 251 uint32 request_id) { |
| 252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 253 |
| 254 if (!initialized_) { |
| 255 PendingRequest request; |
| 256 request.id = request_id; |
| 257 request.type = DEAUTHORIZE_CONTENT_LICENSES; |
| 258 pending_requests_.push_back(request); |
| 259 return; |
| 260 } |
| 261 |
| 262 pending_responses_.insert( |
| 263 std::make_pair(request_id, DEAUTHORIZE_CONTENT_LICENSES)); |
| 264 IPC::Message* msg = |
| 265 new PpapiMsg_DeauthorizeContentLicenses(request_id, plugin_data_path_); |
| 266 if (!channel_->Send(msg)) { |
| 267 LOG(ERROR) << "Couldn't send DeauthorizeContentLicenses message"; |
| 268 // A failure notification for the current request will be sent since |
| 269 // |pending_responses_| has been updated. |
| 270 NotifyErrorFromIOThread(); |
| 271 } |
| 272 } |
| 273 |
| 274 void PepperFlashSettingsManager::Core::NotifyErrorFromIOThread() { |
| 275 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 276 |
| 277 std::vector<std::pair<uint32, RequestType> > notifications; |
| 278 for (std::vector<PendingRequest>::iterator iter = pending_requests_.begin(); |
| 279 iter != pending_requests_.end(); ++iter) { |
| 280 notifications.push_back(std::make_pair(iter->id, iter->type)); |
| 281 } |
| 282 pending_requests_.clear(); |
| 283 notifications.insert(notifications.end(), pending_responses_.begin(), |
| 284 pending_responses_.end()); |
| 285 pending_responses_.clear(); |
| 286 |
| 287 BrowserThread::PostTask( |
| 288 BrowserThread::UI, FROM_HERE, |
| 289 base::Bind(&Core::NotifyError, this, notifications)); |
| 290 } |
| 291 |
| 292 void |
| 293 PepperFlashSettingsManager::Core::NotifyDeauthorizeContentLicensesCompleted( |
| 294 uint32 request_id, |
| 295 bool success) { |
| 296 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 297 |
| 298 if (manager_) { |
| 299 manager_->client_->OnDeauthorizeContentLicensesCompleted( |
| 300 request_id, success); |
| 301 } |
| 302 } |
| 303 |
| 304 void PepperFlashSettingsManager::Core::NotifyError( |
| 305 const std::vector<std::pair<uint32, RequestType> >& notifications) { |
| 306 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 307 |
| 308 scoped_refptr<Core> protector(this); |
| 309 for (std::vector<std::pair<uint32, RequestType> >::const_iterator iter = |
| 310 notifications.begin(); iter != notifications.end(); ++iter) { |
| 311 // Check |manager_| for each iteration in case Detach() happens in one of |
| 312 // the callbacks. |
| 313 if (manager_) { |
| 314 switch (iter->second) { |
| 315 case DEAUTHORIZE_CONTENT_LICENSES: |
| 316 manager_->client_->OnDeauthorizeContentLicensesCompleted(iter->first, |
| 317 false); |
| 318 break; |
| 319 default: |
| 320 NOTREACHED(); |
| 321 break; |
| 322 } |
| 323 } |
| 324 } |
| 325 |
| 326 if (manager_) |
| 327 manager_->OnError(); |
| 328 } |
| 329 |
| 330 void PepperFlashSettingsManager::Core::OnDeauthorizeContentLicensesResult( |
| 331 uint32 request_id, |
| 332 bool success) { |
| 333 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 334 LOG_IF(ERROR, !success) << "DeauthorizeContentLicenses returned error"; |
| 335 |
| 336 std::map<uint32, RequestType>::iterator iter = |
| 337 pending_responses_.find(request_id); |
| 338 if (iter != pending_responses_.end()) { |
| 339 DCHECK_EQ(iter->second, DEAUTHORIZE_CONTENT_LICENSES); |
| 340 |
| 341 pending_responses_.erase(iter); |
| 342 BrowserThread::PostTask( |
| 343 BrowserThread::UI, FROM_HERE, |
| 344 base::Bind(&Core::NotifyDeauthorizeContentLicensesCompleted, this, |
| 345 request_id, success)); |
| 346 } |
| 347 } |
| 348 |
| 349 PepperFlashSettingsManager::PepperFlashSettingsManager( |
| 350 Client* client, |
| 351 content::BrowserContext* browser_context) |
| 352 : client_(client), |
| 353 browser_context_(browser_context), |
| 354 next_request_id_(1) { |
| 355 DCHECK(client); |
| 356 DCHECK(browser_context); |
| 357 } |
| 358 |
| 359 PepperFlashSettingsManager::~PepperFlashSettingsManager() { |
| 360 if (core_.get()) { |
| 361 core_->Detach(); |
| 362 core_ = NULL; |
| 363 } |
| 364 } |
| 365 |
| 14 // static | 366 // static |
| 15 bool PepperFlashSettingsManager::IsPepperFlashInUse( | 367 bool PepperFlashSettingsManager::IsPepperFlashInUse( |
| 16 PluginPrefs* plugin_prefs, | 368 PluginPrefs* plugin_prefs, |
| 17 webkit::WebPluginInfo* plugin_info) { | 369 webkit::WebPluginInfo* plugin_info) { |
| 18 if (!plugin_prefs) | 370 if (!plugin_prefs) |
| 19 return false; | 371 return false; |
| 20 | 372 |
| 21 PluginServiceImpl* plugin_service = PluginServiceImpl::GetInstance(); | 373 PluginServiceImpl* plugin_service = PluginServiceImpl::GetInstance(); |
| 22 std::vector<webkit::WebPluginInfo> plugins; | 374 std::vector<webkit::WebPluginInfo> plugins; |
| 23 plugin_service->GetPluginInfoArray( | 375 plugin_service->GetPluginInfoArray( |
| 24 GURL(), kFlashPluginSwfMimeType, false, &plugins, NULL); | 376 GURL(), kFlashPluginSwfMimeType, false, &plugins, NULL); |
| 25 | 377 |
| 26 for (std::vector<webkit::WebPluginInfo>::iterator iter = plugins.begin(); | 378 for (std::vector<webkit::WebPluginInfo>::iterator iter = plugins.begin(); |
| 27 iter != plugins.end(); ++iter) { | 379 iter != plugins.end(); ++iter) { |
| 28 if (webkit::IsPepperPlugin(*iter) && plugin_prefs->IsPluginEnabled(*iter)) { | 380 if (webkit::IsPepperPlugin(*iter) && plugin_prefs->IsPluginEnabled(*iter)) { |
| 29 if (plugin_info) | 381 if (plugin_info) |
| 30 *plugin_info = *iter; | 382 *plugin_info = *iter; |
| 31 return true; | 383 return true; |
| 32 } | 384 } |
| 33 } | 385 } |
| 34 return false; | 386 return false; |
| 35 } | 387 } |
| 388 |
| 389 uint32 PepperFlashSettingsManager::DeauthorizeContentLicenses() { |
| 390 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 391 |
| 392 EnsureCoreExists(); |
| 393 uint32 id = GetNextRequestId(); |
| 394 core_->DeauthorizeContentLicenses(id); |
| 395 return id; |
| 396 } |
| 397 |
| 398 uint32 PepperFlashSettingsManager::GetNextRequestId() { |
| 399 return next_request_id_++; |
| 400 } |
| 401 |
| 402 void PepperFlashSettingsManager::EnsureCoreExists() { |
| 403 if (!core_.get()) |
| 404 core_ = new Core(this, browser_context_); |
| 405 } |
| 406 |
| 407 void PepperFlashSettingsManager::OnError() { |
| 408 if (core_.get()) { |
| 409 core_->Detach(); |
| 410 core_ = NULL; |
| 411 } else { |
| 412 NOTREACHED(); |
| 413 } |
| 414 } |
| 415 |
| OLD | NEW |