Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / chrome / test / chromedriver / window_commands.cc
blob9a3c40512fa32d40afa8c6e46dcc08131266fa7d
1 // Copyright (c) 2013 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 "chrome/test/chromedriver/window_commands.h"
7 #include <list>
8 #include <string>
10 #include "base/callback.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/threading/platform_thread.h"
14 #include "base/time/time.h"
15 #include "base/values.h"
16 #include "chrome/test/chromedriver/basic_types.h"
17 #include "chrome/test/chromedriver/chrome/automation_extension.h"
18 #include "chrome/test/chromedriver/chrome/chrome.h"
19 #include "chrome/test/chromedriver/chrome/chrome_desktop_impl.h"
20 #include "chrome/test/chromedriver/chrome/devtools_client.h"
21 #include "chrome/test/chromedriver/chrome/geoposition.h"
22 #include "chrome/test/chromedriver/chrome/javascript_dialog_manager.h"
23 #include "chrome/test/chromedriver/chrome/js.h"
24 #include "chrome/test/chromedriver/chrome/status.h"
25 #include "chrome/test/chromedriver/chrome/ui_events.h"
26 #include "chrome/test/chromedriver/chrome/web_view.h"
27 #include "chrome/test/chromedriver/element_util.h"
28 #include "chrome/test/chromedriver/session.h"
29 #include "chrome/test/chromedriver/util.h"
31 namespace {
33 Status GetMouseButton(const base::DictionaryValue& params,
34 MouseButton* button) {
35 int button_num;
36 if (!params.GetInteger("button", &button_num)) {
37 button_num = 0; // Default to left mouse button.
38 } else if (button_num < 0 || button_num > 2) {
39 return Status(kUnknownError,
40 base::StringPrintf("invalid button: %d", button_num));
42 *button = static_cast<MouseButton>(button_num);
43 return Status(kOk);
46 Status GetUrl(WebView* web_view, const std::string& frame, std::string* url) {
47 scoped_ptr<base::Value> value;
48 base::ListValue args;
49 Status status = web_view->CallFunction(
50 frame, "function() { return document.URL; }", args, &value);
51 if (status.IsError())
52 return status;
53 if (!value->GetAsString(url))
54 return Status(kUnknownError, "javascript failed to return the url");
55 return Status(kOk);
58 struct Cookie {
59 Cookie(const std::string& name,
60 const std::string& value,
61 const std::string& domain,
62 const std::string& path,
63 double expiry,
64 bool secure,
65 bool session)
66 : name(name), value(value), domain(domain), path(path), expiry(expiry),
67 secure(secure), session(session) {}
69 std::string name;
70 std::string value;
71 std::string domain;
72 std::string path;
73 double expiry;
74 bool secure;
75 bool session;
78 base::DictionaryValue* CreateDictionaryFrom(const Cookie& cookie) {
79 base::DictionaryValue* dict = new base::DictionaryValue();
80 dict->SetString("name", cookie.name);
81 dict->SetString("value", cookie.value);
82 if (!cookie.domain.empty())
83 dict->SetString("domain", cookie.domain);
84 if (!cookie.path.empty())
85 dict->SetString("path", cookie.path);
86 if (!cookie.session)
87 dict->SetDouble("expiry", cookie.expiry);
88 dict->SetBoolean("secure", cookie.secure);
89 return dict;
92 Status GetVisibleCookies(WebView* web_view,
93 std::list<Cookie>* cookies) {
94 scoped_ptr<base::ListValue> internal_cookies;
95 Status status = web_view->GetCookies(&internal_cookies);
96 if (status.IsError())
97 return status;
98 std::list<Cookie> cookies_tmp;
99 for (size_t i = 0; i < internal_cookies->GetSize(); ++i) {
100 base::DictionaryValue* cookie_dict;
101 if (!internal_cookies->GetDictionary(i, &cookie_dict))
102 return Status(kUnknownError, "DevTools returns a non-dictionary cookie");
104 std::string name;
105 cookie_dict->GetString("name", &name);
106 std::string value;
107 cookie_dict->GetString("value", &value);
108 std::string domain;
109 cookie_dict->GetString("domain", &domain);
110 std::string path;
111 cookie_dict->GetString("path", &path);
112 double expiry = 0;
113 cookie_dict->GetDouble("expires", &expiry);
114 expiry /= 1000; // Convert from millisecond to second.
115 bool session = false;
116 cookie_dict->GetBoolean("session", &session);
117 bool secure = false;
118 cookie_dict->GetBoolean("secure", &secure);
120 cookies_tmp.push_back(
121 Cookie(name, value, domain, path, expiry, secure, session));
123 cookies->swap(cookies_tmp);
124 return Status(kOk);
127 Status ScrollCoordinateInToView(
128 Session* session, WebView* web_view, int x, int y, int* offset_x,
129 int* offset_y) {
130 scoped_ptr<base::Value> value;
131 base::ListValue args;
132 args.AppendInteger(x);
133 args.AppendInteger(y);
134 Status status = web_view->CallFunction(
135 std::string(),
136 "function(x, y) {"
137 " if (x < window.pageXOffset ||"
138 " x >= window.pageXOffset + window.innerWidth ||"
139 " y < window.pageYOffset ||"
140 " y >= window.pageYOffset + window.innerHeight) {"
141 " window.scrollTo(x - window.innerWidth/2, y - window.innerHeight/2);"
142 " }"
143 " return {"
144 " view_x: Math.floor(window.pageXOffset),"
145 " view_y: Math.floor(window.pageYOffset),"
146 " view_width: Math.floor(window.innerWidth),"
147 " view_height: Math.floor(window.innerHeight)};"
148 "}",
149 args,
150 &value);
151 if (!status.IsOk())
152 return status;
153 base::DictionaryValue* view_attrib;
154 value->GetAsDictionary(&view_attrib);
155 int view_x, view_y, view_width, view_height;
156 view_attrib->GetInteger("view_x", &view_x);
157 view_attrib->GetInteger("view_y", &view_y);
158 view_attrib->GetInteger("view_width", &view_width);
159 view_attrib->GetInteger("view_height", &view_height);
160 *offset_x = x - view_x;
161 *offset_y = y - view_y;
162 if (*offset_x < 0 || *offset_x >= view_width || *offset_y < 0 ||
163 *offset_y >= view_height)
164 return Status(kUnknownError, "Failed to scroll coordinate into view");
165 return Status(kOk);
168 Status ExecuteTouchEvent(
169 Session* session, WebView* web_view, TouchEventType type,
170 const base::DictionaryValue& params) {
171 int x, y;
172 if (!params.GetInteger("x", &x))
173 return Status(kUnknownError, "'x' must be an integer");
174 if (!params.GetInteger("y", &y))
175 return Status(kUnknownError, "'y' must be an integer");
176 int relative_x = x;
177 int relative_y = y;
178 Status status = ScrollCoordinateInToView(
179 session, web_view, x, y, &relative_x, &relative_y);
180 if (!status.IsOk())
181 return status;
182 std::list<TouchEvent> events;
183 events.push_back(
184 TouchEvent(type, relative_x, relative_y));
185 return web_view->DispatchTouchEvents(events);
188 } // namespace
190 Status ExecuteWindowCommand(
191 const WindowCommand& command,
192 Session* session,
193 const base::DictionaryValue& params,
194 scoped_ptr<base::Value>* value) {
195 WebView* web_view = NULL;
196 Status status = session->GetTargetWindow(&web_view);
197 if (status.IsError())
198 return status;
200 status = web_view->ConnectIfNecessary();
201 if (status.IsError())
202 return status;
204 status = web_view->HandleReceivedEvents();
205 if (status.IsError())
206 return status;
208 if (web_view->GetJavaScriptDialogManager()->IsDialogOpen())
209 return Status(kUnexpectedAlertOpen);
211 Status nav_status(kOk);
212 for (int attempt = 0; attempt < 2; attempt++) {
213 if (attempt == 1) {
214 if (status.code() == kNoSuchExecutionContext)
215 // Switch to main frame and retry command if subframe no longer exists.
216 session->SwitchToTopFrame();
217 else
218 break;
220 nav_status = web_view->WaitForPendingNavigations(
221 session->GetCurrentFrameId(), session->page_load_timeout, true);
222 if (nav_status.IsError())
223 return nav_status;
225 status = command.Run(session, web_view, params, value);
228 nav_status = web_view->WaitForPendingNavigations(
229 session->GetCurrentFrameId(), session->page_load_timeout, true);
231 if (status.IsOk() && nav_status.IsError() &&
232 nav_status.code() != kUnexpectedAlertOpen)
233 return nav_status;
234 if (status.code() == kUnexpectedAlertOpen)
235 return Status(kOk);
236 return status;
239 Status ExecuteGet(
240 Session* session,
241 WebView* web_view,
242 const base::DictionaryValue& params,
243 scoped_ptr<base::Value>* value) {
244 std::string url;
245 if (!params.GetString("url", &url))
246 return Status(kUnknownError, "'url' must be a string");
247 return web_view->Load(url);
250 Status ExecuteExecuteScript(
251 Session* session,
252 WebView* web_view,
253 const base::DictionaryValue& params,
254 scoped_ptr<base::Value>* value) {
255 std::string script;
256 if (!params.GetString("script", &script))
257 return Status(kUnknownError, "'script' must be a string");
258 if (script == ":takeHeapSnapshot") {
259 return web_view->TakeHeapSnapshot(value);
260 } else if (script == ":startProfile") {
261 return web_view->StartProfile();
262 } else if (script == ":endProfile") {
263 return web_view->EndProfile(value);
264 } else {
265 const base::ListValue* args;
266 if (!params.GetList("args", &args))
267 return Status(kUnknownError, "'args' must be a list");
269 return web_view->CallFunction(session->GetCurrentFrameId(),
270 "function(){" + script + "}", *args, value);
274 Status ExecuteExecuteAsyncScript(
275 Session* session,
276 WebView* web_view,
277 const base::DictionaryValue& params,
278 scoped_ptr<base::Value>* value) {
279 std::string script;
280 if (!params.GetString("script", &script))
281 return Status(kUnknownError, "'script' must be a string");
282 const base::ListValue* args;
283 if (!params.GetList("args", &args))
284 return Status(kUnknownError, "'args' must be a list");
286 return web_view->CallUserAsyncFunction(
287 session->GetCurrentFrameId(), "function(){" + script + "}", *args,
288 session->script_timeout, value);
291 Status ExecuteSwitchToFrame(
292 Session* session,
293 WebView* web_view,
294 const base::DictionaryValue& params,
295 scoped_ptr<base::Value>* value) {
296 const base::Value* id;
297 if (!params.Get("id", &id))
298 return Status(kUnknownError, "missing 'id'");
300 if (id->IsType(base::Value::TYPE_NULL)) {
301 session->SwitchToTopFrame();
302 return Status(kOk);
305 std::string script;
306 base::ListValue args;
307 const base::DictionaryValue* id_dict;
308 if (id->GetAsDictionary(&id_dict)) {
309 script = "function(elem) { return elem; }";
310 args.Append(id_dict->DeepCopy());
311 } else {
312 script =
313 "function(xpath) {"
314 " return document.evaluate(xpath, document, null, "
315 " XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;"
316 "}";
317 std::string xpath = "(/html/body//iframe|/html/frameset/frame)";
318 std::string id_string;
319 int id_int;
320 if (id->GetAsString(&id_string)) {
321 xpath += base::StringPrintf(
322 "[@name=\"%s\" or @id=\"%s\"]", id_string.c_str(), id_string.c_str());
323 } else if (id->GetAsInteger(&id_int)) {
324 xpath += base::StringPrintf("[%d]", id_int + 1);
325 } else {
326 return Status(kUnknownError, "invalid 'id'");
328 args.Append(new base::StringValue(xpath));
330 std::string frame;
331 Status status = web_view->GetFrameByFunction(
332 session->GetCurrentFrameId(), script, args, &frame);
333 if (status.IsError())
334 return status;
336 scoped_ptr<base::Value> result;
337 status = web_view->CallFunction(
338 session->GetCurrentFrameId(), script, args, &result);
339 if (status.IsError())
340 return status;
341 const base::DictionaryValue* element;
342 if (!result->GetAsDictionary(&element))
343 return Status(kUnknownError, "fail to locate the sub frame element");
345 std::string chrome_driver_id = GenerateId();
346 const char kSetFrameIdentifier[] =
347 "function(frame, id) {"
348 " frame.setAttribute('cd_frame_id_', id);"
349 "}";
350 base::ListValue new_args;
351 new_args.Append(element->DeepCopy());
352 new_args.AppendString(chrome_driver_id);
353 result.reset(NULL);
354 status = web_view->CallFunction(
355 session->GetCurrentFrameId(), kSetFrameIdentifier, new_args, &result);
356 if (status.IsError())
357 return status;
358 session->SwitchToSubFrame(frame, chrome_driver_id);
359 return Status(kOk);
362 Status ExecuteSwitchToParentFrame(
363 Session* session,
364 WebView* web_view,
365 const base::DictionaryValue& params,
366 scoped_ptr<base::Value>* value) {
367 session->SwitchToParentFrame();
368 return Status(kOk);
371 Status ExecuteGetTitle(
372 Session* session,
373 WebView* web_view,
374 const base::DictionaryValue& params,
375 scoped_ptr<base::Value>* value) {
376 const char kGetTitleScript[] =
377 "function() {"
378 " if (document.title)"
379 " return document.title;"
380 " else"
381 " return document.URL;"
382 "}";
383 base::ListValue args;
384 return web_view->CallFunction(std::string(), kGetTitleScript, args, value);
387 Status ExecuteGetPageSource(
388 Session* session,
389 WebView* web_view,
390 const base::DictionaryValue& params,
391 scoped_ptr<base::Value>* value) {
392 const char kGetPageSource[] =
393 "function() {"
394 " return new XMLSerializer().serializeToString(document);"
395 "}";
396 base::ListValue args;
397 return web_view->CallFunction(
398 session->GetCurrentFrameId(), kGetPageSource, args, value);
401 Status ExecuteFindElement(
402 int interval_ms,
403 Session* session,
404 WebView* web_view,
405 const base::DictionaryValue& params,
406 scoped_ptr<base::Value>* value) {
407 return FindElement(interval_ms, true, NULL, session, web_view, params, value);
410 Status ExecuteFindElements(
411 int interval_ms,
412 Session* session,
413 WebView* web_view,
414 const base::DictionaryValue& params,
415 scoped_ptr<base::Value>* value) {
416 return FindElement(
417 interval_ms, false, NULL, session, web_view, params, value);
420 Status ExecuteGetCurrentUrl(
421 Session* session,
422 WebView* web_view,
423 const base::DictionaryValue& params,
424 scoped_ptr<base::Value>* value) {
425 std::string url;
426 Status status = GetUrl(web_view, session->GetCurrentFrameId(), &url);
427 if (status.IsError())
428 return status;
429 value->reset(new base::StringValue(url));
430 return Status(kOk);
433 Status ExecuteGoBack(
434 Session* session,
435 WebView* web_view,
436 const base::DictionaryValue& params,
437 scoped_ptr<base::Value>* value) {
438 return web_view->TraverseHistory(-1);
441 Status ExecuteGoForward(
442 Session* session,
443 WebView* web_view,
444 const base::DictionaryValue& params,
445 scoped_ptr<base::Value>* value) {
446 return web_view->TraverseHistory(1);
449 Status ExecuteRefresh(
450 Session* session,
451 WebView* web_view,
452 const base::DictionaryValue& params,
453 scoped_ptr<base::Value>* value) {
454 return web_view->Reload();
457 Status ExecuteMouseMoveTo(
458 Session* session,
459 WebView* web_view,
460 const base::DictionaryValue& params,
461 scoped_ptr<base::Value>* value) {
462 std::string element_id;
463 bool has_element = params.GetString("element", &element_id);
464 int x_offset = 0;
465 int y_offset = 0;
466 bool has_offset = params.GetInteger("xoffset", &x_offset) &&
467 params.GetInteger("yoffset", &y_offset);
468 if (!has_element && !has_offset)
469 return Status(kUnknownError, "at least an element or offset should be set");
471 WebPoint location;
472 if (has_element) {
473 WebPoint offset(x_offset, y_offset);
474 Status status = ScrollElementIntoView(session, web_view, element_id,
475 has_offset ? &offset : nullptr, &location);
476 if (status.IsError())
477 return status;
478 } else {
479 location = session->mouse_position;
480 if (has_offset)
481 location.Offset(x_offset, y_offset);
484 std::list<MouseEvent> events;
485 events.push_back(
486 MouseEvent(kMovedMouseEventType, kNoneMouseButton,
487 location.x, location.y, session->sticky_modifiers, 0));
488 Status status =
489 web_view->DispatchMouseEvents(events, session->GetCurrentFrameId());
490 if (status.IsOk())
491 session->mouse_position = location;
492 return status;
495 Status ExecuteMouseClick(
496 Session* session,
497 WebView* web_view,
498 const base::DictionaryValue& params,
499 scoped_ptr<base::Value>* value) {
500 MouseButton button;
501 Status status = GetMouseButton(params, &button);
502 if (status.IsError())
503 return status;
504 std::list<MouseEvent> events;
505 events.push_back(
506 MouseEvent(kPressedMouseEventType, button,
507 session->mouse_position.x, session->mouse_position.y,
508 session->sticky_modifiers, 1));
509 events.push_back(
510 MouseEvent(kReleasedMouseEventType, button,
511 session->mouse_position.x, session->mouse_position.y,
512 session->sticky_modifiers, 1));
513 return web_view->DispatchMouseEvents(events, session->GetCurrentFrameId());
516 Status ExecuteMouseButtonDown(
517 Session* session,
518 WebView* web_view,
519 const base::DictionaryValue& params,
520 scoped_ptr<base::Value>* value) {
521 MouseButton button;
522 Status status = GetMouseButton(params, &button);
523 if (status.IsError())
524 return status;
525 std::list<MouseEvent> events;
526 events.push_back(
527 MouseEvent(kPressedMouseEventType, button,
528 session->mouse_position.x, session->mouse_position.y,
529 session->sticky_modifiers, 1));
530 return web_view->DispatchMouseEvents(events, session->GetCurrentFrameId());
533 Status ExecuteMouseButtonUp(
534 Session* session,
535 WebView* web_view,
536 const base::DictionaryValue& params,
537 scoped_ptr<base::Value>* value) {
538 MouseButton button;
539 Status status = GetMouseButton(params, &button);
540 if (status.IsError())
541 return status;
542 std::list<MouseEvent> events;
543 events.push_back(
544 MouseEvent(kReleasedMouseEventType, button,
545 session->mouse_position.x, session->mouse_position.y,
546 session->sticky_modifiers, 1));
547 return web_view->DispatchMouseEvents(events, session->GetCurrentFrameId());
550 Status ExecuteMouseDoubleClick(
551 Session* session,
552 WebView* web_view,
553 const base::DictionaryValue& params,
554 scoped_ptr<base::Value>* value) {
555 MouseButton button;
556 Status status = GetMouseButton(params, &button);
557 if (status.IsError())
558 return status;
559 std::list<MouseEvent> events;
560 events.push_back(
561 MouseEvent(kPressedMouseEventType, button,
562 session->mouse_position.x, session->mouse_position.y,
563 session->sticky_modifiers, 2));
564 events.push_back(
565 MouseEvent(kReleasedMouseEventType, button,
566 session->mouse_position.x, session->mouse_position.y,
567 session->sticky_modifiers, 2));
568 return web_view->DispatchMouseEvents(events, session->GetCurrentFrameId());
571 Status ExecuteTouchDown(
572 Session* session,
573 WebView* web_view,
574 const base::DictionaryValue& params,
575 scoped_ptr<base::Value>* value) {
576 return ExecuteTouchEvent(session, web_view, kTouchStart, params);
579 Status ExecuteTouchUp(
580 Session* session,
581 WebView* web_view,
582 const base::DictionaryValue& params,
583 scoped_ptr<base::Value>* value) {
584 return ExecuteTouchEvent(session, web_view, kTouchEnd, params);
587 Status ExecuteTouchMove(
588 Session* session,
589 WebView* web_view,
590 const base::DictionaryValue& params,
591 scoped_ptr<base::Value>* value) {
592 return ExecuteTouchEvent(session, web_view, kTouchMove, params);
595 Status ExecuteGetActiveElement(
596 Session* session,
597 WebView* web_view,
598 const base::DictionaryValue& params,
599 scoped_ptr<base::Value>* value) {
600 return GetActiveElement(session, web_view, value);
603 Status ExecuteSendKeysToActiveElement(
604 Session* session,
605 WebView* web_view,
606 const base::DictionaryValue& params,
607 scoped_ptr<base::Value>* value) {
608 const base::ListValue* key_list;
609 if (!params.GetList("value", &key_list))
610 return Status(kUnknownError, "'value' must be a list");
611 return SendKeysOnWindow(
612 web_view, key_list, false, &session->sticky_modifiers);
615 Status ExecuteGetAppCacheStatus(
616 Session* session,
617 WebView* web_view,
618 const base::DictionaryValue& params,
619 scoped_ptr<base::Value>* value) {
620 return web_view->EvaluateScript(
621 session->GetCurrentFrameId(),
622 "applicationCache.status",
623 value);
626 Status ExecuteIsBrowserOnline(
627 Session* session,
628 WebView* web_view,
629 const base::DictionaryValue& params,
630 scoped_ptr<base::Value>* value) {
631 return web_view->EvaluateScript(
632 session->GetCurrentFrameId(),
633 "navigator.onLine",
634 value);
637 Status ExecuteGetStorageItem(
638 const char* storage,
639 Session* session,
640 WebView* web_view,
641 const base::DictionaryValue& params,
642 scoped_ptr<base::Value>* value) {
643 std::string key;
644 if (!params.GetString("key", &key))
645 return Status(kUnknownError, "'key' must be a string");
646 base::ListValue args;
647 args.Append(new base::StringValue(key));
648 return web_view->CallFunction(
649 session->GetCurrentFrameId(),
650 base::StringPrintf("function(key) { return %s[key]; }", storage),
651 args,
652 value);
655 Status ExecuteGetStorageKeys(
656 const char* storage,
657 Session* session,
658 WebView* web_view,
659 const base::DictionaryValue& params,
660 scoped_ptr<base::Value>* value) {
661 const char script[] =
662 "var keys = [];"
663 "for (var key in %s) {"
664 " keys.push(key);"
666 "keys";
667 return web_view->EvaluateScript(
668 session->GetCurrentFrameId(),
669 base::StringPrintf(script, storage),
670 value);
673 Status ExecuteSetStorageItem(
674 const char* storage,
675 Session* session,
676 WebView* web_view,
677 const base::DictionaryValue& params,
678 scoped_ptr<base::Value>* value) {
679 std::string key;
680 if (!params.GetString("key", &key))
681 return Status(kUnknownError, "'key' must be a string");
682 std::string storage_value;
683 if (!params.GetString("value", &storage_value))
684 return Status(kUnknownError, "'value' must be a string");
685 base::ListValue args;
686 args.Append(new base::StringValue(key));
687 args.Append(new base::StringValue(storage_value));
688 return web_view->CallFunction(
689 session->GetCurrentFrameId(),
690 base::StringPrintf("function(key, value) { %s[key] = value; }", storage),
691 args,
692 value);
695 Status ExecuteRemoveStorageItem(
696 const char* storage,
697 Session* session,
698 WebView* web_view,
699 const base::DictionaryValue& params,
700 scoped_ptr<base::Value>* value) {
701 std::string key;
702 if (!params.GetString("key", &key))
703 return Status(kUnknownError, "'key' must be a string");
704 base::ListValue args;
705 args.Append(new base::StringValue(key));
706 return web_view->CallFunction(
707 session->GetCurrentFrameId(),
708 base::StringPrintf("function(key) { %s.removeItem(key) }", storage),
709 args,
710 value);
713 Status ExecuteClearStorage(
714 const char* storage,
715 Session* session,
716 WebView* web_view,
717 const base::DictionaryValue& params,
718 scoped_ptr<base::Value>* value) {
719 return web_view->EvaluateScript(
720 session->GetCurrentFrameId(),
721 base::StringPrintf("%s.clear()", storage),
722 value);
725 Status ExecuteGetStorageSize(
726 const char* storage,
727 Session* session,
728 WebView* web_view,
729 const base::DictionaryValue& params,
730 scoped_ptr<base::Value>* value) {
731 return web_view->EvaluateScript(
732 session->GetCurrentFrameId(),
733 base::StringPrintf("%s.length", storage),
734 value);
737 Status ExecuteScreenshot(
738 Session* session,
739 WebView* web_view,
740 const base::DictionaryValue& params,
741 scoped_ptr<base::Value>* value) {
742 Status status = session->chrome->ActivateWebView(web_view->GetId());
743 if (status.IsError())
744 return status;
746 std::string screenshot;
747 ChromeDesktopImpl* desktop = NULL;
748 status = session->chrome->GetAsDesktop(&desktop);
749 if (status.IsOk() && !session->force_devtools_screenshot) {
750 AutomationExtension* extension = NULL;
751 status = desktop->GetAutomationExtension(&extension);
752 if (status.IsError())
753 return status;
754 status = extension->CaptureScreenshot(&screenshot);
755 // If the screenshot was forbidden, fallback to DevTools.
756 if (status.code() == kForbidden)
757 status = web_view->CaptureScreenshot(&screenshot);
758 } else {
759 status = web_view->CaptureScreenshot(&screenshot);
761 if (status.IsError())
762 return status;
764 value->reset(new base::StringValue(screenshot));
765 return Status(kOk);
768 Status ExecuteGetCookies(
769 Session* session,
770 WebView* web_view,
771 const base::DictionaryValue& params,
772 scoped_ptr<base::Value>* value) {
773 std::list<Cookie> cookies;
774 Status status = GetVisibleCookies(web_view, &cookies);
775 if (status.IsError())
776 return status;
777 scoped_ptr<base::ListValue> cookie_list(new base::ListValue());
778 for (std::list<Cookie>::const_iterator it = cookies.begin();
779 it != cookies.end(); ++it) {
780 cookie_list->Append(CreateDictionaryFrom(*it));
782 value->reset(cookie_list.release());
783 return Status(kOk);
786 Status ExecuteAddCookie(
787 Session* session,
788 WebView* web_view,
789 const base::DictionaryValue& params,
790 scoped_ptr<base::Value>* value) {
791 const base::DictionaryValue* cookie;
792 if (!params.GetDictionary("cookie", &cookie))
793 return Status(kUnknownError, "missing 'cookie'");
794 base::ListValue args;
795 args.Append(cookie->DeepCopy());
796 scoped_ptr<base::Value> result;
797 return web_view->CallFunction(
798 session->GetCurrentFrameId(), kAddCookieScript, args, &result);
801 Status ExecuteDeleteCookie(
802 Session* session,
803 WebView* web_view,
804 const base::DictionaryValue& params,
805 scoped_ptr<base::Value>* value) {
806 std::string name;
807 if (!params.GetString("name", &name))
808 return Status(kUnknownError, "missing 'name'");
809 base::DictionaryValue params_url;
810 scoped_ptr<base::Value> value_url;
811 std::string url;
812 Status status = GetUrl(web_view, session->GetCurrentFrameId(), &url);
813 if (status.IsError())
814 return status;
815 return web_view->DeleteCookie(name, url);
818 Status ExecuteDeleteAllCookies(
819 Session* session,
820 WebView* web_view,
821 const base::DictionaryValue& params,
822 scoped_ptr<base::Value>* value) {
823 std::list<Cookie> cookies;
824 Status status = GetVisibleCookies(web_view, &cookies);
825 if (status.IsError())
826 return status;
828 if (!cookies.empty()) {
829 base::DictionaryValue params_url;
830 scoped_ptr<base::Value> value_url;
831 std::string url;
832 status = GetUrl(web_view, session->GetCurrentFrameId(), &url);
833 if (status.IsError())
834 return status;
835 for (std::list<Cookie>::const_iterator it = cookies.begin();
836 it != cookies.end(); ++it) {
837 status = web_view->DeleteCookie(it->name, url);
838 if (status.IsError())
839 return status;
843 return Status(kOk);
846 Status ExecuteSetLocation(
847 Session* session,
848 WebView* web_view,
849 const base::DictionaryValue& params,
850 scoped_ptr<base::Value>* value) {
851 const base::DictionaryValue* location = NULL;
852 Geoposition geoposition;
853 if (!params.GetDictionary("location", &location) ||
854 !location->GetDouble("latitude", &geoposition.latitude) ||
855 !location->GetDouble("longitude", &geoposition.longitude))
856 return Status(kUnknownError, "missing or invalid 'location'");
857 if (location->HasKey("accuracy") &&
858 !location->GetDouble("accuracy", &geoposition.accuracy)) {
859 return Status(kUnknownError, "invalid 'accuracy'");
860 } else {
861 // |accuracy| is not part of the WebDriver spec yet, so if it is not given
862 // default to 100 meters accuracy.
863 geoposition.accuracy = 100;
866 Status status = web_view->OverrideGeolocation(geoposition);
867 if (status.IsOk())
868 session->overridden_geoposition.reset(new Geoposition(geoposition));
869 return status;
872 Status ExecuteTakeHeapSnapshot(
873 Session* session,
874 WebView* web_view,
875 const base::DictionaryValue& params,
876 scoped_ptr<base::Value>* value) {
877 return web_view->TakeHeapSnapshot(value);