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 UI_BASE_X_X11_FOREIGN_WINDOW_MANAGER_H_
6#define UI_BASE_X_X11_FOREIGN_WINDOW_MANAGER_H_
7
8#include <map>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "ui/base/ui_base_export.h"
13#include "ui/gfx/x/x11_types.h"
14
15// A process wide singleton for selecting events on X windows which were not
16// created by Chrome.
17template <typename T> struct DefaultSingletonTraits;
18
19namespace ui {
20
21// Manages the events that Chrome has selected on X windows which were not
22// created by Chrome. This class allows several clients to select events
23// on the same X window.
24class UI_BASE_EXPORT XForeignWindowManager {
25 public:
26  static XForeignWindowManager* GetInstance();
27
28  // Requests that events associated with |event_mask| on |xid| be reported to
29  // Chrome. Returns an id to use for canceling the request.
30  int RequestEvents(XID xid, long event_mask) WARN_UNUSED_RESULT;
31
32  // Cancels the request with |request_id|. Unless there is another request for
33  // events on the X window associated with |request_id|, this stops Chrome from
34  // getting events for the X window.
35  void CancelRequest(int request_id);
36
37  // Called by X11DesktopHandler when |xid| is destroyed.
38  void OnWindowDestroyed(XID xid);
39
40 private:
41  friend struct DefaultSingletonTraits<XForeignWindowManager>;
42
43  struct Request {
44    Request(int request_id, long entry_event_mask);
45    ~Request();
46
47    int request_id;
48    long event_mask;
49  };
50
51  XForeignWindowManager();
52  ~XForeignWindowManager();
53
54  // Updates which events are selected on |xid|.
55  void UpdateSelectedEvents(XID xid);
56
57  // The id of the next request.
58  int next_request_id_;
59
60  typedef std::vector<Request> RequestVector;
61  std::map<XID, RequestVector> request_map_;
62
63  DISALLOW_COPY_AND_ASSIGN(XForeignWindowManager);
64};
65
66}  // namespace ui
67
68#endif  // UI_BASE_X_X11_FOREIGN_WINDOW_MANAGER_H_
69