1 // Copyright 2019 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_UI_WEBUI_CHROMEOS_ARC_GRAPHICS_TRACING_ARC_GRAPHICS_TRACING_HANDLER_H_ 6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_ARC_GRAPHICS_TRACING_ARC_GRAPHICS_TRACING_HANDLER_H_ 7 8 #include <memory> 9 #include <string> 10 #include <utility> 11 #include <vector> 12 13 #include "base/macros.h" 14 #include "base/memory/weak_ptr.h" 15 #include "base/timer/timer.h" 16 #include "chrome/browser/ui/webui/chromeos/arc_graphics_tracing/arc_graphics_tracing.h" 17 #include "components/exo/surface_observer.h" 18 #include "content/public/browser/web_ui_message_handler.h" 19 #include "ui/aura/window_observer.h" 20 #include "ui/events/event_handler.h" 21 #include "ui/wm/public/activation_change_observer.h" 22 23 namespace arc { 24 class ArcGraphicsJankDetector; 25 class ArcSystemStatCollector; 26 } // namespace arc 27 28 namespace base { 29 class ListValue; 30 } // namespace base 31 32 namespace exo { 33 class Surface; 34 class WMHelper; 35 } // namespace exo 36 37 namespace chromeos { 38 39 class ArcGraphicsTracingHandler : public content::WebUIMessageHandler, 40 public wm::ActivationChangeObserver, 41 public aura::WindowObserver, 42 public ui::EventHandler, 43 public exo::SurfaceObserver { 44 public: 45 explicit ArcGraphicsTracingHandler(ArcGraphicsTracingMode mode); 46 ~ArcGraphicsTracingHandler() override; 47 48 // content::WebUIMessageHandler: 49 void RegisterMessages() override; 50 51 // wm::ActivationChangeObserver: 52 void OnWindowActivated(ActivationReason reason, 53 aura::Window* gained_active, 54 aura::Window* lost_active) override; 55 56 // aura::WindowObserver: 57 void OnWindowPropertyChanged(aura::Window* window, 58 const void* key, 59 intptr_t old) override; 60 void OnWindowDestroying(aura::Window* window) override; 61 62 // ui::EventHandler: 63 void OnKeyEvent(ui::KeyEvent* event) override; 64 65 // exo::SurfaceObserver: 66 void OnSurfaceDestroying(exo::Surface* surface) override; 67 void OnCommit(exo::Surface* surface) override; 68 69 private: 70 void Activate(); 71 void StartTracing(); 72 void StopTracing(); 73 void StopTracingAndActivate(); 74 void SetStatus(const std::string& status); 75 76 void OnTracingStarted(); 77 void OnTracingStopped(std::unique_ptr<std::string> trace_data); 78 79 // Called when graphics model is built or load. Extra string parameter 80 // contains a status. In case model cannot be built/load empty |base::Value| 81 // is returned. 82 void OnGraphicsModelReady(std::pair<base::Value, std::string> result); 83 84 // Handlers for calls from JS. 85 void HandleReady(const base::ListValue* args); 86 void HandleSetStopOnJank(const base::ListValue* args); 87 void HandleSetMaxTime(const base::ListValue* args); 88 void HandleLoadFromText(const base::ListValue* args); 89 90 // Updates title and icon for the active ARC window. 91 void UpdateActiveArcWindowInfo(); 92 93 // Stops tracking ARC window for janks. 94 void DiscardActiveArcWindow(); 95 96 // Called in case jank is detected in active ARC window. 97 void OnJankDetected(const base::Time& timestamp); 98 99 // Returns max sampling interval to display. 100 base::TimeDelta GetMaxInterval() const; 101 102 // Indicates that tracing was initiated by this handler. 103 bool tracing_active_ = false; 104 105 // Determines if tracing should stop in case jank is detected runtime. 106 // Works only in |ArcGraphicsTracingMode::kFull| mode. 107 bool stop_on_jank_ = true; 108 109 // Determines the maximum tracing time. 110 // Works only in |ArcGraphicsTracingMode::kOverview| mode. 111 base::TimeDelta max_tracing_time_ = base::TimeDelta::FromSeconds(5); 112 113 base::OneShotTimer stop_tracing_timer_; 114 115 exo::WMHelper* const wm_helper_; 116 117 const ArcGraphicsTracingMode mode_; 118 119 aura::Window* arc_active_window_ = nullptr; 120 121 // Time filter for tracing, since ARC++ window was activated last until 122 // tracing is stopped. 123 base::TimeTicks tracing_time_min_; 124 base::TimeTicks tracing_time_max_; 125 126 // Task id and title for current ARC window. 127 int active_task_id_ = -1; 128 129 // Used to detect janks for the currently active ARC++ window. 130 std::unique_ptr<arc::ArcGraphicsJankDetector> jank_detector_; 131 132 // Collects system stat runtime. 133 std::unique_ptr<arc::ArcSystemStatCollector> system_stat_colletor_; 134 135 // Information about active task, title and icon. 136 std::string active_task_title_; 137 std::vector<unsigned char> active_task_icon_png_; 138 base::Time timestamp_; 139 140 base::WeakPtrFactory<ArcGraphicsTracingHandler> weak_ptr_factory_{this}; 141 142 DISALLOW_COPY_AND_ASSIGN(ArcGraphicsTracingHandler); 143 }; 144 145 } // namespace chromeos 146 147 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_ARC_GRAPHICS_TRACING_ARC_GRAPHICS_TRACING_HANDLER_H_ 148