Chromium Code Reviews| 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/browser/extensions/extension_record_api.h" | 5 #include "chrome/browser/extensions/extension_record_api.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/process_util.h" | 12 #include "base/process_util.h" |
| 13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
| 14 #include "base/string_split.h" | 14 #include "base/string_split.h" |
| 15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 | 17 |
| 18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/extensions/api/experimental_record.h" | 19 #include "chrome/common/extensions/api/experimental_record.h" |
| 20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/common/content_switches.h" | 21 #include "content/public/common/content_switches.h" |
| 22 | 22 |
| 23 namespace record = extensions::api::experimental_record; | 23 namespace record = extensions::api::experimental_record; |
| 24 | 24 |
| 25 const FilePath::CharType kFirstRunFile[] = FILE_PATH_LITERAL("First Run"); | |
| 26 | |
| 25 ProcessStrategy::~ProcessStrategy() {} | 27 ProcessStrategy::~ProcessStrategy() {} |
| 26 | 28 |
| 27 void ProductionProcessStrategy::RunProcess(const CommandLine& line) { | 29 void ProductionProcessStrategy::RunProcess(const CommandLine& line) { |
| 28 base::LaunchOptions options; | 30 base::LaunchOptions options; |
| 29 | 31 |
| 30 options.wait = true; | 32 options.wait = true; |
| 31 base::LaunchProcess(line, options, NULL); | 33 base::LaunchProcess(line, options, NULL); |
| 32 } | 34 } |
| 33 | 35 |
| 34 RunPageCyclerFunction::RunPageCyclerFunction(ProcessStrategy* strategy) | 36 RunPageCyclerFunction::RunPageCyclerFunction(ProcessStrategy* strategy) |
| 35 : base_command_line_(*CommandLine::ForCurrentProcess()), | 37 : repeat_count_(1), base_command_line_(*CommandLine::ForCurrentProcess()), |
| 36 process_strategy_(strategy) {} | 38 process_strategy_(strategy) {} |
| 37 | 39 |
| 38 RunPageCyclerFunction::~RunPageCyclerFunction() {} | 40 RunPageCyclerFunction::~RunPageCyclerFunction() {} |
| 39 | 41 |
| 40 bool RunPageCyclerFunction::RunImpl() { | 42 bool RunPageCyclerFunction::RunImpl() { |
| 41 if (!ParseJSParameters()) | 43 if (!ParseJSParameters()) |
| 42 return false; | 44 return false; |
| 43 | 45 |
| 44 // If we've had any errors reportable to the JS caller so far (in | 46 // If we've had any errors reportable to the JS caller so far (in |
| 45 // parameter parsing) then return a list of such errors, else perform | 47 // parameter parsing) then return a list of such errors, else perform |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 remove_switches.push_back(switches::kVisitURLsCount); | 91 remove_switches.push_back(switches::kVisitURLsCount); |
| 90 remove_switches.push_back(switches::kPlaybackMode); | 92 remove_switches.push_back(switches::kPlaybackMode); |
| 91 remove_switches.push_back(switches::kRecordStats); | 93 remove_switches.push_back(switches::kRecordStats); |
| 92 remove_switches.push_back(switches::kLoadExtension); | 94 remove_switches.push_back(switches::kLoadExtension); |
| 93 | 95 |
| 94 CommandLine line = RemoveSwitches(base_command_line_, remove_switches); | 96 CommandLine line = RemoveSwitches(base_command_line_, remove_switches); |
| 95 | 97 |
| 96 // Add the user-data-dir switch, since this is common to both call types. | 98 // Add the user-data-dir switch, since this is common to both call types. |
| 97 line.AppendSwitchPath(switches::kUserDataDir, user_data_dir_); | 99 line.AppendSwitchPath(switches::kUserDataDir, user_data_dir_); |
| 98 | 100 |
| 101 // Test browsers must run as if they are not in first-run mode, so if | |
| 102 // user_data_dir_ doesn't exist, create it and insert "First Run" file | |
| 103 if (!file_util::PathExists(user_data_dir_)) { | |
|
Aaron Boodman
2012/05/16 23:37:00
You can use the --no-first-run command line switch
clintstaley
2012/05/31 01:54:30
Now he tells me :). Sorry -- I should have done b
| |
| 104 file_util::CreateDirectory(user_data_dir_); | |
| 105 FilePath firstRunFilePath | |
| 106 = user_data_dir_.Append(FilePath::StringType(kFirstRunFile)); | |
| 107 file_util::WriteFile(firstRunFilePath, "", 0); | |
| 108 } | |
| 109 | |
| 99 // Do the same for visit-urls, creating a temp file to communicate the | 110 // Do the same for visit-urls, creating a temp file to communicate the |
| 100 // URL list to the test browser. | 111 // URL list to the test browser. |
| 101 FilePath url_path; | 112 FilePath url_path; |
| 102 file_util::CreateTemporaryFile(&url_path); | 113 file_util::CreateTemporaryFile(&url_path); |
| 103 file_util::WriteFile(url_path, url_contents_.c_str(), url_contents_.size()); | 114 file_util::WriteFile(url_path, url_contents_.c_str(), url_contents_.size()); |
| 104 line.AppendSwitchPath(switches::kVisitURLs, url_path); | 115 line.AppendSwitchPath(switches::kVisitURLs, url_path); |
| 105 | 116 |
| 106 // Set up Capture- or Replay-specific commandline switches. | 117 // Set up Capture- or Replay-specific commandline switches. |
| 107 AddSwitches(&line); | 118 AddSwitches(&line); |
| 108 LOG(ERROR) << "Test browser commandline: " << line.GetCommandLineString(); | 119 LOG(ERROR) << "Test browser commandline: " << line.GetCommandLineString(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 | 159 |
| 149 // Fetch data for possible optional switches for a repeat count and an | 160 // Fetch data for possible optional switches for a repeat count and an |
| 150 // extension to load. | 161 // extension to load. |
| 151 bool CaptureURLsFunction::ParseJSParameters() { | 162 bool CaptureURLsFunction::ParseJSParameters() { |
| 152 scoped_ptr<record::CaptureURLs::Params> params( | 163 scoped_ptr<record::CaptureURLs::Params> params( |
| 153 record::CaptureURLs::Params::Create(*args_)); | 164 record::CaptureURLs::Params::Create(*args_)); |
| 154 EXTENSION_FUNCTION_VALIDATE(params.get()); | 165 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 155 | 166 |
| 156 url_contents_ = JoinString(params->urls, '\n'); | 167 url_contents_ = JoinString(params->urls, '\n'); |
| 157 user_data_dir_ = FilePath::FromUTF8Unsafe(params->cache_directory_path); | 168 user_data_dir_ = FilePath::FromUTF8Unsafe(params->cache_directory_path); |
| 169 repeat_count_ = params->repeat_count; | |
| 158 | 170 |
| 159 return true; | 171 return true; |
| 160 } | 172 } |
| 161 | 173 |
| 162 // CaptureURLsFunction adds "record-mode" to sub-browser call, and returns | 174 // CaptureURLsFunction adds "record-mode" to sub-browser call, and returns |
| 163 // just the (possibly empty) error list. | 175 // just the (possibly empty) error list. |
| 164 void CaptureURLsFunction::AddSwitches(CommandLine* line) { | 176 void CaptureURLsFunction::AddSwitches(CommandLine* line) { |
| 177 line->AppendSwitchASCII(switches::kVisitURLsCount, | |
| 178 base::Int64ToString(repeat_count_)); | |
|
Aaron Boodman
2012/05/16 23:37:00
params should all line up.
clintstaley
2012/05/31 01:54:30
Done.
| |
| 179 | |
| 165 if (!line->HasSwitch(switches::kRecordMode)) | 180 if (!line->HasSwitch(switches::kRecordMode)) |
| 166 line->AppendSwitch(switches::kRecordMode); | 181 line->AppendSwitch(switches::kRecordMode); |
| 167 } | 182 } |
| 168 | 183 |
| 169 void CaptureURLsFunction::Finish() { | 184 void CaptureURLsFunction::Finish() { |
| 170 result_.reset(record::CaptureURLs::Result::Create(errors_)); | 185 result_.reset(record::CaptureURLs::Result::Create(errors_)); |
| 171 SendResponse(true); | 186 SendResponse(true); |
| 172 } | 187 } |
| 173 | 188 |
| 174 | 189 |
| 175 // ReplayURLsFunction ------------------------------------------------ | 190 // ReplayURLsFunction ------------------------------------------------ |
| 176 | 191 |
| 177 ReplayURLsFunction::ReplayURLsFunction() | 192 ReplayURLsFunction::ReplayURLsFunction() |
| 178 : RunPageCyclerFunction(new ProductionProcessStrategy()), | 193 : RunPageCyclerFunction(new ProductionProcessStrategy()), |
| 179 repeat_count_(1), run_time_ms_(0) { | 194 run_time_ms_(0) { |
| 180 } | 195 } |
| 181 | 196 |
| 182 ReplayURLsFunction::ReplayURLsFunction(ProcessStrategy* strategy) | 197 ReplayURLsFunction::ReplayURLsFunction(ProcessStrategy* strategy) |
| 183 : RunPageCyclerFunction(strategy), | 198 : RunPageCyclerFunction(strategy), run_time_ms_(0) { |
| 184 repeat_count_(1), run_time_ms_(0) { | |
| 185 } | 199 } |
| 186 | 200 |
| 187 ReplayURLsFunction::~ReplayURLsFunction() {} | 201 ReplayURLsFunction::~ReplayURLsFunction() {} |
| 188 | 202 |
| 189 // Fetch data for possible optional switches for a repeat count and an | 203 // Fetch data for possible optional switches for a repeat count and an |
| 190 // extension to load. | 204 // extension to load. |
| 191 bool ReplayURLsFunction::ParseJSParameters() { | 205 bool ReplayURLsFunction::ParseJSParameters() { |
| 192 scoped_ptr<record::ReplayURLs::Params> params( | 206 scoped_ptr<record::ReplayURLs::Params> params( |
| 193 record::ReplayURLs::Params::Create(*args_)); | 207 record::ReplayURLs::Params::Create(*args_)); |
| 194 EXTENSION_FUNCTION_VALIDATE(params.get()); | 208 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 195 | 209 |
| 196 url_contents_ = JoinString(params->urls, '\n'); | 210 url_contents_ = JoinString(params->urls, '\n'); |
| 197 user_data_dir_ = FilePath::FromUTF8Unsafe(params->capture_directory_path); | 211 user_data_dir_ = FilePath::FromUTF8Unsafe(params->capture_directory_path); |
| 212 repeat_count_ = params->repeat_count; | |
| 198 | 213 |
| 199 if (params->details.get()) { | 214 if (params->details.get()) { |
| 200 if (params->details->repeat_count.get()) | |
| 201 repeat_count_ = *params->details->repeat_count; | |
| 202 if (params->details->extension_path.get()) | 215 if (params->details->extension_path.get()) |
| 203 extension_path_ = | 216 extension_path_ = |
| 204 FilePath::FromUTF8Unsafe(*params->details->extension_path); | 217 FilePath::FromUTF8Unsafe(*params->details->extension_path); |
| 205 } | 218 } |
| 206 | 219 |
| 207 return true; | 220 return true; |
| 208 } | 221 } |
| 209 | 222 |
| 210 // Add special switches, if indicated, for repeat count and extension to load, | 223 // Add special switches, if indicated, for repeat count and extension to load, |
| 211 // plus temp file into which to place stats. (Can't do this in | 224 // plus temp file into which to place stats. (Can't do this in |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 235 record::ReplayURLsResult result; | 248 record::ReplayURLsResult result; |
| 236 | 249 |
| 237 result.run_time = run_time_ms_; | 250 result.run_time = run_time_ms_; |
| 238 result.stats = stats_; | 251 result.stats = stats_; |
| 239 result.errors = errors_; | 252 result.errors = errors_; |
| 240 | 253 |
| 241 result_.reset(record::ReplayURLs::Result::Create(result)); | 254 result_.reset(record::ReplayURLs::Result::Create(result)); |
| 242 SendResponse(true); | 255 SendResponse(true); |
| 243 } | 256 } |
| 244 | 257 |
| OLD | NEW |