1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be 3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file. 4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 5dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "content/browser/geolocation/access_token_store.h" 6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include "base/string_piece.h" 8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/utf_string_conversions.h" 9c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/values.h" 10c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/browser_process.h" 113345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include "chrome/browser/prefs/pref_service.h" 12ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/browser/prefs/scoped_user_pref_update.h" 13c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/common/pref_names.h" 14dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "content/browser/browser_thread.h" 15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "googleurl/src/gurl.h" 16c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochnamespace { 18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass ChromePrefsAccessTokenStore : public AccessTokenStore { 19c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public: 20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ChromePrefsAccessTokenStore(); 21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private: 23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch void LoadDictionaryStoreInUIThread( 24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch scoped_refptr<CancelableRequest<LoadAccessTokensCallbackType> > request); 25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // AccessTokenStore 27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual void DoLoadAccessTokens( 28c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch scoped_refptr<CancelableRequest<LoadAccessTokensCallbackType> > request); 29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual void SaveAccessToken( 30c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const GURL& server_url, const string16& access_token); 31c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 32c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch DISALLOW_COPY_AND_ASSIGN(ChromePrefsAccessTokenStore); 33c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}; 34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 35c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochChromePrefsAccessTokenStore::ChromePrefsAccessTokenStore() { 36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochvoid ChromePrefsAccessTokenStore::LoadDictionaryStoreInUIThread( 39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch scoped_refptr<CancelableRequest<LoadAccessTokensCallbackType> > request) { 40731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch if (request->canceled()) 42c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return; 43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const DictionaryValue* token_dictionary = 44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch g_browser_process->local_state()->GetDictionary( 45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch prefs::kGeolocationAccessToken); 46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch AccessTokenStore::AccessTokenSet access_token_set; 48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // The dictionary value could be NULL if the pref has never been set. 49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch if (token_dictionary != NULL) { 50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch for (DictionaryValue::key_iterator it = token_dictionary->begin_keys(); 51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch it != token_dictionary->end_keys(); ++it) { 523345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick GURL url(*it); 53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch if (!url.is_valid()) 54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch continue; 553345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick token_dictionary->GetStringWithoutPathExpansion(*it, 563345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick &access_token_set[url]); 57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 59c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch request->ForwardResultAsync(MakeTuple(access_token_set)); 60c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 61c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 62c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochvoid ChromePrefsAccessTokenStore::DoLoadAccessTokens( 63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch scoped_refptr<CancelableRequest<LoadAccessTokensCallbackType> > request) { 64731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, NewRunnableMethod( 65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch this, &ChromePrefsAccessTokenStore::LoadDictionaryStoreInUIThread, 66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch request)); 67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochvoid SetAccessTokenOnUIThread(const GURL& server_url, const string16& token) { 70731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 71ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen DictionaryPrefUpdate update(g_browser_process->local_state(), 72ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen prefs::kGeolocationAccessToken); 73ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen DictionaryValue* access_token_dictionary = update.Get(); 74c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch access_token_dictionary->SetWithoutPathExpansion( 753345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick server_url.spec(), Value::CreateStringValue(token)); 76c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 78c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochvoid ChromePrefsAccessTokenStore::SaveAccessToken( 79c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const GURL& server_url, const string16& access_token) { 80731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, NewRunnableFunction( 81c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch &SetAccessTokenOnUIThread, server_url, access_token)); 82c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} // namespace 84c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 85c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochAccessTokenStore::AccessTokenStore() { 86c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 88c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochAccessTokenStore::~AccessTokenStore() { 89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 91c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochvoid AccessTokenStore::RegisterPrefs(PrefService* prefs) { 92c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch prefs->RegisterDictionaryPref(prefs::kGeolocationAccessToken); 93c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 94c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 95c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochAccessTokenStore::Handle AccessTokenStore::LoadAccessTokens( 96c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch CancelableRequestConsumerBase* consumer, 97c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch LoadAccessTokensCallbackType* callback) { 98513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch scoped_refptr<CancelableRequest<LoadAccessTokensCallbackType> > request( 99513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch new CancelableRequest<LoadAccessTokensCallbackType>(callback)); 100c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch AddRequest(request, consumer); 101c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch DCHECK(request->handle()); 102c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 103c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch DoLoadAccessTokens(request); 104c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return request->handle(); 105c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 106c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 107c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Creates a new access token store backed by the global chome prefs. 108c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochAccessTokenStore* NewChromePrefsAccessTokenStore() { 109c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return new ChromePrefsAccessTokenStore; 110c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 111