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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_RECORD_API_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_RECORD_API_H_ | |
7 #pragma once | |
8 | |
9 #include "chrome/browser/extensions/extension_function.h" | |
10 #include "base/command_line.h" | |
11 #include "base/file_path.h" | |
12 #include "base/time.h" | |
13 | |
14 namespace { | |
15 | |
16 const FilePath::CharType kURLErrorsSuffix[] = FILE_PATH_LITERAL(".errors"); | |
17 const char kErrorsKey[] = "errors"; | |
18 const char kStatsKey[] = "stats"; | |
19 | |
20 }; | |
21 | |
22 // ProcessStrategy abstracts the API's starting and waiting on a test | |
23 // browser instance. This lets us browser-test the API without actually | |
24 // firing up a sub browser instance. | |
25 class ProcessStrategy { | |
26 public: | |
27 // Needed to void build warnings | |
28 virtual ~ProcessStrategy(); | |
29 | |
30 // Used only in test version to pump the blocking pool queue, | |
31 // which doesn't otherwise happen during test. | |
32 virtual void PumpBlockingPool() {} | |
33 | |
34 // Start up process with given commandline. Real version does just | |
35 // that; test version mocks it up, generating errors or good results, | |
36 // as configured. If there are any problems running the process itself, | |
37 // as opposed to errors in the cycler URL list, etc, report these via | |
38 // |errors| | |
39 virtual void RunProcess(const CommandLine& line, | |
40 std::vector<std::string> *errors) = 0; | |
41 }; | |
42 | |
43 // Production (default) version of ProcessStrategy. See ProcessStrategy | |
44 // comments for more info. This subclass actually starts a sub browser | |
45 // instance. | |
46 class ProductionProcessStrategy : public ProcessStrategy { | |
47 public: | |
48 virtual void RunProcess(const CommandLine& line, | |
49 std::vector<std::string> *errors) OVERRIDE; | |
50 }; | |
51 | |
52 // Both page cycler calls (capture and replay) have a great deal in common, | |
53 // including the need to build and write a url list file, set the user | |
54 // data dir, start a sub-instance of Chrome, and parse a resultant error | |
55 // file. This base class encapslates those common elements. | |
56 class RunPageCyclerFunction : public AsyncExtensionFunction { | |
57 public: | |
58 explicit RunPageCyclerFunction(ProcessStrategy* strategy); | |
59 | |
60 // Make a CommandLine copy of |original|, removing all switches in | |
61 // |to_remove|. | |
62 static CommandLine RemoveSwitches(const CommandLine& original, | |
63 const std::vector<std::string>& to_remove); | |
64 | |
65 // Return ProcessStrategy, to allow for test versions. | |
66 virtual const ProcessStrategy &GetProcessStrategy(); | |
67 | |
68 protected: | |
69 virtual ~RunPageCyclerFunction(); | |
70 | |
71 // Gather common page cycler parameters and store them, then do blocking | |
72 // thread invocation of RunTestBrowser. | |
73 virtual bool RunImpl() OVERRIDE; | |
74 | |
75 // Parse the JS parameters, and store them as member data. | |
76 virtual bool ParseJSParameters() = 0; | |
77 | |
78 // Do a vanilla test browser run, bracketing it immediately before and | |
79 // after with a call of AddSwitches to add special commandline options | |
80 // for Capture or Replay, and ReadReplyFiles to do any special post-run | |
81 // data collection. Gather any error results into |errors_| and then do a | |
82 // BrowserThread call of Finish to return JS values. | |
83 virtual void RunTestBrowser(); | |
84 virtual void AddSwitches(CommandLine* command_line) {} | |
85 | |
86 // The test browser communicates URL errors, performance statistics, and | |
87 // possibly other data by posting them to text files. ReadReplyFiles | |
88 // collects these data for return to the JS side caller. | |
89 virtual void ReadReplyFiles() {} | |
90 | |
91 // Return the values gathered in RunTestBrowser. No common code here; all | |
92 // logic is in subclasses. | |
93 virtual void Finish() {} | |
94 | |
95 FilePath user_data_dir_; | |
96 std::string url_contents_; | |
97 int repeat_count_; | |
98 std::vector<std::string> errors_; | |
99 | |
100 // Base CommandLine on which to build the test browser CommandLine | |
101 CommandLine base_command_line_; | |
102 | |
103 // ProcessStrategy to use for this run. | |
104 scoped_ptr<ProcessStrategy> process_strategy_; | |
105 }; | |
106 | |
107 class CaptureURLsFunction : public RunPageCyclerFunction { | |
108 public: | |
109 DECLARE_EXTENSION_FUNCTION_NAME("experimental.record.captureURLs"); | |
110 | |
111 CaptureURLsFunction(); | |
112 explicit CaptureURLsFunction(ProcessStrategy* strategy); | |
113 | |
114 private: | |
115 virtual ~CaptureURLsFunction() {} | |
116 | |
117 // Read the ReplayDetails parameter if it exists. | |
118 virtual bool ParseJSParameters() OVERRIDE; | |
119 | |
120 // Add record-mode. | |
121 virtual void AddSwitches(CommandLine* command_line) OVERRIDE; | |
122 | |
123 // Return error list. | |
124 virtual void Finish() OVERRIDE; | |
125 }; | |
126 | |
127 class ReplayURLsFunction : public RunPageCyclerFunction { | |
128 public: | |
129 DECLARE_EXTENSION_FUNCTION_NAME("experimental.record.replayURLs"); | |
130 | |
131 ReplayURLsFunction(); | |
132 explicit ReplayURLsFunction(ProcessStrategy* strategy); | |
133 | |
134 private: | |
135 virtual ~ReplayURLsFunction(); | |
136 | |
137 // Read the ReplayDetails parameter if it exists. | |
138 virtual bool ParseJSParameters() OVERRIDE; | |
139 | |
140 // Add visit-urls-count and load-extension. | |
141 virtual void AddSwitches(CommandLine* command_line) OVERRIDE; | |
142 | |
143 // Read stats file. | |
144 virtual void ReadReplyFiles() OVERRIDE; | |
145 | |
146 // Return error list, statistical results, and runtime. | |
147 virtual void Finish() OVERRIDE; | |
148 | |
149 // These data are additional information added to the sub-browser | |
150 // commandline or used to repeatedly run the sub-browser. | |
151 FilePath extension_path_; | |
152 FilePath stats_file_path_; | |
153 | |
154 // This time datum marks the start and end of the sub-browser run. | |
155 base::Time timer_; | |
156 | |
157 // These two data are additional information returned to caller. | |
158 int run_time_ms_; | |
159 std::string stats_; | |
160 }; | |
161 | |
162 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_RECORD_API_H_ | |
OLD | NEW |