| 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/session_commands.h" | 5 #include "chrome/test/chromedriver/session_commands.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/logging.h" // For CHECK macros. | 12 #include "base/logging.h" // For CHECK macros. |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/message_loop/message_loop_proxy.h" | 14 #include "base/message_loop/message_loop_proxy.h" |
| 15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 16 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "chrome/test/chromedriver/basic_types.h" | 18 #include "chrome/test/chromedriver/basic_types.h" |
| 19 #include "chrome/test/chromedriver/chrome/automation_extension.h" | 19 #include "chrome/test/chromedriver/chrome/automation_extension.h" |
| 20 #include "chrome/test/chromedriver/chrome/chrome.h" | 20 #include "chrome/test/chromedriver/chrome/chrome.h" |
| 21 #include "chrome/test/chromedriver/chrome/geoposition.h" | 21 #include "chrome/test/chromedriver/chrome/geoposition.h" |
| 22 #include "chrome/test/chromedriver/chrome/status.h" | 22 #include "chrome/test/chromedriver/chrome/status.h" |
| 23 #include "chrome/test/chromedriver/chrome/web_view.h" | 23 #include "chrome/test/chromedriver/chrome/web_view.h" |
| 24 #include "chrome/test/chromedriver/logging.h" | 24 #include "chrome/test/chromedriver/logging.h" |
| 25 #include "chrome/test/chromedriver/session.h" | 25 #include "chrome/test/chromedriver/session.h" |
| 26 #include "chrome/test/chromedriver/session_map.h" | |
| 27 #include "chrome/test/chromedriver/util.h" | 26 #include "chrome/test/chromedriver/util.h" |
| 28 | 27 |
| 29 namespace { | 28 namespace { |
| 30 | 29 |
| 31 const char kWindowHandlePrefix[] = "CDwindow-"; | 30 const char kWindowHandlePrefix[] = "CDwindow-"; |
| 32 | 31 |
| 33 std::string WebViewIdToWindowHandle(const std::string& web_view_id) { | 32 std::string WebViewIdToWindowHandle(const std::string& web_view_id) { |
| 34 return kWindowHandlePrefix + web_view_id; | 33 return kWindowHandlePrefix + web_view_id; |
| 35 } | 34 } |
| 36 | 35 |
| 37 bool WindowHandleToWebViewId(const std::string& window_handle, | 36 bool WindowHandleToWebViewId(const std::string& window_handle, |
| 38 std::string* web_view_id) { | 37 std::string* web_view_id) { |
| 39 if (window_handle.find(kWindowHandlePrefix) != 0u) | 38 if (window_handle.find(kWindowHandlePrefix) != 0u) |
| 40 return false; | 39 return false; |
| 41 *web_view_id = window_handle.substr( | 40 *web_view_id = window_handle.substr( |
| 42 std::string(kWindowHandlePrefix).length()); | 41 std::string(kWindowHandlePrefix).length()); |
| 43 return true; | 42 return true; |
| 44 } | 43 } |
| 45 | 44 |
| 46 void ExecuteOnSessionThread( | |
| 47 const SessionCommand& command, | |
| 48 Session* session, | |
| 49 const base::DictionaryValue* params, | |
| 50 scoped_ptr<base::Value>* value, | |
| 51 Status* status, | |
| 52 base::WaitableEvent* event) { | |
| 53 *status = command.Run(session, *params, value); | |
| 54 event->Signal(); | |
| 55 } | |
| 56 | |
| 57 } // namespace | 45 } // namespace |
| 58 | 46 |
| 59 Status ExecuteSessionCommand( | 47 Status ExecuteQuit( |
| 60 SessionMap* session_map, | 48 bool allow_detach, |
| 61 const SessionCommand& command, | 49 Session* session, |
| 62 const base::DictionaryValue& params, | 50 const base::DictionaryValue& params, |
| 63 const std::string& session_id, | 51 scoped_ptr<base::Value>* value) { |
| 64 scoped_ptr<base::Value>* out_value, | 52 if (allow_detach && session->detach) { |
| 65 std::string* out_session_id) { | 53 return Status(kOk); |
| 66 *out_session_id = session_id; | 54 } else { |
| 67 scoped_refptr<SessionAccessor> session_accessor; | 55 session->quit = true; |
| 68 if (!session_map->Get(session_id, &session_accessor)) | 56 return session->chrome->Quit(); |
| 69 return Status(kNoSuchSession, session_id); | 57 } |
| 70 scoped_ptr<base::AutoLock> session_lock; | |
| 71 Session* session = session_accessor->Access(&session_lock); | |
| 72 if (!session) | |
| 73 return Status(kNoSuchSession, session_id); | |
| 74 | |
| 75 Status status(kUnknownError); | |
| 76 base::WaitableEvent event(false, false); | |
| 77 session->thread.message_loop_proxy()->PostTask( | |
| 78 FROM_HERE, | |
| 79 base::Bind(&ExecuteOnSessionThread, command, session, | |
| 80 ¶ms, out_value, &status, &event)); | |
| 81 event.Wait(); | |
| 82 if (status.IsError() && session->chrome) | |
| 83 status.AddDetails("Session info: chrome=" + session->chrome->GetVersion()); | |
| 84 // Delete the session, because concurrent requests might hold a reference to | |
| 85 // the SessionAccessor already. | |
| 86 if (!session_map->Has(session_id)) | |
| 87 session_accessor->DeleteSession(); | |
| 88 return status; | |
| 89 } | 58 } |
| 90 | 59 |
| 91 Status ExecuteGetSessionCapabilities( | 60 Status ExecuteGetSessionCapabilities( |
| 92 SessionMap* session_map, | |
| 93 Session* session, | 61 Session* session, |
| 94 const base::DictionaryValue& params, | 62 const base::DictionaryValue& params, |
| 95 scoped_ptr<base::Value>* value) { | 63 scoped_ptr<base::Value>* value) { |
| 96 value->reset(session->capabilities->DeepCopy()); | 64 value->reset(session->capabilities->DeepCopy()); |
| 97 return Status(kOk); | 65 return Status(kOk); |
| 98 } | 66 } |
| 99 | 67 |
| 100 Status ExecuteGetCurrentWindowHandle( | 68 Status ExecuteGetCurrentWindowHandle( |
| 101 Session* session, | 69 Session* session, |
| 102 const base::DictionaryValue& params, | 70 const base::DictionaryValue& params, |
| 103 scoped_ptr<base::Value>* value) { | 71 scoped_ptr<base::Value>* value) { |
| 104 WebView* web_view = NULL; | 72 WebView* web_view = NULL; |
| 105 Status status = session->GetTargetWindow(&web_view); | 73 Status status = session->GetTargetWindow(&web_view); |
| 106 if (status.IsError()) | 74 if (status.IsError()) |
| 107 return status; | 75 return status; |
| 108 | 76 |
| 109 value->reset(new StringValue(WebViewIdToWindowHandle(web_view->GetId()))); | 77 value->reset(new StringValue(WebViewIdToWindowHandle(web_view->GetId()))); |
| 110 return Status(kOk); | 78 return Status(kOk); |
| 111 } | 79 } |
| 112 | 80 |
| 113 Status ExecuteClose( | 81 Status ExecuteClose( |
| 114 SessionMap* session_map, | |
| 115 Session* session, | 82 Session* session, |
| 116 const base::DictionaryValue& params, | 83 const base::DictionaryValue& params, |
| 117 scoped_ptr<base::Value>* value) { | 84 scoped_ptr<base::Value>* value) { |
| 118 std::list<std::string> web_view_ids; | 85 std::list<std::string> web_view_ids; |
| 119 Status status = session->chrome->GetWebViewIds(&web_view_ids); | 86 Status status = session->chrome->GetWebViewIds(&web_view_ids); |
| 120 if (status.IsError()) | 87 if (status.IsError()) |
| 121 return status; | 88 return status; |
| 122 bool is_last_web_view = web_view_ids.size() == 1u; | 89 bool is_last_web_view = web_view_ids.size() == 1u; |
| 123 web_view_ids.clear(); | 90 web_view_ids.clear(); |
| 124 | 91 |
| 125 WebView* web_view = NULL; | 92 WebView* web_view = NULL; |
| 126 status = session->GetTargetWindow(&web_view); | 93 status = session->GetTargetWindow(&web_view); |
| 127 if (status.IsError()) | 94 if (status.IsError()) |
| 128 return status; | 95 return status; |
| 129 | 96 |
| 130 status = session->chrome->CloseWebView(web_view->GetId()); | 97 status = session->chrome->CloseWebView(web_view->GetId()); |
| 131 if (status.IsError()) | 98 if (status.IsError()) |
| 132 return status; | 99 return status; |
| 133 | 100 |
| 134 status = session->chrome->GetWebViewIds(&web_view_ids); | 101 status = session->chrome->GetWebViewIds(&web_view_ids); |
| 135 if ((status.code() == kChromeNotReachable && is_last_web_view) || | 102 if ((status.code() == kChromeNotReachable && is_last_web_view) || |
| 136 (status.IsOk() && web_view_ids.empty())) { | 103 (status.IsOk() && web_view_ids.empty())) { |
| 137 // If no window is open, close is the equivalent of calling "quit". | 104 // If no window is open, close is the equivalent of calling "quit". |
| 138 CHECK(session_map->Remove(session->id)); | 105 session->quit = true; |
| 139 return session->chrome->Quit(); | 106 return session->chrome->Quit(); |
| 140 } | 107 } |
| 141 | 108 |
| 142 return status; | 109 return status; |
| 143 } | 110 } |
| 144 | 111 |
| 145 Status ExecuteGetWindowHandles( | 112 Status ExecuteGetWindowHandles( |
| 146 Session* session, | 113 Session* session, |
| 147 const base::DictionaryValue& params, | 114 const base::DictionaryValue& params, |
| 148 scoped_ptr<base::Value>* value) { | 115 scoped_ptr<base::Value>* value) { |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 } | 432 } |
| 466 std::string error_msg; | 433 std::string error_msg; |
| 467 base::FilePath upload; | 434 base::FilePath upload; |
| 468 Status status = UnzipSoleFile(upload_dir, zip_data, &upload); | 435 Status status = UnzipSoleFile(upload_dir, zip_data, &upload); |
| 469 if (status.IsError()) | 436 if (status.IsError()) |
| 470 return Status(kUnknownError, "unable to unzip 'file'", status); | 437 return Status(kUnknownError, "unable to unzip 'file'", status); |
| 471 | 438 |
| 472 value->reset(new base::StringValue(upload.value())); | 439 value->reset(new base::StringValue(upload.value())); |
| 473 return Status(kOk); | 440 return Status(kOk); |
| 474 } | 441 } |
| OLD | NEW |