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
3// found in the LICENSE file.
4
5#include "chrome/browser/browsing_data/browsing_data_flash_lso_helper.h"
6
7#include <limits>
8#include <map>
9
10#include "base/callback.h"
11#include "base/logging.h"
12#include "chrome/browser/pepper_flash_settings_manager.h"
13
14namespace {
15
16class BrowsingDataFlashLSOHelperImpl
17    : public BrowsingDataFlashLSOHelper,
18      public PepperFlashSettingsManager::Client {
19 public:
20  explicit BrowsingDataFlashLSOHelperImpl(
21      content::BrowserContext* browser_context);
22
23  // BrowsingDataFlashLSOHelper implementation:
24  virtual void StartFetching(
25      const GetSitesWithFlashDataCallback& callback) OVERRIDE;
26  virtual void DeleteFlashLSOsForSite(const std::string& site) OVERRIDE;
27
28  // PepperFlashSettingsManager::Client overrides:
29  virtual void OnGetSitesWithDataCompleted(
30      uint32 request_id,
31      const std::vector<std::string>& sites) OVERRIDE;
32  virtual void OnClearSiteDataCompleted(
33      uint32 request_id,
34      bool success) OVERRIDE;
35
36 private:
37  virtual ~BrowsingDataFlashLSOHelperImpl();
38
39  // Asynchronously fetches and deletes data and calls us back.
40  PepperFlashSettingsManager settings_manager_;
41
42  // Identifies the request to fetch site data.
43  uint32 get_sites_with_data_request_id_;
44
45  // Contains the pending requests to clear site data. The key is the request
46  // ID, the value the site for which to clear data.
47  std::map<uint32, std::string> clear_site_data_ids_;
48
49  // Called when we have fetched the list of sites.
50  GetSitesWithFlashDataCallback callback_;
51
52  DISALLOW_COPY_AND_ASSIGN(BrowsingDataFlashLSOHelperImpl);
53};
54
55BrowsingDataFlashLSOHelperImpl::BrowsingDataFlashLSOHelperImpl(
56    content::BrowserContext* browser_context)
57    : settings_manager_(this, browser_context),
58      get_sites_with_data_request_id_(0u) {
59}
60
61BrowsingDataFlashLSOHelperImpl::~BrowsingDataFlashLSOHelperImpl() {
62}
63
64void BrowsingDataFlashLSOHelperImpl::StartFetching(
65    const GetSitesWithFlashDataCallback& callback) {
66  DCHECK(callback_.is_null());
67  callback_ = callback;
68  get_sites_with_data_request_id_ = settings_manager_.GetSitesWithData();
69}
70
71void BrowsingDataFlashLSOHelperImpl::DeleteFlashLSOsForSite(
72    const std::string& site) {
73  const uint64 kClearAllData = 0;
74  uint32 id = settings_manager_.ClearSiteData(
75      site, kClearAllData, std::numeric_limits<uint64>::max());
76  clear_site_data_ids_[id] = site;
77}
78
79void BrowsingDataFlashLSOHelperImpl::OnGetSitesWithDataCompleted(
80    uint32 request_id,
81    const std::vector<std::string>& sites) {
82  DCHECK_EQ(get_sites_with_data_request_id_, request_id);
83  callback_.Run(sites);
84  callback_ = GetSitesWithFlashDataCallback();
85}
86
87void BrowsingDataFlashLSOHelperImpl::OnClearSiteDataCompleted(uint32 request_id,
88                                                              bool success) {
89  std::map<uint32, std::string>::iterator entry =
90      clear_site_data_ids_.find(request_id);
91  DCHECK(entry != clear_site_data_ids_.end());
92  LOG_IF(ERROR, !success) << "Couldn't clear Flash LSO data for "
93                          << entry->second;
94  clear_site_data_ids_.erase(entry);
95}
96
97}  // namespace
98
99// static
100BrowsingDataFlashLSOHelper* BrowsingDataFlashLSOHelper::Create(
101    content::BrowserContext* browser_context) {
102  return new BrowsingDataFlashLSOHelperImpl(browser_context);
103}
104