Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / runtime / runtime_api.h
1 // Copyright (c) 2012 The Chromium Authors. 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 CHROME_BROWSER_EXTENSIONS_API_RUNTIME_RUNTIME_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_RUNTIME_RUNTIME_API_H_
7
8 #include <string>
9
10 #include "chrome/browser/extensions/chrome_extension_function.h"
11 #include "chrome/common/extensions/api/runtime.h"
12 #include "content/public/browser/notification_observer.h"
13 #include "content/public/browser/notification_registrar.h"
14 #include "extensions/browser/browser_context_keyed_api_factory.h"
15 #include "extensions/browser/update_observer.h"
16
17 class Profile;
18
19 namespace base {
20 class Version;
21 }
22
23 namespace content {
24 class BrowserContext;
25 }
26
27 namespace extensions {
28 class Extension;
29 class ExtensionHost;
30
31 // Runtime API dispatches onStartup, onInstalled, and similar events to
32 // extensions. There is one instance shared between a browser context and
33 // its related incognito instance.
34 class RuntimeAPI : public BrowserContextKeyedAPI,
35                    public content::NotificationObserver,
36                    public extensions::UpdateObserver {
37  public:
38   static BrowserContextKeyedAPIFactory<RuntimeAPI>* GetFactoryInstance();
39
40   explicit RuntimeAPI(content::BrowserContext* context);
41   virtual ~RuntimeAPI();
42
43   // content::NotificationObserver overrides:
44   virtual void Observe(int type,
45                        const content::NotificationSource& source,
46                        const content::NotificationDetails& details) OVERRIDE;
47
48  private:
49   friend class BrowserContextKeyedAPIFactory<RuntimeAPI>;
50
51   void OnExtensionsReady();
52   void OnExtensionLoaded(const Extension* extension);
53   void OnExtensionInstalled(const Extension* extension);
54   void OnExtensionUninstalled(const Extension* extension);
55
56   // BrowserContextKeyedAPI implementation:
57   static const char* service_name() { return "RuntimeAPI"; }
58   static const bool kServiceRedirectedInIncognito = true;
59   static const bool kServiceIsNULLWhileTesting = true;
60
61   // extensions::UpdateObserver overrides:
62   virtual void OnAppUpdateAvailable(const Extension* extension) OVERRIDE;
63   virtual void OnChromeUpdateAvailable() OVERRIDE;
64
65   content::BrowserContext* browser_context_;
66
67   // True if we should dispatch the chrome.runtime.onInstalled event with
68   // reason "chrome_update" upon loading each extension.
69   bool dispatch_chrome_updated_event_;
70
71   // Whether the API registered with the ExtensionService to receive
72   // update notifications.
73   bool registered_for_updates_;
74
75   content::NotificationRegistrar registrar_;
76
77   DISALLOW_COPY_AND_ASSIGN(RuntimeAPI);
78 };
79
80 class RuntimeEventRouter {
81  public:
82   // Dispatches the onStartup event to all currently-loaded extensions.
83   static void DispatchOnStartupEvent(content::BrowserContext* context,
84                                      const std::string& extension_id);
85
86   // Dispatches the onInstalled event to the given extension.
87   static void DispatchOnInstalledEvent(content::BrowserContext* context,
88                                        const std::string& extension_id,
89                                        const base::Version& old_version,
90                                        bool chrome_updated);
91
92   // Dispatches the onUpdateAvailable event to the given extension.
93   static void DispatchOnUpdateAvailableEvent(
94       Profile* profile,
95       const std::string& extension_id,
96       const base::DictionaryValue* manifest);
97
98   // Dispatches the onBrowserUpdateAvailable event to all extensions.
99   static void DispatchOnBrowserUpdateAvailableEvent(Profile* profile);
100
101   // Dispatches the onRestartRequired event to the given app.
102   static void DispatchOnRestartRequiredEvent(
103       Profile* profile,
104       const std::string& app_id,
105       api::runtime::OnRestartRequired::Reason reason);
106
107   // Does any work needed at extension uninstall (e.g. load uninstall url).
108   static void OnExtensionUninstalled(Profile* profile,
109                                      const std::string& extension_id);
110 };
111
112 class RuntimeGetBackgroundPageFunction : public ChromeAsyncExtensionFunction {
113  public:
114   DECLARE_EXTENSION_FUNCTION("runtime.getBackgroundPage",
115                              RUNTIME_GETBACKGROUNDPAGE)
116
117  protected:
118   virtual ~RuntimeGetBackgroundPageFunction() {}
119   virtual bool RunImpl() OVERRIDE;
120
121  private:
122   void OnPageLoaded(ExtensionHost*);
123 };
124
125 class RuntimeSetUninstallURLFunction : public ChromeSyncExtensionFunction {
126  public:
127   DECLARE_EXTENSION_FUNCTION("runtime.setUninstallURL",
128                              RUNTIME_SETUNINSTALLURL)
129
130  protected:
131   virtual ~RuntimeSetUninstallURLFunction() {}
132   virtual bool RunImpl() OVERRIDE;
133 };
134
135 class RuntimeReloadFunction : public ChromeSyncExtensionFunction {
136  public:
137   DECLARE_EXTENSION_FUNCTION("runtime.reload", RUNTIME_RELOAD)
138
139  protected:
140   virtual ~RuntimeReloadFunction() {}
141   virtual bool RunImpl() OVERRIDE;
142 };
143
144 class RuntimeRequestUpdateCheckFunction : public ChromeAsyncExtensionFunction,
145                                           public content::NotificationObserver {
146  public:
147   DECLARE_EXTENSION_FUNCTION("runtime.requestUpdateCheck",
148                              RUNTIME_REQUESTUPDATECHECK)
149
150   RuntimeRequestUpdateCheckFunction();
151  protected:
152   virtual ~RuntimeRequestUpdateCheckFunction() {}
153   virtual bool RunImpl() OVERRIDE;
154
155   // Implements content::NotificationObserver interface.
156   virtual void Observe(int type,
157                        const content::NotificationSource& source,
158                        const content::NotificationDetails& details) OVERRIDE;
159  private:
160   void CheckComplete();
161   void ReplyUpdateFound(const std::string& version);
162
163   content::NotificationRegistrar registrar_;
164   bool did_reply_;
165 };
166
167 class RuntimeRestartFunction : public ChromeSyncExtensionFunction {
168  public:
169   DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART)
170
171  protected:
172   virtual ~RuntimeRestartFunction() {}
173   virtual bool RunImpl() OVERRIDE;
174 };
175
176 class RuntimeGetPlatformInfoFunction : public ChromeSyncExtensionFunction {
177  public:
178   DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo",
179                              RUNTIME_GETPLATFORMINFO);
180  protected:
181   virtual ~RuntimeGetPlatformInfoFunction() {}
182   virtual bool RunImpl() OVERRIDE;
183 };
184
185 class RuntimeGetPackageDirectoryEntryFunction
186     : public ChromeSyncExtensionFunction {
187  public:
188   DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry",
189                              RUNTIME_GETPACKAGEDIRECTORYENTRY)
190
191  protected:
192   virtual ~RuntimeGetPackageDirectoryEntryFunction() {}
193   virtual bool RunImpl() OVERRIDE;
194 };
195
196 }  // namespace extensions
197
198 #endif  // CHROME_BROWSER_EXTENSIONS_API_RUNTIME_RUNTIME_API_H_