Chromium Code Reviews| 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 <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/callback.h" | |
| 12 #include "base/gtest_prod_util.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/scoped_ptr.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 a user's interests as computed by Google Now. Each interest is | |
| 22 // represented as a tuple of its name, an url to a representative image, and a | |
|
Bernhard Bauer
2015/10/07 15:26:37
Nit: URL
PEConn
2015/10/07 18:27:20
Done.
| |
| 23 // score measuring the level of interest of the user. | |
| 24 // | |
| 25 // Authentication is done via OAuth2 and requires the | |
| 26 // https://www.googleapis.com/auth/googlenow scope. | |
| 27 // | |
| 28 // If the InterestsFetcher is deleted before the callback is called then the | |
| 29 // request will be aborted. | |
| 30 // | |
| 31 // It is up to the user to ensure that both the Profile and the | |
| 32 // OAuth2TokenService outlive the InterestsFetcher. | |
| 33 class InterestsFetcher : public net::URLFetcherDelegate, | |
| 34 public OAuth2TokenService::Consumer { | |
| 35 public: | |
| 36 struct Interest { | |
| 37 Interest(const std::string& name, const GURL& image_url, double relevance); | |
| 38 ~Interest(); | |
| 39 | |
| 40 bool operator==(const Interest& interest) const; | |
| 41 | |
| 42 std::string name; | |
| 43 GURL image_url; | |
| 44 double relevance; | |
| 45 }; | |
| 46 | |
| 47 using InterestsCallback = | |
| 48 base::Callback<void(scoped_ptr<std::vector<Interest>>)>; | |
|
Bernhard Bauer
2015/10/07 15:26:37
Indent four spaces if you are breaking a line.
PEConn
2015/10/07 18:27:20
Done.
| |
| 49 | |
| 50 InterestsFetcher(OAuth2TokenService* oauth2_token_service, | |
| 51 const std::string& account_id, | |
| 52 net::URLRequestContextGetter* context); | |
| 53 | |
| 54 ~InterestsFetcher() override; | |
| 55 | |
| 56 static scoped_ptr<InterestsFetcher> CreateFromProfile(Profile* profile); | |
| 57 | |
| 58 // This should only be called once per instance. | |
| 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 scoped_ptr<net::URLFetcher> CreateFetcher(); | |
| 75 | |
| 76 // Parse the json response. | |
| 77 scoped_ptr<std::vector<Interest>> | |
| 78 ExtractInterests(const std::string& response); | |
| 79 | |
| 80 InterestsCallback callback_; | |
| 81 scoped_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 scoped_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 |