OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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. | |
4 | |
5 #include "chrome/test/webdriver/commands/webdriver_command.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/memory/singleton.h" | |
11 #include "base/strings/string_util.h" | |
12 #include "base/strings/stringprintf.h" | |
13 #include "base/values.h" | |
14 #include "chrome/test/webdriver/commands/response.h" | |
15 #include "chrome/test/webdriver/webdriver_error.h" | |
16 #include "chrome/test/webdriver/webdriver_logging.h" | |
17 #include "chrome/test/webdriver/webdriver_session.h" | |
18 #include "chrome/test/webdriver/webdriver_session_manager.h" | |
19 #include "chrome/test/webdriver/webdriver_util.h" | |
20 | |
21 namespace webdriver { | |
22 | |
23 WebDriverCommand::WebDriverCommand( | |
24 const std::vector<std::string>& path_segments, | |
25 const base::DictionaryValue* const parameters) | |
26 : Command(path_segments, parameters), session_(NULL) { | |
27 } | |
28 | |
29 WebDriverCommand::~WebDriverCommand() {} | |
30 | |
31 bool WebDriverCommand::Init(Response* const response) { | |
32 // There should be at least 3 path segments to match "/session/$id". | |
33 session_id_ = GetPathVariable(2); | |
34 if (session_id_.length() == 0) { | |
35 response->SetError( | |
36 new Error(kBadRequest, "No session ID specified")); | |
37 return false; | |
38 } | |
39 | |
40 session_ = SessionManager::GetInstance()->GetSession(session_id_); | |
41 if (session_ == NULL) { | |
42 response->SetError( | |
43 new Error(kSessionNotFound, "Session not found: " + session_id_)); | |
44 return false; | |
45 } | |
46 | |
47 std::string message = base::StringPrintf( | |
48 "Command received (%s)", JoinString(path_segments_, '/').c_str()); | |
49 if (parameters_.get()) | |
50 message += " with params " + JsonStringifyForDisplay(parameters_.get()); | |
51 session_->logger().Log(kFineLogLevel, message); | |
52 | |
53 if (ShouldRunPreAndPostCommandHandlers()) { | |
54 Error* error = session_->BeforeExecuteCommand(); | |
55 if (error) { | |
56 response->SetError(error); | |
57 return false; | |
58 } | |
59 } | |
60 response->SetField("sessionId", new base::StringValue(session_id_)); | |
61 return true; | |
62 } | |
63 | |
64 void WebDriverCommand::Finish(Response* const response) { | |
65 // The session may have been terminated as a result of the command. | |
66 if (!SessionManager::GetInstance()->Has(session_id_)) | |
67 return; | |
68 | |
69 if (ShouldRunPreAndPostCommandHandlers()) { | |
70 scoped_ptr<Error> error(session_->AfterExecuteCommand()); | |
71 if (error.get()) { | |
72 session_->logger().Log(kWarningLogLevel, | |
73 "AfterExecuteCommand failed: " + error->details()); | |
74 } | |
75 } | |
76 | |
77 LogLevel level = kWarningLogLevel; | |
78 if (response->GetStatus() == kSuccess) | |
79 level = kFineLogLevel; | |
80 session_->logger().Log( | |
81 level, base::StringPrintf( | |
82 "Command finished (%s) with response %s", | |
83 JoinString(path_segments_, '/').c_str(), | |
84 JsonStringifyForDisplay(response->GetDictionary()).c_str())); | |
85 } | |
86 | |
87 bool WebDriverCommand::ShouldRunPreAndPostCommandHandlers() { | |
88 return true; | |
89 } | |
90 | |
91 } // namespace webdriver | |
OLD | NEW |