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 #ifndef XWALK_APPLICATION_BROWSER_APPLICATION_EVENT_MANAGER_H_
6 #define XWALK_APPLICATION_BROWSER_APPLICATION_EVENT_MANAGER_H_
12 #include "base/callback.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/values.h"
17 #include "xwalk/application/browser/application_service.h"
18 #include "xwalk/application/browser/event_observer.h"
25 namespace application {
27 class ApplicationEventRouter;
29 class Event : public base::RefCounted<Event> {
31 static scoped_refptr<Event> CreateEvent(
32 const std::string& event_name, scoped_ptr<base::ListValue> event_args);
34 const std::string& name() const { return name_; }
35 base::ListValue* args() const { return args_.get(); }
38 friend class base::RefCounted<Event>;
39 Event(const std::string& event_name, scoped_ptr<base::ListValue> event_args);
42 // The event to dispatch.
44 // Arguments to send to the event handler.
45 scoped_ptr<base::ListValue> args_;
48 // This's the service class manages all application event routers.
49 class ApplicationEventManager : public ApplicationService::Observer {
51 ApplicationEventManager();
52 virtual ~ApplicationEventManager();
54 // Create app router when app is loaded.
55 void AddEventRouterForApp(scoped_refptr<ApplicationData> app_data);
56 // Destroy app router when app is unloaded.
57 void RemoveEventRouterForApp(scoped_refptr<ApplicationData> app_data);
59 void SendEvent(const std::string& app_id,
60 scoped_refptr<Event> event);
61 // TODO(xiang): Dispatch event to all loaded applications.
62 void BroadcastEvent(scoped_refptr<Event> event) { NOTIMPLEMENTED(); }
64 void AttachObserver(const std::string& app_id,
65 const std::string& event_name,
66 EventObserver* observer);
67 void DetachObserver(const std::string& app_id,
68 const std::string& event_name,
69 EventObserver* observer);
70 void DetachObserver(EventObserver* observer);
73 // Implementation of ApplicationService::Observer.
74 virtual void DidLaunchApplication(Application* app) OVERRIDE;
75 virtual void WillDestroyApplication(Application* app) OVERRIDE;
77 ApplicationEventRouter* GetAppRouter(const std::string& app_id);
79 typedef std::map<std::string, linked_ptr<ApplicationEventRouter> >
81 AppRouterMap app_routers_;
83 DISALLOW_COPY_AND_ASSIGN(ApplicationEventManager);
86 } // namespace application
89 #endif // XWALK_APPLICATION_BROWSER_APPLICATION_EVENT_MANAGER_H_