1 // Copyright (c) 2013 Intel Corporation. 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.
5 #include "xwalk/application/browser/application_event_router.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/stl_util.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/web_contents_observer.h"
14 #include "xwalk/application/browser/application_event_manager.h"
15 #include "xwalk/application/browser/application_system.h"
16 #include "xwalk/application/common/event_names.h"
19 namespace application {
21 ApplicationEventRouter::ApplicationEventRouter(const std::string& app_id)
23 main_document_loaded_(false) {
26 ApplicationEventRouter::~ApplicationEventRouter() {
30 void ApplicationEventRouter::DidStopLoading(
31 content::RenderViewHost* render_view_host) {
32 main_document_loaded_ = true;
36 void ApplicationEventRouter::RenderProcessGone(
37 base::TerminationStatus status) {
38 main_document_loaded_ = false;
42 void ApplicationEventRouter::ObserveMainDocument(
43 content::WebContents* contents) {
44 content::WebContentsObserver::Observe(contents);
47 void ApplicationEventRouter::SetMainEvents(
48 const std::set<std::string>& events) {
49 main_events_ = events;
52 void ApplicationEventRouter::AttachObserver(const std::string& event_name,
53 EventObserver* observer) {
54 if (!ContainsKey(observers_, event_name)) {
55 observers_.insert(std::make_pair(
56 event_name, make_linked_ptr(new ObserverList<EventObserver>())));
58 observers_[event_name]->AddObserver(observer);
61 void ApplicationEventRouter::DetachObserver(const std::string& event_name,
62 EventObserver* observer) {
63 if (!ContainsKey(observers_, event_name)) {
64 DLOG(WARNING) << "Can't find attached observer for application(" << app_id_
65 << ") with event(" << event_name << ").";
69 observers_[event_name]->RemoveObserver(observer);
70 if (!observers_[event_name]->might_have_observers()) {
71 observers_.erase(event_name);
75 void ApplicationEventRouter::DetachObserver(EventObserver* observer) {
76 ObserverListMap::iterator it = observers_.begin();
77 while (it != observers_.end())
78 DetachObserver((it++)->first, observer);
81 void ApplicationEventRouter::DispatchEvent(scoped_refptr<Event> event) {
82 const std::string& event_name = event->name();
84 if (!main_document_loaded_) {
85 lazy_events_.push_back(event);
89 if (!ContainsKey(observers_, event_name) ||
90 !observers_[event_name]->might_have_observers()) {
91 LOG(INFO) << "No registered handler to handle event:" << event_name;
98 void ApplicationEventRouter::DetachAllObservers() {
99 ObserverListMap::iterator it = observers_.begin();
100 for (; it != observers_.end(); ++it) {
106 void ApplicationEventRouter::ProcessLazyEvents() {
107 if (lazy_events_.empty())
110 EventVector::iterator it = lazy_events_.begin();
111 for (; it != lazy_events_.end(); ++it)
113 lazy_events_.clear();
116 void ApplicationEventRouter::ProcessEvent(scoped_refptr<Event> event) {
117 const std::string& event_name = event->name();
118 if (ContainsKey(observers_, event_name)) {
120 EventObserver, *observers_[event_name], Observe(app_id_, event));
124 } // namespace application