OLD | NEW |
| (Empty) |
1 // Copyright 2009-2010 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // Defines the Goopdate XML parser. The clients of this class are | |
17 // UpdateRequest and UpdateResponse. | |
18 | |
19 #ifndef OMAHA_COMMON_XML_PARSER_H_ | |
20 #define OMAHA_COMMON_XML_PARSER_H_ | |
21 | |
22 #include <windows.h> | |
23 #include <objbase.h> | |
24 #include <msxml2.h> | |
25 #include <atlbase.h> | |
26 #include <atlstr.h> | |
27 #include <map> | |
28 #include <vector> | |
29 #include "base/basictypes.h" | |
30 #include "base/object_factory.h" | |
31 #include "omaha/common/const_goopdate.h" | |
32 #include "omaha/common/update_request.h" | |
33 #include "omaha/common/update_response.h" | |
34 | |
35 namespace omaha { | |
36 | |
37 namespace xml { | |
38 | |
39 class ElementHandler; | |
40 | |
41 CString ConvertProcessorArchitectureToString(DWORD processor_architecture); | |
42 | |
43 // Public static methods instantiate a temporary instance of this class, which | |
44 // then parses the specified document. This avoids reusing instances of the | |
45 // parser and dealing with stale and dirty data. | |
46 class XmlParser { | |
47 public: | |
48 // Parses the update response buffer and fills in the UpdateResponse. In case | |
49 // of errors, the UpdateResponse object may contain partial information up | |
50 // to the point of the parsing error. | |
51 // TODO(omaha): since the xml docs are strings we could use a CString as | |
52 // an input parameter, no reason why this should be a buffer. | |
53 static HRESULT DeserializeResponse(const std::vector<uint8>& buffer, | |
54 UpdateResponse* update_response); | |
55 | |
56 // Generates the update request from the request node. | |
57 static HRESULT SerializeRequest(const UpdateRequest& update_request, | |
58 CString* buffer); | |
59 | |
60 private: | |
61 typedef Factory<ElementHandler, CString> ElementHandlerFactory; | |
62 | |
63 XmlParser(); | |
64 void InitializeElementHandlers(); | |
65 void InitializeLegacyElementHandlers(); | |
66 | |
67 // Builds an XML object model corresponding to an xml request. | |
68 HRESULT BuildDom(const request::Request& request); | |
69 | |
70 // Builds the 'request' element. | |
71 HRESULT BuildRequestElement(); | |
72 | |
73 // Creates the 'os' element. | |
74 HRESULT BuildOsElement(IXMLDOMNode* parent_node); | |
75 | |
76 // Creates the 'app' element. This is usually a sequence of elements. | |
77 HRESULT BuildAppElement(IXMLDOMNode* parent_node); | |
78 | |
79 // Creates the 'updatecheck' element for an application. | |
80 HRESULT BuildUpdateCheckElement(const request::App& app, | |
81 IXMLDOMNode* parent_node); | |
82 | |
83 // Creates Ping aka 'event' elements for an application. | |
84 HRESULT BuildPingRequestElement(const request::App& app, | |
85 IXMLDOMNode* parent_node); | |
86 | |
87 // Creates the 'data' element for an application. | |
88 HRESULT BuildDataElement(const request::App& app, | |
89 IXMLDOMNode* parent_node); | |
90 | |
91 // Creates the 'didrun' aka 'active' aka 'ping' element for an application. | |
92 HRESULT BuildDidRunElement(const request::App& app, | |
93 IXMLDOMNode* parent_node); | |
94 | |
95 // Serializes the DOM into a string. | |
96 HRESULT GetXml(CString* buffer); | |
97 | |
98 // Creates an element in the Update2 xml namespace. | |
99 HRESULT CreateElementNode(const TCHAR* name, | |
100 const TCHAR* value, | |
101 IXMLDOMNode** element); | |
102 | |
103 // Starts parsing of the xml document. | |
104 HRESULT Parse(); | |
105 | |
106 // Does a DFS traversal of the dom. | |
107 HRESULT TraverseDOM(IXMLDOMNode* node); | |
108 | |
109 // Handles a single node during traversal. | |
110 HRESULT VisitElement(IXMLDOMNode* node); | |
111 | |
112 // The current xml document. | |
113 CComPtr<IXMLDOMDocument> document_; | |
114 | |
115 // The xml request being serialized. Not owned by this class. | |
116 const request::Request* request_; | |
117 | |
118 // The xml response being deserialized. Not owned by this class. | |
119 response::Response* response_; | |
120 | |
121 ElementHandlerFactory element_handler_factory_; | |
122 | |
123 DISALLOW_COPY_AND_ASSIGN(XmlParser); | |
124 }; | |
125 | |
126 } // namespace xml | |
127 | |
128 } // namespace omaha | |
129 | |
130 #endif // OMAHA_COMMON_XML_PARSER_H_ | |
OLD | NEW |