OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/api/record/record_api.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/command_line.h" | |
9 #include "base/file_util.h" | |
10 #include "base/logging.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/process/kill.h" | |
13 #include "base/process/launch.h" | |
14 #include "base/strings/string_number_conversions.h" | |
15 #include "base/strings/string_split.h" | |
16 #include "base/strings/string_util.h" | |
17 #include "base/strings/utf_string_conversions.h" | |
18 | |
19 #include "chrome/common/chrome_switches.h" | |
20 #include "chrome/common/extensions/api/experimental_record.h" | |
21 #include "content/public/browser/browser_thread.h" | |
22 #include "content/public/common/content_switches.h" | |
23 | |
24 namespace extensions { | |
25 | |
26 namespace record = api::experimental_record; | |
27 | |
28 ProcessStrategy::~ProcessStrategy() {} | |
29 | |
30 void ProductionProcessStrategy::RunProcess(const CommandLine& line, | |
31 std::vector<std::string>* errors) { | |
32 base::LaunchOptions options; | |
33 base::ProcessHandle handle; | |
34 | |
35 base::LaunchProcess(line, options, &handle); | |
36 | |
37 int exit_code = 0; | |
38 | |
39 if (!base::WaitForExitCode(handle, &exit_code) || exit_code != 0) | |
40 errors->push_back("Test browser exited abnormally"); | |
41 } | |
42 | |
43 RunPageCyclerFunction::RunPageCyclerFunction(ProcessStrategy* strategy) | |
44 : repeat_count_(1), base_command_line_(*CommandLine::ForCurrentProcess()), | |
45 process_strategy_(strategy) {} | |
46 | |
47 RunPageCyclerFunction::~RunPageCyclerFunction() {} | |
48 | |
49 bool RunPageCyclerFunction::RunImpl() { | |
50 if (!ParseJSParameters()) | |
51 return false; | |
52 | |
53 // If we've had any errors reportable to the JS caller so far (in | |
54 // parameter parsing) then return a list of such errors, else perform | |
55 // RunTestBrowser on the BlockingPool. | |
56 if (!errors_.empty()) { | |
57 results_ = record::CaptureURLs::Results::Create(errors_); | |
58 SendResponse(true); | |
59 } else { | |
60 content::BrowserThread::PostBlockingPoolTask(FROM_HERE, | |
61 base::Bind(&RunPageCyclerFunction::RunTestBrowser, this)); | |
62 process_strategy_->PumpBlockingPool(); // Test purposes only. | |
63 } | |
64 | |
65 return true; | |
66 } | |
67 | |
68 CommandLine RunPageCyclerFunction::RemoveSwitches(const CommandLine& original, | |
69 const std::vector<std::string>& to_remove) { | |
70 std::vector<const char*> to_keep; | |
71 const CommandLine::SwitchMap& current_switches = original.GetSwitches(); | |
72 CommandLine filtered(original.GetProgram()); | |
73 | |
74 // Retain in |to_keep| all current swtiches *not* in |to_remove|. | |
75 for (CommandLine::SwitchMap::const_iterator itr = current_switches.begin(); | |
76 itr != current_switches.end(); ++itr) { | |
77 if (std::find(to_remove.begin(), to_remove.end(), (*itr).first) == | |
78 to_remove.end()) { | |
79 to_keep.push_back((*itr).first.c_str()); | |
80 } | |
81 } | |
82 | |
83 // Rely on std::vector keeping its contents in contiguous order. | |
84 // (This is documented STL spec.) | |
85 filtered.CopySwitchesFrom(original, &to_keep.front(), to_keep.size()); | |
86 | |
87 return filtered; | |
88 } | |
89 | |
90 // Runs on BlockingPool thread. Invoked from UI thread and passes back to | |
91 // UI thread for |Final()| callback to JS side. | |
92 void RunPageCyclerFunction::RunTestBrowser() { | |
93 // Remove any current switch settings that would interfere with test browser | |
94 // commandline setup. | |
95 std::vector<std::string> remove_switches; | |
96 remove_switches.push_back(switches::kUserDataDir); | |
97 remove_switches.push_back(switches::kVisitURLs); | |
98 remove_switches.push_back(switches::kPlaybackMode); | |
99 remove_switches.push_back(switches::kRecordStats); | |
100 remove_switches.push_back(switches::kLoadExtension); | |
101 | |
102 CommandLine line = RemoveSwitches(base_command_line_, remove_switches); | |
103 | |
104 // Add the user-data-dir switch, since this is common to both call types. | |
105 line.AppendSwitchPath(switches::kUserDataDir, user_data_dir_); | |
106 | |
107 // Test browsers must run as if they are not in first-run mode | |
108 line.AppendSwitch(switches::kNoFirstRun); | |
109 | |
110 // Create and fill a temp file to communicate the URL list to the test | |
111 // browser. | |
112 base::FilePath url_path; | |
113 file_util::CreateTemporaryFile(&url_path); | |
114 file_util::WriteFile(url_path, url_contents_.c_str(), url_contents_.size()); | |
115 line.AppendSwitchPath(switches::kVisitURLs, url_path); | |
116 | |
117 // Set up Capture- or Replay-specific commandline switches. | |
118 AddSwitches(&line); | |
119 | |
120 base::FilePath error_file_path = url_path.DirName(). | |
121 Append(url_path.BaseName().value() + | |
122 base::FilePath::StringType(kURLErrorsSuffix)); | |
123 | |
124 LOG(ERROR) << "Test browser commandline: " << line.GetCommandLineString() << | |
125 " will be repeated " << repeat_count_ << " times...."; | |
126 | |
127 // Run the test browser (or a mockup, depending on |process_strategy_|. | |
128 while (repeat_count_-- && errors_.empty() && | |
129 !base::PathExists(error_file_path)) | |
130 process_strategy_->RunProcess(line, &errors_); | |
131 | |
132 // Read URL errors file if there is one, and save errors in |errors_|. | |
133 // Odd extension handling needed because temp files have lots of "."s in | |
134 // their names, and we need to cleanly add kURLErrorsSuffix as a final | |
135 // extension. | |
136 if (errors_.empty() && base::PathExists(error_file_path)) { | |
137 std::string error_content; | |
138 file_util::ReadFileToString(error_file_path, &error_content); | |
139 | |
140 base::SplitString(error_content, '\n', &errors_); | |
141 } | |
142 | |
143 // Do any special post-test-browser file reading (e.g. stats report)) | |
144 // while we're on the BlockingPool thread. | |
145 ReadReplyFiles(); | |
146 | |
147 // Back to UI thread to finish up the JS call. | |
148 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
149 base::Bind(&RunPageCyclerFunction::Finish, this)); | |
150 } | |
151 | |
152 const ProcessStrategy &RunPageCyclerFunction::GetProcessStrategy() { | |
153 return *process_strategy_; | |
154 } | |
155 | |
156 // RecordCaptureURLsFunction ------------------------------------------------ | |
157 | |
158 RecordCaptureURLsFunction::RecordCaptureURLsFunction() | |
159 : RunPageCyclerFunction(new ProductionProcessStrategy()) {} | |
160 | |
161 RecordCaptureURLsFunction::RecordCaptureURLsFunction(ProcessStrategy* strategy) | |
162 : RunPageCyclerFunction(strategy) {} | |
163 | |
164 // Fetch data for possible optional switch for an extension to load. | |
165 bool RecordCaptureURLsFunction::ParseJSParameters() { | |
166 scoped_ptr<record::CaptureURLs::Params> params( | |
167 record::CaptureURLs::Params::Create(*args_)); | |
168 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
169 | |
170 url_contents_ = JoinString(params->urls, '\n'); | |
171 // TODO(cstaley): Can't just use captureName -- gotta stick it in a temp dir. | |
172 // TODO(cstaley): Ensure that capture name is suitable as directory name. | |
173 user_data_dir_ = base::FilePath::FromUTF8Unsafe(params->capture_name); | |
174 | |
175 return true; | |
176 } | |
177 | |
178 // RecordCaptureURLsFunction adds "record-mode" to sub-browser call, and returns | |
179 // just the (possibly empty) error list. | |
180 void RecordCaptureURLsFunction::AddSwitches(CommandLine* line) { | |
181 if (!line->HasSwitch(switches::kRecordMode)) | |
182 line->AppendSwitch(switches::kRecordMode); | |
183 } | |
184 | |
185 void RecordCaptureURLsFunction::Finish() { | |
186 results_ = record::CaptureURLs::Results::Create(errors_); | |
187 SendResponse(true); | |
188 } | |
189 | |
190 // RecordReplayURLsFunction ------------------------------------------------ | |
191 | |
192 RecordReplayURLsFunction::RecordReplayURLsFunction() | |
193 : RunPageCyclerFunction(new ProductionProcessStrategy()), | |
194 run_time_ms_(0.0) { | |
195 } | |
196 | |
197 RecordReplayURLsFunction::RecordReplayURLsFunction(ProcessStrategy* strategy) | |
198 : RunPageCyclerFunction(strategy), run_time_ms_(0.0) { | |
199 } | |
200 | |
201 RecordReplayURLsFunction::~RecordReplayURLsFunction() {} | |
202 | |
203 // Fetch data for possible optional switches for a repeat count and an | |
204 // extension to load. | |
205 bool RecordReplayURLsFunction::ParseJSParameters() { | |
206 scoped_ptr<record::ReplayURLs::Params> params( | |
207 record::ReplayURLs::Params::Create(*args_)); | |
208 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
209 | |
210 | |
211 // TODO(cstaley): Must build full temp dir from capture_name | |
212 user_data_dir_ = base::FilePath::FromUTF8Unsafe(params->capture_name); | |
213 | |
214 // TODO(cstaley): Get this from user data dir ultimately | |
215 url_contents_ = "http://www.google.com\nhttp://www.amazon.com"; | |
216 | |
217 repeat_count_ = params->repeat_count; | |
218 | |
219 if (params->details.get()) { | |
220 if (params->details->extension_path.get()) | |
221 extension_path_ = | |
222 base::FilePath::FromUTF8Unsafe(*params->details->extension_path); | |
223 } | |
224 | |
225 return true; | |
226 } | |
227 | |
228 // Add special switches, if indicated, for repeat count and extension to load, | |
229 // plus temp file into which to place stats. (Can't do this in | |
230 // ParseJSParameters because file creation can't go on the UI thread.) | |
231 // Plus, initialize time to create run time statistic. | |
232 void RecordReplayURLsFunction::AddSwitches(CommandLine* line) { | |
233 file_util::CreateTemporaryFile(&stats_file_path_); | |
234 | |
235 if (!extension_path_.empty()) | |
236 line->AppendSwitchPath(switches::kLoadExtension, extension_path_); | |
237 line->AppendSwitch(switches::kPlaybackMode); | |
238 line->AppendSwitchPath(switches::kRecordStats, stats_file_path_); | |
239 | |
240 timer_ = base::Time::NowFromSystemTime(); | |
241 } | |
242 | |
243 // Read stats file, and get run time. | |
244 void RecordReplayURLsFunction::ReadReplyFiles() { | |
245 file_util::ReadFileToString(stats_file_path_, &stats_); | |
246 | |
247 run_time_ms_ = (base::Time::NowFromSystemTime() - timer_).InMillisecondsF(); | |
248 } | |
249 | |
250 void RecordReplayURLsFunction::Finish() { | |
251 record::ReplayURLsResult result; | |
252 | |
253 result.run_time = run_time_ms_; | |
254 result.stats = stats_; | |
255 result.errors = errors_; | |
256 | |
257 results_ = record::ReplayURLs::Results::Create(result); | |
258 SendResponse(true); | |
259 } | |
260 | |
261 } // namespace extensions | |
OLD | NEW |