1// Copyright (c) 2011 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_PLUGIN_DATA_REMOVER_H_
6#define CHROME_BROWSER_PLUGIN_DATA_REMOVER_H_
7#pragma once
8
9#include "base/memory/ref_counted.h"
10#include "base/time.h"
11#include "content/browser/plugin_process_host.h"
12
13class Task;
14
15namespace base {
16class MessageLoopProxy;
17class WaitableEvent;
18}
19
20class PluginDataRemover : public base::RefCountedThreadSafe<PluginDataRemover>,
21                          public PluginProcessHost::Client,
22                          public IPC::Channel::Listener {
23 public:
24  PluginDataRemover();
25
26  // The plug-in whose data should be removed (usually Flash) is specified via
27  // its MIME type. This method sets a different MIME type in order to call a
28  // different plug-in (for example in tests).
29  void set_mime_type(const std::string& mime_type) { mime_type_ = mime_type; }
30
31  // Starts removing plug-in data stored since |begin_time|.
32  base::WaitableEvent* StartRemoving(base::Time begin_time);
33
34  // Returns whether there is a plug-in installed that supports removing
35  // LSO data. Because this method possibly has to load the plug-in list, it
36  // should only be called on the FILE thread.
37  static bool IsSupported();
38
39  // Indicates whether we are still in the process of removing plug-in data.
40  bool is_removing() const { return is_removing_; }
41
42  // Wait until removing has finished. When the browser is still running (i.e.
43  // not during shutdown), you should use a WaitableEventWatcher in combination
44  // with the WaitableEvent returned by StartRemoving.
45  void Wait();
46
47  // PluginProcessHost::Client methods.
48  virtual int ID();
49  virtual bool OffTheRecord();
50  virtual void SetPluginInfo(const webkit::npapi::WebPluginInfo& info);
51  virtual void OnChannelOpened(const IPC::ChannelHandle& handle);
52  virtual void OnError();
53
54  // IPC::Channel::Listener methods.
55  virtual bool OnMessageReceived(const IPC::Message& message);
56  virtual void OnChannelError();
57
58 private:
59  friend class base::RefCountedThreadSafe<PluginDataRemover>;
60  friend class PluginDataRemoverTest;
61  ~PluginDataRemover();
62
63  // Signals that we are finished with removing data (successful or not). This
64  // method is safe to call multiple times.
65  void SignalDone();
66  // Connects the client side of a newly opened plug-in channel.
67  void ConnectToChannel(const IPC::ChannelHandle& handle);
68  // Handles the PluginHostMsg_ClearSiteDataResult message.
69  void OnClearSiteDataResult(bool success);
70  // Called when a timeout happens in order not to block the client
71  // indefinitely.
72  void OnTimeout();
73
74  std::string mime_type_;
75  bool is_removing_;
76  // The point in time when we start removing data.
77  base::Time remove_start_time_;
78  // The point in time from which on we remove data.
79  base::Time begin_time_;
80  scoped_ptr<base::WaitableEvent> event_;
81  // We own the channel, but it's used on the IO thread, so it needs to be
82  // deleted there. It's NULL until we have opened a connection to the plug-in
83  // process.
84  IPC::Channel* channel_;
85
86  DISALLOW_COPY_AND_ASSIGN(PluginDataRemover);
87};
88
89#endif  // CHROME_BROWSER_PLUGIN_DATA_REMOVER_H_
90