Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ | |
| 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ | |
| 7 | |
| 8 #include "base/callback.h" | |
| 9 #include "base/callback_list.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "base/observer_list.h" | |
| 13 #include "base/sequenced_task_runner.h" | |
| 14 #include "google_apis/gaia/oauth2_token_service.h" | |
| 15 | |
| 16 namespace net { | |
| 17 class URLRequestContextGetter; | |
| 18 class URLFetcher; | |
| 19 class URLFetcherDelegate; | |
| 20 } // namespace net | |
| 21 | |
| 22 class SigninManager; | |
| 23 | |
| 24 namespace ntp_snippets { | |
| 25 | |
| 26 // Fetches snippet data for the NTP from the server | |
| 27 class NTPSnippetsFetcher : public OAuth2TokenService::Consumer, | |
| 28 public OAuth2TokenService::Observer, | |
| 29 public net::URLFetcherDelegate { | |
| 30 public: | |
| 31 using SnippetsAvailableCallback = base::Callback<void(const base::FilePath&)>; | |
| 32 using SnippetsAvailableCallbackList = | |
| 33 base::CallbackList<void(const base::FilePath&)>; | |
| 34 | |
| 35 NTPSnippetsFetcher(scoped_refptr<base::SequencedTaskRunner> file_task_runner, | |
| 36 SigninManager* signin_manager, | |
| 37 OAuth2TokenService* oauth2_token_service, | |
| 38 net::URLRequestContextGetter* url_request_context_getter, | |
| 39 const base::FilePath& base_download_path); | |
| 40 ~NTPSnippetsFetcher() override; | |
| 41 | |
| 42 // Fetches snippets from the server. |overwrite| is true if existing snippets | |
| 43 // should be overwritten. | |
| 44 void FetchSnippets(bool overwrite); | |
| 45 | |
| 46 // Adds a callback that is called when a new set of snippets are downloaded | |
| 47 scoped_ptr<SnippetsAvailableCallbackList::Subscription> AddCallback( | |
| 48 const SnippetsAvailableCallback& callback) WARN_UNUSED_RESULT; | |
| 49 | |
| 50 private: | |
| 51 void StartTokenRequest(); | |
| 52 void NotifyObservers(); | |
| 53 void OnDownloadSnippetsDone(bool success); | |
| 54 void OnFileExistsCheckDone(bool exists); | |
| 55 void OnFileMoveDone(bool success); | |
| 56 void StartFetch(); | |
| 57 | |
| 58 // OAuth2TokenService::Consumer overrides: | |
| 59 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | |
| 60 const std::string& access_token, | |
| 61 const base::Time& expiration_time) override; | |
| 62 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | |
| 63 const GoogleServiceAuthError& error) override; | |
| 64 | |
| 65 // OAuth2TokenService::Observer overrides: | |
| 66 void OnRefreshTokenAvailable(const std::string& account_id) override; | |
| 67 | |
| 68 // URLFetcherDelegate implementation. | |
| 69 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
| 70 | |
| 71 // The SequencedTaskRunner on which file system operations will be run. | |
| 72 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | |
| 73 | |
| 74 // Holds the URL request context. Not owned. | |
| 75 net::URLRequestContextGetter* url_request_context_getter_; | |
|
mmenke
2016/02/18 16:51:11
You should hold onto a reference to this, or modif
May
2016/02/19 14:49:02
Thanks for the explanation, turned into a scoped_r
| |
| 76 | |
| 77 scoped_ptr<net::URLFetcher> url_fetcher_; | |
| 78 scoped_ptr<SigninManager> signin_manager_; | |
| 79 scoped_ptr<OAuth2TokenService> token_service_; | |
| 80 scoped_ptr<OAuth2TokenService::Request> oauth_request_; | |
| 81 | |
| 82 base::FilePath download_path_; | |
| 83 bool waiting_for_refresh_token_; | |
| 84 | |
| 85 SnippetsAvailableCallbackList callback_list_; | |
| 86 | |
| 87 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; | |
| 88 | |
| 89 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); | |
| 90 }; | |
| 91 } // namespace ntp_snippets | |
| 92 | |
| 93 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ | |
| OLD | NEW |