Home | History | Annotate | Download | only in chromeos
      1 // Copyright 2013 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_CHROMEOS_SWAP_METRICS_H_
      6 #define CHROME_BROWSER_CHROMEOS_SWAP_METRICS_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "chrome/browser/ui/browser_list_observer.h"
     14 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
     15 #include "ui/base/events/event_handler.h"
     16 
     17 namespace base {
     18 class FilePath;
     19 }
     20 
     21 namespace chromeos {
     22 
     23 // Watches for bursts of swap activity and CPU consumption after interesting UI
     24 // events like tab switch or scrolling, recording the values to UMA statistics.
     25 // Only records stats for the last active browser.
     26 class SwapMetrics : public chrome::BrowserListObserver,
     27                     public TabStripModelObserver,
     28                     public ui::EventHandler {
     29  public:
     30   SwapMetrics();
     31   virtual ~SwapMetrics();
     32 
     33   // chrome::BrowserListObserver overrides:
     34   virtual void OnBrowserRemoved(Browser* browser) OVERRIDE;
     35   virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE;
     36 
     37   // TabStripModelObserver overrides:
     38   virtual void ActiveTabChanged(content::WebContents* old_contents,
     39                                 content::WebContents* new_contents,
     40                                 int index,
     41                                 int reason) OVERRIDE;
     42 
     43   // ui::EventHandler overrides:
     44   virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE;
     45   virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE;
     46 
     47  private:
     48   class Backend;
     49 
     50   // Posts a task to record metrics for |sample_index| after |delay_ms|.
     51   static void PostTaskRecordMetrics(scoped_refptr<Backend> backend,
     52                                     size_t sample_index,
     53                                     int delay_ms);
     54 
     55   // Starts a metrics collection run, canceling any run already in progress.
     56   void StartMetricsCollection(const std::string& reason);
     57 
     58   // Sets the browser being monitored for events.
     59   void SetBrowser(Browser* browser);
     60 
     61   // Browser being monitored for UI events.
     62   Browser* browser_;
     63 
     64   // Backend to handle processing in the blocking thread pool.
     65   scoped_refptr<Backend> backend_;
     66 
     67   DISALLOW_COPY_AND_ASSIGN(SwapMetrics);
     68 };
     69 
     70 }  // namespace chromeos
     71 
     72 #endif  // CHROME_BROWSER_CHROMEOS_SWAP_METRICS_H_
     73