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_FEEDBACK_TRACING_MANAGER_H_ 6 #define CHROME_BROWSER_FEEDBACK_TRACING_MANAGER_H_ 7 8 #include <map> 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "base/callback.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/weak_ptr.h" 15 #include "content/public/browser/trace_subscriber.h" 16 17 // Callback used for getting the output of a trace. 18 typedef base::Callback<void(scoped_refptr<base::RefCountedString> trace_data)> 19 TraceDataCallback; 20 21 // This class is used to manage performance meterics that can be attached to 22 // feedback reports. This class is a Singleton that is owned by the preference 23 // system. It should only be created when it is enabled, and should only be 24 // accessed elsewhere via Get(). 25 // 26 // When a performance trace is desired, TracingManager::Get()->RequestTrace() 27 // should be invoked. The TracingManager will then start preparing a zipped 28 // version of the performance data. That data can then be requested via 29 // GetTraceData(). When the data is no longer needed, it should be discarded 30 // via DiscardTraceData(). 31 class TracingManager : public content::TraceSubscriber { 32 public: 33 virtual ~TracingManager(); 34 35 // Create a TracingManager. Can only be called when none exists. 36 static scoped_ptr<TracingManager> Create(); 37 38 // Get the current TracingManager. Returns NULL if one doesn't exist. 39 static TracingManager* Get(); 40 41 // Request a trace ending at the current time. If a trace is already being 42 // collected, the id for that trace is returned. 43 int RequestTrace(); 44 45 // Get the trace data for |id|. On success, true is returned, and the data is 46 // returned via |callback|. Returns false on failure. 47 bool GetTraceData(int id, const TraceDataCallback& callback); 48 49 // Discard the data for trace |id|. 50 void DiscardTraceData(int id); 51 52 private: 53 void StartTracing(); 54 55 // content::TraceSubscriber overrides 56 virtual void OnEndTracingComplete() OVERRIDE; 57 virtual void OnTraceDataCollected( 58 const scoped_refptr<base::RefCountedString>& trace_fragment) OVERRIDE; 59 60 TracingManager(); 61 62 // Data being collected from the current trace. 63 std::string data_; 64 65 // ID of the trace that is being collected. 66 int current_trace_id_; 67 68 // Mapping of trace ID to trace data. 69 std::map<int, scoped_refptr<base::RefCountedString> > trace_data_; 70 71 // Callback for the current trace request. 72 TraceDataCallback trace_callback_; 73 74 base::WeakPtrFactory<TracingManager> weak_ptr_factory_; 75 76 DISALLOW_COPY_AND_ASSIGN(TracingManager); 77 }; 78 79 #endif // CHROME_BROWSER_FEEDBACK_TRACING_MANAGER_H_ 80 81