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 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | |
|
mmenke
2016/02/18 16:51:11
nit: blank line before first include.
May
2016/02/19 14:49:02
Done.
| |
| 5 | |
| 6 #include "base/files/file_path.h" | |
| 7 #include "base/files/file_util.h" | |
| 8 #include "base/path_service.h" | |
| 9 #include "base/strings/stringprintf.h" | |
| 10 #include "base/task_runner_util.h" | |
| 11 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
| 12 #include "components/signin/core/browser/signin_manager.h" | |
| 13 #include "components/signin/core/browser/signin_tracker.h" | |
| 14 #include "net/base/load_flags.h" | |
| 15 #include "net/http/http_request_headers.h" | |
| 16 #include "net/http/http_response_headers.h" | |
| 17 #include "net/http/http_status_code.h" | |
| 18 #include "net/url_request/url_fetcher.h" | |
| 19 | |
| 20 using net::URLFetcher; | |
| 21 using net::URLRequestContextGetter; | |
| 22 using net::HttpRequestHeaders; | |
| 23 using net::URLRequestStatus; | |
| 24 | |
| 25 namespace ntp_snippets { | |
| 26 | |
| 27 const char kSnippetSuggestionsFilename[] = "ntp_snippets.json"; | |
| 28 const char kApiScope[] = "https://www.googleapis.com/auth/webhistory"; | |
| 29 const char kContentSnippetsServer[] = | |
| 30 "https://chromereader-pa.googleapis.com/v1/fetch"; | |
| 31 const char kAuthorizationRequestHeaderFormat[] = "Bearer %s"; | |
| 32 | |
| 33 const char kUnpersonalizedRequestParameters[] = | |
| 34 "{ \"response_detail_level\": \"FULL_DEBUG\", \"advanced_options\": { " | |
| 35 "\"local_scoring_params\": {\"content_params\" : { " | |
| 36 "\"only_return_personalized_results\": false } }, " | |
| 37 "\"global_scoring_params\": { \"num_to_return\": 10 } } }"; | |
| 38 | |
| 39 base::FilePath GetSnippetsSuggestionsPath(const base::FilePath& base_dir) { | |
| 40 return base_dir.AppendASCII(kSnippetSuggestionsFilename); | |
| 41 } | |
| 42 | |
| 43 NTPSnippetsFetcher::NTPSnippetsFetcher( | |
| 44 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | |
| 45 SigninManager* signin_manager, | |
| 46 OAuth2TokenService* token_service, | |
| 47 URLRequestContextGetter* url_request_context_getter, | |
| 48 const base::FilePath& base_download_path) | |
| 49 : OAuth2TokenService::Consumer("NTP_snippets"), | |
| 50 file_task_runner_(file_task_runner), | |
| 51 url_request_context_getter_(url_request_context_getter), | |
| 52 signin_manager_(signin_manager), | |
| 53 token_service_(token_service), | |
| 54 download_path_(GetSnippetsSuggestionsPath(base_download_path)), | |
| 55 waiting_for_refresh_token_(false), | |
| 56 weak_ptr_factory_(this) {} | |
| 57 | |
| 58 NTPSnippetsFetcher::~NTPSnippetsFetcher() { | |
| 59 if (waiting_for_refresh_token_) | |
| 60 token_service_->RemoveObserver(this); | |
| 61 } | |
| 62 | |
| 63 scoped_ptr<NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> | |
| 64 NTPSnippetsFetcher::AddCallback(const SnippetsAvailableCallback& callback) { | |
| 65 return callback_list_.Add(callback); | |
| 66 } | |
| 67 | |
| 68 void NTPSnippetsFetcher::FetchSnippets(bool overwrite) { | |
| 69 if (overwrite) { | |
| 70 StartFetch(); | |
| 71 } else { | |
| 72 base::PostTaskAndReplyWithResult( | |
| 73 file_task_runner_.get(), FROM_HERE, | |
| 74 base::Bind(&base::PathExists, download_path_), | |
| 75 base::Bind(&NTPSnippetsFetcher::OnFileExistsCheckDone, | |
| 76 weak_ptr_factory_.GetWeakPtr())); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 void NTPSnippetsFetcher::OnFileExistsCheckDone(bool exists) { | |
| 81 if (exists) { | |
| 82 NotifyObservers(); | |
| 83 } else { | |
| 84 StartFetch(); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 void NTPSnippetsFetcher::StartFetch() { | |
| 89 if (signin_manager_->IsAuthenticated()) { | |
| 90 StartTokenRequest(); | |
| 91 } else { | |
| 92 if (!waiting_for_refresh_token_) { | |
| 93 // Wait until we get a refresh token. | |
| 94 waiting_for_refresh_token_ = true; | |
| 95 token_service_->AddObserver(this); | |
| 96 } | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 void NTPSnippetsFetcher::StartTokenRequest() { | |
| 101 OAuth2TokenService::ScopeSet scopes; | |
| 102 scopes.insert(kApiScope); | |
| 103 oauth_request_ = token_service_->StartRequest( | |
| 104 signin_manager_->GetAuthenticatedAccountId(), scopes, this); | |
| 105 } | |
| 106 | |
| 107 void NTPSnippetsFetcher::NotifyObservers() { | |
| 108 callback_list_.Notify(download_path_); | |
| 109 } | |
| 110 | |
| 111 //////////////////////////////////////////////////////////////////////////////// | |
| 112 // OAuth2TokenService::Consumer overrides | |
| 113 void NTPSnippetsFetcher::OnGetTokenSuccess( | |
| 114 const OAuth2TokenService::Request* request, | |
| 115 const std::string& access_token, | |
| 116 const base::Time& expiration_time) { | |
| 117 oauth_request_.reset(); | |
| 118 url_fetcher_ = | |
| 119 URLFetcher::Create(GURL(kContentSnippetsServer), URLFetcher::POST, this); | |
| 120 url_fetcher_->SetRequestContext(url_request_context_getter_); | |
| 121 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
| 122 net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 123 HttpRequestHeaders headers; | |
| 124 headers.SetHeader("Authorization", | |
| 125 base::StringPrintf(kAuthorizationRequestHeaderFormat, | |
| 126 access_token.c_str())); | |
| 127 headers.SetHeader("Content-Type", "application/json; charset=UTF-8"); | |
| 128 url_fetcher_->SetExtraRequestHeaders(headers.ToString()); | |
| 129 url_fetcher_->SetUploadData("application/json", | |
| 130 kUnpersonalizedRequestParameters); | |
| 131 url_fetcher_->SaveResponseToTemporaryFile(file_task_runner_.get()); | |
| 132 url_fetcher_->Start(); | |
|
mmenke
2016/02/18 16:51:11
Note that url_fetcher_ has no timeout logic (Other
May
2016/02/19 14:49:02
Acknowledged. Is there a different way to implemen
mmenke
2016/02/19 15:42:22
The only way to address the size issue currently i
| |
| 133 } | |
| 134 | |
| 135 void NTPSnippetsFetcher::OnGetTokenFailure( | |
| 136 const OAuth2TokenService::Request* request, | |
| 137 const GoogleServiceAuthError& error) { | |
| 138 oauth_request_.reset(); | |
| 139 DLOG(ERROR) << "Unable to get token: " << error.ToString(); | |
| 140 } | |
| 141 | |
| 142 //////////////////////////////////////////////////////////////////////////////// | |
| 143 // OAuth2TokenService::Observer overrides | |
| 144 void NTPSnippetsFetcher::OnRefreshTokenAvailable( | |
| 145 const std::string& account_id) { | |
| 146 token_service_->RemoveObserver(this); | |
| 147 waiting_for_refresh_token_ = false; | |
| 148 StartTokenRequest(); | |
| 149 } | |
| 150 | |
| 151 //////////////////////////////////////////////////////////////////////////////// | |
| 152 // URLFetcherDelegate overrides | |
| 153 void NTPSnippetsFetcher::OnURLFetchComplete(const URLFetcher* source) { | |
| 154 DCHECK_EQ(url_fetcher_.get(), source); | |
| 155 | |
| 156 const URLRequestStatus& status = source->GetStatus(); | |
| 157 if (!status.is_success()) { | |
| 158 DLOG(WARNING) << "URLRequestStatus error " << status.error() | |
| 159 << " while trying to download " << source->GetURL().spec(); | |
| 160 return; | |
| 161 } | |
| 162 | |
| 163 int response_code = source->GetResponseCode(); | |
| 164 if (response_code != net::HTTP_OK) { | |
| 165 DLOG(WARNING) << "HTTP error " << response_code | |
| 166 << " while trying to download " << source->GetURL().spec(); | |
| 167 return; | |
| 168 } | |
| 169 | |
| 170 base::FilePath response_path; | |
| 171 source->GetResponseAsFilePath(false, &response_path); | |
| 172 | |
| 173 base::PostTaskAndReplyWithResult( | |
| 174 file_task_runner_.get(), FROM_HERE, | |
| 175 base::Bind(&base::Move, response_path, download_path_), | |
| 176 base::Bind(&NTPSnippetsFetcher::OnFileMoveDone, | |
| 177 weak_ptr_factory_.GetWeakPtr())); | |
| 178 } | |
| 179 | |
| 180 void NTPSnippetsFetcher::OnFileMoveDone(bool success) { | |
| 181 if (!success) { | |
| 182 DLOG(WARNING) << "Could not move file to " | |
| 183 << download_path_.LossyDisplayName(); | |
| 184 return; | |
| 185 } | |
| 186 | |
| 187 NotifyObservers(); | |
| 188 } | |
| 189 | |
| 190 } // namespace ntp_snippets | |
| OLD | NEW |