OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 #include "content/browser/devtools/protocol/browser_handler.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/bind.h" | |
10 #include "content/browser/devtools/devtools_manager.h" | |
11 #include "content/public/browser/devtools_manager_delegate.h" | |
12 | |
13 namespace content { | |
14 namespace devtools { | |
15 namespace browser { | |
16 | |
17 using Response = DevToolsProtocolClient::Response; | |
18 | |
19 BrowserHandler::BrowserHandler() | |
20 : weak_factory_(this) { | |
21 } | |
22 | |
23 BrowserHandler::~BrowserHandler() { | |
24 } | |
25 | |
26 void BrowserHandler::SetClient(std::unique_ptr<Client> client) { | |
27 client_.swap(client); | |
28 } | |
29 | |
30 void BrowserHandler::Detached() { | |
31 for (const auto& host : attached_hosts_) | |
32 host->DetachClient(this); | |
33 attached_hosts_.clear(); | |
34 } | |
35 | |
36 Response BrowserHandler::CreateBrowserContext(std::string* out_context_id) { | |
37 // For layering reasons this needs to be handled by | |
38 // DevToolsManagerDelegate::HandleCommand. | |
39 return Response::ServerError("Not supported"); | |
40 } | |
41 | |
42 Response BrowserHandler::DisposeBrowserContext(const std::string& context_id, | |
43 bool* out_success) { | |
44 // For layering reasons this needs to be handled by | |
45 // DevToolsManagerDelegate::HandleCommand. | |
46 return Response::ServerError("Not supported"); | |
47 } | |
48 | |
49 Response BrowserHandler::CreateTarget(const std::string& url, | |
50 const int* width, | |
51 const int* height, | |
52 const std::string* context_id, | |
53 std::string* out_target_id) { | |
54 DevToolsManagerDelegate* delegate = | |
55 DevToolsManager::GetInstance()->delegate(); | |
56 if (!delegate) | |
57 return Response::ServerError("Not supported"); | |
58 scoped_refptr<content::DevToolsAgentHost> agent_host = | |
59 delegate->CreateNewTarget(GURL(url)); | |
60 if (!agent_host) | |
61 return Response::ServerError("Not supported"); | |
62 *out_target_id = agent_host->GetId(); | |
63 return Response::OK(); | |
64 } | |
65 | |
66 Response BrowserHandler::CloseTarget(const std::string& target_id, | |
67 bool* out_success) { | |
68 scoped_refptr<DevToolsAgentHost> agent_host = | |
69 DevToolsAgentHost::GetForId(target_id); | |
70 if (!agent_host) | |
71 return Response::ServerError("No target with given id found"); | |
72 *out_success = agent_host->Close(); | |
73 return Response::OK(); | |
74 } | |
75 | |
76 Response BrowserHandler::GetTargets(DevToolsCommandId command_id) { | |
77 DevToolsAgentHost::DiscoverAllHosts( | |
78 base::Bind(&BrowserHandler::RespondToGetTargets, | |
79 weak_factory_.GetWeakPtr(), | |
80 command_id)); | |
81 return Response::OK(); | |
82 } | |
83 | |
84 void BrowserHandler::RespondToGetTargets( | |
85 DevToolsCommandId command_id, | |
86 DevToolsAgentHost::List agents) { | |
87 std::vector<scoped_refptr<devtools::browser::TargetInfo>> infos; | |
88 for (const auto& agent_host : agents) { | |
89 scoped_refptr<devtools::browser::TargetInfo> info = | |
90 devtools::browser::TargetInfo::Create()-> | |
91 set_target_id(agent_host->GetId())-> | |
92 set_type(agent_host->GetType())-> | |
93 set_title(agent_host->GetTitle())-> | |
94 set_url(agent_host->GetURL().spec()); | |
95 infos.push_back(info); | |
96 } | |
97 client_->SendGetTargetsResponse( | |
98 command_id, | |
99 GetTargetsResponse::Create()->set_target_info(std::move(infos))); | |
100 } | |
101 | |
102 Response BrowserHandler::Attach(DevToolsCommandId command_id, | |
103 const std::string& target_id) { | |
104 // Discover in order to get ahold of the items. | |
105 DevToolsAgentHost::DiscoverAllHosts( | |
106 base::Bind(&BrowserHandler::RespondToAttach, | |
107 weak_factory_.GetWeakPtr(), command_id, target_id)); | |
108 return Response::OK(); | |
109 } | |
110 | |
111 void BrowserHandler::RespondToAttach(DevToolsCommandId command_id, | |
112 const std::string& target_id, | |
113 DevToolsAgentHost::List agents) { | |
114 // We were discovering to get ahold of the items, discard them. | |
115 scoped_refptr<DevToolsAgentHost> agent_host = | |
116 DevToolsAgentHost::GetForId(target_id); | |
117 bool success = false; | |
118 if (agent_host) | |
119 success = agent_host->AttachClient(this); | |
120 attached_hosts_.push_back(agent_host); | |
121 client_->SendAttachResponse( | |
122 command_id, | |
123 AttachResponse::Create()->set_success(success)); | |
124 } | |
125 | |
126 Response BrowserHandler::Detach(const std::string& target_id, | |
127 bool* out_success) { | |
128 scoped_refptr<DevToolsAgentHost> agent_host = | |
129 DevToolsAgentHost::GetForId(target_id); | |
130 auto it = std::find( | |
131 attached_hosts_.begin(), attached_hosts_.end(), agent_host); | |
132 if (it != attached_hosts_.end()) | |
133 attached_hosts_.erase(it); | |
134 *out_success = agent_host && agent_host->DetachClient(this); | |
135 return Response::OK(); | |
136 } | |
137 | |
138 Response BrowserHandler::SendMessage(const std::string& target_id, | |
139 const std::string& message) { | |
140 scoped_refptr<DevToolsAgentHost> agent_host = | |
141 DevToolsAgentHost::GetForId(target_id); | |
142 if (!agent_host) | |
143 return Response::ServerError("No target with given id found"); | |
144 agent_host->DispatchProtocolMessage(this, message); | |
145 return Response::OK(); | |
146 } | |
147 | |
148 void BrowserHandler::DispatchProtocolMessage( | |
149 DevToolsAgentHost* agent_host, const std::string& message) { | |
150 client_->DispatchMessage(DispatchMessageParams::Create()-> | |
151 set_target_id(agent_host->GetId())-> | |
152 set_message(message)); | |
153 } | |
154 | |
155 void BrowserHandler::AgentHostClosed(DevToolsAgentHost* agent_host, | |
156 bool replaced_with_another_client) { | |
157 auto it = std::find( | |
158 attached_hosts_.begin(), attached_hosts_.end(), agent_host); | |
159 if (it != attached_hosts_.end()) | |
160 attached_hosts_.erase(it); | |
161 } | |
162 | |
163 } // namespace browser | |
164 } // namespace devtools | |
165 } // namespace content | |
OLD | NEW |