blob: 2fa8ea9d83efd5e134c90dd2ace1e77588de0a53 [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
39 // Stops sending notifications to |manager_| and sets it to NULL.
40 void Detach();
41
42 void DeauthorizeContentLicenses(uint32 request_id);
[email protected]ee4dd682012-06-12 15:49:3343 void GetPermissionSettings(
44 uint32 request_id,
45 PP_Flash_BrowserOperations_SettingType setting_type);
46 void SetDefaultPermission(
47 uint32 request_id,
48 PP_Flash_BrowserOperations_SettingType setting_type,
49 PP_Flash_BrowserOperations_Permission permission,
50 bool clear_site_specific);
51 void SetSitePermission(uint32 request_id,
52 PP_Flash_BrowserOperations_SettingType setting_type,
53 const ppapi::FlashSiteSettings& sites);
[email protected]1a559442012-05-27 07:18:4654
[email protected]b44f8ad2012-06-15 20:52:5855 // IPC::Listener implementation.
[email protected]1a559442012-05-27 07:18:4656 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
57 virtual void OnChannelError() OVERRIDE;
58
59 private:
60 friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>;
61 friend class base::DeleteHelper<Core>;
62
63 enum RequestType {
64 INVALID_REQUEST_TYPE = 0,
[email protected]ee4dd682012-06-12 15:49:3365 DEAUTHORIZE_CONTENT_LICENSES,
66 GET_PERMISSION_SETTINGS,
67 SET_DEFAULT_PERMISSION,
68 SET_SITE_PERMISSION
[email protected]1a559442012-05-27 07:18:4669 };
70
71 struct PendingRequest {
[email protected]ee4dd682012-06-12 15:49:3372 PendingRequest()
73 : id(0),
74 type(INVALID_REQUEST_TYPE),
75 setting_type(PP_FLASH_BROWSEROPERATIONS_SETTINGTYPE_CAMERAMIC),
76 permission(PP_FLASH_BROWSEROPERATIONS_PERMISSION_DEFAULT),
77 clear_site_specific(false) {
78 }
[email protected]1a559442012-05-27 07:18:4679
80 uint32 id;
81 RequestType type;
[email protected]ee4dd682012-06-12 15:49:3382
83 // Used by GET_PERMISSION_SETTINGS, SET_DEFAULT_PERMISSION and
84 // SET_SITE_PERMISSION.
85 PP_Flash_BrowserOperations_SettingType setting_type;
86
87 // Used by SET_DEFAULT_PERMISSION.
88 PP_Flash_BrowserOperations_Permission permission;
89 bool clear_site_specific;
90
91 // Used by SET_SITE_PERMISSION.
92 ppapi::FlashSiteSettings sites;
[email protected]1a559442012-05-27 07:18:4693 };
94
95 virtual ~Core();
96
97 void Initialize();
98 void ConnectToChannel(bool success, const IPC::ChannelHandle& handle);
99
100 void DeauthorizeContentLicensesOnIOThread(uint32 request_id);
[email protected]ee4dd682012-06-12 15:49:33101 void GetPermissionSettingsOnIOThread(
102 uint32 request_id,
103 PP_Flash_BrowserOperations_SettingType setting_type);
104 void SetDefaultPermissionOnIOThread(
105 uint32 request_id,
106 PP_Flash_BrowserOperations_SettingType setting_type,
107 PP_Flash_BrowserOperations_Permission permission,
108 bool clear_site_specific);
109 void SetSitePermissionOnIOThread(
110 uint32 request_id,
111 PP_Flash_BrowserOperations_SettingType setting_type,
112 const ppapi::FlashSiteSettings& sites);
[email protected]4d4ee4c2012-06-22 19:11:30113 void DetachOnIOThread();
[email protected]ee4dd682012-06-12 15:49:33114
[email protected]1a559442012-05-27 07:18:46115 void NotifyErrorFromIOThread();
116
117 void NotifyDeauthorizeContentLicensesCompleted(uint32 request_id,
118 bool success);
[email protected]ee4dd682012-06-12 15:49:33119 void NotifyGetPermissionSettingsCompleted(
120 uint32 request_id,
121 bool success,
122 PP_Flash_BrowserOperations_Permission default_permission,
123 const ppapi::FlashSiteSettings& sites);
124 void NotifySetDefaultPermissionCompleted(uint32 request_id, bool success);
125 void NotifySetSitePermissionCompleted(uint32 request_id, bool success);
126
[email protected]1a559442012-05-27 07:18:46127 void NotifyError(
128 const std::vector<std::pair<uint32, RequestType> >& notifications);
129
130 // Message handlers.
131 void OnDeauthorizeContentLicensesResult(uint32 request_id, bool success);
[email protected]ee4dd682012-06-12 15:49:33132 void OnGetPermissionSettingsResult(
133 uint32 request_id,
134 bool success,
135 PP_Flash_BrowserOperations_Permission default_permission,
136 const ppapi::FlashSiteSettings& sites);
137 void OnSetDefaultPermissionResult(uint32 request_id, bool success);
138 void OnSetSitePermissionResult(uint32 request_id, bool success);
[email protected]1a559442012-05-27 07:18:46139
140 // Used only on the UI thread.
141 PepperFlashSettingsManager* manager_;
142
143 // Used only on the I/O thread.
144 FilePath plugin_data_path_;
145
146 // The channel is NULL until we have opened a connection to the broker
147 // process. Used only on the I/O thread.
148 scoped_ptr<IPC::Channel> channel_;
149
150 // Used only on the I/O thread.
151 bool initialized_;
[email protected]4d4ee4c2012-06-22 19:11:30152 // Whether Detach() has been called on the UI thread. When it is true, further
153 // work is not necessary. Used only on the I/O thread.
154 bool detached_;
[email protected]1a559442012-05-27 07:18:46155
156 // Requests that need to be sent once the channel to the broker process is
157 // established. Used only on the I/O thread.
158 std::vector<PendingRequest> pending_requests_;
159 // Requests that have been sent but haven't got replied. Used only on the
160 // I/O thread.
161 std::map<uint32, RequestType> pending_responses_;
162
163 // Used only on the I/O thread.
164 scoped_refptr<content::PepperFlashSettingsHelper> helper_;
165
166 // Path for the current profile. Must be retrieved on the UI thread from the
167 // browser context when we start so we can use it later on the I/O thread.
168 FilePath browser_context_path_;
169
170 scoped_refptr<PluginPrefs> plugin_prefs_;
171};
172
173PepperFlashSettingsManager::Core::Core(PepperFlashSettingsManager* manager,
174 content::BrowserContext* browser_context)
175 : manager_(manager),
176 initialized_(false),
[email protected]4d4ee4c2012-06-22 19:11:30177 detached_(false),
[email protected]1a559442012-05-27 07:18:46178 browser_context_path_(browser_context->GetPath()),
179 plugin_prefs_(PluginPrefs::GetForProfile(
180 Profile::FromBrowserContext(browser_context))) {
181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
182
183 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
184 base::Bind(&Core::Initialize, this));
185}
186
187PepperFlashSettingsManager::Core::~Core() {
188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
189}
190
191void PepperFlashSettingsManager::Core::Detach() {
192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
193
194 manager_ = NULL;
[email protected]4d4ee4c2012-06-22 19:11:30195 // This call guarantees that one ref is retained until |detached_| is set to
196 // true. This is important. Otherwise, if the ref count drops to zero on the
197 // UI thread (which posts a task to delete this object on the I/O thread)
198 // while the I/O thread doesn't know about it, methods on the I/O thread might
199 // increase the ref count again and cause double deletion.
200 BrowserThread::PostTask(
201 BrowserThread::IO, FROM_HERE, base::Bind(&Core::DetachOnIOThread, this));
[email protected]1a559442012-05-27 07:18:46202}
203
204void PepperFlashSettingsManager::Core::DeauthorizeContentLicenses(
205 uint32 request_id) {
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
207
208 BrowserThread::PostTask(
209 BrowserThread::IO, FROM_HERE,
210 base::Bind(&Core::DeauthorizeContentLicensesOnIOThread, this,
211 request_id));
212}
213
[email protected]ee4dd682012-06-12 15:49:33214void PepperFlashSettingsManager::Core::GetPermissionSettings(
215 uint32 request_id,
216 PP_Flash_BrowserOperations_SettingType setting_type) {
217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
218
219 BrowserThread::PostTask(
220 BrowserThread::IO, FROM_HERE,
221 base::Bind(&Core::GetPermissionSettingsOnIOThread, this, request_id,
222 setting_type));
223}
224
225void PepperFlashSettingsManager::Core::SetDefaultPermission(
226 uint32 request_id,
227 PP_Flash_BrowserOperations_SettingType setting_type,
228 PP_Flash_BrowserOperations_Permission permission,
229 bool clear_site_specific) {
230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
231
232 BrowserThread::PostTask(
233 BrowserThread::IO, FROM_HERE,
234 base::Bind(&Core::SetDefaultPermissionOnIOThread, this, request_id,
235 setting_type, permission, clear_site_specific));
236}
237
238void PepperFlashSettingsManager::Core::SetSitePermission(
239 uint32 request_id,
240 PP_Flash_BrowserOperations_SettingType setting_type,
241 const ppapi::FlashSiteSettings& sites) {
242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
243
244 BrowserThread::PostTask(
245 BrowserThread::IO, FROM_HERE,
246 base::Bind(&Core::SetSitePermissionOnIOThread, this, request_id,
247 setting_type, sites));
248}
249
[email protected]1a559442012-05-27 07:18:46250bool PepperFlashSettingsManager::Core::OnMessageReceived(
251 const IPC::Message& message) {
252 IPC_BEGIN_MESSAGE_MAP(Core, message)
253 IPC_MESSAGE_HANDLER(PpapiHostMsg_DeauthorizeContentLicensesResult,
254 OnDeauthorizeContentLicensesResult)
[email protected]ee4dd682012-06-12 15:49:33255 IPC_MESSAGE_HANDLER(PpapiHostMsg_GetPermissionSettingsResult,
256 OnGetPermissionSettingsResult)
257 IPC_MESSAGE_HANDLER(PpapiHostMsg_SetDefaultPermissionResult,
258 OnSetDefaultPermissionResult)
259 IPC_MESSAGE_HANDLER(PpapiHostMsg_SetSitePermissionResult,
260 OnSetSitePermissionResult)
[email protected]1a559442012-05-27 07:18:46261 IPC_MESSAGE_UNHANDLED_ERROR()
262 IPC_END_MESSAGE_MAP()
263
264 return true;
265}
266
267void PepperFlashSettingsManager::Core::OnChannelError() {
268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30269 if (detached_)
270 return;
[email protected]1a559442012-05-27 07:18:46271
272 NotifyErrorFromIOThread();
273}
274
275void PepperFlashSettingsManager::Core::Initialize() {
276 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30277 DCHECK(!detached_);
[email protected]1a559442012-05-27 07:18:46278 DCHECK(!initialized_);
279
280 webkit::WebPluginInfo plugin_info;
281 if (!PepperFlashSettingsManager::IsPepperFlashInUse(plugin_prefs_.get(),
282 &plugin_info)) {
283 NotifyErrorFromIOThread();
284 return;
285 }
286
287 FilePath profile_path =
288 browser_context_path_.Append(content::kPepperDataDirname);
289#if defined(OS_WIN)
290 plugin_data_path_ = profile_path.Append(plugin_info.name);
291#else
292 plugin_data_path_ = profile_path.Append(UTF16ToUTF8(plugin_info.name));
293#endif
294
295 helper_ = content::PepperFlashSettingsHelper::Create();
296 content::PepperFlashSettingsHelper::OpenChannelCallback callback =
297 base::Bind(&Core::ConnectToChannel, this);
298 helper_->OpenChannelToBroker(plugin_info.path, callback);
299}
300
301void PepperFlashSettingsManager::Core::ConnectToChannel(
302 bool success,
303 const IPC::ChannelHandle& handle) {
304 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30305 if (detached_)
306 return;
307
[email protected]1a559442012-05-27 07:18:46308 DCHECK(!initialized_);
309 DCHECK(!channel_.get());
310
311 if (!success) {
[email protected]ee4dd682012-06-12 15:49:33312 DLOG(ERROR) << "Couldn't open plugin channel";
[email protected]1a559442012-05-27 07:18:46313 NotifyErrorFromIOThread();
314 return;
315 }
316
317 channel_.reset(new IPC::Channel(handle, IPC::Channel::MODE_CLIENT, this));
318 if (!channel_->Connect()) {
[email protected]ee4dd682012-06-12 15:49:33319 DLOG(ERROR) << "Couldn't connect to plugin";
[email protected]1a559442012-05-27 07:18:46320 NotifyErrorFromIOThread();
321 return;
322 }
323
324 initialized_ = true;
325
326 std::vector<PendingRequest> temp_pending_requests;
327 temp_pending_requests.swap(pending_requests_);
328 for (std::vector<PendingRequest>::iterator iter =
329 temp_pending_requests.begin();
330 iter != temp_pending_requests.end(); ++iter) {
331 switch (iter->type) {
332 case DEAUTHORIZE_CONTENT_LICENSES:
333 DeauthorizeContentLicensesOnIOThread(iter->id);
334 break;
[email protected]ee4dd682012-06-12 15:49:33335 case GET_PERMISSION_SETTINGS:
336 GetPermissionSettingsOnIOThread(iter->id, iter->setting_type);
337 break;
338 case SET_DEFAULT_PERMISSION:
339 SetDefaultPermissionOnIOThread(
340 iter->id, iter->setting_type, iter->permission,
341 iter->clear_site_specific);
342 break;
343 case SET_SITE_PERMISSION:
344 SetSitePermissionOnIOThread(iter->id, iter->setting_type, iter->sites);
345 break;
[email protected]1a559442012-05-27 07:18:46346 default:
347 NOTREACHED();
348 break;
349 }
350 }
351}
352
353void PepperFlashSettingsManager::Core::DeauthorizeContentLicensesOnIOThread(
354 uint32 request_id) {
355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30356 if (detached_)
357 return;
[email protected]1a559442012-05-27 07:18:46358
359 if (!initialized_) {
360 PendingRequest request;
361 request.id = request_id;
362 request.type = DEAUTHORIZE_CONTENT_LICENSES;
363 pending_requests_.push_back(request);
364 return;
365 }
366
367 pending_responses_.insert(
368 std::make_pair(request_id, DEAUTHORIZE_CONTENT_LICENSES));
369 IPC::Message* msg =
370 new PpapiMsg_DeauthorizeContentLicenses(request_id, plugin_data_path_);
371 if (!channel_->Send(msg)) {
[email protected]ee4dd682012-06-12 15:49:33372 DLOG(ERROR) << "Couldn't send DeauthorizeContentLicenses message";
373 // A failure notification for the current request will be sent since
374 // |pending_responses_| has been updated.
375 NotifyErrorFromIOThread();
376 }
377}
378
379void PepperFlashSettingsManager::Core::GetPermissionSettingsOnIOThread(
380 uint32 request_id,
381 PP_Flash_BrowserOperations_SettingType setting_type) {
382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30383 if (detached_)
384 return;
[email protected]ee4dd682012-06-12 15:49:33385
386 if (!initialized_) {
387 PendingRequest request;
388 request.id = request_id;
389 request.type = GET_PERMISSION_SETTINGS;
390 request.setting_type = setting_type;
391 pending_requests_.push_back(request);
392 return;
393 }
394
395 pending_responses_.insert(
396 std::make_pair(request_id, GET_PERMISSION_SETTINGS));
397 IPC::Message* msg = new PpapiMsg_GetPermissionSettings(
398 request_id, plugin_data_path_, setting_type);
399 if (!channel_->Send(msg)) {
400 DLOG(ERROR) << "Couldn't send GetPermissionSettings message";
401 // A failure notification for the current request will be sent since
402 // |pending_responses_| has been updated.
403 NotifyErrorFromIOThread();
404 }
405}
406
407void PepperFlashSettingsManager::Core::SetDefaultPermissionOnIOThread(
408 uint32 request_id,
409 PP_Flash_BrowserOperations_SettingType setting_type,
410 PP_Flash_BrowserOperations_Permission permission,
411 bool clear_site_specific) {
412 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30413 if (detached_)
414 return;
[email protected]ee4dd682012-06-12 15:49:33415
416 if (!initialized_) {
417 PendingRequest request;
418 request.id = request_id;
419 request.type = SET_DEFAULT_PERMISSION;
420 request.setting_type = setting_type;
421 request.permission = permission;
422 request.clear_site_specific = clear_site_specific;
423 pending_requests_.push_back(request);
424 return;
425 }
426
427 pending_responses_.insert(std::make_pair(request_id, SET_DEFAULT_PERMISSION));
428 IPC::Message* msg = new PpapiMsg_SetDefaultPermission(
429 request_id, plugin_data_path_, setting_type, permission,
430 clear_site_specific);
431 if (!channel_->Send(msg)) {
432 DLOG(ERROR) << "Couldn't send SetDefaultPermission 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::SetSitePermissionOnIOThread(
440 uint32 request_id,
441 PP_Flash_BrowserOperations_SettingType setting_type,
442 const ppapi::FlashSiteSettings& sites) {
443 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30444 if (detached_)
445 return;
[email protected]ee4dd682012-06-12 15:49:33446
447 if (!initialized_) {
448 pending_requests_.push_back(PendingRequest());
449 PendingRequest& request = pending_requests_.back();
450 request.id = request_id;
451 request.type = SET_SITE_PERMISSION;
452 request.setting_type = setting_type;
453 request.sites = sites;
454 return;
455 }
456
457 pending_responses_.insert(std::make_pair(request_id, SET_SITE_PERMISSION));
458 IPC::Message* msg = new PpapiMsg_SetSitePermission(
459 request_id, plugin_data_path_, setting_type, sites);
460 if (!channel_->Send(msg)) {
461 DLOG(ERROR) << "Couldn't send SetSitePermission message";
[email protected]1a559442012-05-27 07:18:46462 // A failure notification for the current request will be sent since
463 // |pending_responses_| has been updated.
464 NotifyErrorFromIOThread();
465 }
466}
467
[email protected]4d4ee4c2012-06-22 19:11:30468void PepperFlashSettingsManager::Core::DetachOnIOThread() {
469 detached_ = true;
470}
471
[email protected]1a559442012-05-27 07:18:46472void PepperFlashSettingsManager::Core::NotifyErrorFromIOThread() {
473 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30474 if (detached_)
475 return;
[email protected]1a559442012-05-27 07:18:46476
477 std::vector<std::pair<uint32, RequestType> > notifications;
478 for (std::vector<PendingRequest>::iterator iter = pending_requests_.begin();
479 iter != pending_requests_.end(); ++iter) {
480 notifications.push_back(std::make_pair(iter->id, iter->type));
481 }
482 pending_requests_.clear();
483 notifications.insert(notifications.end(), pending_responses_.begin(),
484 pending_responses_.end());
485 pending_responses_.clear();
486
487 BrowserThread::PostTask(
488 BrowserThread::UI, FROM_HERE,
489 base::Bind(&Core::NotifyError, this, notifications));
490}
491
492void
493PepperFlashSettingsManager::Core::NotifyDeauthorizeContentLicensesCompleted(
494 uint32 request_id,
495 bool success) {
496 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
497
498 if (manager_) {
499 manager_->client_->OnDeauthorizeContentLicensesCompleted(
500 request_id, success);
501 }
502}
503
[email protected]ee4dd682012-06-12 15:49:33504void PepperFlashSettingsManager::Core::NotifyGetPermissionSettingsCompleted(
505 uint32 request_id,
506 bool success,
507 PP_Flash_BrowserOperations_Permission default_permission,
508 const ppapi::FlashSiteSettings& sites) {
509 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
510
511 if (manager_) {
512 manager_->client_->OnGetPermissionSettingsCompleted(
513 request_id, success, default_permission, sites);
514 }
515}
516
517void PepperFlashSettingsManager::Core::NotifySetDefaultPermissionCompleted(
518 uint32 request_id,
519 bool success) {
520 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
521
522 if (manager_) {
523 manager_->client_->OnSetDefaultPermissionCompleted(
524 request_id, success);
525 }
526}
527
528void PepperFlashSettingsManager::Core::NotifySetSitePermissionCompleted(
529 uint32 request_id,
530 bool success) {
531 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
532
533 if (manager_) {
534 manager_->client_->OnSetSitePermissionCompleted(
535 request_id, success);
536 }
537}
538
[email protected]1a559442012-05-27 07:18:46539void PepperFlashSettingsManager::Core::NotifyError(
540 const std::vector<std::pair<uint32, RequestType> >& notifications) {
541 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
542
543 scoped_refptr<Core> protector(this);
544 for (std::vector<std::pair<uint32, RequestType> >::const_iterator iter =
545 notifications.begin(); iter != notifications.end(); ++iter) {
546 // Check |manager_| for each iteration in case Detach() happens in one of
547 // the callbacks.
548 if (manager_) {
549 switch (iter->second) {
550 case DEAUTHORIZE_CONTENT_LICENSES:
[email protected]ee4dd682012-06-12 15:49:33551 manager_->client_->OnDeauthorizeContentLicensesCompleted(
552 iter->first, false);
553 break;
554 case GET_PERMISSION_SETTINGS:
555 manager_->client_->OnGetPermissionSettingsCompleted(
556 iter->first, false, PP_FLASH_BROWSEROPERATIONS_PERMISSION_DEFAULT,
557 ppapi::FlashSiteSettings());
558 break;
559 case SET_DEFAULT_PERMISSION:
560 manager_->client_->OnSetDefaultPermissionCompleted(
561 iter->first, false);
562 break;
563 case SET_SITE_PERMISSION:
564 manager_->client_->OnSetSitePermissionCompleted(iter->first, false);
[email protected]1a559442012-05-27 07:18:46565 break;
566 default:
567 NOTREACHED();
568 break;
569 }
570 }
571 }
572
573 if (manager_)
574 manager_->OnError();
575}
576
577void PepperFlashSettingsManager::Core::OnDeauthorizeContentLicensesResult(
578 uint32 request_id,
579 bool success) {
580 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30581 if (detached_)
582 return;
583
[email protected]ee4dd682012-06-12 15:49:33584 DLOG_IF(ERROR, !success) << "DeauthorizeContentLicenses returned error";
[email protected]1a559442012-05-27 07:18:46585
586 std::map<uint32, RequestType>::iterator iter =
587 pending_responses_.find(request_id);
588 if (iter != pending_responses_.end()) {
589 DCHECK_EQ(iter->second, DEAUTHORIZE_CONTENT_LICENSES);
590
591 pending_responses_.erase(iter);
592 BrowserThread::PostTask(
593 BrowserThread::UI, FROM_HERE,
594 base::Bind(&Core::NotifyDeauthorizeContentLicensesCompleted, this,
595 request_id, success));
596 }
597}
598
[email protected]ee4dd682012-06-12 15:49:33599void PepperFlashSettingsManager::Core::OnGetPermissionSettingsResult(
600 uint32 request_id,
601 bool success,
602 PP_Flash_BrowserOperations_Permission default_permission,
603 const ppapi::FlashSiteSettings& sites) {
604 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30605 if (detached_)
606 return;
607
[email protected]ee4dd682012-06-12 15:49:33608 DLOG_IF(ERROR, !success) << "GetPermissionSettings returned error";
609
610 std::map<uint32, RequestType>::iterator iter =
611 pending_responses_.find(request_id);
612 if (iter != pending_responses_.end()) {
613 DCHECK_EQ(iter->second, GET_PERMISSION_SETTINGS);
614
615 pending_responses_.erase(iter);
616 BrowserThread::PostTask(
617 BrowserThread::UI, FROM_HERE,
618 base::Bind(&Core::NotifyGetPermissionSettingsCompleted, this,
619 request_id, success, default_permission, sites));
620 }
621}
622
623void PepperFlashSettingsManager::Core::OnSetDefaultPermissionResult(
624 uint32 request_id,
625 bool success) {
626 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30627 if (detached_)
628 return;
629
[email protected]ee4dd682012-06-12 15:49:33630 DLOG_IF(ERROR, !success) << "SetDefaultPermission returned error";
631
632 std::map<uint32, RequestType>::iterator iter =
633 pending_responses_.find(request_id);
634 if (iter != pending_responses_.end()) {
635 DCHECK_EQ(iter->second, SET_DEFAULT_PERMISSION);
636
637 pending_responses_.erase(iter);
638 BrowserThread::PostTask(
639 BrowserThread::UI, FROM_HERE,
640 base::Bind(&Core::NotifySetDefaultPermissionCompleted, this,
641 request_id, success));
642 }
643}
644
645void PepperFlashSettingsManager::Core::OnSetSitePermissionResult(
646 uint32 request_id,
647 bool success) {
648 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4d4ee4c2012-06-22 19:11:30649 if (detached_)
650 return;
651
[email protected]ee4dd682012-06-12 15:49:33652 DLOG_IF(ERROR, !success) << "SetSitePermission returned error";
653
654 std::map<uint32, RequestType>::iterator iter =
655 pending_responses_.find(request_id);
656 if (iter != pending_responses_.end()) {
657 DCHECK_EQ(iter->second, SET_SITE_PERMISSION);
658
659 pending_responses_.erase(iter);
660 BrowserThread::PostTask(
661 BrowserThread::UI, FROM_HERE,
662 base::Bind(&Core::NotifySetSitePermissionCompleted, this, request_id,
663 success));
664 }
665}
666
[email protected]1a559442012-05-27 07:18:46667PepperFlashSettingsManager::PepperFlashSettingsManager(
668 Client* client,
669 content::BrowserContext* browser_context)
670 : client_(client),
671 browser_context_(browser_context),
672 next_request_id_(1) {
673 DCHECK(client);
674 DCHECK(browser_context);
675}
676
677PepperFlashSettingsManager::~PepperFlashSettingsManager() {
678 if (core_.get()) {
679 core_->Detach();
680 core_ = NULL;
681 }
682}
683
[email protected]18a4d63c82012-05-25 23:37:03684// static
685bool PepperFlashSettingsManager::IsPepperFlashInUse(
686 PluginPrefs* plugin_prefs,
687 webkit::WebPluginInfo* plugin_info) {
688 if (!plugin_prefs)
689 return false;
690
691 content::PluginService* plugin_service =
692 content::PluginService::GetInstance();
693 std::vector<webkit::WebPluginInfo> plugins;
694 plugin_service->GetPluginInfoArray(
695 GURL(), kFlashPluginSwfMimeType, false, &plugins, NULL);
696
697 for (std::vector<webkit::WebPluginInfo>::iterator iter = plugins.begin();
698 iter != plugins.end(); ++iter) {
699 if (webkit::IsPepperPlugin(*iter) && plugin_prefs->IsPluginEnabled(*iter)) {
700 if (plugin_info)
701 *plugin_info = *iter;
702 return true;
703 }
704 }
705 return false;
706}
707
708// static
709void PepperFlashSettingsManager::RegisterUserPrefs(PrefService* prefs) {
710 prefs->RegisterBooleanPref(prefs::kDeauthorizeContentLicenses,
711 false,
712 PrefService::UNSYNCABLE_PREF);
713
714 prefs->RegisterBooleanPref(prefs::kPepperFlashSettingsEnabled,
715 true,
716 PrefService::UNSYNCABLE_PREF);
717}
[email protected]1a559442012-05-27 07:18:46718
719uint32 PepperFlashSettingsManager::DeauthorizeContentLicenses() {
720 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
721
722 EnsureCoreExists();
723 uint32 id = GetNextRequestId();
724 core_->DeauthorizeContentLicenses(id);
725 return id;
726}
727
[email protected]ee4dd682012-06-12 15:49:33728uint32 PepperFlashSettingsManager::GetPermissionSettings(
729 PP_Flash_BrowserOperations_SettingType setting_type) {
730 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
731
732 EnsureCoreExists();
733 uint32 id = GetNextRequestId();
734 core_->GetPermissionSettings(id, setting_type);
735 return id;
736}
737
738uint32 PepperFlashSettingsManager::SetDefaultPermission(
739 PP_Flash_BrowserOperations_SettingType setting_type,
740 PP_Flash_BrowserOperations_Permission permission,
741 bool clear_site_specific) {
742 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
743
744 EnsureCoreExists();
745 uint32 id = GetNextRequestId();
746 core_->SetDefaultPermission(id, setting_type, permission,
747 clear_site_specific);
748 return id;
749}
750
751uint32 PepperFlashSettingsManager::SetSitePermission(
752 PP_Flash_BrowserOperations_SettingType setting_type,
753 const ppapi::FlashSiteSettings& sites) {
754 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
755
756 EnsureCoreExists();
757 uint32 id = GetNextRequestId();
758 core_->SetSitePermission(id, setting_type, sites);
759 return id;
760}
761
[email protected]1a559442012-05-27 07:18:46762uint32 PepperFlashSettingsManager::GetNextRequestId() {
763 return next_request_id_++;
764}
765
766void PepperFlashSettingsManager::EnsureCoreExists() {
767 if (!core_.get())
768 core_ = new Core(this, browser_context_);
769}
770
771void PepperFlashSettingsManager::OnError() {
772 if (core_.get()) {
773 core_->Detach();
774 core_ = NULL;
775 } else {
776 NOTREACHED();
777 }
778}
779