blob: 1f867378ef991cb4bce7cf8e1e7a8a8ebf149b95 [file] [log] [blame]
[email protected]18a4d63c82012-05-25 23:37:031// 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
3// found in the LICENSE file.
4
5#include "chrome/browser/pepper_flash_settings_manager.h"
6
[email protected]1a559442012-05-27 07:18:467#include <map>
8#include <utility>
[email protected]18a4d63c82012-05-25 23:37:039#include <vector>
10
[email protected]1a559442012-05-27 07:18:4611#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"
[email protected]18a4d63c82012-05-25 23:37:0315#include "chrome/browser/plugin_prefs.h"
16#include "chrome/browser/prefs/pref_service.h"
[email protected]1a559442012-05-27 07:18:4617#include "chrome/browser/profiles/profile.h"
[email protected]18a4d63c82012-05-25 23:37:0318#include "chrome/common/pref_names.h"
[email protected]1a559442012-05-27 07:18:4619#include "content/public/browser/browser_context.h"
20#include "content/public/browser/browser_thread.h"
21#include "content/public/browser/pepper_flash_settings_helper.h"
[email protected]18a4d63c82012-05-25 23:37:0322#include "content/public/browser/plugin_service.h"
[email protected]1a559442012-05-27 07:18:4623#include "content/public/common/content_constants.h"
[email protected]18a4d63c82012-05-25 23:37:0324#include "googleurl/src/gurl.h"
[email protected]1a559442012-05-27 07:18:4625#include "ipc/ipc_channel.h"
26#include "ppapi/proxy/ppapi_messages.h"
[email protected]18a4d63c82012-05-25 23:37:0327#include "webkit/plugins/plugin_constants.h"
28#include "webkit/plugins/webplugininfo.h"
29
[email protected]1a559442012-05-27 07:18:4630using content::BrowserThread;
31
32class PepperFlashSettingsManager::Core
[email protected]b44f8ad2012-06-15 20:52:5833 : public IPC::Listener,
[email protected]1a559442012-05-27 07:18:4634 public base::RefCountedThreadSafe<Core, BrowserThread::DeleteOnIOThread> {
35 public:
36 Core(PepperFlashSettingsManager* manager,
37 content::BrowserContext* browser_context);
38
[email protected]6464cc12012-07-12 09:25:5339 void Initialize();
[email protected]1a559442012-05-27 07:18:4640 // Stops sending notifications to |manager_| and sets it to NULL.
41 void Detach();
42
43 void DeauthorizeContentLicenses(uint32 request_id);
[email protected]ee4dd682012-06-12 15:49:3344 void GetPermissionSettings(
45 uint32 request_id,
46 PP_Flash_BrowserOperations_SettingType setting_type);
47 void SetDefaultPermission(
48 uint32 request_id,
49 PP_Flash_BrowserOperations_SettingType setting_type,
50 PP_Flash_BrowserOperations_Permission permission,
51 bool clear_site_specific);
52 void SetSitePermission(uint32 request_id,
53 PP_Flash_BrowserOperations_SettingType setting_type,
54 const ppapi::FlashSiteSettings& sites);
[email protected]951ef0b2012-07-27 22:46:5355 void GetSitesWithData(uint32 request_id);
56 void ClearSiteData(uint32 request_id,
57 const std::string& site,
58 uint64 flags,
59 uint64 max_age);
[email protected]1a559442012-05-27 07:18:4660
[email protected]b44f8ad2012-06-15 20:52:5861 // IPC::Listener implementation.
[email protected]1a559442012-05-27 07:18:4662 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
63 virtual void OnChannelError() OVERRIDE;
64
65 private:
66 friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>;
67 friend class base::DeleteHelper<Core>;
68
69 enum RequestType {
70 INVALID_REQUEST_TYPE = 0,
[email protected]ee4dd682012-06-12 15:49:3371 DEAUTHORIZE_CONTENT_LICENSES,
72 GET_PERMISSION_SETTINGS,
73 SET_DEFAULT_PERMISSION,
[email protected]951ef0b2012-07-27 22:46:5374 SET_SITE_PERMISSION,
75 GET_SITES_WITH_DATA,
76 CLEAR_SITE_DATA,
[email protected]1a559442012-05-27 07:18:4677 };
78
79 struct PendingRequest {
[email protected]ee4dd682012-06-12 15:49:3380 PendingRequest()
81 : id(0),
82 type(INVALID_REQUEST_TYPE),
83 setting_type(PP_FLASH_BROWSEROPERATIONS_SETTINGTYPE_CAMERAMIC),
84 permission(PP_FLASH_BROWSEROPERATIONS_PERMISSION_DEFAULT),
85 clear_site_specific(false) {
86 }
[email protected]1a559442012-05-27 07:18:4687
88 uint32 id;
89 RequestType type;
[email protected]ee4dd682012-06-12 15:49:3390
91 // Used by GET_PERMISSION_SETTINGS, SET_DEFAULT_PERMISSION and
92 // SET_SITE_PERMISSION.
93 PP_Flash_BrowserOperations_SettingType setting_type;
94
95 // Used by SET_DEFAULT_PERMISSION.
96 PP_Flash_BrowserOperations_Permission permission;
97 bool clear_site_specific;
98
99 // Used by SET_SITE_PERMISSION.
100 ppapi::FlashSiteSettings sites;
[email protected]951ef0b2012-07-27 22:46:53101
102 // Used by CLEAR_SITE_DATA
103 std::string site;
104 uint64 flags;
105 uint64 max_age;
[email protected]1a559442012-05-27 07:18:46106 };
107
108 virtual ~Core();
109
[email protected]1a559442012-05-27 07:18:46110 void ConnectToChannel(bool success, const IPC::ChannelHandle& handle);
111
[email protected]6464cc12012-07-12 09:25:53112 void InitializeOnIOThread();
[email protected]1a559442012-05-27 07:18:46113 void DeauthorizeContentLicensesOnIOThread(uint32 request_id);
[email protected]ee4dd682012-06-12 15:49:33114 void GetPermissionSettingsOnIOThread(
115 uint32 request_id,
116 PP_Flash_BrowserOperations_SettingType setting_type);
117 void SetDefaultPermissionOnIOThread(
118 uint32 request_id,
119 PP_Flash_BrowserOperations_SettingType setting_type,
120 PP_Flash_BrowserOperations_Permission permission,
121 bool clear_site_specific);
122 void SetSitePermissionOnIOThread(
123 uint32 request_id,
124 PP_Flash_BrowserOperations_SettingType setting_type,
125 const ppapi::FlashSiteSettings& sites);
[email protected]951ef0b2012-07-27 22:46:53126 void GetSitesWithDataOnIOThread(uint32 request_id);
127 void ClearSiteDataOnIOThread(uint32 request_id,
128 const std::string& site,
129 uint64 flags,
130 uint64 max_age);
[email protected]4d4ee4c2012-06-22 19:11:30131 void DetachOnIOThread();
[email protected]ee4dd682012-06-12 15:49:33132
[email protected]1a559442012-05-27 07:18:46133 void NotifyErrorFromIOThread();
134
135 void NotifyDeauthorizeContentLicensesCompleted(uint32 request_id,
136 bool success);
[email protected]ee4dd682012-06-12 15:49:33137 void NotifyGetPermissionSettingsCompleted(
138 uint32 request_id,
139 bool success,
140 PP_Flash_BrowserOperations_Permission default_permission,
141 const ppapi::FlashSiteSettings& sites);
142 void NotifySetDefaultPermissionCompleted(uint32 request_id, bool success);
143 void NotifySetSitePermissionCompleted(uint32 request_id, bool success);
[email protected]951ef0b2012-07-27 22:46:53144 void NotifyGetSitesWithDataCompleted(uint32 request_id,
145 const std::vector<std::string>& sites);
146 void NotifyClearSiteDataCompleted(uint32 request_id, bool success);
[email protected]ee4dd682012-06-12 15:49:33147
[email protected]1a559442012-05-27 07:18:46148 void NotifyError(
149 const std::vector<std::pair<uint32, RequestType> >& notifications);
150
151 // Message handlers.
152 void OnDeauthorizeContentLicensesResult(uint32 request_id, bool success);
[email protected]ee4dd682012-06-12 15:49:33153 void OnGetPermissionSettingsResult(
154 uint32 request_id,
155 bool success,
156 PP_Flash_BrowserOperations_Permission default_permission,
157 const ppapi::FlashSiteSettings& sites);
158 void OnSetDefaultPermissionResult(uint32 request_id, bool success);
159 void OnSetSitePermissionResult(uint32 request_id, bool success);
[email protected]951ef0b2012-07-27 22:46:53160 void OnGetSitesWithDataResult(uint32 request_id,
161 const std::vector<std::string>& sites);
162 void OnClearSiteDataResult(uint32 request_id, bool success);
[email protected]1a559442012-05-27 07:18:46163
164 // Used only on the UI thread.
165 PepperFlashSettingsManager* manager_;
166
167 // Used only on the I/O thread.
168 FilePath plugin_data_path_;
169
170 // The channel is NULL until we have opened a connection to the broker
171 // process. Used only on the I/O thread.
172 scoped_ptr<IPC::Channel> channel_;
173
174 // Used only on the I/O thread.
175 bool initialized_;
[email protected]4d4ee4c2012-06-22 19:11:30176 // Whether Detach() has been called on the UI thread. When it is true, further
177 // work is not necessary. Used only on the I/O thread.
178 bool detached_;
[email protected]1a559442012-05-27 07:18:46179
180 // Requests that need to be sent once the channel to the broker process is
181 // established. Used only on the I/O thread.
182 std::vector<PendingRequest> pending_requests_;
183 // Requests that have been sent but haven't got replied. Used only on the
184 // I/O thread.
185 std::map<uint32, RequestType> pending_responses_;
186
187 // Used only on the I/O thread.
188 scoped_refptr<content::PepperFlashSettingsHelper> helper_;
189
190 // Path for the current profile. Must be retrieved on the UI thread from the
191 // browser context when we start so we can use it later on the I/O thread.
192 FilePath browser_context_path_;
193
194 scoped_refptr<PluginPrefs> plugin_prefs_;
195};
196
197PepperFlashSettingsManager::Core::Core(PepperFlashSettingsManager* manager,
198 content::BrowserContext* browser_context)
199 : manager_(manager),
200 initialized_(false),
[email protected]4d4ee4c2012-06-22 19:11:30201 detached_(false),
[email protected]1a559442012-05-27 07:18:46202 browser_context_path_(browser_context->GetPath()),
203 plugin_prefs_(PluginPrefs::GetForProfile(
204 Profile::FromBrowserContext(browser_context))) {
205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]1a559442012-05-27 07:18:46206}
207
208PepperFlashSettingsManager::Core::~Core() {
209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
210}
211
[email protected]6464cc12012-07-12 09:25:53212void PepperFlashSettingsManager::Core::Initialize() {
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
214 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
215 base::Bind(&Core::InitializeOnIOThread, this));
216}
217
[email protected]1a559442012-05-27 07:18:46218void PepperFlashSettingsManager::Core::Detach() {
219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
220
221 manager_ = NULL;
[email protected]4d4ee4c2012-06-22 19:11:30222 // This call guarantees that one ref is retained until |detached_| is set to
223 // true. This is important. Otherwise, if the ref count drops to zero on the
224 // UI thread (which posts a task to delete this object on the I/O thread)
225 // while the I/O thread doesn't know about it, methods on the I/O thread might
226 // increase the ref count again and cause double deletion.
227 BrowserThread::PostTask(
228 BrowserThread::IO, FROM_HERE, base::Bind(&Core::DetachOnIOThread, this));
[email protected]1a559442012-05-27 07:18:46229}
230
231void PepperFlashSettingsManager::Core::DeauthorizeContentLicenses(
232 uint32 request_id) {
233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
234
235 BrowserThread::PostTask(
236 BrowserThread::IO, FROM_HERE,
237 base::Bind(&Core::DeauthorizeContentLicensesOnIOThread, this,
238 request_id));
239}
240
[email protected]ee4dd682012-06-12 15:49:33241void PepperFlashSettingsManager::Core::GetPermissionSettings(
242 uint32 request_id,
243 PP_Flash_BrowserOperations_SettingType setting_type) {
244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
245
246 BrowserThread::PostTask(
247 BrowserThread::IO, FROM_HERE,
248 base::Bind(&Core::GetPermissionSettingsOnIOThread, this, request_id,
249 setting_type));
250}
251
252void PepperFlashSettingsManager::Core::SetDefaultPermission(
253 uint32 request_id,
254 PP_Flash_BrowserOperations_SettingType setting_type,
255 PP_Flash_BrowserOperations_Permission permission,
256 bool clear_site_specific) {
257 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
258
259 BrowserThread::PostTask(
260 BrowserThread::IO, FROM_HERE,
261 base::Bind(&Core::SetDefaultPermissionOnIOThread, this, request_id,
262 setting_type, permission, clear_site_specific));
263}
264
265void PepperFlashSettingsManager::Core::SetSitePermission(
266 uint32 request_id,
267 PP_Flash_BrowserOperations_SettingType setting_type,
268 const ppapi::FlashSiteSettings& sites) {
269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
270
271 BrowserThread::PostTask(
272 BrowserThread::IO, FROM_HERE,
273 base::Bind(&Core::SetSitePermissionOnIOThread, this, request_id,
274 setting_type, sites));
275}
276
[email protected]951ef0b2012-07-27 22:46:53277void PepperFlashSettingsManager::Core::GetSitesWithData(uint32 request_id) {
278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
279
280 BrowserThread::PostTask(
281 BrowserThread::IO, FROM_HERE,
282 base::Bind(&Core::GetSitesWithDataOnIOThread, this, request_id));
283}
284
285void PepperFlashSettingsManager::Core::ClearSiteData(uint32 request_id,
286 const std::string& site,
287 uint64 flags,
288 uint64 max_age) {
289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
290
291 BrowserThread::PostTask(
292 BrowserThread::IO, FROM_HERE,
293 base::Bind(&Core::ClearSiteDataOnIOThread, this, request_id,
294 site, flags, max_age));
295}
296
[email protected]1a559442012-05-27 07:18:46297bool PepperFlashSettingsManager::Core::OnMessageReceived(
298 const IPC::Message& message) {
299 IPC_BEGIN_MESSAGE_MAP(Core, message)
300 IPC_MESSAGE_HANDLER(PpapiHostMsg_DeauthorizeContentLicensesResult,
301 OnDeauthorizeContentLicensesResult)
[email protected]ee4dd682012-06-12 15:49:33302 IPC_MESSAGE_HANDLER(PpapiHostMsg_GetPermissionSettingsResult,
303 OnGetPermissionSettingsResult)
304 IPC_MESSAGE_HANDLER(PpapiHostMsg_SetDefaultPermissionResult,
305 OnSetDefaultPermissionResult)
306 IPC_MESSAGE_HANDLER(PpapiHostMsg_SetSitePermissionResult,
307 OnSetSitePermissionResult)
[email protected]951ef0b2012-07-27 22:46:53308 IPC_MESSAGE_HANDLER(PpapiHostMsg_GetSitesWithDataResult,
309 OnGetSitesWithDataResult)
310 IPC_MESSAGE_HANDLER(PpapiHostMsg_ClearSiteDataResult,
311 OnClearSiteDataResult)
[email protected]1a559442012-05-27 07:18:46312 IPC_MESSAGE_UNHANDLED_ERROR()
313 IPC_END_MESSAGE_MAP()
314
315 return true;
316}
317
318void PepperFlashSettingsManager::Core::OnChannelError() {
319 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30320 if (detached_)
321 return;
[email protected]1a559442012-05-27 07:18:46322
323 NotifyErrorFromIOThread();
324}
325
[email protected]1a559442012-05-27 07:18:46326void PepperFlashSettingsManager::Core::ConnectToChannel(
327 bool success,
328 const IPC::ChannelHandle& handle) {
329 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30330 if (detached_)
331 return;
332
[email protected]1a559442012-05-27 07:18:46333 DCHECK(!initialized_);
334 DCHECK(!channel_.get());
335
336 if (!success) {
[email protected]ee4dd682012-06-12 15:49:33337 DLOG(ERROR) << "Couldn't open plugin channel";
[email protected]1a559442012-05-27 07:18:46338 NotifyErrorFromIOThread();
339 return;
340 }
341
342 channel_.reset(new IPC::Channel(handle, IPC::Channel::MODE_CLIENT, this));
343 if (!channel_->Connect()) {
[email protected]ee4dd682012-06-12 15:49:33344 DLOG(ERROR) << "Couldn't connect to plugin";
[email protected]1a559442012-05-27 07:18:46345 NotifyErrorFromIOThread();
346 return;
347 }
348
349 initialized_ = true;
350
351 std::vector<PendingRequest> temp_pending_requests;
352 temp_pending_requests.swap(pending_requests_);
353 for (std::vector<PendingRequest>::iterator iter =
354 temp_pending_requests.begin();
355 iter != temp_pending_requests.end(); ++iter) {
356 switch (iter->type) {
[email protected]951ef0b2012-07-27 22:46:53357 case INVALID_REQUEST_TYPE:
358 NOTREACHED();
359 break;
[email protected]1a559442012-05-27 07:18:46360 case DEAUTHORIZE_CONTENT_LICENSES:
361 DeauthorizeContentLicensesOnIOThread(iter->id);
362 break;
[email protected]ee4dd682012-06-12 15:49:33363 case GET_PERMISSION_SETTINGS:
364 GetPermissionSettingsOnIOThread(iter->id, iter->setting_type);
365 break;
366 case SET_DEFAULT_PERMISSION:
367 SetDefaultPermissionOnIOThread(
368 iter->id, iter->setting_type, iter->permission,
369 iter->clear_site_specific);
370 break;
371 case SET_SITE_PERMISSION:
372 SetSitePermissionOnIOThread(iter->id, iter->setting_type, iter->sites);
373 break;
[email protected]951ef0b2012-07-27 22:46:53374 case GET_SITES_WITH_DATA:
375 GetSitesWithDataOnIOThread(iter->id);
376 break;
377 case CLEAR_SITE_DATA:
378 ClearSiteDataOnIOThread(iter->id, iter->site, iter->flags,
379 iter->max_age);
[email protected]1a559442012-05-27 07:18:46380 break;
381 }
382 }
383}
384
[email protected]6464cc12012-07-12 09:25:53385void PepperFlashSettingsManager::Core::InitializeOnIOThread() {
386 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
387 DCHECK(!initialized_);
388
389 if (detached_)
390 return;
391
392 webkit::WebPluginInfo plugin_info;
393 if (!PepperFlashSettingsManager::IsPepperFlashInUse(plugin_prefs_.get(),
394 &plugin_info)) {
395 NotifyErrorFromIOThread();
396 return;
397 }
398
399 FilePath profile_path =
400 browser_context_path_.Append(content::kPepperDataDirname);
401#if defined(OS_WIN)
402 plugin_data_path_ = profile_path.Append(plugin_info.name);
403#else
404 plugin_data_path_ = profile_path.Append(UTF16ToUTF8(plugin_info.name));
405#endif
406
407 helper_ = content::PepperFlashSettingsHelper::Create();
408 content::PepperFlashSettingsHelper::OpenChannelCallback callback =
409 base::Bind(&Core::ConnectToChannel, this);
410 helper_->OpenChannelToBroker(plugin_info.path, callback);
411}
412
[email protected]1a559442012-05-27 07:18:46413void PepperFlashSettingsManager::Core::DeauthorizeContentLicensesOnIOThread(
414 uint32 request_id) {
415 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30416 if (detached_)
417 return;
[email protected]1a559442012-05-27 07:18:46418
419 if (!initialized_) {
420 PendingRequest request;
421 request.id = request_id;
422 request.type = DEAUTHORIZE_CONTENT_LICENSES;
423 pending_requests_.push_back(request);
424 return;
425 }
426
427 pending_responses_.insert(
428 std::make_pair(request_id, DEAUTHORIZE_CONTENT_LICENSES));
429 IPC::Message* msg =
430 new PpapiMsg_DeauthorizeContentLicenses(request_id, plugin_data_path_);
431 if (!channel_->Send(msg)) {
[email protected]ee4dd682012-06-12 15:49:33432 DLOG(ERROR) << "Couldn't send DeauthorizeContentLicenses message";
433 // A failure notification for the current request will be sent since
434 // |pending_responses_| has been updated.
435 NotifyErrorFromIOThread();
436 }
437}
438
439void PepperFlashSettingsManager::Core::GetPermissionSettingsOnIOThread(
440 uint32 request_id,
441 PP_Flash_BrowserOperations_SettingType setting_type) {
442 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30443 if (detached_)
444 return;
[email protected]ee4dd682012-06-12 15:49:33445
446 if (!initialized_) {
447 PendingRequest request;
448 request.id = request_id;
449 request.type = GET_PERMISSION_SETTINGS;
450 request.setting_type = setting_type;
451 pending_requests_.push_back(request);
452 return;
453 }
454
455 pending_responses_.insert(
456 std::make_pair(request_id, GET_PERMISSION_SETTINGS));
457 IPC::Message* msg = new PpapiMsg_GetPermissionSettings(
458 request_id, plugin_data_path_, setting_type);
459 if (!channel_->Send(msg)) {
460 DLOG(ERROR) << "Couldn't send GetPermissionSettings message";
461 // A failure notification for the current request will be sent since
462 // |pending_responses_| has been updated.
463 NotifyErrorFromIOThread();
464 }
465}
466
467void PepperFlashSettingsManager::Core::SetDefaultPermissionOnIOThread(
468 uint32 request_id,
469 PP_Flash_BrowserOperations_SettingType setting_type,
470 PP_Flash_BrowserOperations_Permission permission,
471 bool clear_site_specific) {
472 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30473 if (detached_)
474 return;
[email protected]ee4dd682012-06-12 15:49:33475
476 if (!initialized_) {
477 PendingRequest request;
478 request.id = request_id;
479 request.type = SET_DEFAULT_PERMISSION;
480 request.setting_type = setting_type;
481 request.permission = permission;
482 request.clear_site_specific = clear_site_specific;
483 pending_requests_.push_back(request);
484 return;
485 }
486
487 pending_responses_.insert(std::make_pair(request_id, SET_DEFAULT_PERMISSION));
488 IPC::Message* msg = new PpapiMsg_SetDefaultPermission(
489 request_id, plugin_data_path_, setting_type, permission,
490 clear_site_specific);
491 if (!channel_->Send(msg)) {
492 DLOG(ERROR) << "Couldn't send SetDefaultPermission message";
493 // A failure notification for the current request will be sent since
494 // |pending_responses_| has been updated.
495 NotifyErrorFromIOThread();
496 }
497}
498
499void PepperFlashSettingsManager::Core::SetSitePermissionOnIOThread(
500 uint32 request_id,
501 PP_Flash_BrowserOperations_SettingType setting_type,
502 const ppapi::FlashSiteSettings& sites) {
503 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30504 if (detached_)
505 return;
[email protected]ee4dd682012-06-12 15:49:33506
507 if (!initialized_) {
508 pending_requests_.push_back(PendingRequest());
509 PendingRequest& request = pending_requests_.back();
510 request.id = request_id;
511 request.type = SET_SITE_PERMISSION;
512 request.setting_type = setting_type;
513 request.sites = sites;
514 return;
515 }
516
517 pending_responses_.insert(std::make_pair(request_id, SET_SITE_PERMISSION));
518 IPC::Message* msg = new PpapiMsg_SetSitePermission(
519 request_id, plugin_data_path_, setting_type, sites);
520 if (!channel_->Send(msg)) {
521 DLOG(ERROR) << "Couldn't send SetSitePermission message";
[email protected]1a559442012-05-27 07:18:46522 // A failure notification for the current request will be sent since
523 // |pending_responses_| has been updated.
524 NotifyErrorFromIOThread();
525 }
526}
527
[email protected]951ef0b2012-07-27 22:46:53528void PepperFlashSettingsManager::Core::GetSitesWithDataOnIOThread(
529 uint32 request_id) {
530 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
531 if (detached_)
532 return;
533
534 if (!initialized_) {
535 pending_requests_.push_back(PendingRequest());
536 PendingRequest& request = pending_requests_.back();
537 request.id = request_id;
538 request.type = GET_SITES_WITH_DATA;
539 return;
540 }
541
542 pending_responses_.insert(std::make_pair(request_id, GET_SITES_WITH_DATA));
543 IPC::Message* msg = new PpapiMsg_GetSitesWithData(
544 request_id, plugin_data_path_);
545 if (!channel_->Send(msg)) {
546 DLOG(ERROR) << "Couldn't send GetSitesWithData message";
547 // A failure notification for the current request will be sent since
548 // |pending_responses_| has been updated.
549 NotifyErrorFromIOThread();
550 }
551}
552
553void PepperFlashSettingsManager::Core::ClearSiteDataOnIOThread(
554 uint32 request_id,
555 const std::string& site,
556 uint64 flags,
557 uint64 max_age) {
558 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
559 if (detached_)
560 return;
561
562 if (!initialized_) {
563 pending_requests_.push_back(PendingRequest());
564 PendingRequest& request = pending_requests_.back();
565 request.id = request_id;
566 request.type = CLEAR_SITE_DATA;
567 request.site = site;
568 request.flags = flags;
569 request.max_age = max_age;
570 return;
571 }
572
573 pending_responses_.insert(std::make_pair(request_id, CLEAR_SITE_DATA));
574 IPC::Message* msg = new PpapiMsg_ClearSiteData(
575 request_id, plugin_data_path_, site, flags, max_age);
576 if (!channel_->Send(msg)) {
577 DLOG(ERROR) << "Couldn't send ClearSiteData message";
578 // A failure notification for the current request will be sent since
579 // |pending_responses_| has been updated.
580 NotifyErrorFromIOThread();
581 }
582}
583
[email protected]4d4ee4c2012-06-22 19:11:30584void PepperFlashSettingsManager::Core::DetachOnIOThread() {
585 detached_ = true;
586}
587
[email protected]1a559442012-05-27 07:18:46588void PepperFlashSettingsManager::Core::NotifyErrorFromIOThread() {
589 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30590 if (detached_)
591 return;
[email protected]1a559442012-05-27 07:18:46592
593 std::vector<std::pair<uint32, RequestType> > notifications;
594 for (std::vector<PendingRequest>::iterator iter = pending_requests_.begin();
595 iter != pending_requests_.end(); ++iter) {
596 notifications.push_back(std::make_pair(iter->id, iter->type));
597 }
598 pending_requests_.clear();
599 notifications.insert(notifications.end(), pending_responses_.begin(),
600 pending_responses_.end());
601 pending_responses_.clear();
602
603 BrowserThread::PostTask(
604 BrowserThread::UI, FROM_HERE,
605 base::Bind(&Core::NotifyError, this, notifications));
606}
607
608void
609PepperFlashSettingsManager::Core::NotifyDeauthorizeContentLicensesCompleted(
610 uint32 request_id,
611 bool success) {
612 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
613
614 if (manager_) {
615 manager_->client_->OnDeauthorizeContentLicensesCompleted(
616 request_id, success);
617 }
618}
619
[email protected]ee4dd682012-06-12 15:49:33620void PepperFlashSettingsManager::Core::NotifyGetPermissionSettingsCompleted(
621 uint32 request_id,
622 bool success,
623 PP_Flash_BrowserOperations_Permission default_permission,
624 const ppapi::FlashSiteSettings& sites) {
625 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
626
627 if (manager_) {
628 manager_->client_->OnGetPermissionSettingsCompleted(
629 request_id, success, default_permission, sites);
630 }
631}
632
633void PepperFlashSettingsManager::Core::NotifySetDefaultPermissionCompleted(
634 uint32 request_id,
635 bool success) {
636 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
637
638 if (manager_) {
639 manager_->client_->OnSetDefaultPermissionCompleted(
640 request_id, success);
641 }
642}
643
644void PepperFlashSettingsManager::Core::NotifySetSitePermissionCompleted(
645 uint32 request_id,
646 bool success) {
647 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
648
649 if (manager_) {
650 manager_->client_->OnSetSitePermissionCompleted(
651 request_id, success);
652 }
653}
654
[email protected]951ef0b2012-07-27 22:46:53655void PepperFlashSettingsManager::Core::NotifyGetSitesWithDataCompleted(
656 uint32 request_id,
657 const std::vector<std::string>& sites) {
658 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
659
660 if (manager_) {
661 manager_->client_->OnGetSitesWithDataCompleted(
662 request_id, sites);
663 }
664}
665
666void PepperFlashSettingsManager::Core::NotifyClearSiteDataCompleted(
667 uint32 request_id,
668 bool success) {
669 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
670
671 if (manager_)
672 manager_->client_->OnClearSiteDataCompleted(request_id, success);
673}
674
[email protected]1a559442012-05-27 07:18:46675void PepperFlashSettingsManager::Core::NotifyError(
676 const std::vector<std::pair<uint32, RequestType> >& notifications) {
677 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
678
679 scoped_refptr<Core> protector(this);
680 for (std::vector<std::pair<uint32, RequestType> >::const_iterator iter =
681 notifications.begin(); iter != notifications.end(); ++iter) {
682 // Check |manager_| for each iteration in case Detach() happens in one of
683 // the callbacks.
[email protected]951ef0b2012-07-27 22:46:53684 if (!manager_)
685 return;
686
687 switch (iter->second) {
688 case INVALID_REQUEST_TYPE:
689 NOTREACHED();
690 break;
691 case DEAUTHORIZE_CONTENT_LICENSES:
692 manager_->client_->OnDeauthorizeContentLicensesCompleted(
693 iter->first, false);
694 break;
695 case GET_PERMISSION_SETTINGS:
696 manager_->client_->OnGetPermissionSettingsCompleted(
697 iter->first, false, PP_FLASH_BROWSEROPERATIONS_PERMISSION_DEFAULT,
698 ppapi::FlashSiteSettings());
699 break;
700 case SET_DEFAULT_PERMISSION:
701 manager_->client_->OnSetDefaultPermissionCompleted(
702 iter->first, false);
703 break;
704 case SET_SITE_PERMISSION:
705 manager_->client_->OnSetSitePermissionCompleted(iter->first, false);
706 break;
707 case GET_SITES_WITH_DATA:
708 manager_->client_->OnGetSitesWithDataCompleted(
709 iter->first, std::vector<std::string>());
710 break;
711 case CLEAR_SITE_DATA:
712 manager_->client_->OnClearSiteDataCompleted(iter->first, false);
713 break;
[email protected]1a559442012-05-27 07:18:46714 }
715 }
716
717 if (manager_)
718 manager_->OnError();
719}
720
721void PepperFlashSettingsManager::Core::OnDeauthorizeContentLicensesResult(
722 uint32 request_id,
723 bool success) {
724 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30725 if (detached_)
726 return;
727
[email protected]ee4dd682012-06-12 15:49:33728 DLOG_IF(ERROR, !success) << "DeauthorizeContentLicenses returned error";
[email protected]1a559442012-05-27 07:18:46729
730 std::map<uint32, RequestType>::iterator iter =
731 pending_responses_.find(request_id);
[email protected]951ef0b2012-07-27 22:46:53732 if (iter == pending_responses_.end())
733 return;
[email protected]1a559442012-05-27 07:18:46734
[email protected]951ef0b2012-07-27 22:46:53735 DCHECK_EQ(iter->second, DEAUTHORIZE_CONTENT_LICENSES);
736
737 pending_responses_.erase(iter);
738 BrowserThread::PostTask(
739 BrowserThread::UI, FROM_HERE,
740 base::Bind(&Core::NotifyDeauthorizeContentLicensesCompleted, this,
741 request_id, success));
[email protected]1a559442012-05-27 07:18:46742}
743
[email protected]ee4dd682012-06-12 15:49:33744void PepperFlashSettingsManager::Core::OnGetPermissionSettingsResult(
745 uint32 request_id,
746 bool success,
747 PP_Flash_BrowserOperations_Permission default_permission,
748 const ppapi::FlashSiteSettings& sites) {
749 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30750 if (detached_)
751 return;
752
[email protected]ee4dd682012-06-12 15:49:33753 DLOG_IF(ERROR, !success) << "GetPermissionSettings returned error";
754
755 std::map<uint32, RequestType>::iterator iter =
756 pending_responses_.find(request_id);
[email protected]951ef0b2012-07-27 22:46:53757 if (iter == pending_responses_.end())
758 return;
[email protected]ee4dd682012-06-12 15:49:33759
[email protected]951ef0b2012-07-27 22:46:53760 DCHECK_EQ(iter->second, GET_PERMISSION_SETTINGS);
761
762 pending_responses_.erase(iter);
763 BrowserThread::PostTask(
764 BrowserThread::UI, FROM_HERE,
765 base::Bind(&Core::NotifyGetPermissionSettingsCompleted, this,
766 request_id, success, default_permission, sites));
[email protected]ee4dd682012-06-12 15:49:33767}
768
769void PepperFlashSettingsManager::Core::OnSetDefaultPermissionResult(
770 uint32 request_id,
771 bool success) {
772 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30773 if (detached_)
774 return;
775
[email protected]ee4dd682012-06-12 15:49:33776 DLOG_IF(ERROR, !success) << "SetDefaultPermission returned error";
777
778 std::map<uint32, RequestType>::iterator iter =
779 pending_responses_.find(request_id);
[email protected]951ef0b2012-07-27 22:46:53780 if (iter == pending_responses_.end())
781 return;
[email protected]ee4dd682012-06-12 15:49:33782
[email protected]951ef0b2012-07-27 22:46:53783 DCHECK_EQ(iter->second, SET_DEFAULT_PERMISSION);
784
785 pending_responses_.erase(iter);
786 BrowserThread::PostTask(
787 BrowserThread::UI, FROM_HERE,
788 base::Bind(&Core::NotifySetDefaultPermissionCompleted, this,
789 request_id, success));
[email protected]ee4dd682012-06-12 15:49:33790}
791
792void PepperFlashSettingsManager::Core::OnSetSitePermissionResult(
793 uint32 request_id,
794 bool success) {
795 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30796 if (detached_)
797 return;
798
[email protected]ee4dd682012-06-12 15:49:33799 DLOG_IF(ERROR, !success) << "SetSitePermission returned error";
800
801 std::map<uint32, RequestType>::iterator iter =
802 pending_responses_.find(request_id);
[email protected]951ef0b2012-07-27 22:46:53803 if (iter == pending_responses_.end())
804 return;
[email protected]ee4dd682012-06-12 15:49:33805
[email protected]951ef0b2012-07-27 22:46:53806 DCHECK_EQ(iter->second, SET_SITE_PERMISSION);
807
808 pending_responses_.erase(iter);
809 BrowserThread::PostTask(
810 BrowserThread::UI, FROM_HERE,
811 base::Bind(&Core::NotifySetSitePermissionCompleted, this, request_id,
812 success));
813}
814
815void PepperFlashSettingsManager::Core::OnGetSitesWithDataResult(
816 uint32 request_id,
817 const std::vector<std::string>& sites) {
818 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
819 if (detached_)
820 return;
821
822 std::map<uint32, RequestType>::iterator iter =
823 pending_responses_.find(request_id);
824 if (iter == pending_responses_.end())
825 return;
826
827 DCHECK_EQ(iter->second, GET_SITES_WITH_DATA);
828
829 pending_responses_.erase(iter);
830 BrowserThread::PostTask(
831 BrowserThread::UI, FROM_HERE,
832 base::Bind(&Core::NotifyGetSitesWithDataCompleted, this, request_id,
833 sites));
834}
835
836void PepperFlashSettingsManager::Core::OnClearSiteDataResult(
837 uint32 request_id,
838 bool success) {
839 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
840 if (detached_)
841 return;
842
843 DLOG_IF(ERROR, !success) << "ClearSiteData returned error";
844
845 std::map<uint32, RequestType>::iterator iter =
846 pending_responses_.find(request_id);
847 if (iter == pending_responses_.end())
848 return;
849
850 DCHECK_EQ(iter->second, CLEAR_SITE_DATA);
851
852 pending_responses_.erase(iter);
853 BrowserThread::PostTask(
854 BrowserThread::UI, FROM_HERE,
855 base::Bind(&Core::NotifyClearSiteDataCompleted, this, request_id,
856 success));
[email protected]ee4dd682012-06-12 15:49:33857}
858
[email protected]1a559442012-05-27 07:18:46859PepperFlashSettingsManager::PepperFlashSettingsManager(
860 Client* client,
861 content::BrowserContext* browser_context)
862 : client_(client),
863 browser_context_(browser_context),
864 next_request_id_(1) {
865 DCHECK(client);
866 DCHECK(browser_context);
867}
868
869PepperFlashSettingsManager::~PepperFlashSettingsManager() {
[email protected]951ef0b2012-07-27 22:46:53870 if (core_.get())
[email protected]1a559442012-05-27 07:18:46871 core_->Detach();
[email protected]1a559442012-05-27 07:18:46872}
873
[email protected]18a4d63c82012-05-25 23:37:03874// static
875bool PepperFlashSettingsManager::IsPepperFlashInUse(
876 PluginPrefs* plugin_prefs,
877 webkit::WebPluginInfo* plugin_info) {
878 if (!plugin_prefs)
879 return false;
880
881 content::PluginService* plugin_service =
882 content::PluginService::GetInstance();
883 std::vector<webkit::WebPluginInfo> plugins;
884 plugin_service->GetPluginInfoArray(
885 GURL(), kFlashPluginSwfMimeType, false, &plugins, NULL);
886
887 for (std::vector<webkit::WebPluginInfo>::iterator iter = plugins.begin();
888 iter != plugins.end(); ++iter) {
889 if (webkit::IsPepperPlugin(*iter) && plugin_prefs->IsPluginEnabled(*iter)) {
890 if (plugin_info)
891 *plugin_info = *iter;
892 return true;
893 }
894 }
895 return false;
896}
897
898// static
899void PepperFlashSettingsManager::RegisterUserPrefs(PrefService* prefs) {
900 prefs->RegisterBooleanPref(prefs::kDeauthorizeContentLicenses,
901 false,
902 PrefService::UNSYNCABLE_PREF);
903
904 prefs->RegisterBooleanPref(prefs::kPepperFlashSettingsEnabled,
905 true,
906 PrefService::UNSYNCABLE_PREF);
907}
[email protected]1a559442012-05-27 07:18:46908
909uint32 PepperFlashSettingsManager::DeauthorizeContentLicenses() {
910 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
911
912 EnsureCoreExists();
913 uint32 id = GetNextRequestId();
914 core_->DeauthorizeContentLicenses(id);
915 return id;
916}
917
[email protected]ee4dd682012-06-12 15:49:33918uint32 PepperFlashSettingsManager::GetPermissionSettings(
919 PP_Flash_BrowserOperations_SettingType setting_type) {
920 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
921
922 EnsureCoreExists();
923 uint32 id = GetNextRequestId();
924 core_->GetPermissionSettings(id, setting_type);
925 return id;
926}
927
928uint32 PepperFlashSettingsManager::SetDefaultPermission(
929 PP_Flash_BrowserOperations_SettingType setting_type,
930 PP_Flash_BrowserOperations_Permission permission,
931 bool clear_site_specific) {
932 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
933
934 EnsureCoreExists();
935 uint32 id = GetNextRequestId();
936 core_->SetDefaultPermission(id, setting_type, permission,
937 clear_site_specific);
938 return id;
939}
940
941uint32 PepperFlashSettingsManager::SetSitePermission(
942 PP_Flash_BrowserOperations_SettingType setting_type,
943 const ppapi::FlashSiteSettings& sites) {
944 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
945
946 EnsureCoreExists();
947 uint32 id = GetNextRequestId();
948 core_->SetSitePermission(id, setting_type, sites);
949 return id;
950}
951
[email protected]951ef0b2012-07-27 22:46:53952uint32 PepperFlashSettingsManager::GetSitesWithData() {
953 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
954
955 EnsureCoreExists();
956 uint32 id = GetNextRequestId();
957 core_->GetSitesWithData(id);
958 return id;
959}
960
961uint32 PepperFlashSettingsManager::ClearSiteData(const std::string& site,
962 uint64 flags,
963 uint64 max_age) {
964 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
965
966 EnsureCoreExists();
967 uint32 id = GetNextRequestId();
968 core_->ClearSiteData(id, site, flags, max_age);
969 return id;
970}
971
[email protected]1a559442012-05-27 07:18:46972uint32 PepperFlashSettingsManager::GetNextRequestId() {
973 return next_request_id_++;
974}
975
976void PepperFlashSettingsManager::EnsureCoreExists() {
[email protected]6464cc12012-07-12 09:25:53977 if (!core_.get()) {
[email protected]1a559442012-05-27 07:18:46978 core_ = new Core(this, browser_context_);
[email protected]6464cc12012-07-12 09:25:53979 core_->Initialize();
980 }
[email protected]1a559442012-05-27 07:18:46981}
982
983void PepperFlashSettingsManager::OnError() {
984 if (core_.get()) {
985 core_->Detach();
986 core_ = NULL;
987 } else {
988 NOTREACHED();
989 }
990}
991