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 CHROME_BROWSER_TASK_MANAGER_WEB_CONTENTS_INFORMATION_H_
6#define CHROME_BROWSER_TASK_MANAGER_WEB_CONTENTS_INFORMATION_H_
7
8#include "base/basictypes.h"
9#include "base/callback.h"
10#include "base/memory/scoped_ptr.h"
11#include "content/public/browser/notification_observer.h"
12#include "content/public/browser/notification_registrar.h"
13
14namespace content {
15class WebContents;
16}
17
18namespace task_manager {
19
20class RendererResource;
21
22// This interface gives a WebContentsResourceProvider information about a
23// category of WebContentses owned by some service. The instances are expected
24// to be owned by their WebContentsResourceProvider, which will track and adapt
25// the WebContentses into TaskManager resources.
26class WebContentsInformation {
27 public:
28  typedef base::Callback<void(content::WebContents*)> NewWebContentsCallback;
29
30  WebContentsInformation();
31  virtual ~WebContentsInformation();
32
33  // Retrieve all known WebContents instances from the service we're tracking.
34  // Invoke |callback| on each one.
35  virtual void GetAll(const NewWebContentsCallback& callback) = 0;
36
37  // Return true if |web_contents| is from the service we're tracking.
38  virtual bool CheckOwnership(content::WebContents* web_contents) = 0;
39
40  // Start listening to the creation of new WebContents instances from
41  // the service. While listening, invoke |callback| on each new instance.
42  virtual void StartObservingCreation(
43      const NewWebContentsCallback& callback) = 0;
44
45  // Stop listening to creation of new WebContents instances.
46  virtual void StopObservingCreation() = 0;
47
48  // Create a new task manager resource for the given WebContents instance.
49  virtual scoped_ptr<RendererResource> MakeResource(
50      content::WebContents* web_contents) = 0;
51
52 private:
53  DISALLOW_COPY_AND_ASSIGN(WebContentsInformation);
54};
55
56// Implements the observer methods (StartObservingCreation() /
57// StopObservingCreation()) of WebContentsInformation using
58// NOTIFICATION_WEB_CONTENTS_CONNECTED.
59class NotificationObservingWebContentsInformation
60    : public WebContentsInformation,
61      public content::NotificationObserver {
62 public:
63  NotificationObservingWebContentsInformation();
64  virtual ~NotificationObservingWebContentsInformation();
65
66  // WebContentsInformation partial implementation.
67  virtual void StartObservingCreation(const NewWebContentsCallback& callback)
68      OVERRIDE;
69  virtual void StopObservingCreation() OVERRIDE;
70
71  // content::NotificationObserver implementation.
72  virtual void Observe(int type,
73                       const content::NotificationSource& source,
74                       const content::NotificationDetails& details) OVERRIDE;
75
76 private:
77  content::NotificationRegistrar registrar_;
78  NewWebContentsCallback observer_callback_;
79  DISALLOW_COPY_AND_ASSIGN(NotificationObservingWebContentsInformation);
80};
81
82}  // namespace task_manager
83
84#endif  // CHROME_BROWSER_TASK_MANAGER_WEB_CONTENTS_INFORMATION_H_
85