Index: components/ntp_snippets/ntp_snippets_fetcher.h |
diff --git a/components/ntp_snippets/ntp_snippets_fetcher.h b/components/ntp_snippets/ntp_snippets_fetcher.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bcf4dff00cbd09c809c2ab09aee508f7c580b3cf |
--- /dev/null |
+++ b/components/ntp_snippets/ntp_snippets_fetcher.h |
@@ -0,0 +1,93 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ |
+#define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ |
+ |
+#include "base/callback.h" |
+#include "base/callback_list.h" |
+#include "base/files/file_path.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/observer_list.h" |
+#include "base/sequenced_task_runner.h" |
+#include "google_apis/gaia/oauth2_token_service.h" |
+ |
+namespace net { |
+class URLRequestContextGetter; |
+class URLFetcher; |
+class URLFetcherDelegate; |
+} // namespace net |
+ |
+class SigninManager; |
+ |
+namespace ntp_snippets { |
+ |
+// Fetches snippet data for the NTP from the server |
+class NTPSnippetsFetcher : public OAuth2TokenService::Consumer, |
+ public OAuth2TokenService::Observer, |
+ public net::URLFetcherDelegate { |
+ public: |
+ using SnippetsAvailableCallback = base::Callback<void(const base::FilePath&)>; |
+ using SnippetsAvailableCallbackList = |
+ base::CallbackList<void(const base::FilePath&)>; |
+ |
+ NTPSnippetsFetcher(scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
+ SigninManager* signin_manager, |
+ OAuth2TokenService* oauth2_token_service, |
+ net::URLRequestContextGetter* url_request_context_getter, |
+ const base::FilePath& base_download_path); |
+ ~NTPSnippetsFetcher() override; |
+ |
+ // Fetches snippets from the server. |overwrite| is true if existing snippets |
+ // should be overwritten. |
+ void FetchSnippets(bool overwrite); |
+ |
+ // Adds a callback that is called when a new set of snippets are downloaded |
+ scoped_ptr<SnippetsAvailableCallbackList::Subscription> AddCallback( |
+ const SnippetsAvailableCallback& callback) WARN_UNUSED_RESULT; |
+ |
+ private: |
+ void StartTokenRequest(); |
+ void NotifyObservers(); |
+ void OnDownloadSnippetsDone(bool success); |
+ void OnFileExistsCheckDone(bool exists); |
+ void OnFileMoveDone(bool success); |
+ void StartFetch(); |
+ |
+ // OAuth2TokenService::Consumer overrides: |
+ void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
+ const std::string& access_token, |
+ const base::Time& expiration_time) override; |
+ void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
+ const GoogleServiceAuthError& error) override; |
+ |
+ // OAuth2TokenService::Observer overrides: |
+ void OnRefreshTokenAvailable(const std::string& account_id) override; |
+ |
+ // URLFetcherDelegate implementation. |
+ void OnURLFetchComplete(const net::URLFetcher* source) override; |
+ |
+ // The SequencedTaskRunner on which file system operations will be run. |
+ scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
+ |
+ // Holds the URL request context. Not owned. |
+ 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
|
+ |
+ scoped_ptr<net::URLFetcher> url_fetcher_; |
+ scoped_ptr<SigninManager> signin_manager_; |
+ scoped_ptr<OAuth2TokenService> token_service_; |
+ scoped_ptr<OAuth2TokenService::Request> oauth_request_; |
+ |
+ base::FilePath download_path_; |
+ bool waiting_for_refresh_token_; |
+ |
+ SnippetsAvailableCallbackList callback_list_; |
+ |
+ base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); |
+}; |
+} // namespace ntp_snippets |
+ |
+#endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ |