1 // Copyright (c) 2013 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_COMMON_EXTENSIONS_BACKGROUND_INFO_H_ 6 #define CHROME_COMMON_EXTENSIONS_BACKGROUND_INFO_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/values.h" 12 #include "chrome/common/extensions/extension.h" 13 #include "chrome/common/extensions/manifest_handler.h" 14 #include "url/gurl.h" 15 16 namespace extensions { 17 18 class BackgroundInfo : public Extension::ManifestData { 19 public: 20 BackgroundInfo(); 21 virtual ~BackgroundInfo(); 22 23 static GURL GetBackgroundURL(const Extension* extension); 24 static const std::vector<std::string>& GetBackgroundScripts( 25 const Extension* extension); 26 static bool HasBackgroundPage(const Extension* extension); 27 static bool HasPersistentBackgroundPage(const Extension* extension); 28 static bool HasLazyBackgroundPage(const Extension* extension); 29 static bool AllowJSAccess(const Extension* extension); 30 31 bool has_background_page() const { 32 return background_url_.is_valid() || !background_scripts_.empty(); 33 } 34 35 bool has_persistent_background_page() const { 36 return has_background_page() && is_persistent_; 37 } 38 39 bool has_lazy_background_page() const { 40 return has_background_page() && !is_persistent_; 41 } 42 43 bool Parse(const Extension* extension, string16* error); 44 45 private: 46 bool LoadBackgroundScripts(const Extension* extension, 47 const std::string& key, 48 string16* error); 49 bool LoadBackgroundPage(const Extension* extension, 50 const std::string& key, 51 string16* error); 52 bool LoadBackgroundPage(const Extension* extension, string16* error); 53 bool LoadBackgroundPersistent(const Extension* extension, string16* error); 54 bool LoadAllowJSAccess(const Extension* extension, string16* error); 55 56 // Optional URL to a master page of which a single instance should be always 57 // loaded in the background. 58 GURL background_url_; 59 60 // Optional list of scripts to use to generate a background page. If this is 61 // present, background_url_ will be empty and generated by GetBackgroundURL(). 62 std::vector<std::string> background_scripts_; 63 64 // True if the background page should stay loaded forever; false if it should 65 // load on-demand (when it needs to handle an event). Defaults to true. 66 bool is_persistent_; 67 68 // True if the background page can be scripted by pages of the app or 69 // extension, in which case all such pages must run in the same process. 70 // False if such pages are not permitted to script the background page, 71 // allowing them to run in different processes. 72 // Defaults to true. 73 bool allow_js_access_; 74 }; 75 76 // Parses all background/event page-related keys in the manifest. 77 class BackgroundManifestHandler : public ManifestHandler { 78 public: 79 BackgroundManifestHandler(); 80 virtual ~BackgroundManifestHandler(); 81 82 virtual bool Parse(Extension* extension, string16* error) OVERRIDE; 83 virtual bool Validate(const Extension* extension, 84 std::string* error, 85 std::vector<InstallWarning>* warnings) const OVERRIDE; 86 virtual bool AlwaysParseForType(Manifest::Type type) const OVERRIDE; 87 88 private: 89 virtual const std::vector<std::string> Keys() const OVERRIDE; 90 }; 91 92 } // namespace extensions 93 94 #endif // CHROME_COMMON_EXTENSIONS_BACKGROUND_INFO_H_ 95