1 // Copyright 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 CHROME_BROWSER_EXTENSIONS_INSTALL_SIGNER_H_
6 #define CHROME_BROWSER_EXTENSIONS_INSTALL_SIGNER_H_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "extensions/common/extension.h"
18 class DictionaryValue
;
23 class URLRequestContextGetter
;
26 namespace extensions
{
28 // This represents a list of ids signed with a private key using an algorithm
29 // that includes some salt bytes.
30 struct InstallSignature
{
31 // The set of ids that have been signed.
34 // Both of these are just arrays of bytes, NOT base64-encoded.
36 std::string signature
;
38 // The date that the signature should expire, in YYYY-MM-DD format.
39 std::string expire_date
;
44 // Helper methods for serialization to/from a base::DictionaryValue.
45 void ToValue(base::DictionaryValue
* value
) const;
47 static scoped_ptr
<InstallSignature
> FromValue(
48 const base::DictionaryValue
& value
);
51 // Objects of this class encapsulate an operation to get a signature proving
52 // that a set of ids are hosted in the webstore.
55 typedef base::Callback
<void(scoped_ptr
<InstallSignature
>)> SignatureCallback
;
57 // IMPORTANT NOTE: It is possible that only some, but not all, of the entries
58 // in |ids| will be successfully signed by the backend. Callers should always
59 // check the set of ids in the InstallSignature passed to their callback, as
60 // it may contain only a subset of the ids they passed in.
61 InstallSigner(net::URLRequestContextGetter
* context_getter
,
62 const ExtensionIdSet
& ids
);
65 // Returns a set of ids that are forced to be considered not from webstore,
66 // e.g. by a command line flag used for testing.
67 static ExtensionIdSet
GetForcedNotFromWebstore();
69 // Begins the process of fetching a signature from the backend. This should
70 // only be called once! If you want to get another signature, make another
71 // instance of this class.
72 void GetSignature(const SignatureCallback
& callback
);
74 // Returns whether the signature in InstallSignature is properly signed with a
76 static bool VerifySignature(const InstallSignature
& signature
);
79 // A very simple delegate just used to call ourself back when a url fetch is
81 class FetcherDelegate
;
83 // A helper function that calls |callback_| with an indication that an error
84 // happened (currently done by passing an empty pointer).
85 void ReportErrorViaCallback();
87 // Called when |url_fetcher_| has returned a result to parse the response,
88 // and then call HandleSignatureResult with structured data.
89 void ParseFetchResponse();
91 // Handles the result from a backend fetch.
92 void HandleSignatureResult(const std::string
& signature
,
93 const std::string
& expire_date
,
94 const ExtensionIdSet
& invalid_ids
);
96 // The final callback for when we're done.
97 SignatureCallback callback_
;
99 // The current set of ids we're trying to verify. This may contain fewer ids
100 // than we started with.
103 // An array of random bytes used as an input to hash with the machine id,
104 // which will need to be persisted in the eventual InstallSignature we get.
107 // These are used to make the call to a backend server for a signature.
108 net::URLRequestContextGetter
* context_getter_
;
109 scoped_ptr
<net::URLFetcher
> url_fetcher_
;
110 scoped_ptr
<FetcherDelegate
> delegate_
;
112 DISALLOW_COPY_AND_ASSIGN(InstallSigner
);
115 } // namespace extensions
117 #endif // CHROME_BROWSER_EXTENSIONS_INSTALL_SIGNER_H_