summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension.cc
blob: b2db579c7ce791cac91156d13eb16379b8778882 (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
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2006-2008 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/extensions/extension.h"

#include "base/logging.h"
#include "base/string_util.h"

const FilePath::CharType* Extension::kManifestFilename =
    FILE_PATH_LITERAL("manifest");

const wchar_t* Extension::kFormatVersionKey = L"format_version";
const wchar_t* Extension::kIdKey = L"id";
const wchar_t* Extension::kNameKey = L"name";
const wchar_t* Extension::kDescriptionKey = L"description";
const wchar_t* Extension::kContentScriptsKey = L"content_scripts";

const wchar_t* Extension::kInvalidManifestError =
    L"Manifest is missing or invalid.";
const wchar_t* Extension::kInvalidFormatVersionError =
    StringPrintf(L"Required key '%ls' is missing or invalid",
                 kFormatVersionKey).c_str();
const wchar_t* Extension::kInvalidIdError =
    StringPrintf(L"Required key '%ls' is missing or invalid.",
                 kIdKey).c_str();
const wchar_t* Extension::kInvalidNameError =
    StringPrintf(L"Required key '%ls' is missing or has invalid type.",
                 kNameKey).c_str();
const wchar_t* Extension::kInvalidDescriptionError =
    StringPrintf(L"Invalid type for '%ls' key.",
                 kDescriptionKey).c_str();
const wchar_t* Extension::kInvalidContentScriptsListError =
    StringPrintf(L"Invalid type for '%ls' key.",
                 kContentScriptsKey).c_str();
const wchar_t* Extension::kInvalidContentScriptError =
    StringPrintf(L"Invalid type for %ls at index ",
                 kContentScriptsKey).c_str();

bool Extension::InitFromValue(const DictionaryValue& source,
                              std::wstring* error) {
  // Check format version.
  int format_version = 0;
  if (!source.GetInteger(kFormatVersionKey, &format_version) ||
      format_version != kExpectedFormatVersion) {
    *error = kInvalidFormatVersionError;
    return false;
  }

  // Initialize id.
  if (!source.GetString(kIdKey, &id_)) {
    *error = kInvalidIdError;
    return false;
  }

  // Initialize name.
  if (!source.GetString(kNameKey, &name_)) {
    *error = kInvalidNameError;
    return false;
  }

  // Initialize description (optional).
  Value* value = NULL;
  if (source.Get(kDescriptionKey, &value)) {
    if (!value->GetAsString(&description_)) {
      *error = kInvalidDescriptionError;
      return false;
    }
  }

  // Initialize content scripts (optional).
  if (source.Get(kContentScriptsKey, &value)) {
    ListValue* list_value = NULL;
    if (value->GetType() != Value::TYPE_LIST) {
      *error = kInvalidContentScriptsListError;
      return false;
    } else {
      list_value = static_cast<ListValue*>(value);
    }

    for (size_t i = 0; i < list_value->GetSize(); ++i) {
      std::wstring content_script;
      if (!list_value->Get(i, &value) || !value->GetAsString(&content_script)) {
        *error = kInvalidContentScriptError;
        *error += IntToWString(i);
        return false;
      }

      content_scripts_.push_back(content_script);
    }
  }

  return true;
}

void Extension::CopyToValue(DictionaryValue* destination) {
  // Set format version
  destination->SetInteger(kFormatVersionKey,
                          kExpectedFormatVersion);

  // Copy id.
  destination->SetString(kIdKey, id_);

  // Copy name.
  destination->SetString(kNameKey, name_);

  // Copy description (optional).
  if (description_.size() > 0)
    destination->SetString(kDescriptionKey, description_);

  // Copy content scripts (optional).
  if (content_scripts_.size() > 0) {
    ListValue* list_value = new ListValue();
    destination->Set(kContentScriptsKey, list_value);

    for (size_t i = 0; i < content_scripts_.size(); ++i) {
      list_value->Set(i, Value::CreateStringValue(content_scripts_[i]));
    }
  }
}