OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/test/chromedriver/window_commands.h" | 5 #include "chrome/test/chromedriver/window_commands.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/threading/platform_thread.h" |
| 13 #include "base/time.h" |
12 #include "base/values.h" | 14 #include "base/values.h" |
13 #include "chrome/test/chromedriver/basic_types.h" | 15 #include "chrome/test/chromedriver/basic_types.h" |
14 #include "chrome/test/chromedriver/chrome.h" | 16 #include "chrome/test/chromedriver/chrome.h" |
15 #include "chrome/test/chromedriver/element_util.h" | 17 #include "chrome/test/chromedriver/element_util.h" |
16 #include "chrome/test/chromedriver/js.h" | 18 #include "chrome/test/chromedriver/js.h" |
17 #include "chrome/test/chromedriver/session.h" | 19 #include "chrome/test/chromedriver/session.h" |
18 #include "chrome/test/chromedriver/status.h" | 20 #include "chrome/test/chromedriver/status.h" |
19 #include "chrome/test/chromedriver/ui_events.h" | 21 #include "chrome/test/chromedriver/ui_events.h" |
20 #include "chrome/test/chromedriver/web_view.h" | 22 #include "chrome/test/chromedriver/web_view.h" |
21 | 23 |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 if (!params.GetString("script", &script)) | 179 if (!params.GetString("script", &script)) |
178 return Status(kUnknownError, "'script' must be a string"); | 180 return Status(kUnknownError, "'script' must be a string"); |
179 const base::ListValue* args; | 181 const base::ListValue* args; |
180 if (!params.GetList("args", &args)) | 182 if (!params.GetList("args", &args)) |
181 return Status(kUnknownError, "'args' must be a list"); | 183 return Status(kUnknownError, "'args' must be a list"); |
182 | 184 |
183 return web_view->CallFunction( | 185 return web_view->CallFunction( |
184 session->frame, "function(){" + script + "}", *args, value); | 186 session->frame, "function(){" + script + "}", *args, value); |
185 } | 187 } |
186 | 188 |
| 189 Status ExecuteExecuteAsyncScript( |
| 190 Session* session, |
| 191 WebView* web_view, |
| 192 const base::DictionaryValue& params, |
| 193 scoped_ptr<base::Value>* value) { |
| 194 std::string script; |
| 195 if (!params.GetString("script", &script)) |
| 196 return Status(kUnknownError, "'script' must be a string"); |
| 197 const base::ListValue* script_args; |
| 198 if (!params.GetList("args", &script_args)) |
| 199 return Status(kUnknownError, "'args' must be a list"); |
| 200 |
| 201 base::ListValue args; |
| 202 args.AppendString(script); |
| 203 args.Append(script_args->DeepCopy()); |
| 204 args.AppendInteger(session->script_timeout); |
| 205 scoped_ptr<base::Value> tmp; |
| 206 Status status = web_view->CallFunction( |
| 207 session->frame, kExecuteAsyncScriptScript, args, &tmp); |
| 208 if (status.IsError()) |
| 209 return status; |
| 210 |
| 211 base::Time start_time = base::Time::Now(); |
| 212 base::ListValue args_tmp; |
| 213 while (true) { |
| 214 scoped_ptr<base::Value> result; |
| 215 status = web_view->CallFunction( |
| 216 session->frame, |
| 217 "function() {" |
| 218 " return document.chromedriverAsyncScriptInfo;" |
| 219 "}", |
| 220 args_tmp, |
| 221 &result); |
| 222 if (status.IsError()) { |
| 223 if (status.code() == kNoSuchFrame) |
| 224 return Status(kJavaScriptError, "page or frame unload", status); |
| 225 return status; |
| 226 } |
| 227 const base::DictionaryValue* dict; |
| 228 if (!result->GetAsDictionary(&dict)) |
| 229 return Status(kUnknownError, "failed to tell if script has finished"); |
| 230 bool finished = false; |
| 231 if (!dict->GetBoolean("finished", &finished)) |
| 232 return Status(kUnknownError, "flag 'finished' is not returned"); |
| 233 if (finished) { |
| 234 if (!dict->HasKey("result")) { |
| 235 value->reset(base::Value::CreateNullValue()); |
| 236 } else { |
| 237 const base::Value* script_result; |
| 238 dict->Get("result", &script_result); |
| 239 value->reset(script_result->DeepCopy()); |
| 240 } |
| 241 return Status(kOk); |
| 242 } |
| 243 |
| 244 if ((base::Time::Now() - start_time).InMilliseconds() >= |
| 245 session->script_timeout) |
| 246 break; |
| 247 |
| 248 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(50)); |
| 249 } |
| 250 return Status( |
| 251 kScriptTimeout, |
| 252 base::StringPrintf("timed out waiting for result after %d ms", |
| 253 session->script_timeout)); |
| 254 } |
| 255 |
187 Status ExecuteSwitchToFrame( | 256 Status ExecuteSwitchToFrame( |
188 Session* session, | 257 Session* session, |
189 WebView* web_view, | 258 WebView* web_view, |
190 const base::DictionaryValue& params, | 259 const base::DictionaryValue& params, |
191 scoped_ptr<base::Value>* value) { | 260 scoped_ptr<base::Value>* value) { |
192 const base::Value* id; | 261 const base::Value* id; |
193 if (!params.Get("id", &id)) | 262 if (!params.Get("id", &id)) |
194 return Status(kUnknownError, "missing 'id'"); | 263 return Status(kUnknownError, "missing 'id'"); |
195 | 264 |
196 if (id->IsType(base::Value::TYPE_NULL)) { | 265 if (id->IsType(base::Value::TYPE_NULL)) { |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
649 for (std::list<Cookie>::const_iterator it = cookies.begin(); | 718 for (std::list<Cookie>::const_iterator it = cookies.begin(); |
650 it != cookies.end(); ++it) { | 719 it != cookies.end(); ++it) { |
651 status = web_view->DeleteCookie(it->name, url); | 720 status = web_view->DeleteCookie(it->name, url); |
652 if (status.IsError()) | 721 if (status.IsError()) |
653 return status; | 722 return status; |
654 } | 723 } |
655 } | 724 } |
656 | 725 |
657 return Status(kOk); | 726 return Status(kOk); |
658 } | 727 } |
OLD | NEW |