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.
5 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_FETCHER_H_
6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_FETCHER_H_
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "components/password_manager/core/browser/affiliation_fetcher_delegate.h"
13 #include "components/password_manager/core/browser/affiliation_utils.h"
14 #include "net/url_request/url_fetcher_delegate.h"
19 class URLRequestContextGetter
;
22 namespace password_manager
{
24 class TestAffiliationFetcherFactory
;
26 // Fetches authoritative information regarding which facets are affiliated with
27 // each other, that is, which facets belong to the same logical application.
28 // See affiliation_utils.h for a definition of what this means.
30 // An instance is good for exactly one fetch, and may be used from any thread
31 // that runs a message loop (i.e. not a worker pool thread).
32 class AffiliationFetcher
: net::URLFetcherDelegate
{
34 ~AffiliationFetcher() override
;
36 // Constructs a fetcher to retrieve affiliations for each facet in |facet_ids|
37 // using the specified |request_context_getter|, and will provide the results
38 // to the |delegate| on the same thread that creates the instance.
39 static AffiliationFetcher
* Create(
40 net::URLRequestContextGetter
* request_context_getter
,
41 const std::vector
<FacetURI
>& facet_uris
,
42 AffiliationFetcherDelegate
* delegate
);
44 // Sets the |factory| to be used by Create() to construct AffiliationFetcher
45 // instances. To be used only for testing.
47 // The caller must ensure that the |factory| outlives all potential Create()
48 // calls. The caller may pass in NULL to resume using the default factory.
49 static void SetFactoryForTesting(TestAffiliationFetcherFactory
* factory
);
51 // Actually starts the request, and will call the delegate with the results on
52 // the same thread when done. If |this| is destroyed before completion, the
53 // in-flight request is cancelled, and the delegate will not be called.
55 // * No cookies are sent/saved with the request.
56 // * In case of network/server errors, the request will not be retried.
57 // * Results are guaranteed to be always fresh and will never be cached.
58 virtual void StartRequest();
60 const std::vector
<FacetURI
>& requested_facet_uris() const {
61 return requested_facet_uris_
;
64 AffiliationFetcherDelegate
* delegate() const { return delegate_
; }
67 AffiliationFetcher(net::URLRequestContextGetter
* request_context_getter
,
68 const std::vector
<FacetURI
>& facet_uris
,
69 AffiliationFetcherDelegate
* delegate
);
72 // Builds the URL for the Affiliation API's lookup method.
73 GURL
BuildQueryURL() const;
75 // Prepares and returns the serialized protocol buffer message that will be
76 // the payload of the POST request.
77 std::string
PreparePayload() const;
79 // Parses and validates the response protocol buffer message for a list of
80 // equivalence classes, stores them into |result| and returns true on success.
81 // Returns false if the response was gravely ill-formed or self-inconsistent.
82 // Unknown kinds of facet URIs and new protocol buffer fields will be ignored.
83 bool ParseResponse(AffiliationFetcherDelegate::Result
* result
) const;
85 // net::URLFetcherDelegate:
86 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
88 const scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
89 const std::vector
<FacetURI
> requested_facet_uris_
;
90 AffiliationFetcherDelegate
* const delegate_
;
92 scoped_ptr
<net::URLFetcher
> fetcher_
;
94 DISALLOW_COPY_AND_ASSIGN(AffiliationFetcher
);
97 } // namespace password_manager
99 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_FETCHER_H_