summaryrefslogtreecommitdiffstats
path: root/chrome/browser/task_manager/web_contents_resource_provider.h
blob: b07e2119b83eaba4a933d22133d038b4bb449f85 (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
// Copyright 2014 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 CHROME_BROWSER_TASK_MANAGER_WEB_CONTENTS_RESOURCE_PROVIDER_H_
#define CHROME_BROWSER_TASK_MANAGER_WEB_CONTENTS_RESOURCE_PROVIDER_H_

#include <map>
#include <set>

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/task_manager/renderer_resource.h"
#include "chrome/browser/task_manager/resource_provider.h"

class TaskManager;

namespace content {
class WebContents;
}

namespace task_manager {

class RendererResource;
class TaskManagerWebContentsObserver;
class WebContentsInformation;

// Provides resources to the task manager on behalf of a chrome service that
// owns WebContentses. The chrome service is parameterized as a
// WebContentsInformation, which provides a list of WebContentses to track.
//
// This ResourceProvider is instantiated several times by the task manager, each
// with a different implementation of WebContentsInformation.
class WebContentsResourceProvider : public ResourceProvider {
 public:
  WebContentsResourceProvider(TaskManager* task_manager,
                              scoped_ptr<WebContentsInformation> info);

  // ResourceProvider implementation.
  virtual RendererResource* GetResource(int origin_pid,
                                        int child_id,
                                        int route_id) OVERRIDE;
  virtual void StartUpdating() OVERRIDE;
  virtual void StopUpdating() OVERRIDE;

  // Start observing |web_contents| for changes via WebContentsObserver, and
  // add it to the task manager.
  void OnWebContentsCreated(content::WebContents* web_contents);

  // Create TaskManager resources for |web_contents|, and add them to the
  // TaskManager.
  bool AddToTaskManager(content::WebContents* web_contents);

  // Remove the task manager resources associated with |web_contents|.
  void RemoveFromTaskManager(content::WebContents* web_contents);

  // Remove a WebContentsObserver from our tracking list, and delete it.
  void DeleteObserver(TaskManagerWebContentsObserver* observer);

 protected:
  virtual ~WebContentsResourceProvider();

 private:
  // Whether we are currently reporting to the task manager. Used to ignore
  // notifications sent after StopUpdating().
  bool updating_;

  TaskManager* task_manager_;

  // Maps the actual resources (the WebContentses) to the TaskManager
  // resources. The RendererResources are owned by us and registered with
  // the TaskManager.
  std::map<content::WebContents*, RendererResource*> resources_;

  // Set of current active WebContentsObserver instances owned by this class.
  std::set<TaskManagerWebContentsObserver*> web_contents_observers_;

  // The WebContentsInformation that informs us when a new WebContents* is
  // created, and which serves as a RendererResource factory for our type.
  scoped_ptr<WebContentsInformation> info_;

  DISALLOW_COPY_AND_ASSIGN(WebContentsResourceProvider);
};

}  // namespace task_manager

#endif  // CHROME_BROWSER_TASK_MANAGER_WEB_CONTENTS_RESOURCE_PROVIDER_H_