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_PROCESSES_API_H__
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESSES_API_H__
7#pragma once
8
9#include <set>
10#include <string>
11
12#include "chrome/browser/extensions/extension_function.h"
13#include "chrome/browser/task_manager/task_manager.h"
14#include "content/common/notification_registrar.h"
15
16// Observes the Task Manager and routes the notifications as events to the
17// extension system.
18class ExtensionProcessesEventRouter : public TaskManagerModelObserver {
19 public:
20  // Single instance of the event router.
21  static ExtensionProcessesEventRouter* GetInstance();
22
23  // Safe to call multiple times.
24  void ObserveProfile(Profile* profile);
25
26  // Called when an extension process wants to listen to process events.
27  void ListenerAdded();
28
29  // Called when an extension process with a listener exits or removes it.
30  void ListenerRemoved();
31
32 private:
33  friend struct DefaultSingletonTraits<ExtensionProcessesEventRouter>;
34
35  ExtensionProcessesEventRouter();
36  virtual ~ExtensionProcessesEventRouter();
37
38  // TaskManagerModelObserver methods.
39  virtual void OnModelChanged() {}
40  virtual void OnItemsChanged(int start, int length);
41  virtual void OnItemsAdded(int start, int length) {}
42  virtual void OnItemsRemoved(int start, int length) {}
43
44  void DispatchEvent(Profile* profile,
45                     const char* event_name,
46                     const std::string& json_args);
47
48  // Used for tracking registrations to process related notifications.
49  NotificationRegistrar registrar_;
50
51  // Registered profiles.
52  typedef std::set<Profile*> ProfileSet;
53  ProfileSet profiles_;
54
55  // TaskManager to observe for updates.
56  TaskManagerModel* model_;
57
58  DISALLOW_COPY_AND_ASSIGN(ExtensionProcessesEventRouter);
59};
60
61
62// This extension function returns the Process object for the renderer process
63// currently in use by the specified Tab.
64class GetProcessIdForTabFunction : public SyncExtensionFunction {
65  virtual ~GetProcessIdForTabFunction() {}
66  virtual bool RunImpl();
67  DECLARE_EXTENSION_FUNCTION_NAME("experimental.processes.getProcessIdForTab")
68};
69
70#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESSES_API_H__
71