1 // Copyright (c) 2012 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.
5 #include "content/browser/devtools/devtools_manager.h"
8 #include "base/message_loop/message_loop.h"
9 #include "content/browser/devtools/devtools_agent_host_impl.h"
10 #include "content/browser/devtools/devtools_netlog_observer.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/content_browser_client.h"
13 #include "content/public/browser/devtools_target.h"
19 int kObserverThrottleInterval
= 500; // ms
24 DevToolsManager
* DevToolsManager::GetInstance() {
25 return Singleton
<DevToolsManager
>::get();
28 DevToolsManager::DevToolsManager()
29 : delegate_(GetContentClient()->browser()->GetDevToolsManagerDelegate()),
30 update_target_list_required_(false),
31 update_target_list_scheduled_(false),
32 update_target_list_callback_(base::Bind(
33 &DevToolsManager::UpdateTargetListThrottled
,
34 base::Unretained(this))) {
37 DevToolsManager::~DevToolsManager() {
38 DCHECK(!attached_hosts_
.size());
39 update_target_list_callback_
.Cancel();
42 void DevToolsManager::RenderViewCreated(
43 WebContents
* web_contents
, RenderViewHost
* rvh
) {
44 if (observer_list_
.might_have_observers()) {
45 // Force agent host creation.
46 DevToolsAgentHost::GetOrCreateFor(web_contents
);
50 void DevToolsManager::AgentHostChanged(
51 scoped_refptr
<DevToolsAgentHost
> agent_host
) {
53 attached_hosts_
.find(agent_host
.get()) != attached_hosts_
.end();
54 if (agent_host
->IsAttached() && !was_attached
) {
55 if (!attached_hosts_
.size()) {
56 BrowserThread::PostTask(
59 base::Bind(&DevToolsNetLogObserver::Attach
));
61 attached_hosts_
.insert(agent_host
.get());
62 } else if (!agent_host
->IsAttached() && was_attached
) {
63 attached_hosts_
.erase(agent_host
.get());
64 if (!attached_hosts_
.size()) {
65 BrowserThread::PostTask(
68 base::Bind(&DevToolsNetLogObserver::Detach
));
75 void DevToolsManager::AddObserver(Observer
* observer
) {
76 observer_list_
.AddObserver(observer
);
80 void DevToolsManager::RemoveObserver(Observer
* observer
) {
81 observer_list_
.RemoveObserver(observer
);
84 void DevToolsManager::UpdateTargetList() {
85 if (!observer_list_
.might_have_observers())
88 update_target_list_required_
= true;
89 if (!update_target_list_scheduled_
)
90 UpdateTargetListThrottled();
93 void DevToolsManager::UpdateTargetListThrottled() {
94 if (!update_target_list_required_
) {
95 update_target_list_scheduled_
= false;
99 update_target_list_scheduled_
= true;
100 if (scheduler_
.is_null()) {
101 base::MessageLoop::current()->PostDelayedTask(
103 update_target_list_callback_
.callback(),
104 base::TimeDelta::FromMilliseconds(kObserverThrottleInterval
));
106 scheduler_
.Run(update_target_list_callback_
.callback());
109 update_target_list_required_
= false;
111 Observer::TargetList empty_list
;
112 NotifyTargetListChanged(empty_list
);
115 delegate_
->EnumerateTargets(base::Bind(
116 &DevToolsManager::NotifyTargetListChanged
,
117 base::Unretained(this)));
120 void DevToolsManager::NotifyTargetListChanged(
121 const Observer::TargetList
& targets
) {
122 FOR_EACH_OBSERVER(Observer
, observer_list_
, TargetListChanged(targets
));
123 STLDeleteContainerPointers(targets
.begin(), targets
.end());
126 void DevToolsManager::SetUpForTest(Scheduler scheduler
) {
127 scheduler_
= scheduler
;
128 delegate_
.reset(GetContentClient()->browser()->GetDevToolsManagerDelegate());
131 } // namespace content