OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/chromedriver.h" | 5 #include "chrome/test/chromedriver/chromedriver.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/lazy_instance.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/synchronization/lock.h" |
11 #include "base/values.h" | 13 #include "base/values.h" |
12 #include "chrome/test/chromedriver/command_executor.h" | 14 #include "chrome/test/chromedriver/command_executor.h" |
13 #include "chrome/test/chromedriver/status.h" | 15 #include "chrome/test/chromedriver/status.h" |
14 | 16 |
15 namespace { | 17 namespace { |
16 | 18 |
| 19 // Guards |g_executor_initialized|. |
| 20 base::LazyInstance<base::Lock> g_lazy_lock = LAZY_INSTANCE_INITIALIZER; |
| 21 bool g_executor_initialized = false; |
17 CommandExecutor* g_command_executor = NULL; | 22 CommandExecutor* g_command_executor = NULL; |
18 | 23 |
19 void SetResponse(StatusCode status, | 24 void SetResponse(StatusCode status, |
20 const base::Value* value, | 25 const base::Value* value, |
21 const std::string& session_id, | 26 const std::string& session_id, |
22 std::string* response) { | 27 std::string* response) { |
23 base::DictionaryValue response_dict; | 28 base::DictionaryValue response_dict; |
24 response_dict.SetInteger("status", status); | 29 response_dict.SetInteger("status", status); |
25 response_dict.Set("value", value->DeepCopy()); | 30 response_dict.Set("value", value->DeepCopy()); |
26 response_dict.SetString("sessionId", session_id); | 31 response_dict.SetString("sessionId", session_id); |
27 std::string json; | 32 std::string json; |
28 base::JSONWriter::Write(&response_dict, response); | 33 base::JSONWriter::Write(&response_dict, response); |
29 } | 34 } |
30 | 35 |
31 void SetError(const std::string& error_msg, | 36 void SetError(const std::string& error_msg, |
32 std::string* response) { | 37 std::string* response) { |
33 base::DictionaryValue value; | 38 base::DictionaryValue value; |
34 value.SetString("message", error_msg); | 39 value.SetString("message", error_msg); |
35 SetResponse(kUnknownError, &value, "", response); | 40 SetResponse(kUnknownError, &value, "", response); |
36 } | 41 } |
37 | 42 |
38 } // namespace | 43 } // namespace |
39 | 44 |
40 void Init(scoped_ptr<CommandExecutor> executor) { | 45 void Init(scoped_ptr<CommandExecutor> executor) { |
41 g_command_executor = executor.release(); | 46 g_command_executor = executor.release(); |
| 47 // We do not call CommandExecutor::Init here because you can't do some things |
| 48 // (e.g., creating threads) during DLL loading on Windows. |
42 } | 49 } |
43 | 50 |
44 void ExecuteCommand(const std::string& command, std::string* response) { | 51 void ExecuteCommand(const std::string& command, std::string* response) { |
45 CHECK(g_command_executor); | 52 CHECK(g_command_executor); |
| 53 { |
| 54 base::AutoLock(g_lazy_lock.Get()); |
| 55 if (!g_executor_initialized) { |
| 56 g_command_executor->Init(); |
| 57 g_executor_initialized = true; |
| 58 } |
| 59 } |
46 std::string error_msg; | 60 std::string error_msg; |
47 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( | 61 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
48 command, 0, NULL, &error_msg)); | 62 command, 0, NULL, &error_msg)); |
49 if (!value.get()) { | 63 if (!value.get()) { |
50 SetError("failed to parse command: " + error_msg, response); | 64 SetError("failed to parse command: " + error_msg, response); |
51 return; | 65 return; |
52 } | 66 } |
53 base::DictionaryValue* command_dict; | 67 base::DictionaryValue* command_dict; |
54 if (!value->GetAsDictionary(&command_dict)) { | 68 if (!value->GetAsDictionary(&command_dict)) { |
55 SetError("invalid command (must be dictionary)", response); | 69 SetError("invalid command (must be dictionary)", response); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 // temp directories, unless we make Chrome clean its own directory too. | 117 // temp directories, unless we make Chrome clean its own directory too. |
104 // If Chrome crashes the directory would be leaked. | 118 // If Chrome crashes the directory would be leaked. |
105 base::DictionaryValue params; | 119 base::DictionaryValue params; |
106 StatusCode status_code; | 120 StatusCode status_code; |
107 scoped_ptr<base::Value> value; | 121 scoped_ptr<base::Value> value; |
108 std::string session_id; | 122 std::string session_id; |
109 g_command_executor->ExecuteCommand( | 123 g_command_executor->ExecuteCommand( |
110 "quitAll", params, "", &status_code, &value, &session_id); | 124 "quitAll", params, "", &status_code, &value, &session_id); |
111 delete g_command_executor; | 125 delete g_command_executor; |
112 } | 126 } |
OLD | NEW |