Upstream version 7.35.138.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application_event_manager.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_manager.h"
6
7 #include "content/public/browser/browser_thread.h"
8 #include "content/public/browser/web_contents_observer.h"
9 #include "xwalk/application/browser/application_event_router.h"
10 #include "xwalk/application/browser/application.h"
11
12 using content::BrowserThread;
13
14 namespace xwalk {
15 namespace application {
16
17 scoped_refptr<Event> Event::CreateEvent(
18     const std::string& event_name, scoped_ptr<base::ListValue> event_args) {
19   return scoped_refptr<Event>(new Event(event_name, event_args.Pass()));
20 }
21
22 Event::Event(const std::string& event_name,
23              scoped_ptr<base::ListValue> event_args)
24   : name_(event_name),
25     args_(event_args.Pass()) {
26   DCHECK(args_);
27 }
28
29 Event::~Event() {
30 }
31
32 ApplicationEventManager::ApplicationEventManager() {
33 }
34
35 ApplicationEventManager::~ApplicationEventManager() {
36 }
37
38 void ApplicationEventManager::AddEventRouterForApp(
39     scoped_refptr<ApplicationData> app_data) {
40   std::set<std::string> events = app_data->GetEvents();
41
42   linked_ptr<ApplicationEventRouter> router(
43       new ApplicationEventRouter(app_data->ID()));
44   router->SetMainEvents(events);
45
46   app_routers_.insert(std::make_pair(app_data->ID(), router));
47 }
48
49 void ApplicationEventManager::RemoveEventRouterForApp(
50     scoped_refptr<ApplicationData> app_data) {
51   DCHECK(app_routers_.find(app_data->ID()) != app_routers_.end());
52   app_routers_.erase(app_data->ID());
53 }
54
55 void ApplicationEventManager::SendEvent(const std::string& app_id,
56                                         scoped_refptr<Event> event) {
57   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
58   if (ApplicationEventRouter* app_router = GetAppRouter(app_id))
59     app_router->DispatchEvent(event);
60 }
61
62 void ApplicationEventManager::AttachObserver(const std::string& app_id,
63                                              const std::string& event_name,
64                                              EventObserver* observer) {
65   DCHECK(content::BrowserThread::CurrentlyOn(BrowserThread::UI));
66   if (ApplicationEventRouter* app_router = GetAppRouter(app_id))
67     app_router->AttachObserver(event_name, observer);
68 }
69
70 void ApplicationEventManager::DetachObserver(const std::string& app_id,
71                                              const std::string& event_name,
72                                              EventObserver* observer) {
73   DCHECK(content::BrowserThread::CurrentlyOn(BrowserThread::UI));
74   if (ApplicationEventRouter* app_router = GetAppRouter(app_id))
75     app_router->DetachObserver(event_name, observer);
76 }
77
78 void ApplicationEventManager::DetachObserver(EventObserver* observer) {
79   DCHECK(content::BrowserThread::CurrentlyOn(BrowserThread::UI));
80   AppRouterMap::iterator it = app_routers_.begin();
81   for (; it != app_routers_.end(); ++it)
82     it->second->DetachObserver(observer);
83 }
84
85 void ApplicationEventManager::DidLaunchApplication(Application* app) {
86   if (Runtime* runtime = app->GetMainDocumentRuntime()) {
87     ApplicationEventRouter* app_router = GetAppRouter(app->id());
88     CHECK(app_router);
89     app_router->ObserveMainDocument(runtime->web_contents());
90   }
91 }
92
93 void ApplicationEventManager::WillDestroyApplication(Application* app) {
94   RemoveEventRouterForApp(app->data());
95 }
96
97 ApplicationEventRouter* ApplicationEventManager::GetAppRouter(
98     const std::string& app_id) {
99   AppRouterMap::iterator it = app_routers_.find(app_id);
100   if (it != app_routers_.end())
101     return it->second.get();
102
103   DLOG(WARNING) << "Application " << app_id << " router not found.";
104   return NULL;
105 }
106
107 }  // namespace application
108 }  // namespace xwalk