OLD | NEW |
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 | 4 |
5 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 5 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 NTPSnippetsFetcher::~NTPSnippetsFetcher() { | 59 NTPSnippetsFetcher::~NTPSnippetsFetcher() { |
60 if (waiting_for_refresh_token_) | 60 if (waiting_for_refresh_token_) |
61 token_service_->RemoveObserver(this); | 61 token_service_->RemoveObserver(this); |
62 } | 62 } |
63 | 63 |
64 scoped_ptr<NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> | 64 scoped_ptr<NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> |
65 NTPSnippetsFetcher::AddCallback(const SnippetsAvailableCallback& callback) { | 65 NTPSnippetsFetcher::AddCallback(const SnippetsAvailableCallback& callback) { |
66 return callback_list_.Add(callback); | 66 return callback_list_.Add(callback); |
67 } | 67 } |
68 | 68 |
69 void NTPSnippetsFetcher::FetchSnippets(bool overwrite) { | 69 void NTPSnippetsFetcher::FetchSnippets() { |
70 if (overwrite) { | |
71 StartFetch(); | |
72 } else { | |
73 base::PostTaskAndReplyWithResult( | |
74 file_task_runner_.get(), FROM_HERE, | |
75 base::Bind(&base::PathExists, download_path_), | |
76 base::Bind(&NTPSnippetsFetcher::OnFileExistsCheckDone, | |
77 weak_ptr_factory_.GetWeakPtr())); | |
78 } | |
79 } | |
80 | |
81 void NTPSnippetsFetcher::OnFileExistsCheckDone(bool exists) { | |
82 if (exists) { | |
83 NotifyObservers(); | |
84 } else { | |
85 StartFetch(); | |
86 } | |
87 } | |
88 | |
89 void NTPSnippetsFetcher::StartFetch() { | |
90 if (signin_manager_->IsAuthenticated()) { | 70 if (signin_manager_->IsAuthenticated()) { |
91 StartTokenRequest(); | 71 StartTokenRequest(); |
92 } else { | 72 } else { |
93 if (!waiting_for_refresh_token_) { | 73 if (!waiting_for_refresh_token_) { |
94 // Wait until we get a refresh token. | 74 // Wait until we get a refresh token. |
95 waiting_for_refresh_token_ = true; | 75 waiting_for_refresh_token_ = true; |
96 token_service_->AddObserver(this); | 76 token_service_->AddObserver(this); |
97 } | 77 } |
98 } | 78 } |
99 } | 79 } |
100 | 80 |
101 void NTPSnippetsFetcher::StartTokenRequest() { | 81 void NTPSnippetsFetcher::StartTokenRequest() { |
102 OAuth2TokenService::ScopeSet scopes; | 82 OAuth2TokenService::ScopeSet scopes; |
103 scopes.insert(kApiScope); | 83 scopes.insert(kApiScope); |
104 oauth_request_ = token_service_->StartRequest( | 84 oauth_request_ = token_service_->StartRequest( |
105 signin_manager_->GetAuthenticatedAccountId(), scopes, this); | 85 signin_manager_->GetAuthenticatedAccountId(), scopes, this); |
106 } | 86 } |
107 | 87 |
| 88 void NTPSnippetsFetcher::OnFileMoveDone(bool success) { |
| 89 if (!success) { |
| 90 DLOG(WARNING) << "Could not move file to " |
| 91 << download_path_.LossyDisplayName(); |
| 92 return; |
| 93 } |
| 94 |
| 95 NotifyObservers(); |
| 96 } |
| 97 |
108 void NTPSnippetsFetcher::NotifyObservers() { | 98 void NTPSnippetsFetcher::NotifyObservers() { |
109 callback_list_.Notify(download_path_); | 99 callback_list_.Notify(download_path_); |
110 } | 100 } |
111 | 101 |
112 //////////////////////////////////////////////////////////////////////////////// | 102 //////////////////////////////////////////////////////////////////////////////// |
113 // OAuth2TokenService::Consumer overrides | 103 // OAuth2TokenService::Consumer overrides |
114 void NTPSnippetsFetcher::OnGetTokenSuccess( | 104 void NTPSnippetsFetcher::OnGetTokenSuccess( |
115 const OAuth2TokenService::Request* request, | 105 const OAuth2TokenService::Request* request, |
116 const std::string& access_token, | 106 const std::string& access_token, |
117 const base::Time& expiration_time) { | 107 const base::Time& expiration_time) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 base::FilePath response_path; | 161 base::FilePath response_path; |
172 source->GetResponseAsFilePath(false, &response_path); | 162 source->GetResponseAsFilePath(false, &response_path); |
173 | 163 |
174 base::PostTaskAndReplyWithResult( | 164 base::PostTaskAndReplyWithResult( |
175 file_task_runner_.get(), FROM_HERE, | 165 file_task_runner_.get(), FROM_HERE, |
176 base::Bind(&base::Move, response_path, download_path_), | 166 base::Bind(&base::Move, response_path, download_path_), |
177 base::Bind(&NTPSnippetsFetcher::OnFileMoveDone, | 167 base::Bind(&NTPSnippetsFetcher::OnFileMoveDone, |
178 weak_ptr_factory_.GetWeakPtr())); | 168 weak_ptr_factory_.GetWeakPtr())); |
179 } | 169 } |
180 | 170 |
181 void NTPSnippetsFetcher::OnFileMoveDone(bool success) { | |
182 if (!success) { | |
183 DLOG(WARNING) << "Could not move file to " | |
184 << download_path_.LossyDisplayName(); | |
185 return; | |
186 } | |
187 | |
188 NotifyObservers(); | |
189 } | |
190 | |
191 } // namespace ntp_snippets | 171 } // namespace ntp_snippets |
OLD | NEW |