2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-08-13 04:12:43 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/atom_access_token_store.h"
|
|
|
|
|
2016-09-20 20:24:45 +00:00
|
|
|
#include <string>
|
2014-08-13 06:43:21 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2014-08-13 04:12:43 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
2014-10-08 10:27:39 +00:00
|
|
|
#include "atom/common/google_api_key.h"
|
2016-09-20 19:01:59 +00:00
|
|
|
#include "base/environment.h"
|
2016-06-22 06:41:56 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2016-11-09 07:50:03 +00:00
|
|
|
#include "device/geolocation/geolocation_provider.h"
|
2014-08-13 05:28:05 +00:00
|
|
|
|
2016-09-09 12:18:11 +00:00
|
|
|
using content::BrowserThread;
|
|
|
|
|
2014-08-13 04:12:43 +00:00
|
|
|
namespace atom {
|
|
|
|
|
2016-10-14 19:30:54 +00:00
|
|
|
namespace internal {
|
2014-08-13 05:28:05 +00:00
|
|
|
|
2016-09-09 12:18:11 +00:00
|
|
|
// Loads access tokens and other necessary data on the UI thread, and
|
|
|
|
// calls back to the originator on the originating thread.
|
|
|
|
class TokenLoadingJob : public base::RefCountedThreadSafe<TokenLoadingJob> {
|
|
|
|
public:
|
|
|
|
explicit TokenLoadingJob(
|
2016-11-09 07:50:03 +00:00
|
|
|
const device::AccessTokenStore::LoadAccessTokensCallback& callback)
|
2016-09-09 12:18:11 +00:00
|
|
|
: callback_(callback), request_context_getter_(nullptr) {}
|
|
|
|
|
2016-10-14 19:30:54 +00:00
|
|
|
void Run(AtomBrowserContext* browser_context) {
|
2016-09-09 12:18:11 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
request_context_getter_ = browser_context->GetRequestContext();
|
2016-09-20 20:24:45 +00:00
|
|
|
std::unique_ptr<base::Environment> env(base::Environment::Create());
|
|
|
|
if (!env->GetVar("GOOGLE_API_KEY", &api_key_))
|
|
|
|
api_key_ = GOOGLEAPIS_API_KEY;
|
2016-10-14 19:30:54 +00:00
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&TokenLoadingJob::RespondOnIOThread, this));
|
2016-09-09 12:18:11 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 19:30:54 +00:00
|
|
|
private:
|
|
|
|
friend class base::RefCountedThreadSafe<TokenLoadingJob>;
|
|
|
|
|
|
|
|
~TokenLoadingJob() {}
|
|
|
|
|
|
|
|
void RespondOnIOThread() {
|
2016-09-20 20:24:45 +00:00
|
|
|
// Equivalent to access_token_map[kGeolocationProviderURL].
|
2016-09-09 12:18:11 +00:00
|
|
|
// Somehow base::string16 is causing compilation errors when used in a pair
|
|
|
|
// of std::map on Linux, this can work around it.
|
2016-11-09 07:50:03 +00:00
|
|
|
device::AccessTokenStore::AccessTokenMap access_token_map;
|
2016-09-09 12:18:11 +00:00
|
|
|
std::pair<GURL, base::string16> token_pair;
|
2016-09-20 19:01:59 +00:00
|
|
|
token_pair.first = GURL(GOOGLEAPIS_ENDPOINT + api_key_);
|
2016-09-09 12:18:11 +00:00
|
|
|
access_token_map.insert(token_pair);
|
|
|
|
|
|
|
|
callback_.Run(access_token_map, request_context_getter_);
|
|
|
|
}
|
|
|
|
|
2016-11-09 07:50:03 +00:00
|
|
|
device::AccessTokenStore::LoadAccessTokensCallback callback_;
|
2016-09-09 12:18:11 +00:00
|
|
|
net::URLRequestContextGetter* request_context_getter_;
|
2016-09-20 20:24:45 +00:00
|
|
|
std::string api_key_;
|
2016-09-09 12:18:11 +00:00
|
|
|
};
|
|
|
|
|
2016-10-14 19:30:54 +00:00
|
|
|
} // namespace internal
|
2014-08-13 05:28:05 +00:00
|
|
|
|
2014-08-13 04:12:43 +00:00
|
|
|
AtomAccessTokenStore::AtomAccessTokenStore() {
|
2016-10-14 19:30:54 +00:00
|
|
|
browser_context_ = AtomBrowserContext::From("", false);
|
2016-11-09 07:50:03 +00:00
|
|
|
device::GeolocationProvider::GetInstance()->UserDidOptIntoLocationServices();
|
2014-08-13 04:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AtomAccessTokenStore::~AtomAccessTokenStore() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomAccessTokenStore::LoadAccessTokens(
|
2016-04-27 17:59:46 +00:00
|
|
|
const LoadAccessTokensCallback& callback) {
|
2016-10-14 19:30:54 +00:00
|
|
|
scoped_refptr<internal::TokenLoadingJob> job(
|
|
|
|
new internal::TokenLoadingJob(callback));
|
|
|
|
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
|
|
|
base::Bind(&AtomAccessTokenStore::RunTokenLoadingJob,
|
|
|
|
this, base::RetainedRef(job)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomAccessTokenStore::RunTokenLoadingJob(
|
|
|
|
scoped_refptr<internal::TokenLoadingJob> job) {
|
|
|
|
job->Run(browser_context_.get());
|
2016-06-22 06:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AtomAccessTokenStore::SaveAccessToken(const GURL& server_url,
|
|
|
|
const base::string16& access_token) {
|
|
|
|
}
|
|
|
|
|
2014-08-13 04:12:43 +00:00
|
|
|
} // namespace atom
|