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 const char* kGetAndClearResult = |
| 214 "function() {" |
| 215 " var toReturn = {};" |
| 216 " var info = document.chromedriverAsyncScriptInfo;" |
| 217 " toReturn.finished = info.finished;" |
| 218 " if (info.finished) {" |
| 219 " toReturn.result = info.result;" |
| 220 " delete info.result;" |
| 221 " }" |
| 222 " return toReturn;" |
| 223 "}"; |
| 224 while (true) { |
| 225 scoped_ptr<base::Value> result; |
| 226 status = web_view->CallFunction( |
| 227 session->frame, kGetAndClearResult, args_tmp, &result); |
| 228 if (status.IsError()) { |
| 229 if (status.code() == kNoSuchFrame) |
| 230 return Status(kJavaScriptError, "page or frame unload", status); |
| 231 return status; |
| 232 } |
| 233 const base::DictionaryValue* dict; |
| 234 if (!result->GetAsDictionary(&dict)) |
| 235 return Status(kUnknownError, "failed to tell if script has finished"); |
| 236 bool finished = false; |
| 237 if (!dict->GetBoolean("finished", &finished)) |
| 238 return Status(kUnknownError, "flag 'finished' is not returned"); |
| 239 if (finished) { |
| 240 if (!dict->HasKey("result")) { |
| 241 value->reset(base::Value::CreateNullValue()); |
| 242 } else { |
| 243 const base::Value* script_result; |
| 244 dict->Get("result", &script_result); |
| 245 value->reset(script_result->DeepCopy()); |
| 246 } |
| 247 return Status(kOk); |
| 248 } |
| 249 |
| 250 if ((base::Time::Now() - start_time).InMilliseconds() >= |
| 251 session->script_timeout) |
| 252 break; |
| 253 |
| 254 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(50)); |
| 255 } |
| 256 |
| 257 // Clear the result if it is set, otherwise prevent the script to set result |
| 258 // by increasing the id. |
| 259 const char* kClearResultForTimeoutScript = |
| 260 "var info = document.chromedriverAsyncScriptInfo;" |
| 261 "info.id++;" |
| 262 "delete info.result;"; |
| 263 web_view->EvaluateScript(session->frame, kClearResultForTimeoutScript, &tmp); |
| 264 |
| 265 return Status( |
| 266 kScriptTimeout, |
| 267 base::StringPrintf("timed out waiting for result after %d ms", |
| 268 session->script_timeout)); |
| 269 } |
| 270 |
187 Status ExecuteSwitchToFrame( | 271 Status ExecuteSwitchToFrame( |
188 Session* session, | 272 Session* session, |
189 WebView* web_view, | 273 WebView* web_view, |
190 const base::DictionaryValue& params, | 274 const base::DictionaryValue& params, |
191 scoped_ptr<base::Value>* value) { | 275 scoped_ptr<base::Value>* value) { |
192 const base::Value* id; | 276 const base::Value* id; |
193 if (!params.Get("id", &id)) | 277 if (!params.Get("id", &id)) |
194 return Status(kUnknownError, "missing 'id'"); | 278 return Status(kUnknownError, "missing 'id'"); |
195 | 279 |
196 if (id->IsType(base::Value::TYPE_NULL)) { | 280 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(); | 733 for (std::list<Cookie>::const_iterator it = cookies.begin(); |
650 it != cookies.end(); ++it) { | 734 it != cookies.end(); ++it) { |
651 status = web_view->DeleteCookie(it->name, url); | 735 status = web_view->DeleteCookie(it->name, url); |
652 if (status.IsError()) | 736 if (status.IsError()) |
653 return status; | 737 return status; |
654 } | 738 } |
655 } | 739 } |
656 | 740 |
657 return Status(kOk); | 741 return Status(kOk); |
658 } | 742 } |
OLD | NEW |