OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 SERVICES_RESOURCE_COORDINATOR_TRACING_RECORDER_H_ |
| 6 #define SERVICES_RESOURCE_COORDINATOR_TRACING_RECORDER_H_ |
| 7 |
| 8 #include "base/callback_forward.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/sequenced_task_runner.h" |
| 14 #include "base/values.h" |
| 15 #include "mojo/public/cpp/bindings/binding.h" |
| 16 #include "services/resource_coordinator/public/interfaces/tracing/tracing.mojom.
h" |
| 17 |
| 18 namespace tracing { |
| 19 |
| 20 class Recorder : public mojom::Recorder { |
| 21 public: |
| 22 // The tracing service creates instances of the |Recorder| class and send them |
| 23 // to agents. The agents then use the recorder for sending trace data to the |
| 24 // tracing service. |
| 25 // |
| 26 // |data_is_array| tells the recorder whether the data is of type array or |
| 27 // string. Chunks of type array are concatenated using a comma as the |
| 28 // separator; chuunks of type string are concatenated without a separator. |
| 29 // |
| 30 // |on_data_change_callback| is run whenever the recorder receives data from |
| 31 // the agent or when the connection is lost to notify the tracing service of |
| 32 // the data change. |
| 33 // |
| 34 // |background_task_runner| is used so that if the tracing service is run on |
| 35 // the UI thread we do not block it. So buffering trace events and copying |
| 36 // them to data pipes are done on a background thread. |
| 37 Recorder( |
| 38 mojom::RecorderRequest request, |
| 39 bool data_is_array, |
| 40 const base::Closure& on_data_change_callback, |
| 41 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner); |
| 42 ~Recorder() override; |
| 43 |
| 44 const std::string& data() const { |
| 45 // All access to |data_| should be done on the background thread. |
| 46 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 47 return data_; |
| 48 } |
| 49 |
| 50 void clear_data() { |
| 51 // All access to |data_| should be done on the background thread. |
| 52 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 53 data_.clear(); |
| 54 } |
| 55 |
| 56 const base::DictionaryValue& metadata() const { return metadata_; } |
| 57 bool is_recording() const { return is_recording_; } |
| 58 bool data_is_array() const { return data_is_array_; } |
| 59 |
| 60 private: |
| 61 friend class RecorderTest; // For testing. |
| 62 // mojom::Recorder |
| 63 // These are called by agents for sending trace data to the tracing service. |
| 64 void AddChunk(const std::string& chunk) override; |
| 65 void AddMetadata(std::unique_ptr<base::DictionaryValue> metadata) override; |
| 66 |
| 67 void OnConnectionError(); |
| 68 |
| 69 std::string data_; |
| 70 base::DictionaryValue metadata_; |
| 71 bool is_recording_; |
| 72 bool data_is_array_; |
| 73 base::Closure on_data_change_callback_; |
| 74 // To avoid blocking the UI thread if the tracing service is run on the UI |
| 75 // thread, we make sure that buffering trace events and copying them to the |
| 76 // final stream is done on a background thread. |
| 77 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; |
| 78 mojo::Binding<mojom::Recorder> binding_; |
| 79 base::WeakPtrFactory<Recorder> weak_factory_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(Recorder); |
| 82 }; |
| 83 |
| 84 } // namespace tracing |
| 85 #endif // SERVICES_RESOURCE_COORDINATOR_TRACING_RECORDER_H_ |
OLD | NEW |