1// Copyright 2014 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 COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_UTILS_H_
6#define COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_UTILS_H_
7
8#include <string>
9
10struct CrxComponent;
11class GURL;
12
13namespace base {
14class FilePath;
15}
16
17namespace net {
18class URLFetcher;
19class URLFetcherDelegate;
20class URLRequestContextGetter;
21}
22
23namespace component_updater {
24
25class Configurator;
26struct CrxUpdateItem;
27
28// An update protocol request starts with a common preamble which includes
29// version and platform information for Chrome and the operating system,
30// followed by a request body, which is the actual payload of the request.
31// For example:
32//
33// <?xml version="1.0" encoding="UTF-8"?>
34// <request protocol="3.0" version="chrome-32.0.1.0"  prodversion="32.0.1.0"
35//        requestid="{7383396D-B4DD-46E1-9104-AAC6B918E792}"
36//        updaterchannel="canary" arch="x86" nacl_arch="x86-64"
37//        ADDITIONAL ATTRIBUTES>
38//   <hw physmemory="16"/>
39//   <os platform="win" version="6.1" arch="x86"/>
40//   ... REQUEST BODY ...
41// </request>
42
43// Builds a protocol request string by creating the outer envelope for
44// the request and including the request body specified as a parameter.
45// If specified, |additional_attributes| are appended as attributes of the
46// request element. The additional attributes have to be well-formed for
47// insertion in the request element.
48std::string BuildProtocolRequest(const std::string& browser_version,
49                                 const std::string& channel,
50                                 const std::string& lang,
51                                 const std::string& os_long_name,
52                                 const std::string& request_body,
53                                 const std::string& additional_attributes);
54
55// Sends a protocol request to the the service endpoint specified by |url|.
56// The body of the request is provided by |protocol_request| and it is
57// expected to contain XML data. The caller owns the returned object.
58net::URLFetcher* SendProtocolRequest(
59    const GURL& url,
60    const std::string& protocol_request,
61    net::URLFetcherDelegate* url_fetcher_delegate,
62    net::URLRequestContextGetter* url_request_context_getter);
63
64// Returns true if the url request of |fetcher| was succesful.
65bool FetchSuccess(const net::URLFetcher& fetcher);
66
67// Returns the error code which occured during the fetch. The function returns 0
68// if the fetch was successful. If errors happen, the function could return a
69// network error, an http response code, or the status of the fetch, if the
70// fetch is pending or canceled.
71int GetFetchError(const net::URLFetcher& fetcher);
72
73// Returns true if the |update_item| contains a valid differential update url.
74bool HasDiffUpdate(const CrxUpdateItem* update_item);
75
76// Returns true if the |status_code| represents a server error 5xx.
77bool IsHttpServerError(int status_code);
78
79// Deletes the file and its directory, if the directory is empty. If the
80// parent directory is not empty, the function ignores deleting the directory.
81// Returns true if the file and the empty directory are deleted.
82bool DeleteFileAndEmptyParentDirectory(const base::FilePath& filepath);
83
84// Returns the component id of the |component|. The component id is in a
85// format similar with the format of an extension id.
86std::string GetCrxComponentID(const CrxComponent& component);
87
88}  // namespace component_updater
89
90#endif  // COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_UTILS_H_
91