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.
5 #ifndef XWALK_APPLICATION_COMMON_MANIFEST_HANDLER_H_
6 #define XWALK_APPLICATION_COMMON_MANIFEST_HANDLER_H_
12 #include "base/memory/linked_ptr.h"
13 #include "base/strings/string16.h"
14 #include "xwalk/application/common/application_data.h"
15 #include "xwalk/application/common/manifest.h"
18 namespace application {
20 class ManifestHandler {
22 virtual ~ManifestHandler();
24 // Returns false in case of failure and sets writes error message
25 // in |error| if present.
26 virtual bool Parse(scoped_refptr<ApplicationData> application,
27 base::string16* error) = 0;
29 // Returns false in case of failure and sets writes error message
30 // in |error| if present.
31 virtual bool Validate(scoped_refptr<const ApplicationData> application,
33 std::vector<InstallWarning>* warnings) const;
35 // If false (the default), only parse the manifest if a registered
36 // key is present in the manifest. If true, always attempt to parse
37 // the manifest for this application type, even if no registered keys
38 // are present. This allows specifying a default parsed value for
39 // application that don't declare our key in the manifest.
40 virtual bool AlwaysParseForType(Manifest::Type type) const;
42 // Same as AlwaysParseForType, but for Validate instead of Parse.
43 virtual bool AlwaysValidateForType(Manifest::Type type) const;
45 // The list of keys that, if present, should be parsed before calling our
46 // Parse (typically, because our Parse needs to read those keys).
48 virtual std::vector<std::string> PrerequisiteKeys() const;
50 // The keys to register handler for (in Register).
51 virtual std::vector<std::string> Keys() const = 0;
54 class ManifestHandlerRegistry {
56 ~ManifestHandlerRegistry();
58 static ManifestHandlerRegistry* GetInstance(
59 Manifest::PackageType package_type);
61 bool ParseAppManifest(
62 scoped_refptr<ApplicationData> application, base::string16* error);
63 bool ValidateAppManifest(scoped_refptr<const ApplicationData> application,
65 std::vector<InstallWarning>* warnings);
68 friend class ScopedTestingManifestHandlerRegistry;
69 explicit ManifestHandlerRegistry(
70 const std::vector<ManifestHandler*>& handlers);
72 // Register a manifest handler for keys, which are provided by Keys() method
73 // in ManifestHandler implementer.
74 void Register(ManifestHandler* handler);
76 void ReorderHandlersGivenDependencies();
78 // Sets a new global registry, for testing purposes.
79 static void SetInstanceForTesting(ManifestHandlerRegistry* registry,
80 Manifest::PackageType package_type);
82 static ManifestHandlerRegistry* GetInstanceForWGT();
83 static ManifestHandlerRegistry* GetInstanceForXPK();
85 typedef std::map<std::string, ManifestHandler*> ManifestHandlerMap;
86 typedef std::map<ManifestHandler*, int> ManifestHandlerOrderMap;
88 ManifestHandlerMap handlers_;
90 // Handlers are executed in order; lowest order first.
91 ManifestHandlerOrderMap order_map_;
93 static ManifestHandlerRegistry* xpk_registry_;
94 static ManifestHandlerRegistry* widget_registry_;
97 } // namespace application
100 #endif // XWALK_APPLICATION_COMMON_MANIFEST_HANDLER_H_