1// Copyright 2014 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#ifndef CHROME_BROWSER_PREFS_INTERCEPTABLE_PREF_FILTER_H_
6#define CHROME_BROWSER_PREFS_INTERCEPTABLE_PREF_FILTER_H_
7
8#include "base/callback.h"
9#include "base/memory/weak_ptr.h"
10#include "base/prefs/pref_filter.h"
11#include "base/values.h"
12
13// A partial implementation of a PrefFilter whose FilterOnLoad call may be
14// intercepted by a FilterOnLoadInterceptor. Implementations of
15// InterceptablePrefFilter are expected to override FinalizeFilterOnLoad rather
16// than re-overriding FilterOnLoad.
17class InterceptablePrefFilter
18    : public PrefFilter,
19      public base::SupportsWeakPtr<InterceptablePrefFilter> {
20 public:
21  // A callback to be invoked by a FilterOnLoadInterceptor when its ready to
22  // hand back the |prefs| it was handed for early filtering. |prefs_altered|
23  // indicates whether the |prefs| were actually altered by the
24  // FilterOnLoadInterceptor before being handed back.
25  typedef base::Callback<void(scoped_ptr<base::DictionaryValue> prefs,
26                              bool prefs_altered)> FinalizeFilterOnLoadCallback;
27
28  // A callback to be invoked from FilterOnLoad. It takes ownership of prefs
29  // and may modify them before handing them back to this
30  // InterceptablePrefFilter via |finalize_filter_on_load|.
31  typedef base::Callback<
32      void(const FinalizeFilterOnLoadCallback& finalize_filter_on_load,
33           scoped_ptr<base::DictionaryValue> prefs)> FilterOnLoadInterceptor;
34
35  InterceptablePrefFilter();
36  virtual ~InterceptablePrefFilter();
37
38  // PrefFilter partial implementation.
39  virtual void FilterOnLoad(
40      const PostFilterOnLoadCallback& post_filter_on_load_callback,
41      scoped_ptr<base::DictionaryValue> pref_store_contents) OVERRIDE;
42
43  // Registers |filter_on_load_interceptor| to intercept the next FilterOnLoad
44  // event. At most one FilterOnLoadInterceptor should be registered per
45  // PrefFilter.
46  void InterceptNextFilterOnLoad(
47      const FilterOnLoadInterceptor& filter_on_load_interceptor);
48
49 private:
50  // Does any extra filtering required by the implementation of this
51  // InterceptablePrefFilter and hands back the |pref_store_contents| to the
52  // initial caller of FilterOnLoad.
53  virtual void FinalizeFilterOnLoad(
54      const PostFilterOnLoadCallback& post_filter_on_load_callback,
55      scoped_ptr<base::DictionaryValue> pref_store_contents,
56      bool prefs_altered) = 0;
57
58  // Callback to be invoked only once (and subsequently reset) on the next
59  // FilterOnLoad event. It will be allowed to modify the |prefs| handed to
60  // FilterOnLoad before handing them back to this PrefHashFilter.
61  FilterOnLoadInterceptor filter_on_load_interceptor_;
62};
63
64#endif  // CHROME_BROWSER_PREFS_INTERCEPTABLE_PREF_FILTER_H_
65