OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 CHROME_BROWSER_INTERESTS_INTERESTS_FETCHER_H_ | |
6 #define CHROME_BROWSER_INTERESTS_INTERESTS_FETCHER_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/callback.h" | |
13 #include "base/gtest_prod_util.h" | |
14 #include "base/macros.h" | |
15 #include "chrome/browser/profiles/profile.h" | |
16 #include "components/signin/core/browser/signin_manager.h" | |
17 #include "google_apis/gaia/oauth2_token_service.h" | |
18 #include "net/url_request/url_fetcher.h" | |
19 #include "net/url_request/url_fetcher_delegate.h" | |
20 | |
21 // Class to fetch topics of interest for a user as computed by Google Now. Each | |
22 // interest is a tuple of a name, a thumbnail URL and a relevance score. This | |
23 // class is one-shot. To retrieve interests multiple times use multiple | |
24 // instances of this class. | |
25 // | |
26 // Authentication is done via OAuth2 and requires the | |
27 // https://www.googleapis.com/auth/googlenow scope. | |
28 // | |
29 // If the InterestsFetcher is deleted before the callback is called then the | |
30 // request will be aborted. It is up to the user to ensure that both the | |
31 // Profile and the OAuth2TokenService outlive the InterestsFetcher. | |
32 // | |
33 // The server required for this feature is not publicly available yet. | |
34 class InterestsFetcher : public net::URLFetcherDelegate, | |
35 public OAuth2TokenService::Consumer { | |
36 public: | |
37 struct Interest { | |
38 Interest(const std::string& name, const GURL& image_url, double relevance); | |
39 ~Interest(); | |
40 | |
41 bool operator==(const Interest& interest) const; | |
42 | |
43 std::string name; | |
44 GURL image_url; | |
45 double relevance; | |
46 }; | |
47 | |
48 using InterestsCallback = | |
49 base::Callback<void(std::unique_ptr<std::vector<Interest>>)>; | |
50 | |
51 InterestsFetcher(OAuth2TokenService* oauth2_token_service, | |
52 const std::string& account_id, | |
53 net::URLRequestContextGetter* context); | |
54 | |
55 ~InterestsFetcher() override; | |
56 | |
57 static std::unique_ptr<InterestsFetcher> CreateFromProfile(Profile* profile); | |
58 | |
59 void FetchInterests(const InterestsCallback& callback); | |
60 | |
61 private: | |
62 // net::URLFetcherDelegate implementation. | |
63 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
64 | |
65 // OAuth2TokenService::Consumer implementation. | |
66 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | |
67 const std::string& access_token, | |
68 const base::Time& experiation_time) override; | |
69 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | |
70 const GoogleServiceAuthError& error) override; | |
71 | |
72 void StartOAuth2Request(); | |
73 OAuth2TokenService::ScopeSet GetApiScopes(); | |
74 std::unique_ptr<net::URLFetcher> CreateFetcher(); | |
75 | |
76 // Parse the json response. | |
77 std::unique_ptr<std::vector<Interest>> ExtractInterests( | |
78 const std::string& response); | |
79 | |
80 InterestsCallback callback_; | |
81 std::unique_ptr<net::URLFetcher> fetcher_; | |
82 std::string account_id_; | |
83 net::URLRequestContextGetter* url_request_context_; | |
84 bool access_token_expired_; | |
85 std::string access_token_; | |
86 | |
87 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; | |
88 OAuth2TokenService* token_service_; | |
89 | |
90 DISALLOW_COPY_AND_ASSIGN(InterestsFetcher); | |
91 }; | |
92 | |
93 #endif // CHROME_BROWSER_INTERESTS_INTERESTS_FETCHER_H_ | |
OLD | NEW |