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