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" | |
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() { | |
40 base::FilePath dir; | |
41 #if defined(OS_ANDROID) | |
42 DCHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &dir)); | |
43 return dir.AppendASCII(kSnippetSuggestionsFilename); | |
44 #else | |
45 NOTIMPLEMENTED(); | |
46 return dir; | |
47 #endif | |
48 } | |
49 | |
50 NTPSnippetsFetcher::NTPSnippetsFetcher( | |
51 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | |
52 SigninManager* signin_manager, | |
53 OAuth2TokenService* token_service, | |
54 URLRequestContextGetter* url_request_context_getter) | |
55 : OAuth2TokenService::Consumer("NTP_snippets"), | |
56 file_task_runner_(file_task_runner), | |
57 url_request_context_getter_(url_request_context_getter), | |
58 signin_manager_(signin_manager), | |
59 token_service_(token_service), | |
60 weak_ptr_factory_(this) {} | |
61 | |
62 NTPSnippetsFetcher::~NTPSnippetsFetcher() {} | |
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, GetSnippetsSuggestionsPath()), | |
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 // Wait until we get a refresh token. | |
94 token_service_->AddObserver(this); | |
Marc Treib
2016/02/16 09:06:33
This will fail if we're already observing the toke
May
2016/02/17 17:32:54
Done.
| |
95 } | |
96 } | |
97 | |
98 void NTPSnippetsFetcher::StartTokenRequest() { | |
99 OAuth2TokenService::ScopeSet scopes; | |
100 scopes.insert(kApiScope); | |
101 oauth_request_ = token_service_->StartRequest( | |
102 signin_manager_->GetAuthenticatedAccountId(), scopes, this); | |
103 } | |
104 | |
105 void NTPSnippetsFetcher::NotifyObservers() { | |
106 callback_list_.Notify(GetSnippetsSuggestionsPath()); | |
107 } | |
108 | |
109 //////////////////////////////////////////////////////////////////////////////// | |
110 // OAuth2TokenService::Consumer overrides | |
111 void NTPSnippetsFetcher::OnGetTokenSuccess( | |
112 const OAuth2TokenService::Request* request, | |
113 const std::string& access_token, | |
114 const base::Time& expiration_time) { | |
115 oauth_request_.reset(); | |
116 url_fetcher_ = | |
117 URLFetcher::Create(GURL(kContentSnippetsServer), URLFetcher::POST, this); | |
118 url_fetcher_->SetRequestContext(url_request_context_getter_); | |
119 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
120 net::LOAD_DO_NOT_SAVE_COOKIES); | |
121 HttpRequestHeaders headers; | |
122 headers.SetHeader("Authorization", | |
123 base::StringPrintf(kAuthorizationRequestHeaderFormat, | |
124 access_token.c_str())); | |
125 headers.SetHeader("Content-Type", "application/json; charset=UTF-8"); | |
126 url_fetcher_->SetExtraRequestHeaders(headers.ToString()); | |
127 url_fetcher_->SetUploadData("application/json", | |
128 kUnpersonalizedRequestParameters); | |
129 url_fetcher_->SaveResponseToTemporaryFile(file_task_runner_.get()); | |
130 url_fetcher_->Start(); | |
131 } | |
132 | |
133 void NTPSnippetsFetcher::OnGetTokenFailure( | |
134 const OAuth2TokenService::Request* request, | |
135 const GoogleServiceAuthError& error) { | |
136 oauth_request_.reset(); | |
137 DLOG(ERROR) << "Unable to get token: " << error.ToString(); | |
138 } | |
139 | |
140 //////////////////////////////////////////////////////////////////////////////// | |
141 // OAuth2TokenService::Observer overrides | |
142 void NTPSnippetsFetcher::OnRefreshTokenAvailable( | |
143 const std::string& account_id) { | |
144 token_service_->RemoveObserver(this); | |
145 StartTokenRequest(); | |
146 } | |
147 | |
148 //////////////////////////////////////////////////////////////////////////////// | |
149 // URLFetcherDelegate overrides | |
150 void NTPSnippetsFetcher::OnURLFetchComplete(const URLFetcher* source) { | |
151 DCHECK_EQ(url_fetcher_.get(), source); | |
152 | |
153 const URLRequestStatus& status = source->GetStatus(); | |
154 if (!status.is_success()) { | |
155 DLOG(WARNING) << "URLRequestStatus error " << status.error() | |
156 << " while trying to download " << source->GetURL().spec(); | |
157 return; | |
158 } | |
159 | |
160 int response_code = source->GetResponseCode(); | |
161 if (response_code != net::HTTP_OK) { | |
162 DLOG(WARNING) << "HTTP error " << response_code | |
163 << " while trying to download " << source->GetURL().spec(); | |
164 return; | |
165 } | |
166 | |
167 base::FilePath response_path; | |
168 source->GetResponseAsFilePath(false, &response_path); | |
169 | |
170 base::PostTaskAndReplyWithResult( | |
171 file_task_runner_.get(), FROM_HERE, | |
172 base::Bind(&base::Move, response_path, GetSnippetsSuggestionsPath()), | |
173 base::Bind(&NTPSnippetsFetcher::OnFileMoveDone, | |
174 weak_ptr_factory_.GetWeakPtr())); | |
175 } | |
176 | |
177 void NTPSnippetsFetcher::OnFileMoveDone(bool success) { | |
178 if (!success) { | |
179 DLOG(WARNING) << "Could not move file to " | |
180 << GetSnippetsSuggestionsPath().LossyDisplayName(); | |
181 return; | |
182 } | |
183 | |
184 NotifyObservers(); | |
185 } | |
186 | |
187 } // namespace ntp_snippets | |
OLD | NEW |