summaryrefslogtreecommitdiffstats
path: root/chromium/content/renderer/pepper/pepper_hung_plugin_filter.h
blob: aee539167230b8198a97eeda534556a7fdd0868d (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
// Copyright (c) 2012 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 CONTENT_RENDERER_PEPPER_PEPPER_HUNG_PLUGIN_FILTER_H_
#define CONTENT_RENDERER_PEPPER_PEPPER_HUNG_PLUGIN_FILTER_H_

#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"
#include "ipc/ipc_channel_proxy.h"
#include "ipc/ipc_sync_message_filter.h"
#include "ppapi/proxy/host_dispatcher.h"

namespace content {

// This class monitors a renderer <-> pepper plugin channel on the I/O thread
// of the renderer for a hung plugin.
//
// If the plugin is not responding to sync messages, it will notify the browser
// process and give the user the option to kill the hung plugin.
//
// Note that this class must be threadsafe since it will get the begin/end
// block notifications on the main thread, but the filter is run on the I/O
// thread. This is important since when we're blocked on a sync message to a
// hung plugin, the main thread is frozen.
//
// NOTE: This class is refcounted (via IPC::MessageFilter).
class PepperHungPluginFilter
    : public ppapi::proxy::HostDispatcher::SyncMessageStatusObserver,
      public IPC::MessageFilter {
 public:
  // The |frame_routing_id| is the ID of the render_frame so that this class can
  // send messages to the browser via that frame's route. The |plugin_child_id|
  // is the ID in the browser process of the pepper plugin process host. We use
  // this to identify the proper plugin process to terminate.
  PepperHungPluginFilter(const base::FilePath& plugin_path,
                         int frame_routing_id,
                         int plugin_child_id);

  // SyncMessageStatusReceiver implementation.
  void BeginBlockOnSyncMessage() override;
  void EndBlockOnSyncMessage() override;

  // MessageFilter implementation.
  void OnFilterRemoved() override;
  void OnChannelError() override;
  bool OnMessageReceived(const IPC::Message& message) override;

 protected:
  ~PepperHungPluginFilter() override;

 private:
  // Makes sure that the hung timer is scheduled.
  void EnsureTimerScheduled();

  // Checks whether the plugin could have transitioned from hung to unhung and
  // notifies the browser if so.
  void MayHaveBecomeUnhung();

  // Calculate the point at which the plugin could next be considered hung.
  base::TimeTicks GetHungTime() const;

  // Checks if the plugin is considered hung based on whether it has been
  // blocked for long enough.
  bool IsHung() const;

  // Timer handler that checks for a hang after a timeout.
  void OnHangTimer();

  // Sends the hung/unhung message to the browser process.
  void SendHungMessage(bool is_hung);

  base::Lock lock_;

  base::FilePath plugin_path_;
  int frame_routing_id_;
  int plugin_child_id_;

  // Used to post messages to the renderer <-> browser message channel from
  // other threads without having to worry about the lifetime of that object.
  // We don't actually use the "sync" feature of this filter.
  scoped_refptr<IPC::SyncMessageFilter> filter_;

  scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;

  // The time when we start being blocked on a sync message. If there are
  // nested ones, this is the time of the outermost one.
  //
  // This will be is_null() if we've never blocked.
  base::TimeTicks began_blocking_time_;

  // Time that the last message was received from the plugin.
  //
  // This will be is_null() if we've never received any messages.
  base::TimeTicks last_message_received_;

  // Number of nested sync messages that we're blocked on.
  int pending_sync_message_count_;

  // True when we've sent the "plugin is hung" message to the browser. We track
  // this so we know to look for it becoming unhung and send the corresponding
  // message to the browser.
  bool hung_plugin_showing_;

  bool timer_task_pending_;

  DISALLOW_COPY_AND_ASSIGN(PepperHungPluginFilter);
};

}  // namespace content

#endif  // CONTENT_RENDERER_PEPPER_PEPPER_HUNG_PLUGIN_FILTER_H_