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