| 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_PAGE_CYCLER_PAGE_CYCLER_H_ | |
| 6 #define CHROME_BROWSER_PAGE_CYCLER_PAGE_CYCLER_H_ | |
| 7 | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "chrome/browser/ui/browser_list_observer.h" | |
| 11 #include "content/public/browser/navigation_controller.h" | |
| 12 #include "content/public/browser/web_contents_observer.h" | |
| 13 | |
| 14 class Browser; | |
| 15 | |
| 16 namespace content { | |
| 17 class RenderViewHost; | |
| 18 } // namespace content | |
| 19 | |
| 20 namespace base { | |
| 21 class TimeTicks; | |
| 22 } // namespace base | |
| 23 | |
| 24 // Performance test to track the resources used and speed with which chromium | |
| 25 // fully loads a given set of URLs. This class is created on the UI thread and | |
| 26 // does most of its work there. However, some work happens on background threads | |
| 27 // too; those are named with 'OnBackgroundThread'. | |
| 28 class PageCycler : public base::RefCountedThreadSafe<PageCycler>, | |
| 29 public chrome::BrowserListObserver, | |
| 30 public content::WebContentsObserver { | |
| 31 public: | |
| 32 PageCycler(Browser* browser, const base::FilePath& urls_file); | |
| 33 | |
| 34 // Begin running the page cycler. | |
| 35 void Run(); | |
| 36 | |
| 37 // content::WebContentsObserver | |
| 38 virtual void DidFinishLoad( | |
| 39 int64 frame_id, | |
| 40 const GURL& validated_url, | |
| 41 bool is_main_frame, | |
| 42 content::RenderViewHost* render_view_host) OVERRIDE; | |
| 43 virtual void DidFailProvisionalLoad( | |
| 44 int64 frame_id, | |
| 45 bool is_main_frame, | |
| 46 const GURL& validated_url, | |
| 47 int error_code, | |
| 48 const string16& error_description, | |
| 49 content::RenderViewHost* render_view_host) OVERRIDE; | |
| 50 | |
| 51 // This method should never be necessary while running PageCycler; this is | |
| 52 // for testing purposes only. | |
| 53 const std::vector<GURL>* urls_for_test() { return &urls_; } | |
| 54 | |
| 55 void set_stats_file(const base::FilePath& stats_file) { | |
| 56 stats_file_ = stats_file; | |
| 57 } | |
| 58 void set_errors_file(const base::FilePath& errors_file) { | |
| 59 errors_file_ = errors_file; | |
| 60 } | |
| 61 | |
| 62 | |
| 63 protected: | |
| 64 virtual ~PageCycler(); | |
| 65 | |
| 66 private: | |
| 67 friend class base::RefCountedThreadSafe<PageCycler>; | |
| 68 friend class MockPageCycler; | |
| 69 | |
| 70 // Check to see if a load callback is valid; i.e. the load should be from the | |
| 71 // main frame, the url should not be a chrome error url, and |url_index| | |
| 72 // should not be 0. | |
| 73 bool IsLoadCallbackValid(const GURL& validated_url, | |
| 74 bool is_main_frame); | |
| 75 | |
| 76 // Read in the urls from |urls_file_| and store them in |urls_|. | |
| 77 void ReadURLsOnBackgroundThread(); | |
| 78 | |
| 79 // Perform any initial setup neccessary, and begin visiting the pages. | |
| 80 void BeginCycle(); | |
| 81 | |
| 82 // If |url_index_| points to a valid position in |urls_|, load the url, | |
| 83 // capturing any statistics information. Otherwise, call WriteResults. | |
| 84 void LoadNextURL(); | |
| 85 | |
| 86 // Complete statistics gathering for the finished visit, and try to load the | |
| 87 // next url. | |
| 88 void LoadSucceeded(); | |
| 89 | |
| 90 // Inidicate that the load failed with an error; try to load the next url. | |
| 91 void LoadFailed(const GURL& url, const string16& error_description); | |
| 92 | |
| 93 // Finalize the output strings. | |
| 94 void PrepareResultsOnBackgroundThread(); | |
| 95 | |
| 96 // Write the data stored within |output| to the file indicated by | |
| 97 // |stats_file_|, if |stats_file_| is not empty. Write any errors to | |
| 98 // |errors_file_|. | |
| 99 void WriteResultsOnBackgroundThread(const std::string& output); | |
| 100 | |
| 101 // Perform any necessary cleanup and exit |browser_|; virtual since tests may | |
| 102 // need to override this function. | |
| 103 virtual void Finish(); | |
| 104 | |
| 105 // Called when the Browser to which |browser_| points is closed; exits | |
| 106 // PageCycler. | |
| 107 void Abort(); | |
| 108 | |
| 109 // chrome::BrowserListObserver | |
| 110 virtual void OnBrowserAdded(Browser* browser) OVERRIDE; | |
| 111 virtual void OnBrowserRemoved(Browser* browser) OVERRIDE; | |
| 112 | |
| 113 // The Browser context in which the page cycler is running. | |
| 114 Browser* browser_; | |
| 115 | |
| 116 // The path to the file containing the list of urls to visit. | |
| 117 base::FilePath urls_file_; | |
| 118 | |
| 119 // The path to the file to which we write any errors encountered. | |
| 120 base::FilePath errors_file_; | |
| 121 | |
| 122 // The path to the file to which we write the statistics (optional, may be | |
| 123 // an empty path). | |
| 124 base::FilePath stats_file_; | |
| 125 | |
| 126 // The list of urls to visit. | |
| 127 std::vector<GURL> urls_; | |
| 128 | |
| 129 // The current index into the |urls_| vector. | |
| 130 size_t url_index_; | |
| 131 | |
| 132 // The generated string of urls which we have visited; this is built one url | |
| 133 // at a time as we iterate through the |urls_| vector. This is primarily | |
| 134 // included for interfacing with the previous page_cycler's output style. | |
| 135 std::string urls_string_; | |
| 136 | |
| 137 // The generated string of the times taken to visit each url. As with | |
| 138 // |urls_string_|, this is built as we visit each url, and is primarily to | |
| 139 // produce output similar to the previous page_cycler's. | |
| 140 std::string timings_string_; | |
| 141 | |
| 142 // The time at which we begin the process of loading the next url; this is | |
| 143 // used to calculate the time taken for each url load. | |
| 144 base::TimeTicks initial_time_; | |
| 145 | |
| 146 // Indicates the abort status of the page cycler; true means aborted. | |
| 147 bool aborted_; | |
| 148 | |
| 149 string16 error_; | |
| 150 | |
| 151 DISALLOW_COPY_AND_ASSIGN(PageCycler); | |
| 152 }; | |
| 153 | |
| 154 #endif // CHROME_BROWSER_PAGE_CYCLER_PAGE_CYCLER_H_ | |
| OLD | NEW |