summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_settings_api.cc
blob: 411d3b38c0980a3369cede0b39810267b02dff07 (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
// Copyright (c) 2011 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 "base/bind.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_settings_api.h"
#include "chrome/browser/profiles/profile.h"

namespace {
const char* kUnsupportedArgumentType = "Unsupported argument type";
}  // namespace

// StorageResultCallback

SettingsFunction::StorageResultCallback::StorageResultCallback(
    SettingsFunction* settings_function)
    : settings_function_(settings_function) {
}

SettingsFunction::StorageResultCallback::~StorageResultCallback() {
}

void SettingsFunction::StorageResultCallback::OnSuccess(
    DictionaryValue* settings) {
  settings_function_->result_.reset(settings);
  settings_function_->SendResponse(true);
}

void SettingsFunction::StorageResultCallback::OnFailure(
    const std::string& message) {
  settings_function_->error_ = message;
  settings_function_->SendResponse(false);
}

// SettingsFunction

bool SettingsFunction::RunImpl() {
  profile()->GetExtensionService()->extension_settings()->GetStorage(
      extension_id(),
      base::Bind(&SettingsFunction::RunWithStorage, this));
  return true;
}

void SettingsFunction::RunWithStorage(ExtensionSettingsStorage* storage) {
  // Mimic how RunImpl() is handled in extensions code.
  if (!RunWithStorageImpl(storage)) {
    SendResponse(false);
  }
}

// Concrete settings functions

bool GetSettingsFunction::RunWithStorageImpl(
    ExtensionSettingsStorage* storage) {
  Value *input;
  EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input));

  std::string as_string;
  ListValue* as_list;
  if (input->IsType(Value::TYPE_NULL)) {
    storage->Get(new StorageResultCallback(this));
  } else if (input->GetAsString(&as_string)) {
    storage->Get(as_string, new StorageResultCallback(this));
  } else if (input->GetAsList(&as_list)) {
    storage->Get(*as_list, new StorageResultCallback(this));
  } else {
    error_ = kUnsupportedArgumentType;
    return false;
  }

  return true;
}

bool SetSettingsFunction::RunWithStorageImpl(
    ExtensionSettingsStorage* storage) {
  DictionaryValue *input;
  EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input));
  storage->Set(*input, new StorageResultCallback(this));
  return true;
}

bool RemoveSettingsFunction::RunWithStorageImpl(
    ExtensionSettingsStorage* storage) {
  Value *input;
  EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input));

  std::string as_string;
  ListValue* as_list;
  if (input->GetAsString(&as_string)) {
    storage->Remove(as_string, new StorageResultCallback(this));
  } else if (input->GetAsList(&as_list)) {
    storage->Remove(*as_list, new StorageResultCallback(this));
  } else {
    error_ = kUnsupportedArgumentType;
    return false;
  }

  return true;
}

bool ClearSettingsFunction::RunWithStorageImpl(
    ExtensionSettingsStorage* storage) {
  storage->Clear(new StorageResultCallback(this));
  return true;
}