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_manager.h"
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"
12 using content::BrowserThread;
15 namespace application {
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()));
22 Event::Event(const std::string& event_name,
23 scoped_ptr<base::ListValue> event_args)
25 args_(event_args.Pass()) {
32 ApplicationEventManager::ApplicationEventManager() {
35 ApplicationEventManager::~ApplicationEventManager() {
38 void ApplicationEventManager::AddEventRouterForApp(
39 scoped_refptr<ApplicationData> app_data) {
40 std::set<std::string> events = app_data->GetEvents();
42 linked_ptr<ApplicationEventRouter> router(
43 new ApplicationEventRouter(app_data->ID()));
44 router->SetMainEvents(events);
46 app_routers_.insert(std::make_pair(app_data->ID(), router));
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());
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);
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);
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);
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);
85 void ApplicationEventManager::DidLaunchApplication(Application* app) {
86 if (Runtime* runtime = app->GetMainDocumentRuntime()) {
87 ApplicationEventRouter* app_router = GetAppRouter(app->id());
89 app_router->ObserveMainDocument(runtime->web_contents());
93 void ApplicationEventManager::WillDestroyApplication(Application* app) {
94 RemoveEventRouterForApp(app->data());
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();
103 DLOG(WARNING) << "Application " << app_id << " router not found.";
107 } // namespace application