OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/devtools/chrome_devtools_manager_delegate.h" | 5 #include "chrome/browser/devtools/chrome_devtools_manager_delegate.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "chrome/browser/devtools/device/android_device_manager.h" | 10 #include "chrome/browser/devtools/device/android_device_manager.h" |
11 #include "chrome/browser/devtools/device/tcp_device_provider.h" | 11 #include "chrome/browser/devtools/device/tcp_device_provider.h" |
12 #include "chrome/browser/devtools/devtools_network_protocol_handler.h" | 12 #include "chrome/browser/devtools/devtools_network_protocol_handler.h" |
13 #include "chrome/browser/devtools/devtools_protocol_constants.h" | 13 #include "chrome/browser/devtools/devtools_protocol_constants.h" |
14 #include "chrome/browser/devtools/devtools_window.h" | 14 #include "chrome/browser/devtools/devtools_window.h" |
15 #include "chrome/browser/extensions/extension_tab_util.h" | 15 #include "chrome/browser/extensions/extension_tab_util.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
18 #include "chrome/browser/ui/browser_navigator.h" | 18 #include "chrome/browser/ui/browser_navigator.h" |
19 #include "chrome/browser/ui/browser_navigator_params.h" | 19 #include "chrome/browser/ui/browser_navigator_params.h" |
20 #include "chrome/browser/ui/browser_window.h" | |
21 #include "chrome/browser/ui/exclusive_access/exclusive_access_context.h" | |
20 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 22 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
21 #include "chrome/grit/browser_resources.h" | 24 #include "chrome/grit/browser_resources.h" |
22 #include "components/guest_view/browser/guest_view_base.h" | 25 #include "components/guest_view/browser/guest_view_base.h" |
23 #include "content/public/browser/devtools_agent_host.h" | 26 #include "content/public/browser/devtools_agent_host.h" |
24 #include "content/public/browser/render_frame_host.h" | 27 #include "content/public/browser/render_frame_host.h" |
25 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
26 #include "extensions/browser/extension_host.h" | 29 #include "extensions/browser/extension_host.h" |
27 #include "extensions/browser/extension_registry.h" | 30 #include "extensions/browser/extension_registry.h" |
28 #include "extensions/browser/process_manager.h" | 31 #include "extensions/browser/process_manager.h" |
29 #include "ui/base/resource/resource_bundle.h" | 32 #include "ui/base/resource/resource_bundle.h" |
30 | 33 |
31 using content::DevToolsAgentHost; | 34 using content::DevToolsAgentHost; |
32 | 35 |
33 char ChromeDevToolsManagerDelegate::kTypeApp[] = "app"; | 36 char ChromeDevToolsManagerDelegate::kTypeApp[] = "app"; |
34 char ChromeDevToolsManagerDelegate::kTypeBackgroundPage[] = "background_page"; | 37 char ChromeDevToolsManagerDelegate::kTypeBackgroundPage[] = "background_page"; |
35 char ChromeDevToolsManagerDelegate::kTypeWebView[] = "webview"; | 38 char ChromeDevToolsManagerDelegate::kTypeWebView[] = "webview"; |
36 | 39 |
37 char kLocationsParam[] = "locations"; | 40 char kLocationsParam[] = "locations"; |
38 char kHostParam[] = "host"; | 41 char kHostParam[] = "host"; |
39 char kPortParam[] = "port"; | 42 char kPortParam[] = "port"; |
40 | 43 |
44 BrowserWindow* GetBrowserWindow(int window_id) { | |
dgozman
2017/03/22 21:28:34
Place all these functions into a single anonymous
jzfeng
2017/03/23 06:58:25
Done.
| |
45 for (auto* b : *BrowserList::GetInstance()) { | |
46 if (b->session_id().id() == window_id) | |
47 return b->window(); | |
48 } | |
49 return nullptr; | |
50 } | |
51 | |
52 base::DictionaryValue* GetWindowBounds(BrowserWindow* window) { | |
53 base::DictionaryValue* bounds_object = new base::DictionaryValue(); | |
54 gfx::Rect bounds; | |
55 if (window->IsMinimized()) | |
56 bounds = window->GetRestoredBounds(); | |
57 else | |
58 bounds = window->GetBounds(); | |
59 std::unique_ptr<base::DictionaryValue> result( | |
60 base::MakeUnique<base::DictionaryValue>()); | |
61 bounds_object->SetInteger("left", bounds.x()); | |
62 bounds_object->SetInteger("top", bounds.y()); | |
63 bounds_object->SetInteger("width", bounds.width()); | |
64 bounds_object->SetInteger("height", bounds.height()); | |
65 | |
66 std::string window_state = "normal"; | |
67 if (window->IsMinimized()) | |
68 window_state = "minimized"; | |
69 if (window->IsMaximized()) | |
70 window_state = "maximized"; | |
71 if (window->IsFullscreen()) | |
72 window_state = "fullscreen"; | |
73 bounds_object->SetString("windowState", window_state); | |
74 return bounds_object; | |
75 } | |
76 | |
77 std::unique_ptr<base::DictionaryValue> | |
78 GetWindowForTarget(int id, std::string method, base::DictionaryValue* params) { | |
79 std::string target_id; | |
80 if (!params->GetString("targetId", &target_id)) | |
81 return DevToolsProtocol::CreateInvalidParamsResponse(id, "targetId"); | |
82 | |
83 Browser* browser = nullptr; | |
84 scoped_refptr<DevToolsAgentHost> host = | |
85 DevToolsAgentHost::GetForId(target_id); | |
86 if (!host) | |
87 return DevToolsProtocol::CreateErrorResponse(id, "No target with given id"); | |
88 content::WebContents* web_contents = host->GetWebContents(); | |
89 if (!web_contents) | |
90 return DevToolsProtocol::CreateErrorResponse( | |
dgozman
2017/03/22 21:28:34
style: when |if| body takes more than one line, wr
jzfeng
2017/03/23 06:58:25
Thanks for the tip! Done.
| |
91 id, "No web contents in the target"); | |
92 for (auto* b : *BrowserList::GetInstance()) { | |
93 int tab_index = b->tab_strip_model()->GetIndexOfWebContents(web_contents); | |
94 if (tab_index != TabStripModel::kNoTab) | |
95 browser = b; | |
96 } | |
97 if (!browser) | |
98 return DevToolsProtocol::CreateErrorResponse(id, | |
99 "Browser window not found"); | |
100 | |
101 std::unique_ptr<base::DictionaryValue> result( | |
dgozman
2017/03/22 21:28:34
You don't have to use MakeUnique when you have a v
jzfeng
2017/03/23 06:58:25
The explanation is very helpful! Done.
| |
102 base::MakeUnique<base::DictionaryValue>()); | |
103 result->SetInteger("windowId", browser->session_id().id()); | |
104 result->Set("bounds", GetWindowBounds(browser->window())); | |
105 return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); | |
106 } | |
107 | |
108 std::unique_ptr<base::DictionaryValue> | |
109 GetWindowBounds(int id, std::string method, base::DictionaryValue* params) { | |
110 int window_id; | |
111 if (!params->GetInteger("windowId", &window_id)) | |
112 return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowId"); | |
113 BrowserWindow* window = GetBrowserWindow(window_id); | |
114 if (!window) | |
115 return DevToolsProtocol::CreateErrorResponse(id, | |
116 "Browser window not found"); | |
117 std::unique_ptr<base::DictionaryValue> result( | |
118 base::MakeUnique<base::DictionaryValue>()); | |
119 result->Set("bounds", GetWindowBounds(window)); | |
120 return DevToolsProtocol::CreateSuccessResponse(id, std::move(result)); | |
121 } | |
122 | |
123 std::unique_ptr<base::DictionaryValue> | |
124 SetWindowBounds(int id, std::string method, base::DictionaryValue* params) { | |
125 int window_id; | |
126 if (!params->GetInteger("windowId", &window_id)) | |
127 return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowId"); | |
128 BrowserWindow* window = GetBrowserWindow(window_id); | |
129 if (!window) | |
130 return DevToolsProtocol::CreateErrorResponse(id, | |
131 "Browser window not found"); | |
132 | |
133 base::Value* value = nullptr; | |
134 base::DictionaryValue* bounds_dict = nullptr; | |
135 if (!params->Get("bounds", &value) || !value->GetAsDictionary(&bounds_dict)) | |
136 return DevToolsProtocol::CreateInvalidParamsResponse(id, "bounds"); | |
137 | |
138 std::string window_state; | |
139 if (!bounds_dict->GetString("windowState", &window_state)) | |
140 window_state = "normal"; | |
141 else if (window_state != "normal" && window_state != "minimized" && | |
142 window_state != "maximized" && window_state != "fullscreen") | |
143 return DevToolsProtocol::CreateInvalidParamsResponse(id, "windowState"); | |
144 | |
145 if (window_state != "fullscreen" && window->IsFullscreen()) | |
146 window->GetExclusiveAccessContext()->ExitFullscreen(); | |
147 | |
148 if (window_state == "minimized") { | |
149 window->Minimize(); | |
150 } else if (window_state == "maximized") { | |
151 window->Maximize(); | |
152 } else if (window_state == "fullscreen") { | |
153 if (window->IsMinimized() || window->IsMaximized()) | |
154 window->Restore(); | |
155 window->GetExclusiveAccessContext()->EnterFullscreen( | |
156 GURL(), EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE); | |
157 } else { | |
158 window->Restore(); | |
dgozman
2017/03/22 21:28:34
Is it ok to call Restore when not maximized nor mi
jzfeng
2017/03/23 06:58:25
Changed to restore only when the window is maximiz
| |
159 } | |
160 | |
161 bool set_bounds = false; | |
162 gfx::Rect bounds; | |
163 if (window->IsMinimized()) | |
dgozman
2017/03/22 21:28:34
|| window->IsMaximized() ?
jzfeng
2017/03/23 06:58:25
Done.
| |
164 bounds = window->GetRestoredBounds(); | |
165 else | |
166 bounds = window->GetBounds(); | |
167 | |
168 int left, top, width, height; | |
169 if (bounds_dict->GetInteger("left", &left)) { | |
170 bounds.set_x(left); | |
171 set_bounds = true; | |
172 } | |
173 if (bounds_dict->GetInteger("top", &top)) { | |
174 bounds.set_y(top); | |
175 set_bounds = true; | |
176 } | |
177 if (bounds_dict->GetInteger("width", &width)) { | |
178 if (width < 0) | |
179 return DevToolsProtocol::CreateInvalidParamsResponse(id, "width"); | |
180 bounds.set_width(width); | |
181 set_bounds = true; | |
182 } | |
183 if (bounds_dict->GetInteger("height", &height)) { | |
184 if (height < 0) | |
185 return DevToolsProtocol::CreateInvalidParamsResponse(id, "height"); | |
186 bounds.set_height(height); | |
187 set_bounds = true; | |
188 } | |
189 | |
190 if (set_bounds && window_state != "normal") | |
191 return DevToolsProtocol::CreateErrorResponse( | |
192 id, | |
193 "The 'minimized', 'maximized' and 'fullscreen' states cannot be " | |
194 "combined with 'left', 'top', 'width' or 'height'"); | |
195 | |
196 if (set_bounds) | |
197 window->SetBounds(bounds); | |
198 | |
199 return DevToolsProtocol::CreateSuccessResponse(id, nullptr); | |
200 } | |
201 | |
202 std::unique_ptr<base::DictionaryValue> HandleBrowserCommand( | |
203 int id, | |
204 std::string method, | |
205 base::DictionaryValue* params) { | |
206 if (method == chrome::devtools::Browser::getWindowForTarget::kName) | |
207 return GetWindowForTarget(id, method, params); | |
208 if (method == chrome::devtools::Browser::getWindowBounds::kName) | |
209 return GetWindowBounds(id, method, params); | |
210 if (method == chrome::devtools::Browser::setWindowBounds::kName) | |
211 return SetWindowBounds(id, method, params); | |
212 return nullptr; | |
213 } | |
214 | |
41 class ChromeDevToolsManagerDelegate::HostData { | 215 class ChromeDevToolsManagerDelegate::HostData { |
42 public: | 216 public: |
43 HostData() {} | 217 HostData() {} |
44 ~HostData() {} | 218 ~HostData() {} |
45 | 219 |
46 RemoteLocations& remote_locations() { return remote_locations_; } | 220 RemoteLocations& remote_locations() { return remote_locations_; } |
47 | 221 |
48 void set_remote_locations(RemoteLocations& locations) { | 222 void set_remote_locations(RemoteLocations& locations) { |
49 remote_locations_.swap(locations); | 223 remote_locations_.swap(locations); |
50 } | 224 } |
(...skipping 19 matching lines...) Expand all Loading... | |
70 base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( | 244 base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( |
71 DevToolsAgentHost* agent_host, | 245 DevToolsAgentHost* agent_host, |
72 base::DictionaryValue* command_dict) { | 246 base::DictionaryValue* command_dict) { |
73 | 247 |
74 int id = 0; | 248 int id = 0; |
75 std::string method; | 249 std::string method; |
76 base::DictionaryValue* params = nullptr; | 250 base::DictionaryValue* params = nullptr; |
77 if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, ¶ms)) | 251 if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, ¶ms)) |
78 return nullptr; | 252 return nullptr; |
79 | 253 |
254 if (method.find("Browser.") == 0) | |
255 return HandleBrowserCommand(id, method, params).release(); | |
dgozman
2017/03/22 21:28:34
Let's only do this if |agent_host->GetType() == De
jzfeng
2017/03/23 06:58:24
Done.
| |
256 | |
80 if (method == chrome::devtools::Target::setRemoteLocations::kName) | 257 if (method == chrome::devtools::Target::setRemoteLocations::kName) |
81 return SetRemoteLocations(agent_host, id, params).release(); | 258 return SetRemoteLocations(agent_host, id, params).release(); |
82 | 259 |
83 return network_protocol_handler_->HandleCommand(agent_host, command_dict); | 260 return network_protocol_handler_->HandleCommand(agent_host, command_dict); |
84 } | 261 } |
85 | 262 |
86 std::string ChromeDevToolsManagerDelegate::GetTargetType( | 263 std::string ChromeDevToolsManagerDelegate::GetTargetType( |
87 content::RenderFrameHost* host) { | 264 content::RenderFrameHost* host) { |
88 content::WebContents* web_contents = | 265 content::WebContents* web_contents = |
89 content::WebContents::FromRenderFrameHost(host); | 266 content::WebContents::FromRenderFrameHost(host); |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
273 tcp_locations.insert(net::HostPortPair(host, port)); | 450 tcp_locations.insert(net::HostPortPair(host, port)); |
274 } | 451 } |
275 | 452 |
276 host_data_[agent_host]->set_remote_locations(tcp_locations); | 453 host_data_[agent_host]->set_remote_locations(tcp_locations); |
277 UpdateDeviceDiscovery(); | 454 UpdateDeviceDiscovery(); |
278 | 455 |
279 std::unique_ptr<base::DictionaryValue> result( | 456 std::unique_ptr<base::DictionaryValue> result( |
280 base::MakeUnique<base::DictionaryValue>()); | 457 base::MakeUnique<base::DictionaryValue>()); |
281 return DevToolsProtocol::CreateSuccessResponse(command_id, std::move(result)); | 458 return DevToolsProtocol::CreateSuccessResponse(command_id, std::move(result)); |
282 } | 459 } |
OLD | NEW |