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_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_
7#pragma once
8
9#include <map>
10#include <set>
11#include <string>
12
13#include "base/memory/ref_counted.h"
14#include "content/common/notification_observer.h"
15#include "content/common/notification_registrar.h"
16
17class GURL;
18class Extension;
19class ExtensionDevToolsManager;
20class Profile;
21class RenderProcessHost;
22
23class ExtensionEventRouter : public NotificationObserver {
24 public:
25  // Returns true if the given extension can see events and data from another
26  // sub-profile (incognito to original profile, or vice versa).
27  static bool CanCrossIncognito(Profile* profile,
28                                const std::string& extension_id);
29  static bool CanCrossIncognito(Profile* profile, const Extension* extension);
30
31  explicit ExtensionEventRouter(Profile* profile);
32  ~ExtensionEventRouter();
33
34  // Add or remove the process/extension pair as a listener for |event_name|.
35  // Note that multiple extensions can share a process due to process
36  // collapsing. Also, a single extension can have 2 processes if it is a split
37  // mode extension.
38  void AddEventListener(const std::string& event_name,
39                        RenderProcessHost* process,
40                        const std::string& extension_id);
41  void RemoveEventListener(const std::string& event_name,
42                           RenderProcessHost* process,
43                           const std::string& extension_id);
44
45  // Returns true if there is at least one listener for the given event.
46  bool HasEventListener(const std::string& event_name);
47
48  // Returns true if the extension is listening to the given event.
49  bool ExtensionHasEventListener(const std::string& extension_id,
50                                 const std::string& event_name);
51
52  // Send an event to every registered extension renderer. If
53  // |restrict_to_profile| is non-NULL, then the event will not be sent to other
54  // profiles unless the extension has permission (e.g. incognito tab update ->
55  // normal profile only works if extension is allowed incognito access). If
56  // |event_url| is not empty, the event is only sent to extension with host
57  // permissions for this url.
58  void DispatchEventToRenderers(
59      const std::string& event_name, const std::string& event_args,
60      Profile* restrict_to_profile, const GURL& event_url);
61
62  // Same as above, except only send the event to the given extension.
63  void DispatchEventToExtension(
64      const std::string& extension_id,
65      const std::string& event_name, const std::string& event_args,
66      Profile* restrict_to_profile, const GURL& event_url);
67
68 protected:
69  // Shared by DispatchEvent*. If |extension_id| is empty, the event is
70  // broadcast.
71  virtual void DispatchEventImpl(
72      const std::string& extension_id,
73      const std::string& event_name, const std::string& event_args,
74      Profile* restrict_to_profile, const GURL& event_url);
75
76 private:
77  // An extension listening to an event.
78  struct EventListener;
79
80  virtual void Observe(NotificationType type,
81                       const NotificationSource& source,
82                       const NotificationDetails& details);
83
84  Profile* profile_;
85
86  NotificationRegistrar registrar_;
87
88  scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_;
89
90  // A map between an event name and a set of extensions that are listening
91  // to that event.
92  typedef std::map<std::string, std::set<EventListener> > ListenerMap;
93  ListenerMap listeners_;
94
95  DISALLOW_COPY_AND_ASSIGN(ExtensionEventRouter);
96};
97
98#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_
99