summaryrefslogtreecommitdiffstats
path: root/extensions/common/manifest_handlers/background_info.h
blob: 947ee650ab2f85accb9068e21d14f4f619195a8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_COMMON_MANIFEST_HANDLERS_BACKGROUND_INFO_H_
#define EXTENSIONS_COMMON_MANIFEST_HANDLERS_BACKGROUND_INFO_H_

#include <string>
#include <vector>

#include "base/values.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_handler.h"
#include "url/gurl.h"

namespace extensions {

class BackgroundInfo : public Extension::ManifestData {
 public:
  BackgroundInfo();
  virtual ~BackgroundInfo();

  static GURL GetBackgroundURL(const Extension* extension);
  static const std::vector<std::string>& GetBackgroundScripts(
      const Extension* extension);
  static const std::string& GetServiceWorkerScript(const Extension* extension);
  static bool HasBackgroundPage(const Extension* extension);
  static bool HasPersistentBackgroundPage(const Extension* extension);
  static bool HasLazyBackgroundPage(const Extension* extension);
  static bool HasGeneratedBackgroundPage(const Extension* extension);
  static bool HasServiceWorker(const Extension* extension);
  static bool AllowJSAccess(const Extension* extension);

  bool has_background_page() const {
    return background_url_.is_valid() || !background_scripts_.empty();
  }

  bool has_persistent_background_page() const {
    return has_background_page() && is_persistent_;
  }

  bool has_lazy_background_page() const {
    return has_background_page() && !is_persistent_;
  }

  bool has_service_worker() const { return !service_worker_script_.empty(); }

  bool Parse(const Extension* extension, base::string16* error);

 private:
  bool LoadServiceWorkerScript(const Extension* extension,
                               const std::string& key,
                               base::string16* error);
  bool LoadBackgroundScripts(const Extension* extension,
                             const std::string& key,
                             base::string16* error);
  bool LoadBackgroundPage(const Extension* extension,
                          const std::string& key,
                          base::string16* error);
  bool LoadBackgroundPage(const Extension* extension, base::string16* error);
  bool LoadBackgroundPersistent(const Extension* extension,
                                base::string16* error);
  bool LoadAllowJSAccess(const Extension* extension, base::string16* error);

  // Optional URL to a master page of which a single instance should be always
  // loaded in the background.
  GURL background_url_;

  // Optional list of scripts to use to generate a background page. If this is
  // present, background_url_ will be empty and generated by GetBackgroundURL().
  std::vector<std::string> background_scripts_;

  // True if the background page should stay loaded forever; false if it should
  // load on-demand (when it needs to handle an event). Defaults to true.
  bool is_persistent_;

  // Optional script to register as a service worker. This is mutually exclusive
  // to the use of background_url_ or background_scripts_.
  std::string service_worker_script_;

  // True if the background page can be scripted by pages of the app or
  // extension, in which case all such pages must run in the same process.
  // False if such pages are not permitted to script the background page,
  // allowing them to run in different processes.
  // Defaults to true.
  bool allow_js_access_;

  DISALLOW_COPY_AND_ASSIGN(BackgroundInfo);
};

// Parses all background/event page-related keys in the manifest.
class BackgroundManifestHandler : public ManifestHandler {
 public:
  BackgroundManifestHandler();
  virtual ~BackgroundManifestHandler();

  virtual bool Parse(Extension* extension, base::string16* error) OVERRIDE;
  virtual bool Validate(const Extension* extension,
                        std::string* error,
                        std::vector<InstallWarning>* warnings) const OVERRIDE;
  virtual bool AlwaysParseForType(Manifest::Type type) const OVERRIDE;

 private:
  virtual const std::vector<std::string> Keys() const OVERRIDE;

  DISALLOW_COPY_AND_ASSIGN(BackgroundManifestHandler);
};

}  // namespace extensions

#endif  // EXTENSIONS_COMMON_MANIFEST_HANDLERS_BACKGROUND_INFO_H_