| OLD | NEW |
| (Empty) | |
| 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_EXTENSION_ACTION_EXTENSION_ACTION_API_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_EXTENSION_ACTION_EXTENSION_ACTION_API_H_ |
| 7 |
| 8 #include "base/memory/weak_ptr.h" |
| 9 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h" |
| 10 #include "chrome/browser/extensions/extension_action.h" |
| 11 #include "chrome/browser/extensions/extension_function.h" |
| 12 #include "content/public/browser/notification_observer.h" |
| 13 #include "content/public/browser/notification_registrar.h" |
| 14 |
| 15 namespace base { |
| 16 class DictionaryValue; |
| 17 } |
| 18 |
| 19 namespace content { |
| 20 class WebContents; |
| 21 } |
| 22 |
| 23 namespace extensions { |
| 24 class TabHelper; |
| 25 |
| 26 class ExtensionActionAPI : public ProfileKeyedAPI { |
| 27 public: |
| 28 explicit ExtensionActionAPI(Profile* profile); |
| 29 virtual ~ExtensionActionAPI(); |
| 30 |
| 31 // ProfileKeyedAPI implementation. |
| 32 static ProfileKeyedAPIFactory<ExtensionActionAPI>* GetFactoryInstance(); |
| 33 |
| 34 private: |
| 35 friend class ProfileKeyedAPIFactory<ExtensionActionAPI>; |
| 36 |
| 37 // ProfileKeyedAPI implementation. |
| 38 static const char* service_name() { return "ExtensionActionAPI"; } |
| 39 |
| 40 DISALLOW_COPY_AND_ASSIGN(ExtensionActionAPI); |
| 41 }; |
| 42 |
| 43 // This class manages reading and writing browser action values from storage. |
| 44 class ExtensionActionStorageManager |
| 45 : public content::NotificationObserver, |
| 46 public base::SupportsWeakPtr<ExtensionActionStorageManager> { |
| 47 public: |
| 48 explicit ExtensionActionStorageManager(Profile* profile); |
| 49 virtual ~ExtensionActionStorageManager(); |
| 50 |
| 51 private: |
| 52 // NotificationObserver: |
| 53 virtual void Observe(int type, |
| 54 const content::NotificationSource& source, |
| 55 const content::NotificationDetails& details) OVERRIDE; |
| 56 |
| 57 // Reads/Writes the ExtensionAction's default values to/from storage. |
| 58 void WriteToStorage(ExtensionAction* extension_action); |
| 59 void ReadFromStorage( |
| 60 const std::string& extension_id, scoped_ptr<base::Value> value); |
| 61 |
| 62 Profile* profile_; |
| 63 content::NotificationRegistrar registrar_; |
| 64 }; |
| 65 |
| 66 // Implementation of the browserAction, pageAction, and scriptBadge APIs. |
| 67 // |
| 68 // Divergent behaviour between the three is minimal (pageAction and scriptBadge |
| 69 // have required tabIds while browserAction's are optional, they have different |
| 70 // internal browser notification requirements, and not all functions are defined |
| 71 // for all APIs). |
| 72 class ExtensionActionFunction : public SyncExtensionFunction { |
| 73 public: |
| 74 static bool ParseCSSColorString(const std::string& color_string, |
| 75 SkColor* result); |
| 76 |
| 77 protected: |
| 78 ExtensionActionFunction(); |
| 79 virtual ~ExtensionActionFunction(); |
| 80 virtual bool RunImpl() OVERRIDE; |
| 81 virtual bool RunExtensionAction() = 0; |
| 82 |
| 83 bool ExtractDataFromArguments(); |
| 84 void NotifyChange(); |
| 85 void NotifyBrowserActionChange(); |
| 86 void NotifyLocationBarChange(); |
| 87 void NotifySystemIndicatorChange(); |
| 88 bool SetVisible(bool visible); |
| 89 |
| 90 // Extension-related information for |tab_id_|. |
| 91 // CHECK-fails if there is no tab. |
| 92 extensions::TabHelper& tab_helper() const; |
| 93 |
| 94 // All the extension action APIs take a single argument called details that |
| 95 // is a dictionary. |
| 96 base::DictionaryValue* details_; |
| 97 |
| 98 // The tab id the extension action function should apply to, if any, or |
| 99 // kDefaultTabId if none was specified. |
| 100 int tab_id_; |
| 101 |
| 102 // WebContents for |tab_id_| if one exists. |
| 103 content::WebContents* contents_; |
| 104 |
| 105 // The extension action for the current extension. |
| 106 ExtensionAction* extension_action_; |
| 107 }; |
| 108 |
| 109 // |
| 110 // Implementations of each extension action API. |
| 111 // |
| 112 // pageAction and browserAction bindings are created for these by extending them |
| 113 // then declaring an EXTENSION_FUNCTION_NAME. |
| 114 // |
| 115 |
| 116 // show |
| 117 class ExtensionActionShowFunction : public ExtensionActionFunction { |
| 118 protected: |
| 119 virtual ~ExtensionActionShowFunction() {} |
| 120 virtual bool RunExtensionAction() OVERRIDE; |
| 121 }; |
| 122 |
| 123 // hide |
| 124 class ExtensionActionHideFunction : public ExtensionActionFunction { |
| 125 protected: |
| 126 virtual ~ExtensionActionHideFunction() {} |
| 127 virtual bool RunExtensionAction() OVERRIDE; |
| 128 }; |
| 129 |
| 130 // setIcon |
| 131 class ExtensionActionSetIconFunction : public ExtensionActionFunction { |
| 132 protected: |
| 133 virtual ~ExtensionActionSetIconFunction() {} |
| 134 virtual bool RunExtensionAction() OVERRIDE; |
| 135 }; |
| 136 |
| 137 // setTitle |
| 138 class ExtensionActionSetTitleFunction : public ExtensionActionFunction { |
| 139 protected: |
| 140 virtual ~ExtensionActionSetTitleFunction() {} |
| 141 virtual bool RunExtensionAction() OVERRIDE; |
| 142 }; |
| 143 |
| 144 // setPopup |
| 145 class ExtensionActionSetPopupFunction : public ExtensionActionFunction { |
| 146 protected: |
| 147 virtual ~ExtensionActionSetPopupFunction() {} |
| 148 virtual bool RunExtensionAction() OVERRIDE; |
| 149 }; |
| 150 |
| 151 // setBadgeText |
| 152 class ExtensionActionSetBadgeTextFunction : public ExtensionActionFunction { |
| 153 protected: |
| 154 virtual ~ExtensionActionSetBadgeTextFunction() {} |
| 155 virtual bool RunExtensionAction() OVERRIDE; |
| 156 }; |
| 157 |
| 158 // setBadgeBackgroundColor |
| 159 class ExtensionActionSetBadgeBackgroundColorFunction |
| 160 : public ExtensionActionFunction { |
| 161 protected: |
| 162 virtual ~ExtensionActionSetBadgeBackgroundColorFunction() {} |
| 163 virtual bool RunExtensionAction() OVERRIDE; |
| 164 }; |
| 165 |
| 166 // getTitle |
| 167 class ExtensionActionGetTitleFunction : public ExtensionActionFunction { |
| 168 protected: |
| 169 virtual ~ExtensionActionGetTitleFunction() {} |
| 170 virtual bool RunExtensionAction() OVERRIDE; |
| 171 }; |
| 172 |
| 173 // getPopup |
| 174 class ExtensionActionGetPopupFunction : public ExtensionActionFunction { |
| 175 protected: |
| 176 virtual ~ExtensionActionGetPopupFunction() {} |
| 177 virtual bool RunExtensionAction() OVERRIDE; |
| 178 }; |
| 179 |
| 180 // getBadgeText |
| 181 class ExtensionActionGetBadgeTextFunction : public ExtensionActionFunction { |
| 182 protected: |
| 183 virtual ~ExtensionActionGetBadgeTextFunction() {} |
| 184 virtual bool RunExtensionAction() OVERRIDE; |
| 185 }; |
| 186 |
| 187 // getBadgeBackgroundColor |
| 188 class ExtensionActionGetBadgeBackgroundColorFunction |
| 189 : public ExtensionActionFunction { |
| 190 protected: |
| 191 virtual ~ExtensionActionGetBadgeBackgroundColorFunction() {} |
| 192 virtual bool RunExtensionAction() OVERRIDE; |
| 193 }; |
| 194 |
| 195 // |
| 196 // browserAction.* aliases for supported browserAction APIs. |
| 197 // |
| 198 |
| 199 class BrowserActionSetIconFunction : public ExtensionActionSetIconFunction { |
| 200 public: |
| 201 DECLARE_EXTENSION_FUNCTION("browserAction.setIcon", BROWSERACTION_SETICON) |
| 202 |
| 203 protected: |
| 204 virtual ~BrowserActionSetIconFunction() {} |
| 205 }; |
| 206 |
| 207 class BrowserActionSetTitleFunction : public ExtensionActionSetTitleFunction { |
| 208 public: |
| 209 DECLARE_EXTENSION_FUNCTION("browserAction.setTitle", BROWSERACTION_SETTITLE) |
| 210 |
| 211 protected: |
| 212 virtual ~BrowserActionSetTitleFunction() {} |
| 213 }; |
| 214 |
| 215 class BrowserActionSetPopupFunction : public ExtensionActionSetPopupFunction { |
| 216 public: |
| 217 DECLARE_EXTENSION_FUNCTION("browserAction.setPopup", BROWSERACTION_SETPOPUP) |
| 218 |
| 219 protected: |
| 220 virtual ~BrowserActionSetPopupFunction() {} |
| 221 }; |
| 222 |
| 223 class BrowserActionGetTitleFunction : public ExtensionActionGetTitleFunction { |
| 224 public: |
| 225 DECLARE_EXTENSION_FUNCTION("browserAction.getTitle", BROWSERACTION_GETTITLE) |
| 226 |
| 227 protected: |
| 228 virtual ~BrowserActionGetTitleFunction() {} |
| 229 }; |
| 230 |
| 231 class BrowserActionGetPopupFunction : public ExtensionActionGetPopupFunction { |
| 232 public: |
| 233 DECLARE_EXTENSION_FUNCTION("browserAction.getPopup", BROWSERACTION_GETPOPUP) |
| 234 |
| 235 protected: |
| 236 virtual ~BrowserActionGetPopupFunction() {} |
| 237 }; |
| 238 |
| 239 class BrowserActionSetBadgeTextFunction |
| 240 : public ExtensionActionSetBadgeTextFunction { |
| 241 public: |
| 242 DECLARE_EXTENSION_FUNCTION("browserAction.setBadgeText", |
| 243 BROWSERACTION_SETBADGETEXT) |
| 244 |
| 245 protected: |
| 246 virtual ~BrowserActionSetBadgeTextFunction() {} |
| 247 }; |
| 248 |
| 249 class BrowserActionSetBadgeBackgroundColorFunction |
| 250 : public ExtensionActionSetBadgeBackgroundColorFunction { |
| 251 public: |
| 252 DECLARE_EXTENSION_FUNCTION("browserAction.setBadgeBackgroundColor", |
| 253 BROWSERACTION_SETBADGEBACKGROUNDCOLOR) |
| 254 |
| 255 protected: |
| 256 virtual ~BrowserActionSetBadgeBackgroundColorFunction() {} |
| 257 }; |
| 258 |
| 259 class BrowserActionGetBadgeTextFunction |
| 260 : public ExtensionActionGetBadgeTextFunction { |
| 261 public: |
| 262 DECLARE_EXTENSION_FUNCTION("browserAction.getBadgeText", |
| 263 BROWSERACTION_GETBADGETEXT) |
| 264 |
| 265 protected: |
| 266 virtual ~BrowserActionGetBadgeTextFunction() {} |
| 267 }; |
| 268 |
| 269 class BrowserActionGetBadgeBackgroundColorFunction |
| 270 : public ExtensionActionGetBadgeBackgroundColorFunction { |
| 271 public: |
| 272 DECLARE_EXTENSION_FUNCTION("browserAction.getBadgeBackgroundColor", |
| 273 BROWSERACTION_GETBADGEBACKGROUNDCOLOR) |
| 274 |
| 275 protected: |
| 276 virtual ~BrowserActionGetBadgeBackgroundColorFunction() {} |
| 277 }; |
| 278 |
| 279 class BrowserActionEnableFunction : public ExtensionActionShowFunction { |
| 280 public: |
| 281 DECLARE_EXTENSION_FUNCTION("browserAction.enable", BROWSERACTION_ENABLE) |
| 282 |
| 283 protected: |
| 284 virtual ~BrowserActionEnableFunction() {} |
| 285 }; |
| 286 |
| 287 class BrowserActionDisableFunction : public ExtensionActionHideFunction { |
| 288 public: |
| 289 DECLARE_EXTENSION_FUNCTION("browserAction.disable", BROWSERACTION_DISABLE) |
| 290 |
| 291 protected: |
| 292 virtual ~BrowserActionDisableFunction() {} |
| 293 }; |
| 294 |
| 295 // |
| 296 // scriptBadge.* aliases for supported scriptBadge APIs. |
| 297 // |
| 298 |
| 299 class ScriptBadgeSetPopupFunction : public ExtensionActionSetPopupFunction { |
| 300 public: |
| 301 DECLARE_EXTENSION_FUNCTION("scriptBadge.setPopup", SCRIPTBADGE_SETPOPUP) |
| 302 |
| 303 protected: |
| 304 virtual ~ScriptBadgeSetPopupFunction() {} |
| 305 }; |
| 306 |
| 307 class ScriptBadgeGetPopupFunction : public ExtensionActionGetPopupFunction { |
| 308 public: |
| 309 DECLARE_EXTENSION_FUNCTION("scriptBadge.getPopup", SCRIPTBADGE_GETPOPUP) |
| 310 |
| 311 protected: |
| 312 virtual ~ScriptBadgeGetPopupFunction() {} |
| 313 }; |
| 314 |
| 315 // scriptBadge.getAttention(tabId) |
| 316 class ScriptBadgeGetAttentionFunction : public ExtensionActionFunction { |
| 317 public: |
| 318 DECLARE_EXTENSION_FUNCTION("scriptBadge.getAttention", |
| 319 SCRIPTBADGE_GETATTENTION) |
| 320 |
| 321 virtual bool RunExtensionAction() OVERRIDE; |
| 322 |
| 323 protected: |
| 324 virtual ~ScriptBadgeGetAttentionFunction(); |
| 325 }; |
| 326 |
| 327 } // namespace extensions |
| 328 |
| 329 // |
| 330 // pageAction.* aliases for supported pageAction APIs. |
| 331 // |
| 332 |
| 333 class PageActionShowFunction : public extensions::ExtensionActionShowFunction { |
| 334 public: |
| 335 DECLARE_EXTENSION_FUNCTION("pageAction.show", PAGEACTION_SHOW) |
| 336 |
| 337 protected: |
| 338 virtual ~PageActionShowFunction() {} |
| 339 }; |
| 340 |
| 341 class PageActionHideFunction : public extensions::ExtensionActionHideFunction { |
| 342 public: |
| 343 DECLARE_EXTENSION_FUNCTION("pageAction.hide", PAGEACTION_HIDE) |
| 344 |
| 345 protected: |
| 346 virtual ~PageActionHideFunction() {} |
| 347 }; |
| 348 |
| 349 class PageActionSetIconFunction |
| 350 : public extensions::ExtensionActionSetIconFunction { |
| 351 public: |
| 352 DECLARE_EXTENSION_FUNCTION("pageAction.setIcon", PAGEACTION_SETICON) |
| 353 |
| 354 protected: |
| 355 virtual ~PageActionSetIconFunction() {} |
| 356 }; |
| 357 |
| 358 class PageActionSetTitleFunction |
| 359 : public extensions::ExtensionActionSetTitleFunction { |
| 360 public: |
| 361 DECLARE_EXTENSION_FUNCTION("pageAction.setTitle", PAGEACTION_SETTITLE) |
| 362 |
| 363 protected: |
| 364 virtual ~PageActionSetTitleFunction() {} |
| 365 }; |
| 366 |
| 367 class PageActionSetPopupFunction |
| 368 : public extensions::ExtensionActionSetPopupFunction { |
| 369 public: |
| 370 DECLARE_EXTENSION_FUNCTION("pageAction.setPopup", PAGEACTION_SETPOPUP) |
| 371 |
| 372 protected: |
| 373 virtual ~PageActionSetPopupFunction() {} |
| 374 }; |
| 375 |
| 376 class PageActionGetTitleFunction |
| 377 : public extensions::ExtensionActionGetTitleFunction { |
| 378 public: |
| 379 DECLARE_EXTENSION_FUNCTION("pageAction.getTitle", PAGEACTION_GETTITLE) |
| 380 |
| 381 protected: |
| 382 virtual ~PageActionGetTitleFunction() {} |
| 383 }; |
| 384 |
| 385 class PageActionGetPopupFunction |
| 386 : public extensions::ExtensionActionGetPopupFunction { |
| 387 public: |
| 388 DECLARE_EXTENSION_FUNCTION("pageAction.getPopup", PAGEACTION_GETPOPUP) |
| 389 |
| 390 protected: |
| 391 virtual ~PageActionGetPopupFunction() {} |
| 392 }; |
| 393 |
| 394 // Base class for deprecated page actions APIs |
| 395 class PageActionsFunction : public SyncExtensionFunction { |
| 396 protected: |
| 397 PageActionsFunction(); |
| 398 virtual ~PageActionsFunction(); |
| 399 bool SetPageActionEnabled(bool enable); |
| 400 }; |
| 401 |
| 402 // Implement chrome.pageActions.enableForTab(). |
| 403 class EnablePageActionsFunction : public PageActionsFunction { |
| 404 public: |
| 405 DECLARE_EXTENSION_FUNCTION("pageActions.enableForTab", |
| 406 PAGEACTIONS_ENABLEFORTAB) |
| 407 |
| 408 protected: |
| 409 virtual ~EnablePageActionsFunction() {} |
| 410 |
| 411 // ExtensionFunction: |
| 412 virtual bool RunImpl() OVERRIDE; |
| 413 }; |
| 414 |
| 415 // Implement chrome.pageActions.disableForTab(). |
| 416 class DisablePageActionsFunction : public PageActionsFunction { |
| 417 public: |
| 418 DECLARE_EXTENSION_FUNCTION("pageActions.disableForTab", |
| 419 PAGEACTIONS_DISABLEFORTAB) |
| 420 |
| 421 protected: |
| 422 virtual ~DisablePageActionsFunction() {} |
| 423 |
| 424 // ExtensionFunction: |
| 425 virtual bool RunImpl() OVERRIDE; |
| 426 }; |
| 427 |
| 428 #endif // CHROME_BROWSER_EXTENSIONS_API_EXTENSION_ACTION_EXTENSION_ACTION_API_H
_ |
| OLD | NEW |