Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(449)

Side by Side Diff: components/ntp_snippets/ntp_snippets_fetcher.cc

Issue 1699143002: [NTP Snippets] Schedule periodic fetching (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@snippets_feature
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 #include "components/ntp_snippets/ntp_snippets_fetcher.h" 4 #include "components/ntp_snippets/ntp_snippets_fetcher.h"
5 5
6 #include "base/files/file_path.h" 6 #include "base/files/file_path.h"
7 #include "base/files/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/path_service.h" 8 #include "base/path_service.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/task_runner_util.h" 10 #include "base/task_runner_util.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 NTPSnippetsFetcher::NTPSnippetsFetcher( 50 NTPSnippetsFetcher::NTPSnippetsFetcher(
51 scoped_refptr<base::SequencedTaskRunner> file_task_runner, 51 scoped_refptr<base::SequencedTaskRunner> file_task_runner,
52 SigninManager* signin_manager, 52 SigninManager* signin_manager,
53 OAuth2TokenService* token_service, 53 OAuth2TokenService* token_service,
54 URLRequestContextGetter* url_request_context_getter) 54 URLRequestContextGetter* url_request_context_getter)
55 : OAuth2TokenService::Consumer("NTP_snippets"), 55 : OAuth2TokenService::Consumer("NTP_snippets"),
56 file_task_runner_(file_task_runner), 56 file_task_runner_(file_task_runner),
57 url_request_context_getter_(url_request_context_getter), 57 url_request_context_getter_(url_request_context_getter),
58 signin_manager_(signin_manager), 58 signin_manager_(signin_manager),
59 token_service_(token_service), 59 token_service_(token_service),
60 waiting_for_refresh_token_(false),
60 weak_ptr_factory_(this) {} 61 weak_ptr_factory_(this) {}
61 62
62 NTPSnippetsFetcher::~NTPSnippetsFetcher() {} 63 NTPSnippetsFetcher::~NTPSnippetsFetcher() {}
63 64
64 scoped_ptr<NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> 65 scoped_ptr<NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription>
65 NTPSnippetsFetcher::AddCallback(const SnippetsAvailableCallback& callback) { 66 NTPSnippetsFetcher::AddCallback(const SnippetsAvailableCallback& callback) {
66 return callback_list_.Add(callback); 67 return callback_list_.Add(callback);
67 } 68 }
68 69
69 void NTPSnippetsFetcher::FetchSnippets(bool overwrite) { 70 void NTPSnippetsFetcher::FetchSnippets(bool overwrite) {
(...skipping 12 matching lines...) Expand all
82 if (exists) { 83 if (exists) {
83 NotifyObservers(); 84 NotifyObservers();
84 } else { 85 } else {
85 StartFetch(); 86 StartFetch();
86 } 87 }
87 } 88 }
88 89
89 void NTPSnippetsFetcher::StartFetch() { 90 void NTPSnippetsFetcher::StartFetch() {
90 if (signin_manager_->IsAuthenticated()) { 91 if (signin_manager_->IsAuthenticated()) {
91 StartTokenRequest(); 92 StartTokenRequest();
92 } else { 93 } else if (!waiting_for_refresh_token_) {
93 // Wait until we get a refresh token. 94 // Wait until we get a refresh token.
94 token_service_->AddObserver(this); 95 token_service_->AddObserver(this);
96 waiting_for_refresh_token_ = true;
95 } 97 }
96 } 98 }
97 99
98 void NTPSnippetsFetcher::StartTokenRequest() { 100 void NTPSnippetsFetcher::StartTokenRequest() {
99 OAuth2TokenService::ScopeSet scopes; 101 OAuth2TokenService::ScopeSet scopes;
100 scopes.insert(kApiScope); 102 scopes.insert(kApiScope);
101 oauth_request_ = token_service_->StartRequest( 103 oauth_request_ = token_service_->StartRequest(
102 signin_manager_->GetAuthenticatedAccountId(), scopes, this); 104 signin_manager_->GetAuthenticatedAccountId(), scopes, this);
103 } 105 }
104 106
(...skipping 30 matching lines...) Expand all
135 const GoogleServiceAuthError& error) { 137 const GoogleServiceAuthError& error) {
136 oauth_request_.reset(); 138 oauth_request_.reset();
137 DLOG(ERROR) << "Unable to get token: " << error.ToString(); 139 DLOG(ERROR) << "Unable to get token: " << error.ToString();
138 } 140 }
139 141
140 //////////////////////////////////////////////////////////////////////////////// 142 ////////////////////////////////////////////////////////////////////////////////
141 // OAuth2TokenService::Observer overrides 143 // OAuth2TokenService::Observer overrides
142 void NTPSnippetsFetcher::OnRefreshTokenAvailable( 144 void NTPSnippetsFetcher::OnRefreshTokenAvailable(
143 const std::string& account_id) { 145 const std::string& account_id) {
144 token_service_->RemoveObserver(this); 146 token_service_->RemoveObserver(this);
147 waiting_for_refresh_token_ = false;
145 StartTokenRequest(); 148 StartTokenRequest();
146 } 149 }
147 150
148 //////////////////////////////////////////////////////////////////////////////// 151 ////////////////////////////////////////////////////////////////////////////////
149 // URLFetcherDelegate overrides 152 // URLFetcherDelegate overrides
150 void NTPSnippetsFetcher::OnURLFetchComplete(const URLFetcher* source) { 153 void NTPSnippetsFetcher::OnURLFetchComplete(const URLFetcher* source) {
151 DCHECK_EQ(url_fetcher_.get(), source); 154 DCHECK_EQ(url_fetcher_.get(), source);
152 155
153 const URLRequestStatus& status = source->GetStatus(); 156 const URLRequestStatus& status = source->GetStatus();
154 if (!status.is_success()) { 157 if (!status.is_success()) {
(...skipping 23 matching lines...) Expand all
178 if (!success) { 181 if (!success) {
179 DLOG(WARNING) << "Could not move file to " 182 DLOG(WARNING) << "Could not move file to "
180 << GetSnippetsSuggestionsPath().LossyDisplayName(); 183 << GetSnippetsSuggestionsPath().LossyDisplayName();
181 return; 184 return;
182 } 185 }
183 186
184 NotifyObservers(); 187 NotifyObservers();
185 } 188 }
186 189
187 } // namespace ntp_snippets 190 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698