// Copyright (c) 2017 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. #ifndef ATOM_BROWSER_API_STREAM_SUBSCRIBER_H_ #define ATOM_BROWSER_API_STREAM_SUBSCRIBER_H_ #include #include #include #include #include "base/callback.h" #include "base/memory/ref_counted.h" #include "base/memory/ref_counted_delete_on_sequence.h" #include "base/memory/weak_ptr.h" #include "content/public/browser/browser_thread.h" #include "v8/include/v8.h" namespace atom { class URLRequestStreamJob; } namespace mate { class Arguments; class StreamSubscriber : public base::RefCountedDeleteOnSequence { public: REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE(); StreamSubscriber(v8::Isolate* isolate, v8::Local emitter, base::WeakPtr url_job, scoped_refptr ui_task_runner); private: friend class base::DeleteHelper; friend class base::RefCountedDeleteOnSequence; using JSHandlersMap = std::map>; using EventCallback = base::Callback; ~StreamSubscriber(); void On(const std::string& event, EventCallback&& callback); // NOLINT void Off(const std::string& event); void OnData(mate::Arguments* args); void OnEnd(mate::Arguments* args); void OnError(mate::Arguments* args); void RemoveAllListeners(); void RemoveListener(JSHandlersMap::iterator it); v8::Isolate* isolate_; v8::Global emitter_; base::WeakPtr url_job_; JSHandlersMap js_handlers_; base::WeakPtrFactory weak_factory_; }; } // namespace mate #endif // ATOM_BROWSER_API_STREAM_SUBSCRIBER_H_