summaryrefslogtreecommitdiffstats
path: root/chrome/browser/plugin_data_remover_helper.cc
blob: 599ece9938dc323f9f4141a2052b37d39fca9cea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/plugin_data_remover_helper.h"

#include <string>

#include "base/memory/ref_counted.h"
#include "chrome/browser/plugin_data_remover.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "content/browser/browser_thread.h"
#include "content/common/notification_service.h"

// The internal class is refcounted so it can outlive PluginDataRemoverHelper.
class PluginDataRemoverHelper::Internal
    : public base::RefCountedThreadSafe<PluginDataRemoverHelper::Internal> {
 public:
  explicit Internal(const char* pref_name, PrefService* prefs)
      : pref_name_(pref_name), prefs_(prefs) {}

  void StartUpdate() {
    BrowserThread::PostTask(
        BrowserThread::FILE,
        FROM_HERE,
        NewRunnableMethod(
            this,
            &PluginDataRemoverHelper::Internal::UpdateOnFileThread));
  }

  void Invalidate() {
    prefs_ = NULL;
  }

 private:
  friend class base::RefCountedThreadSafe<Internal>;

  ~Internal() {}

  void UpdateOnFileThread() {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
    bool result = PluginDataRemover::IsSupported();
    BrowserThread::PostTask(
        BrowserThread::UI,
        FROM_HERE,
        NewRunnableMethod(this,
                          &PluginDataRemoverHelper::Internal::SetPrefOnUIThread,
                          result));
  }

  void SetPrefOnUIThread(bool value) {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    if (prefs_)
      prefs_->SetBoolean(pref_name_.c_str(), value);
  }

  std::string pref_name_;
  // Weak pointer.
  PrefService* prefs_;

  DISALLOW_COPY_AND_ASSIGN(Internal);
};

PluginDataRemoverHelper::PluginDataRemoverHelper() {}

PluginDataRemoverHelper::~PluginDataRemoverHelper() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  if (internal_)
    internal_->Invalidate();
}

void PluginDataRemoverHelper::Init(const char* pref_name,
                                   PrefService* prefs,
                                   NotificationObserver* observer) {
  pref_.Init(pref_name, prefs, observer);
  registrar_.Add(this, content::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED,
                 NotificationService::AllSources());
  internal_ = make_scoped_refptr(new Internal(pref_name, prefs));
  internal_->StartUpdate();
}

void PluginDataRemoverHelper::Observe(int type,
                                      const NotificationSource& source,
                                      const NotificationDetails& details) {
  if (type == content::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED) {
    internal_->StartUpdate();
  } else {
    NOTREACHED();
  }
}