Upstream version 7.35.141.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / manifest_handler.h
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 XWALK_APPLICATION_COMMON_MANIFEST_HANDLER_H_
6 #define XWALK_APPLICATION_COMMON_MANIFEST_HANDLER_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
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"
16
17 namespace xwalk {
18 namespace application {
19
20 class ManifestHandler {
21  public:
22   virtual ~ManifestHandler();
23
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;
28
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,
32                         std::string* error,
33                         std::vector<InstallWarning>* warnings) const;
34
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;
41
42   // Same as AlwaysParseForType, but for Validate instead of Parse.
43   virtual bool AlwaysValidateForType(Manifest::Type type) const;
44
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).
47   // Defaults to empty.
48   virtual std::vector<std::string> PrerequisiteKeys() const;
49
50   // The keys to register handler for (in Register).
51   virtual std::vector<std::string> Keys() const = 0;
52 };
53
54 class ManifestHandlerRegistry {
55  public:
56   ~ManifestHandlerRegistry();
57
58   static ManifestHandlerRegistry* GetInstance(
59       Package::Type package_type);
60
61   bool ParseAppManifest(
62        scoped_refptr<ApplicationData> application, base::string16* error);
63   bool ValidateAppManifest(scoped_refptr<const ApplicationData> application,
64                            std::string* error,
65                            std::vector<InstallWarning>* warnings);
66
67  private:
68   friend class ScopedTestingManifestHandlerRegistry;
69   explicit ManifestHandlerRegistry(
70        const std::vector<ManifestHandler*>& handlers);
71
72   // Register a manifest handler for keys, which are provided by Keys() method
73   // in ManifestHandler implementer.
74   void Register(ManifestHandler* handler);
75
76   void ReorderHandlersGivenDependencies();
77
78   // Sets a new global registry, for testing purposes.
79   static void SetInstanceForTesting(ManifestHandlerRegistry* registry,
80                                     Package::Type package_type);
81
82   static ManifestHandlerRegistry* GetInstanceForWGT();
83   static ManifestHandlerRegistry* GetInstanceForXPK();
84
85   typedef std::map<std::string, ManifestHandler*> ManifestHandlerMap;
86   typedef std::map<ManifestHandler*, int> ManifestHandlerOrderMap;
87
88   ManifestHandlerMap handlers_;
89
90   // Handlers are executed in order; lowest order first.
91   ManifestHandlerOrderMap order_map_;
92
93   static ManifestHandlerRegistry* xpk_registry_;
94   static ManifestHandlerRegistry* widget_registry_;
95 };
96
97 }  // namespace application
98 }  // namespace xwalk
99
100 #endif  // XWALK_APPLICATION_COMMON_MANIFEST_HANDLER_H_