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