12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef CHROME_COMMON_EXTENSIONS_BACKGROUND_INFO_H_
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define CHROME_COMMON_EXTENSIONS_BACKGROUND_INFO_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <vector>
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/values.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/extensions/extension.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/extensions/manifest_handler.h"
147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "url/gurl.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace extensions {
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class BackgroundInfo : public Extension::ManifestData {
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BackgroundInfo();
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~BackgroundInfo();
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static GURL GetBackgroundURL(const Extension* extension);
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static const std::vector<std::string>& GetBackgroundScripts(
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const Extension* extension);
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static bool HasBackgroundPage(const Extension* extension);
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static bool HasPersistentBackgroundPage(const Extension* extension);
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static bool HasLazyBackgroundPage(const Extension* extension);
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static bool AllowJSAccess(const Extension* extension);
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool has_background_page() const {
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return background_url_.is_valid() || !background_scripts_.empty();
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool has_persistent_background_page() const {
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return has_background_page() && is_persistent_;
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool has_lazy_background_page() const {
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return has_background_page() && !is_persistent_;
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool Parse(const Extension* extension, string16* error);
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool LoadBackgroundScripts(const Extension* extension,
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                             const std::string& key,
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                             string16* error);
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool LoadBackgroundPage(const Extension* extension,
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          const std::string& key,
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          string16* error);
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool LoadBackgroundPage(const Extension* extension, string16* error);
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool LoadBackgroundPersistent(const Extension* extension, string16* error);
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool LoadAllowJSAccess(const Extension* extension, string16* error);
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Optional URL to a master page of which a single instance should be always
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // loaded in the background.
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GURL background_url_;
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Optional list of scripts to use to generate a background page. If this is
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // present, background_url_ will be empty and generated by GetBackgroundURL().
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<std::string> background_scripts_;
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // True if the background page should stay loaded forever; false if it should
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // load on-demand (when it needs to handle an event). Defaults to true.
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool is_persistent_;
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // True if the background page can be scripted by pages of the app or
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // extension, in which case all such pages must run in the same process.
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // False if such pages are not permitted to script the background page,
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // allowing them to run in different processes.
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Defaults to true.
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool allow_js_access_;
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Parses all background/event page-related keys in the manifest.
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class BackgroundManifestHandler : public ManifestHandler {
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BackgroundManifestHandler();
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~BackgroundManifestHandler();
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool Parse(Extension* extension, string16* error) OVERRIDE;
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool Validate(const Extension* extension,
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        std::string* error,
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        std::vector<InstallWarning>* warnings) const OVERRIDE;
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual bool AlwaysParseForType(Manifest::Type type) const OVERRIDE;
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual const std::vector<std::string> Keys() const OVERRIDE;
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace extensions
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // CHROME_COMMON_EXTENSIONS_BACKGROUND_INFO_H_
95