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