7a18dc27b23e164bb26eefc7220713191ddb0672
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application_service.h
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 #ifndef XWALK_APPLICATION_BROWSER_APPLICATION_SERVICE_H_
6 #define XWALK_APPLICATION_BROWSER_APPLICATION_SERVICE_H_
7
8 #include <string>
9 #include "base/files/file_path.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/observer_list.h"
13 #include "xwalk/application/browser/application.h"
14 #include "xwalk/application/common/permission_policy_manager.h"
15 #include "xwalk/runtime/browser/runtime_context.h"
16 #include "xwalk/application/common/application_data.h"
17
18 namespace xwalk {
19
20 class RuntimeContext;
21
22 namespace application {
23
24 class ApplicationStorage;
25 class ApplicationEventManager;
26
27 // The application service manages install, uninstall and updates of
28 // applications.
29 class ApplicationService : public Application::Observer {
30  public:
31   // Client code may use this class (and register with AddObserver below) to
32   // keep track of [un]installation of applications.
33   class Observer {
34    public:
35     virtual void OnApplicationInstalled(const std::string& app_id) {}
36     virtual void OnApplicationUninstalled(const std::string& app_id) {}
37     virtual void OnApplicationUpdated(const std::string& app_id) {}
38     // When we change the application locale, we might get a new name in
39     // the new locale.
40     virtual void OnApplicationNameChanged(const std::string& app_id,
41                                           const std::string& app_name) {}
42
43     virtual void DidLaunchApplication(Application* app) {}
44     virtual void WillDestroyApplication(Application* app) {}
45    protected:
46     virtual ~Observer() {}
47   };
48
49   ApplicationService(RuntimeContext* runtime_context,
50                      ApplicationStorage* app_storage,
51                      ApplicationEventManager* event_manager);
52   virtual ~ApplicationService();
53
54   bool Install(const base::FilePath& path, std::string* id);
55   bool Uninstall(const std::string& id);
56   bool Update(const std::string& id, const base::FilePath& path);
57   void ChangeLocale(const std::string& locale);
58
59   Application* Launch(scoped_refptr<ApplicationData> application_data,
60                       const Application::LaunchParams& launch_params);
61   // Launch an installed application using application id.
62   Application* Launch(
63       const std::string& id,
64       const Application::LaunchParams& params = Application::LaunchParams());
65   // Launch an unpacked application using path to a local directory which
66   // contains manifest file.
67   Application* Launch(
68       const base::FilePath& path,
69       const Application::LaunchParams& params = Application::LaunchParams());
70
71   Application* GetApplicationByRenderHostID(int id) const;
72   Application* GetApplicationByID(const std::string& app_id) const;
73
74   const ScopedVector<Application>& active_applications() const {
75       return applications_; }
76
77   void AddObserver(Observer* observer);
78   void RemoveObserver(Observer* observer);
79
80   // Check whether application has permission to access API of extension.
81   void CheckAPIAccessControl(const std::string& app_id,
82       const std::string& extension_name,
83       const std::string& api_name, const PermissionCallback& callback);
84   // Register APIs implemented by extension. This method will be called
85   // when application register extensions.
86   // Parameter perm_table is a string which is a map between extension
87   // and it includes APIs. For example perm_table is like '{"bluetooth":
88   // ["read", "write", "management"]}'.
89   bool RegisterPermissions(const std::string& app_id,
90       const std::string& extension_name,
91       const std::string& perm_table);
92
93  private:
94   // Implementation of Application::Observer.
95   virtual void OnApplicationTerminated(Application* app) OVERRIDE;
96
97
98   xwalk::RuntimeContext* runtime_context_;
99   ApplicationStorage* application_storage_;
100   ApplicationEventManager* event_manager_;
101   ScopedVector<Application> applications_;
102   ObserverList<Observer> observers_;
103   scoped_ptr<PermissionPolicyManager> permission_policy_handler_;
104
105   DISALLOW_COPY_AND_ASSIGN(ApplicationService);
106 };
107
108 }  // namespace application
109 }  // namespace xwalk
110
111 #endif  // XWALK_APPLICATION_BROWSER_APPLICATION_SERVICE_H_