summaryrefslogtreecommitdiffstats
path: root/chrome/browser/plugin_prefs.h
blob: c0dd4451f9f51b2ed901dcc1c10266e1ba40b098 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// 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.

#ifndef CHROME_BROWSER_PLUGIN_PREFS_H_
#define CHROME_BROWSER_PLUGIN_PREFS_H_
#pragma once

#include <map>
#include <set>
#include <vector>

#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"
#include "chrome/browser/prefs/pref_change_registrar.h"
#include "content/common/notification_observer.h"

class NotificationDetails;
class NotificationSource;
class Profile;

namespace content {
class ResourceContext;
}

namespace base {
class DictionaryValue;
class ListValue;
}

namespace webkit {
struct WebPluginInfo;
namespace npapi {
class PluginGroup;
class PluginList;
}
}

// This class stores information about whether a plug-in or a plug-in group is
// enabled or disabled.
// Except where otherwise noted, it can be used on every thread.
class PluginPrefs : public base::RefCountedThreadSafe<PluginPrefs>,
                    public NotificationObserver {
 public:
  enum PolicyStatus {
    NO_POLICY = 0,  // Neither enabled or disabled by policy.
    POLICY_ENABLED,
    POLICY_DISABLED,
  };

  // Initializes the factory for this class for dependency tracking.
  // This should be called before the first profile is created.
  static void Initialize();

  // Returns the instance associated with |profile|, creating it if necessary.
  static PluginPrefs* GetForProfile(Profile* profile);

  // Usually the PluginPrefs associated with a TestingProfile is NULL.
  // This method overrides that for a given TestingProfile, returning the newly
  // created PluginPrefs object.
  static PluginPrefs* GetForTestingProfile(Profile* profile);

  // Sets the plug-in list for tests.
  void SetPluginListForTesting(webkit::npapi::PluginList* plugin_list);

  // Creates a new instance. This method should only be used for testing.
  PluginPrefs();

  // Associates this instance with |prefs|. This enables or disables
  // plugin groups as defined by the user's preferences.
  // This method should only be called on the UI thread.
  void SetPrefs(PrefService* prefs);

  // Detaches from the PrefService before it is destroyed.
  // As the name says, this method should only be called on the UI thread.
  void ShutdownOnUIThread();

  // Enable or disable a plugin group.
  void EnablePluginGroup(bool enable, const string16& group_name);

  // Enable or disable a specific plugin file.
  // Returns false if the plug-in state cannot be changed because of a policy.
  bool EnablePlugin(bool enable, const FilePath& file_path);

  // Enable or disable a plug-in in all profiles. This sets a default for
  // profiles which are created later as well.
  // Returns false if the plug-in state cannot be changed because of a policy.
  // This method should only be called on the UI thread.
  static bool EnablePluginGlobally(bool enable, const FilePath& file_path);

  // Returns whether there is a policy enabling or disabling plug-ins of the
  // given name.
  PolicyStatus PolicyStatusForPlugin(const string16& name);

  // Returns whether the plugin is enabled or not.
  bool IsPluginEnabled(const webkit::WebPluginInfo& plugin);

  // Registers the preferences used by this class.
  // This method should only be called on the UI thread.
  static void RegisterPrefs(PrefService* prefs);

  // NotificationObserver method override.
  virtual void Observe(int type,
                       const NotificationSource& source,
                       const NotificationDetails& details) OVERRIDE;

 private:
  friend class base::RefCountedThreadSafe<PluginPrefs>;
  friend class PluginPrefsTest;

  class Factory;

  virtual ~PluginPrefs();

  // Allows unit tests to directly set enforced plug-in patterns.
  void SetPolicyEnforcedPluginPatterns(
      const std::set<string16>& disabled_patterns,
      const std::set<string16>& disabled_exception_patterns,
      const std::set<string16>& enabled_patterns);

  // Returns the plugin list to use, either the singleton or the override.
  webkit::npapi::PluginList* GetPluginList();

  // Called on the file thread to update the plug-in state.
  void EnablePluginInternal(bool enabled, const FilePath& path);

  // Called on the file thread to get the data necessary to update the saved
  // preferences.
  void GetPreferencesDataOnFileThread();

  // Called on the UI thread with the plugin data to save the preferences.
  void OnUpdatePreferences(std::vector<webkit::npapi::PluginGroup> groups);

  // Sends the notification that plugin data has changed.
  void NotifyPluginStatusChanged();

  static void ListValueToStringSet(const base::ListValue* src,
                                   std::set<string16>* dest);

  // Checks if |name| matches any of the patterns in |pattern_set|.
  static bool IsStringMatchedInSet(const string16& name,
                                   const std::set<string16>& pattern_set);

  // Guards access to the following data structures.
  mutable base::Lock lock_;

  std::map<FilePath, bool> plugin_state_;
  std::map<string16, bool> plugin_group_state_;

  std::set<string16> policy_disabled_plugin_patterns_;
  std::set<string16> policy_disabled_plugin_exception_patterns_;
  std::set<string16> policy_enabled_plugin_patterns_;

  // Weak pointer, owned by the profile (which owns us).
  PrefService* prefs_;

  // PluginList to use for testing. If this is NULL, defaults to the global
  // singleton.
  webkit::npapi::PluginList* plugin_list_;

  PrefChangeRegistrar registrar_;

  DISALLOW_COPY_AND_ASSIGN(PluginPrefs);
};

#endif  // CHROME_BROWSER_PLUGIN_PREFS_H_