OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/common/extensions/api/extension_api.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/json/json_reader.h" | |
11 #include "base/logging.h" | |
12 #include "base/string_split.h" | |
13 #include "base/string_util.h" | |
14 #include "base/values.h" | |
15 #include "grit/common_resources.h" | |
16 #include "ui/base/resource/resource_bundle.h" | |
17 | |
18 namespace extensions { | |
19 | |
20 // static | |
21 ExtensionAPI* ExtensionAPI::GetInstance() { | |
22 return Singleton<ExtensionAPI>::get(); | |
23 } | |
24 | |
25 ExtensionAPI::ExtensionAPI() { | |
26 const bool kAllowTrailingCommas = false; | |
27 std::string error_message; | |
28 scoped_ptr<Value> temp_value( | |
29 base::JSONReader::ReadAndReturnError( | |
Matt Perry
2011/11/11 20:07:19
since this is used in the renderer, doesn't this m
Aaron Boodman
2011/11/11 23:12:02
In the latest patch, I've changed the place where
Matt Perry
2011/11/11 23:35:24
awesome
| |
30 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
31 IDR_EXTENSION_API_JSON).as_string(), | |
32 kAllowTrailingCommas, | |
33 NULL, // error code | |
34 &error_message)); | |
35 CHECK(temp_value.get()) << error_message; | |
36 CHECK(temp_value->IsType(base::Value::TYPE_LIST)) | |
37 << "Top-level node in extension API is not dictionary"; | |
38 | |
39 value_.reset(static_cast<base::ListValue*>(temp_value.release())); | |
40 } | |
41 | |
42 bool ExtensionAPI::IsFullNameUnprivileged(const std::string& full_name) const { | |
43 std::vector<std::string> name_space; | |
44 base::SplitString(full_name, '.', &name_space); | |
45 std::string name = name_space.back(); | |
46 name_space.pop_back(); | |
47 | |
48 base::DictionaryValue* name_space_node = | |
49 FindListItem(value_.get(), "namespace", JoinString(name_space, '.')); | |
50 CHECK(name_space_node); | |
51 | |
52 return IsChildNameUnprivileged(name_space_node, "functions", name) || | |
53 IsChildNameUnprivileged(name_space_node, "events", name); | |
54 } | |
55 | |
56 DictionaryValue* ExtensionAPI::FindListItem( | |
57 base::ListValue* list, | |
58 const std::string& property_name, | |
59 const std::string& property_value) const { | |
60 for (size_t i = 0; i < list->GetSize(); ++i) { | |
61 DictionaryValue* item = NULL; | |
62 CHECK(list->GetDictionary(i, &item)); | |
63 std::string value; | |
64 if (item->GetString(property_name, &value) && value == property_value) | |
65 return item; | |
66 } | |
67 | |
68 return NULL; | |
69 } | |
70 | |
71 bool ExtensionAPI::IsChildNameUnprivileged( | |
72 DictionaryValue* name_space_node, | |
73 const std::string& child_kind, | |
74 const std::string& child_name) const { | |
75 ListValue* child_list = NULL; | |
76 name_space_node->GetList(child_kind, &child_list); | |
77 if (!child_list) | |
78 return false; | |
79 | |
80 bool unprivileged = false; | |
81 DictionaryValue* child = FindListItem(child_list, "name", child_name); | |
82 if (!child || !child->GetBoolean("unprivileged", &unprivileged)) | |
83 return false; | |
84 | |
85 return unprivileged; | |
86 } | |
87 | |
88 } | |
OLD | NEW |