OLD | NEW |
---|---|
(Empty) | |
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_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_FETCHER_H_ | |
6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_FETCHER_H_ | |
7 | |
8 #include <vector> | |
9 | |
10 #include "base/macros.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/threading/thread_checker.h" | |
13 #include "components/password_manager/core/browser/affiliation_utils.h" | |
14 #include "net/url_request/url_fetcher_delegate.h" | |
15 | |
16 class GURL; | |
17 | |
18 namespace net { | |
19 class URLRequestContextGetter; | |
20 } // namespace net | |
21 | |
22 namespace password_manager { | |
23 | |
24 class AffiliationFetcherDelegate; | |
25 class AffiliationFetcherFactory; | |
26 | |
27 // Fetches authoritative information regarding which facets are affiliated with | |
28 // each other, that is, which facets belong to the same logical application. | |
29 // See affiliation_utils.h for a definition of what this means. | |
30 // | |
31 // An instance is good for exactly one fetch, and may be used from any thread. | |
32 // However, it should always be accessed on the thread it was created on. | |
Mike West
2014/12/02 14:32:42
"should" or "must"? It looks like you're only usin
engedy
2014/12/02 18:33:56
Actually, I have just removed the ThreadChecker. U
| |
33 class AffiliationFetcher : net::URLFetcherDelegate { | |
34 public: | |
35 // Encapsulates the response to an affiliations request. | |
36 typedef std::vector<AffiliatedFacets> Result; | |
37 | |
38 ~AffiliationFetcher() override; | |
39 | |
40 // Constructs a fetcher to retrieve affiliations for each facet in |facet_ids| | |
41 // using the specified |request_context_getter|, and will provide the results | |
42 // to the |delegate| on the same thread that creates the instance. | |
43 static AffiliationFetcher* Create( | |
44 net::URLRequestContextGetter* request_context_getter, | |
45 const std::vector<std::string>& facet_uris, | |
46 AffiliationFetcherDelegate* delegate); | |
47 | |
48 // Sets the |factory| to be used by the Create() method instead of the vanilla | |
49 // constructor for creating AffiliationFetcher instances. To revert to using | |
50 // the vanilla constructor, set NULL. Used only for unit testing. | |
51 static void SetFactoryForTesting(AffiliationFetcherFactory* factory); | |
52 | |
53 // Actually starts the request, and will call the delegate with the results | |
54 // once done. If |this| is destroyed before completion, the in-flight request | |
55 // is cancelled, and the delegate will not be called. Further details: | |
56 // * No cookies are sent/saved with the request. | |
57 // * In case of network/server errors, the request will not be retried. | |
58 // * Results are guaranteed to be always fresh and will never be cached. | |
Mike West
2014/12/02 14:32:42
Is it easily possible to add tests for these asser
engedy
2014/12/02 18:33:56
We could verify in the unit test that the load fla
| |
59 virtual void StartRequest(); | |
60 | |
61 const std::vector<std::string>& requested_facet_uris() const { | |
62 return requested_facet_uris_; | |
63 } | |
64 | |
65 AffiliationFetcherDelegate* delegate() const { return delegate_; } | |
66 | |
67 protected: | |
68 AffiliationFetcher(net::URLRequestContextGetter* request_context_getter, | |
69 const std::vector<std::string>& facet_uris, | |
70 AffiliationFetcherDelegate* delegate); | |
71 | |
72 private: | |
73 // Builds the URL at which the affiliation service is available. | |
74 GURL BuildQueryURL() const; | |
75 | |
76 // Prepares and returns the serialized form of the JSON dictionary that will | |
77 // be the payload of the POST request. | |
78 std::string PreparePayload() const; | |
79 | |
80 // Parses the server response into |result| and returns true on success. We | |
81 // gracefully ignore most unknown fields in the response, so this method will | |
82 // return false only if the response is gravely ill-formed. | |
83 bool ParseResponse(Result* result) const; | |
84 | |
85 // net::URLFetcherDelegate: | |
86 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
87 | |
88 base::ThreadChecker thread_checker_; | |
89 | |
90 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
91 std::vector<std::string> requested_facet_uris_; | |
92 AffiliationFetcherDelegate* delegate_; | |
93 | |
94 scoped_ptr<net::URLFetcher> fetcher_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(AffiliationFetcher); | |
97 }; | |
98 | |
99 // Interface that users of AffiliationFetcher should implement to get results of | |
100 // the fetch. It is safe to destroy the fetcher in any of the event handlers. | |
101 class AffiliationFetcherDelegate { | |
Mike West
2014/12/02 14:32:43
Nit: Why bundle this into the same file as the fet
engedy
2014/12/02 18:33:55
I just didn't want to frighten potential code revi
| |
102 public: | |
103 // Called when affiliation information has been successfully retrieved. The | |
104 // |result| will contain at most as many equivalence class as facet URIs | |
105 // requested. Each facet URI will appear in exactly one equivalence class. | |
106 virtual void OnFetchSucceeded( | |
107 scoped_ptr<AffiliationFetcher::Result> result) = 0; | |
108 | |
109 // Called when affiliation information could not be fetched due to network or | |
110 // server error. | |
111 virtual void OnFetchFailed() = 0; | |
112 | |
113 // This will be called when an affiliation response was received, but it was | |
114 // gravely malformed. | |
115 virtual void OnMalformedResponse() = 0; | |
116 | |
117 protected: | |
118 virtual ~AffiliationFetcherDelegate() {} | |
119 }; | |
120 | |
121 // Interface for a factory to construct AffiliationFetcher objects, to be used | |
122 // as an alternative instead of simply calling the constructor. | |
123 class AffiliationFetcherFactory { | |
Mike West
2014/12/02 14:32:42
Nit: Ditto.
engedy
2014/12/02 18:33:56
Actually, I will remove this for now, and will add
| |
124 public: | |
125 virtual AffiliationFetcher* CreateInstance( | |
126 net::URLRequestContextGetter* request_context_getter, | |
127 const std::vector<std::string>& facet_uris, | |
128 AffiliationFetcherDelegate* delegate) = 0; | |
129 | |
130 protected: | |
131 virtual ~AffiliationFetcherFactory() {} | |
132 }; | |
133 | |
134 } // namespace password_manager | |
135 | |
136 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_FETCHER_H_ | |
OLD | NEW |