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