ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / content / browser / devtools / devtools_manager.cc
blobc30d3e981ee81ff7667069e1a7c4ec9f28b330b3
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"
7 #include "base/bind.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"
15 namespace content {
17 namespace {
19 int kObserverThrottleInterval = 500; // ms
21 } // namespace
23 // static
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) {
52 bool was_attached =
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(
57 BrowserThread::IO,
58 FROM_HERE,
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(
66 BrowserThread::IO,
67 FROM_HERE,
68 base::Bind(&DevToolsNetLogObserver::Detach));
72 UpdateTargetList();
75 void DevToolsManager::AddObserver(Observer* observer) {
76 observer_list_.AddObserver(observer);
77 UpdateTargetList();
80 void DevToolsManager::RemoveObserver(Observer* observer) {
81 observer_list_.RemoveObserver(observer);
84 void DevToolsManager::UpdateTargetList() {
85 if (!observer_list_.might_have_observers())
86 return;
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;
96 return;
99 update_target_list_scheduled_ = true;
100 if (scheduler_.is_null()) {
101 base::MessageLoop::current()->PostDelayedTask(
102 FROM_HERE,
103 update_target_list_callback_.callback(),
104 base::TimeDelta::FromMilliseconds(kObserverThrottleInterval));
105 } else {
106 scheduler_.Run(update_target_list_callback_.callback());
109 update_target_list_required_ = false;
110 if (!delegate_) {
111 Observer::TargetList empty_list;
112 NotifyTargetListChanged(empty_list);
113 return;
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