1 // Copyright 2015 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_PROVIDERS_WEB_CONTENTS_WEB_CONTENTS_TASK_PROVIDER_H_
6 #define CHROME_BROWSER_TASK_MANAGER_PROVIDERS_WEB_CONTENTS_WEB_CONTENTS_TASK_PROVIDER_H_
7 
8 #include <map>
9 
10 #include "base/macros.h"
11 #include "chrome/browser/task_manager/providers/task_provider.h"
12 
13 namespace content {
14 class RenderFrameHost;
15 class WebContents;
16 }  // namespace content
17 
18 namespace task_manager {
19 
20 class WebContentsTag;
21 
22 // Defines a provider to provide the renderer tasks that are associated with
23 // various |WebContents| from various services.
24 // There should be no or only one instance of this class at any time.
25 class WebContentsTaskProvider : public TaskProvider {
26  public:
27   WebContentsTaskProvider();
28   ~WebContentsTaskProvider() override;
29 
30   // This will be called every time we're notified that a new |WebContentsTag|
31   // has been created.
32   void OnWebContentsTagCreated(const WebContentsTag* tag);
33 
34   // Manually remove |tag|'s corresponding Task.
35   void OnWebContentsTagRemoved(const WebContentsTag* tag);
36 
37   // task_manager::TaskProvider:
38   Task* GetTaskOfUrlRequest(int child_id, int route_id) override;
39 
40   // Checks if the given |web_contents| is tracked by the provider.
41   bool HasWebContents(content::WebContents* web_contents) const;
42 
43   // Returns the task, if any, of the provided frame.
44   Task* GetTaskOfFrame(content::RenderFrameHost* frame);
45 
46  private:
47   class WebContentsEntry;
48 
49   // task_manager::TaskProvider:
50   void StartUpdating() override;
51   void StopUpdating() override;
52 
53   // Called when the given |web_contents| are destroyed so that we can delete
54   // its associated entry.
55   void DeleteEntry(content::WebContents* web_contents);
56 
57   // A map to associate a |WebContents| with its corresponding entry that we
58   // create for it to be able to track it.
59   std::map<content::WebContents*, std::unique_ptr<WebContentsEntry>>
60       entries_map_;
61 
62   // True if this provider is listening to WebContentsTags and updating its
63   // observers, false otherwise.
64   bool is_updating_ = false;
65 
66   DISALLOW_COPY_AND_ASSIGN(WebContentsTaskProvider);
67 };
68 
69 }  // namespace task_manager
70 
71 #endif  // CHROME_BROWSER_TASK_MANAGER_PROVIDERS_WEB_CONTENTS_WEB_CONTENTS_TASK_PROVIDER_H_
72