summaryrefslogtreecommitdiffstats
path: root/chrome/browser/geolocation/access_token_store.cc
blob: b0d2d5e792075e80788e0c48c2f5ac560cc198ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/geolocation/access_token_store.h"

#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/pref_service.h"
#include "chrome/common/pref_names.h"
#include "googleurl/src/gurl.h"

namespace {
class ChromePrefsAccessTokenStore : public AccessTokenStore {
 public:
  ChromePrefsAccessTokenStore();

 private:
  void LoadDictionaryStoreInUIThread(
      scoped_refptr<CancelableRequest<LoadAccessTokensCallbackType> > request);

  // AccessTokenStore
  virtual void DoLoadAccessTokens(
      scoped_refptr<CancelableRequest<LoadAccessTokensCallbackType> > request);
  virtual void SaveAccessToken(
      const GURL& server_url, const string16& access_token);

  DISALLOW_COPY_AND_ASSIGN(ChromePrefsAccessTokenStore);
};

ChromePrefsAccessTokenStore::ChromePrefsAccessTokenStore() {
}

void ChromePrefsAccessTokenStore::LoadDictionaryStoreInUIThread(
      scoped_refptr<CancelableRequest<LoadAccessTokensCallbackType> > request) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
  if (request->canceled())
    return;
  const DictionaryValue* token_dictionary =
          g_browser_process->local_state()->GetDictionary(
              prefs::kGeolocationAccessToken);

  AccessTokenStore::AccessTokenSet access_token_set;
  // The dictionary value could be NULL if the pref has never been set.
  if (token_dictionary != NULL) {
    for (DictionaryValue::key_iterator it = token_dictionary->begin_keys();
        it != token_dictionary->end_keys(); ++it) {
      GURL url(WideToUTF8(*it));
      if (!url.is_valid())
        continue;
      token_dictionary->GetStringAsUTF16WithoutPathExpansion(
          *it, &access_token_set[url]);
    }
  }
  request->ForwardResultAsync(MakeTuple(access_token_set));
}

void ChromePrefsAccessTokenStore::DoLoadAccessTokens(
    scoped_refptr<CancelableRequest<LoadAccessTokensCallbackType> > request) {
  ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, NewRunnableMethod(
      this, &ChromePrefsAccessTokenStore::LoadDictionaryStoreInUIThread,
      request));
}

void SetAccessTokenOnUIThread(const GURL& server_url, const string16& token) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
  DictionaryValue* access_token_dictionary =
      g_browser_process->local_state()->GetMutableDictionary(
          prefs::kGeolocationAccessToken);
  access_token_dictionary->SetWithoutPathExpansion(
      UTF8ToWide(server_url.spec()),
      Value::CreateStringValueFromUTF16(token));
}

void ChromePrefsAccessTokenStore::SaveAccessToken(
      const GURL& server_url, const string16& access_token) {
  ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, NewRunnableFunction(
      &SetAccessTokenOnUIThread, server_url, access_token));
}
}  // namespace

AccessTokenStore::AccessTokenStore() {
}

AccessTokenStore::~AccessTokenStore() {
}

void AccessTokenStore::RegisterPrefs(PrefService* prefs) {
  prefs->RegisterDictionaryPref(prefs::kGeolocationAccessToken);
}

AccessTokenStore::Handle AccessTokenStore::LoadAccessTokens(
    CancelableRequestConsumerBase* consumer,
    LoadAccessTokensCallbackType* callback) {
  scoped_refptr<CancelableRequest<LoadAccessTokensCallbackType> > request =
      new CancelableRequest<LoadAccessTokensCallbackType>(callback);
  AddRequest(request, consumer);
  DCHECK(request->handle());

  DoLoadAccessTokens(request);
  return request->handle();
}

// Creates a new access token store backed by the global chome prefs.
AccessTokenStore* NewChromePrefsAccessTokenStore() {
  return new ChromePrefsAccessTokenStore;
}