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_MANAGEMENT_MANAGEMENT_API_H_ 6 #define CHROME_BROWSER_EXTENSIONS_API_MANAGEMENT_MANAGEMENT_API_H_ 7 8 #include "base/compiler_specific.h" 9 #include "base/scoped_observer.h" 10 #include "base/task/cancelable_task_tracker.h" 11 #include "chrome/browser/extensions/bookmark_app_helper.h" 12 #include "chrome/browser/extensions/chrome_extension_function.h" 13 #include "chrome/browser/extensions/extension_install_prompt.h" 14 #include "chrome/browser/extensions/extension_uninstall_dialog.h" 15 #include "chrome/common/web_application_info.h" 16 #include "components/favicon_base/favicon_types.h" 17 #include "components/keyed_service/core/keyed_service.h" 18 #include "extensions/browser/browser_context_keyed_api_factory.h" 19 #include "extensions/browser/event_router.h" 20 #include "extensions/browser/extension_registry_observer.h" 21 22 class ExtensionService; 23 class ExtensionUninstallDialog; 24 25 namespace extensions { 26 class ExtensionRegistry; 27 28 class ManagementFunction : public ChromeSyncExtensionFunction { 29 protected: 30 virtual ~ManagementFunction() {} 31 32 ExtensionService* service(); 33 }; 34 35 class AsyncManagementFunction : public ChromeAsyncExtensionFunction { 36 protected: 37 virtual ~AsyncManagementFunction() {} 38 39 ExtensionService* service(); 40 }; 41 42 class ManagementGetAllFunction : public ManagementFunction { 43 public: 44 DECLARE_EXTENSION_FUNCTION("management.getAll", MANAGEMENT_GETALL) 45 46 protected: 47 virtual ~ManagementGetAllFunction() {} 48 49 // ExtensionFunction: 50 virtual bool RunSync() OVERRIDE; 51 }; 52 53 class ManagementGetFunction : public ManagementFunction { 54 public: 55 DECLARE_EXTENSION_FUNCTION("management.get", MANAGEMENT_GET) 56 57 protected: 58 virtual ~ManagementGetFunction() {} 59 60 // ExtensionFunction: 61 virtual bool RunSync() OVERRIDE; 62 }; 63 64 class ManagementGetSelfFunction : public ManagementFunction { 65 public: 66 DECLARE_EXTENSION_FUNCTION("management.getSelf", MANAGEMENT_GETSELF) 67 68 protected: 69 virtual ~ManagementGetSelfFunction() {} 70 71 // ExtensionFunction: 72 virtual bool RunSync() OVERRIDE; 73 }; 74 75 class ManagementGetPermissionWarningsByIdFunction : public ManagementFunction { 76 public: 77 DECLARE_EXTENSION_FUNCTION("management.getPermissionWarningsById", 78 MANAGEMENT_GETPERMISSIONWARNINGSBYID) 79 80 protected: 81 virtual ~ManagementGetPermissionWarningsByIdFunction() {} 82 83 // ExtensionFunction: 84 virtual bool RunSync() OVERRIDE; 85 }; 86 87 class ManagementGetPermissionWarningsByManifestFunction 88 : public ChromeAsyncExtensionFunction { 89 public: 90 DECLARE_EXTENSION_FUNCTION( 91 "management.getPermissionWarningsByManifest", 92 MANAGEMENT_GETPERMISSIONWARNINGSBYMANIFEST); 93 94 // Called when utility process finishes. 95 void OnParseSuccess(scoped_ptr<base::DictionaryValue> parsed_manifest); 96 void OnParseFailure(const std::string& error); 97 98 protected: 99 virtual ~ManagementGetPermissionWarningsByManifestFunction() {} 100 101 // ExtensionFunction: 102 virtual bool RunAsync() OVERRIDE; 103 }; 104 105 class ManagementLaunchAppFunction : public ManagementFunction { 106 public: 107 DECLARE_EXTENSION_FUNCTION("management.launchApp", MANAGEMENT_LAUNCHAPP) 108 109 protected: 110 virtual ~ManagementLaunchAppFunction() {} 111 112 // ExtensionFunction: 113 virtual bool RunSync() OVERRIDE; 114 }; 115 116 class ManagementSetEnabledFunction : public AsyncManagementFunction, 117 public ExtensionInstallPrompt::Delegate { 118 public: 119 DECLARE_EXTENSION_FUNCTION("management.setEnabled", MANAGEMENT_SETENABLED) 120 121 ManagementSetEnabledFunction(); 122 123 protected: 124 virtual ~ManagementSetEnabledFunction(); 125 126 // ExtensionFunction: 127 virtual bool RunAsync() OVERRIDE; 128 129 // ExtensionInstallPrompt::Delegate. 130 virtual void InstallUIProceed() OVERRIDE; 131 virtual void InstallUIAbort(bool user_initiated) OVERRIDE; 132 133 private: 134 std::string extension_id_; 135 136 // Used for prompting to re-enable items with permissions escalation updates. 137 scoped_ptr<ExtensionInstallPrompt> install_prompt_; 138 }; 139 140 class ManagementUninstallFunctionBase : public AsyncManagementFunction, 141 public ExtensionUninstallDialog::Delegate { 142 public: 143 ManagementUninstallFunctionBase(); 144 145 static void SetAutoConfirmForTest(bool should_proceed); 146 147 // ExtensionUninstallDialog::Delegate implementation. 148 virtual void ExtensionUninstallAccepted() OVERRIDE; 149 virtual void ExtensionUninstallCanceled() OVERRIDE; 150 151 protected: 152 virtual ~ManagementUninstallFunctionBase(); 153 154 bool Uninstall(const std::string& extension_id, bool show_confirm_dialog); 155 private: 156 157 // If should_uninstall is true, this method does the actual uninstall. 158 // If |show_uninstall_dialog|, then this function will be called by one of the 159 // Accepted/Canceled callbacks. Otherwise, it's called directly from RunAsync. 160 void Finish(bool should_uninstall); 161 162 std::string extension_id_; 163 scoped_ptr<ExtensionUninstallDialog> extension_uninstall_dialog_; 164 }; 165 166 class ManagementUninstallFunction : public ManagementUninstallFunctionBase { 167 public: 168 DECLARE_EXTENSION_FUNCTION("management.uninstall", MANAGEMENT_UNINSTALL) 169 170 ManagementUninstallFunction(); 171 172 private: 173 virtual ~ManagementUninstallFunction(); 174 175 virtual bool RunAsync() OVERRIDE; 176 }; 177 178 class ManagementUninstallSelfFunction : public ManagementUninstallFunctionBase { 179 public: 180 DECLARE_EXTENSION_FUNCTION("management.uninstallSelf", 181 MANAGEMENT_UNINSTALLSELF); 182 183 ManagementUninstallSelfFunction(); 184 185 private: 186 virtual ~ManagementUninstallSelfFunction(); 187 188 virtual bool RunAsync() OVERRIDE; 189 }; 190 191 class ManagementCreateAppShortcutFunction : public AsyncManagementFunction { 192 public: 193 DECLARE_EXTENSION_FUNCTION("management.createAppShortcut", 194 MANAGEMENT_CREATEAPPSHORTCUT); 195 196 ManagementCreateAppShortcutFunction(); 197 198 void OnCloseShortcutPrompt(bool created); 199 200 static void SetAutoConfirmForTest(bool should_proceed); 201 202 protected: 203 virtual ~ManagementCreateAppShortcutFunction(); 204 205 virtual bool RunAsync() OVERRIDE; 206 }; 207 208 class ManagementSetLaunchTypeFunction : public ManagementFunction { 209 public: 210 DECLARE_EXTENSION_FUNCTION("management.setLaunchType", 211 MANAGEMENT_SETLAUNCHTYPE); 212 213 protected: 214 virtual ~ManagementSetLaunchTypeFunction() {} 215 216 virtual bool RunSync() OVERRIDE; 217 }; 218 219 class ManagementGenerateAppForLinkFunction : public AsyncManagementFunction { 220 public: 221 DECLARE_EXTENSION_FUNCTION("management.generateAppForLink", 222 MANAGEMENT_GENERATEAPPFORLINK); 223 224 ManagementGenerateAppForLinkFunction(); 225 226 protected: 227 virtual ~ManagementGenerateAppForLinkFunction(); 228 229 virtual bool RunAsync() OVERRIDE; 230 231 private: 232 void OnFaviconForApp(const favicon_base::FaviconImageResult& image_result); 233 void FinishCreateBookmarkApp(const Extension* extension, 234 const WebApplicationInfo& web_app_info); 235 236 std::string title_; 237 GURL launch_url_; 238 239 scoped_ptr<BookmarkAppHelper> bookmark_app_helper_; 240 241 // Used for favicon loading tasks. 242 base::CancelableTaskTracker cancelable_task_tracker_; 243 }; 244 245 class ManagementEventRouter : public ExtensionRegistryObserver { 246 public: 247 explicit ManagementEventRouter(content::BrowserContext* context); 248 virtual ~ManagementEventRouter(); 249 250 private: 251 // ExtensionRegistryObserver implementation. 252 virtual void OnExtensionLoaded(content::BrowserContext* browser_context, 253 const Extension* extension) OVERRIDE; 254 virtual void OnExtensionUnloaded( 255 content::BrowserContext* browser_context, 256 const Extension* extension, 257 UnloadedExtensionInfo::Reason reason) OVERRIDE; 258 virtual void OnExtensionInstalled(content::BrowserContext* browser_context, 259 const Extension* extension, 260 bool is_update) OVERRIDE; 261 virtual void OnExtensionUninstalled( 262 content::BrowserContext* browser_context, 263 const Extension* extension, 264 extensions::UninstallReason reason) OVERRIDE; 265 266 // Dispatches management api events to listening extensions. 267 void BroadcastEvent(const Extension* extension, const char* event_name); 268 269 content::BrowserContext* browser_context_; 270 271 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> 272 extension_registry_observer_; 273 274 DISALLOW_COPY_AND_ASSIGN(ManagementEventRouter); 275 }; 276 277 class ManagementAPI : public BrowserContextKeyedAPI, 278 public EventRouter::Observer { 279 public: 280 explicit ManagementAPI(content::BrowserContext* context); 281 virtual ~ManagementAPI(); 282 283 // KeyedService implementation. 284 virtual void Shutdown() OVERRIDE; 285 286 // BrowserContextKeyedAPI implementation. 287 static BrowserContextKeyedAPIFactory<ManagementAPI>* GetFactoryInstance(); 288 289 // EventRouter::Observer implementation. 290 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE; 291 292 private: 293 friend class BrowserContextKeyedAPIFactory<ManagementAPI>; 294 295 content::BrowserContext* browser_context_; 296 297 // BrowserContextKeyedAPI implementation. 298 static const char* service_name() { 299 return "ManagementAPI"; 300 } 301 static const bool kServiceIsNULLWhileTesting = true; 302 303 // Created lazily upon OnListenerAdded. 304 scoped_ptr<ManagementEventRouter> management_event_router_; 305 306 DISALLOW_COPY_AND_ASSIGN(ManagementAPI); 307 }; 308 309 } // namespace extensions 310 311 #endif // CHROME_BROWSER_EXTENSIONS_API_MANAGEMENT_MANAGEMENT_API_H_ 312