042048b40cf9783fe0c3a980e74137ebac1ed1ea
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application_event_router.cc
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.
4
5 #include "xwalk/application/browser/application_event_router.h"
6
7 #include <algorithm>
8
9 #include "base/bind.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"
17
18 namespace xwalk {
19 namespace application {
20
21 ApplicationEventRouter::ApplicationEventRouter(const std::string& app_id)
22     : app_id_(app_id),
23       main_document_loaded_(false) {
24 }
25
26 ApplicationEventRouter::~ApplicationEventRouter() {
27   DetachAllObservers();
28 }
29
30 void ApplicationEventRouter::DidStopLoading(
31     content::RenderViewHost* render_view_host) {
32   main_document_loaded_ = true;
33   ProcessLazyEvents();
34 }
35
36 void ApplicationEventRouter::RenderProcessGone(
37     base::TerminationStatus status) {
38   main_document_loaded_ = false;
39   DetachAllObservers();
40 }
41
42 void ApplicationEventRouter::ObserveMainDocument(
43     content::WebContents* contents) {
44   content::WebContentsObserver::Observe(contents);
45 }
46
47 void ApplicationEventRouter::SetMainEvents(
48     const std::set<std::string>& events) {
49   main_events_ = events;
50 }
51
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>())));
57   }
58   observers_[event_name]->AddObserver(observer);
59 }
60
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 << ").";
66     return;
67   }
68
69   observers_[event_name]->RemoveObserver(observer);
70   if (!observers_[event_name]->might_have_observers()) {
71     observers_.erase(event_name);
72   }
73 }
74
75 void ApplicationEventRouter::DetachObserver(EventObserver* observer) {
76   ObserverListMap::iterator it = observers_.begin();
77   while (it != observers_.end())
78     DetachObserver((it++)->first, observer);
79 }
80
81 void ApplicationEventRouter::DispatchEvent(scoped_refptr<Event> event) {
82   const std::string& event_name = event->name();
83
84   if (!main_document_loaded_) {
85     lazy_events_.push_back(event);
86     return;
87   }
88
89   if (!ContainsKey(observers_, event_name) ||
90       !observers_[event_name]->might_have_observers()) {
91     LOG(INFO) << "No registered handler to handle event:" << event_name;
92     return;
93   }
94
95   ProcessEvent(event);
96 }
97
98 void ApplicationEventRouter::DetachAllObservers() {
99   ObserverListMap::iterator it = observers_.begin();
100   for (; it != observers_.end(); ++it) {
101     it->second->Clear();
102   }
103   observers_.clear();
104 }
105
106 void ApplicationEventRouter::ProcessLazyEvents() {
107   if (lazy_events_.empty())
108     return;
109
110   EventVector::iterator it = lazy_events_.begin();
111   for (; it != lazy_events_.end(); ++it)
112     ProcessEvent(*it);
113   lazy_events_.clear();
114 }
115
116 void ApplicationEventRouter::ProcessEvent(scoped_refptr<Event> event) {
117   const std::string& event_name = event->name();
118   if (ContainsKey(observers_, event_name)) {
119     FOR_EACH_OBSERVER(
120         EventObserver, *observers_[event_name], Observe(app_id_, event));
121   }
122 }
123
124 }  // namespace application
125 }  // namespace xwalk