diff options
27 files changed, 3015 insertions, 183 deletions
diff --git a/build/json_schema_compile.gypi b/build/json_schema_compile.gypi new file mode 100644 index 0000000..940e65c --- /dev/null +++ b/build/json_schema_compile.gypi @@ -0,0 +1,59 @@ +# Copyright (c) 2012 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. + +{ + 'variables': { + # When including this gypi, the following variables must be set: + # json_schema_files: an array of json files that comprise the api model. + # cc_dir: path to generated files + # root_namespace: the C++ namespace that all generated files go under + 'api_gen_dir': '<(DEPTH)/tools/json_schema_compiler', + 'api_gen': '<(api_gen_dir)/compiler.py', + }, + 'rules': [ + { + 'rule_name': 'genapi', + 'extension': 'json', + 'inputs': [ + '<(api_gen_dir)/code.py', + '<(api_gen_dir)/compiler.py', + '<(api_gen_dir)/model.py', + '<(api_gen_dir)/cc_generator.py', + '<(api_gen_dir)/h_generator.py', + '<(api_gen_dir)/cpp_type_generator.py', + '<@(json_schema_files)', + ], + 'outputs': [ + '<(SHARED_INTERMEDIATE_DIR)/<(cc_dir)/<(RULE_INPUT_ROOT).cc', + '<(SHARED_INTERMEDIATE_DIR)/<(cc_dir)/<(RULE_INPUT_ROOT).h', + ], + 'action': [ + 'python', + '<(api_gen)', + '<(RULE_INPUT_PATH)', + '--root=<(DEPTH)', + '--destdir=<(SHARED_INTERMEDIATE_DIR)', + '--namespace=<(root_namespace)', + '<@(json_schema_files)', + ], + 'message': 'Generating C++ code from <(RULE_INPUT_PATH) jsons', + 'process_outputs_as_sources': 1, + }, + ], + 'include_dirs': [ + '<(SHARED_INTERMEDIATE_DIR)', + '<(DEPTH)', + ], + 'dependencies':[ + '<(DEPTH)/tools/json_schema_compiler/api_gen_util.gyp:api_gen_util', + ], + 'direct_dependent_settings': { + 'include_dirs': [ + '<(SHARED_INTERMEDIATE_DIR)', + ] + }, + # This target exports a hard dependency because it generates header + # files. + 'hard_dependency': 1, +} diff --git a/chrome/browser/extensions/api/permissions/permissions_api.cc b/chrome/browser/extensions/api/permissions/permissions_api.cc index 357b820..40b31bb 100644 --- a/chrome/browser/extensions/api/permissions/permissions_api.cc +++ b/chrome/browser/extensions/api/permissions/permissions_api.cc @@ -5,20 +5,21 @@ #include "chrome/browser/extensions/api/permissions/permissions_api.h" #include "base/memory/scoped_ptr.h" -#include "base/values.h" #include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/permissions_updater.h" #include "chrome/browser/profiles/profile.h" #include "chrome/common/chrome_notification_types.h" +#include "chrome/common/extensions/api/permissions.h" #include "chrome/common/extensions/extension.h" #include "chrome/common/extensions/extension_error_utils.h" #include "chrome/common/extensions/url_pattern_set.h" #include "googleurl/src/gurl.h" using extensions::PermissionsUpdater; -using extensions::permissions_api::PackPermissionsToValue; -using extensions::permissions_api::UnpackPermissionsFromValue; +using extensions::permissions_api_helpers::PackPermissionSet; +using extensions::permissions_api_helpers::UnpackPermissionSet; +using namespace extensions::api::permissions; namespace { @@ -42,38 +43,34 @@ bool ignore_user_gesture_for_tests = false; } // namespace bool ContainsPermissionsFunction::RunImpl() { - DictionaryValue* args = NULL; - EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); - std::string error; - if (!args) - return false; + scoped_ptr<Contains::Params> params(Contains::Params::Create(*args_)); + EXTENSION_FUNCTION_VALIDATE(params.get()); - scoped_refptr<ExtensionPermissionSet> permissions; - if (!UnpackPermissionsFromValue(args, &permissions, &bad_message_, &error_)) + scoped_refptr<ExtensionPermissionSet> permissions = + UnpackPermissionSet(params->permissions, &error_); + if (!permissions.get()) return false; - CHECK(permissions.get()); - result_.reset(Value::CreateBooleanValue( + result_.reset(Contains::Result::Create( GetExtension()->GetActivePermissions()->Contains(*permissions))); return true; } bool GetAllPermissionsFunction::RunImpl() { - result_.reset(PackPermissionsToValue( - GetExtension()->GetActivePermissions())); + scoped_ptr<Permissions> permissions = + PackPermissionSet(GetExtension()->GetActivePermissions()); + result_.reset(GetAll::Result::Create(*permissions)); return true; } bool RemovePermissionsFunction::RunImpl() { - DictionaryValue* args = NULL; - EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); - if (!args) - return false; + scoped_ptr<Remove::Params> params(Remove::Params::Create(*args_)); + EXTENSION_FUNCTION_VALIDATE(params.get()); - scoped_refptr<ExtensionPermissionSet> permissions; - if (!UnpackPermissionsFromValue(args, &permissions, &bad_message_, &error_)) + scoped_refptr<ExtensionPermissionSet> permissions = + UnpackPermissionSet(params->permissions, &error_); + if (!permissions.get()) return false; - CHECK(permissions.get()); const Extension* extension = GetExtension(); ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); @@ -96,12 +93,12 @@ bool RemovePermissionsFunction::RunImpl() { ExtensionPermissionSet::CreateIntersection(permissions.get(), required)); if (!intersection->IsEmpty()) { error_ = kCantRemoveRequiredPermissionsError; - result_.reset(Value::CreateBooleanValue(false)); + result_.reset(Remove::Result::Create(false)); return false; } PermissionsUpdater(profile()).RemovePermissions(extension, permissions.get()); - result_.reset(Value::CreateBooleanValue(true)); + result_.reset(Remove::Result::Create(true)); return true; } @@ -125,15 +122,12 @@ bool RequestPermissionsFunction::RunImpl() { return false; } - DictionaryValue* args = NULL; - EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); - if (!args) - return false; + scoped_ptr<Request::Params> params(Request::Params::Create(*args_)); + EXTENSION_FUNCTION_VALIDATE(params.get()); - if (!UnpackPermissionsFromValue( - args, &requested_permissions_, &bad_message_, &error_)) + requested_permissions_ = UnpackPermissionSet(params->permissions, &error_); + if (!requested_permissions_.get()) return false; - CHECK(requested_permissions_.get()); ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); ExtensionPrefs* prefs = profile()->GetExtensionService()->extension_prefs(); @@ -154,7 +148,7 @@ bool RequestPermissionsFunction::RunImpl() { if (!GetExtension()->optional_permission_set()->Contains( *requested_permissions_)) { error_ = kNotInOptionalPermissionsError; - result_.reset(Value::CreateBooleanValue(false)); + result_.reset(Request::Result::Create(false)); return false; } @@ -165,7 +159,7 @@ bool RequestPermissionsFunction::RunImpl() { if (granted && granted->Contains(*requested_permissions_)) { PermissionsUpdater perms_updater(profile()); perms_updater.AddPermissions(GetExtension(), requested_permissions_.get()); - result_.reset(Value::CreateBooleanValue(true)); + result_.reset(Request::Result::Create(true)); SendResponse(true); return true; } @@ -199,14 +193,14 @@ void RequestPermissionsFunction::InstallUIProceed() { PermissionsUpdater perms_updater(profile()); perms_updater.AddPermissions(GetExtension(), requested_permissions_.get()); - result_.reset(Value::CreateBooleanValue(true)); + result_.reset(Request::Result::Create(true)); SendResponse(true); Release(); // Balanced in RunImpl(). } void RequestPermissionsFunction::InstallUIAbort(bool user_initiated) { - result_.reset(Value::CreateBooleanValue(false)); + result_.reset(Request::Result::Create(false)); SendResponse(true); Release(); // Balanced in RunImpl(). diff --git a/chrome/browser/extensions/api/permissions/permissions_api_helpers.cc b/chrome/browser/extensions/api/permissions/permissions_api_helpers.cc index 7479770..e5c31f6 100644 --- a/chrome/browser/extensions/api/permissions/permissions_api_helpers.cc +++ b/chrome/browser/extensions/api/permissions/permissions_api_helpers.cc @@ -5,13 +5,17 @@ #include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" #include "base/values.h" +#include "chrome/common/extensions/api/permissions.h" #include "chrome/common/extensions/extension.h" #include "chrome/common/extensions/extension_error_utils.h" #include "chrome/common/extensions/extension_permission_set.h" #include "chrome/common/extensions/url_pattern_set.h" namespace extensions { -namespace permissions_api { + +using api::permissions::Permissions; + +namespace permissions_api_helpers { namespace { @@ -20,93 +24,63 @@ const char kInvalidOrigin[] = const char kUnknownPermissionError[] = "'*' is not a recognized permission."; -const char kApisKey[] = "permissions"; -const char kOriginsKey[] = "origins"; - } // namespace -DictionaryValue* PackPermissionsToValue(const ExtensionPermissionSet* set) { - DictionaryValue* value = new DictionaryValue(); +scoped_ptr<Permissions> PackPermissionSet(const ExtensionPermissionSet* set) { + Permissions* permissions(new Permissions()); - // Generate the list of API permissions. - ListValue* apis = new ListValue(); + permissions->permissions.reset(new std::vector<std::string>()); ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); for (ExtensionAPIPermissionSet::const_iterator i = set->apis().begin(); - i != set->apis().end(); ++i) - apis->Append(Value::CreateStringValue(info->GetByID(*i)->name())); + i != set->apis().end(); ++i) { + permissions->permissions->push_back(info->GetByID(*i)->name()); + } - // Generate the list of origin permissions. + permissions->origins.reset(new std::vector<std::string>()); URLPatternSet hosts = set->explicit_hosts(); - ListValue* origins = new ListValue(); for (URLPatternSet::const_iterator i = hosts.begin(); i != hosts.end(); ++i) - origins->Append(Value::CreateStringValue(i->GetAsString())); + permissions->origins->push_back(i->GetAsString()); - value->Set(kApisKey, apis); - value->Set(kOriginsKey, origins); - return value; + return scoped_ptr<Permissions>(permissions); } -// Creates a new ExtensionPermissionSet from its |value| and passes ownership to -// the caller through |ptr|. Sets |bad_message| to true if the message is badly -// formed. Returns false if the method fails to unpack a permission set. -bool UnpackPermissionsFromValue(DictionaryValue* value, - scoped_refptr<ExtensionPermissionSet>* ptr, - bool* bad_message, - std::string* error) { - ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); +scoped_refptr<ExtensionPermissionSet> UnpackPermissionSet( + const Permissions& permissions, std::string* error) { ExtensionAPIPermissionSet apis; - if (value->HasKey(kApisKey)) { - ListValue* api_list = NULL; - if (!value->GetList(kApisKey, &api_list)) { - *bad_message = true; - return false; - } - for (size_t i = 0; i < api_list->GetSize(); ++i) { - std::string api_name; - if (!api_list->GetString(i, &api_name)) { - *bad_message = true; - return false; - } - - ExtensionAPIPermission* permission = info->GetByName(api_name); + std::vector<std::string>* permissions_list = permissions.permissions.get(); + if (permissions_list) { + ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); + for (std::vector<std::string>::iterator it = permissions_list->begin(); + it != permissions_list->end(); ++it) { + ExtensionAPIPermission* permission = info->GetByName(*it); if (!permission) { *error = ExtensionErrorUtils::FormatErrorMessage( - kUnknownPermissionError, api_name); - return false; + kUnknownPermissionError, *it); + return NULL; } apis.insert(permission->id()); } } URLPatternSet origins; - if (value->HasKey(kOriginsKey)) { - ListValue* origin_list = NULL; - if (!value->GetList(kOriginsKey, &origin_list)) { - *bad_message = true; - return false; - } - for (size_t i = 0; i < origin_list->GetSize(); ++i) { - std::string pattern; - if (!origin_list->GetString(i, &pattern)) { - *bad_message = true; - return false; - } - + if (permissions.origins.get()) { + for (std::vector<std::string>::iterator it = permissions.origins->begin(); + it != permissions.origins->end(); ++it) { URLPattern origin(Extension::kValidHostPermissionSchemes); - URLPattern::ParseResult parse_result = origin.Parse(pattern); + URLPattern::ParseResult parse_result = origin.Parse(*it); if (URLPattern::PARSE_SUCCESS != parse_result) { *error = ExtensionErrorUtils::FormatErrorMessage( kInvalidOrigin, - pattern, + *it, URLPattern::GetParseResultString(parse_result)); - return false; + return NULL; } origins.AddPattern(origin); } } - *ptr = new ExtensionPermissionSet(apis, origins, URLPatternSet()); - return true; + return scoped_refptr<ExtensionPermissionSet>( + new ExtensionPermissionSet(apis, origins, URLPatternSet())); } } // namespace permissions_api diff --git a/chrome/browser/extensions/api/permissions/permissions_api_helpers.h b/chrome/browser/extensions/api/permissions/permissions_api_helpers.h index e9e2255..158ca04 100644 --- a/chrome/browser/extensions/api/permissions/permissions_api_helpers.h +++ b/chrome/browser/extensions/api/permissions/permissions_api_helpers.h @@ -7,30 +7,35 @@ #pragma once #include <string> - +#include "base/memory/scoped_ptr.h" #include "base/memory/ref_counted.h" namespace base { class DictionaryValue; } + class ExtensionPermissionSet; namespace extensions { -namespace permissions_api { +namespace api { +namespace permissions { +struct Permissions; +} +} + +namespace permissions_api_helpers { -// Converts the permission |set| to a dictionary value. -base::DictionaryValue* PackPermissionsToValue( +// Converts the permission |set| to a permissions object. +scoped_ptr<api::permissions::Permissions> PackPermissionSet( const ExtensionPermissionSet* set); -// Converts the |value| to a permission set. -bool UnpackPermissionsFromValue(base::DictionaryValue* value, - scoped_refptr<ExtensionPermissionSet>* ptr, - bool* bad_message, - std::string* error); - -} // namespace permissions_api +// Creates a permission set from |permissions|. Returns NULL if the permissions +// cannot be converted to a permission set, in which case |error| will be set. +scoped_refptr<ExtensionPermissionSet> UnpackPermissionSet( + const api::permissions::Permissions& permissions, std::string* error); +} // namespace permissions_api_helpers } // namespace extensions #endif // CHROME_BROWSER_EXTENSIONS_API_PERMISSIONS_PERMISSIONS_API_HELPERS_H_ diff --git a/chrome/browser/extensions/api/permissions/permissions_api_helpers_unittest.cc b/chrome/browser/extensions/api/permissions/permissions_api_helpers_unittest.cc index 4e88b96..7d03989d 100644 --- a/chrome/browser/extensions/api/permissions/permissions_api_helpers_unittest.cc +++ b/chrome/browser/extensions/api/permissions/permissions_api_helpers_unittest.cc @@ -5,13 +5,15 @@ #include "base/memory/scoped_ptr.h" #include "base/values.h" #include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" +#include "chrome/common/extensions/api/permissions.h" #include "chrome/common/extensions/extension_permission_set.h" #include "chrome/common/extensions/url_pattern_set.h" #include "googleurl/src/gurl.h" #include "testing/gtest/include/gtest/gtest.h" -using extensions::permissions_api::PackPermissionsToValue; -using extensions::permissions_api::UnpackPermissionsFromValue; +using extensions::permissions_api_helpers::PackPermissionSet; +using extensions::permissions_api_helpers::UnpackPermissionSet; +using extensions::api::permissions::Permissions; namespace { @@ -31,42 +33,42 @@ TEST(ExtensionPermissionsAPIHelpers, Pack) { AddPattern(&hosts, "http://a.com/*"); AddPattern(&hosts, "http://b.com/*"); - scoped_refptr<ExtensionPermissionSet> permissions = + scoped_refptr<ExtensionPermissionSet> permission_set = new ExtensionPermissionSet(apis, hosts, URLPatternSet()); // Pack the permission set to value and verify its contents. - scoped_ptr<DictionaryValue> value(PackPermissionsToValue(permissions)); + scoped_ptr<Permissions> permissions(PackPermissionSet(permission_set)); + scoped_ptr<DictionaryValue> value(permissions->ToValue()); ListValue* api_list = NULL; ListValue* origin_list = NULL; - ASSERT_TRUE(value->GetList("permissions", &api_list)); - ASSERT_TRUE(value->GetList("origins", &origin_list)); + EXPECT_TRUE(value->GetList("permissions", &api_list)); + EXPECT_TRUE(value->GetList("origins", &origin_list)); - ASSERT_EQ(2u, api_list->GetSize()); - ASSERT_EQ(2u, origin_list->GetSize()); + EXPECT_EQ(2u, api_list->GetSize()); + EXPECT_EQ(2u, origin_list->GetSize()); std::string expected_apis[] = { "tabs", "webRequest" }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expected_apis); ++i) { scoped_ptr<Value> value(Value::CreateStringValue(expected_apis[i])); - ASSERT_NE(api_list->end(), api_list->Find(*value)); + EXPECT_NE(api_list->end(), api_list->Find(*value)); } std::string expected_origins[] = { "http://a.com/*", "http://b.com/*" }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expected_origins); ++i) { scoped_ptr<Value> value(Value::CreateStringValue(expected_origins[i])); - ASSERT_NE(origin_list->end(), origin_list->Find(*value)); + EXPECT_NE(origin_list->end(), origin_list->Find(*value)); } // Unpack the value back to a permission set and make sure its equal to the // original one. scoped_refptr<ExtensionPermissionSet> from_value; - bool bad_message = false; std::string error; - ASSERT_TRUE(UnpackPermissionsFromValue( - value.get(), &from_value, &bad_message, &error)); - ASSERT_FALSE(bad_message); - ASSERT_TRUE(error.empty()); + Permissions permissions_object; + EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); + from_value = UnpackPermissionSet(permissions_object, &error); + EXPECT_TRUE(error.empty()); - ASSERT_EQ(*permissions, *from_value); + EXPECT_EQ(*permission_set, *from_value); } // Tests various error conditions and edge cases when unpacking values @@ -79,79 +81,90 @@ TEST(ExtensionPermissionsAPIHelpers, Unpack) { scoped_ptr<DictionaryValue> value(new DictionaryValue()); scoped_refptr<ExtensionPermissionSet> permissions; - bool bad_message = false; std::string error; // Origins shouldn't have to be present. - value->Set("permissions", apis->DeepCopy()); - ASSERT_TRUE(UnpackPermissionsFromValue( - value.get(), &permissions, &bad_message, &error)); - ASSERT_TRUE(permissions->HasAPIPermission(ExtensionAPIPermission::kTab)); - ASSERT_FALSE(bad_message); - ASSERT_TRUE(error.empty()); + { + Permissions permissions_object; + value->Set("permissions", apis->DeepCopy()); + EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); + permissions = UnpackPermissionSet(permissions_object, &error); + EXPECT_TRUE(permissions->HasAPIPermission(ExtensionAPIPermission::kTab)); + EXPECT_TRUE(permissions); + EXPECT_TRUE(error.empty()); + } // The api permissions don't need to be present either. - value->Clear(); - value->Set("origins", origins->DeepCopy()); - ASSERT_TRUE(UnpackPermissionsFromValue( - value.get(), &permissions, &bad_message, &error)); - ASSERT_FALSE(bad_message); - ASSERT_TRUE(error.empty()); - ASSERT_TRUE(permissions->HasExplicitAccessToOrigin(GURL("http://a.com/"))); + { + Permissions permissions_object; + value->Clear(); + value->Set("origins", origins->DeepCopy()); + EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); + permissions = UnpackPermissionSet(permissions_object, &error); + EXPECT_TRUE(permissions); + EXPECT_TRUE(error.empty()); + EXPECT_TRUE(permissions->HasExplicitAccessToOrigin(GURL("http://a.com/"))); + } // Throw errors for non-string API permissions. - value->Clear(); - scoped_ptr<ListValue> invalid_apis(apis->DeepCopy()); - invalid_apis->Append(Value::CreateIntegerValue(3)); - value->Set("permissions", invalid_apis->DeepCopy()); - ASSERT_FALSE(UnpackPermissionsFromValue( - value.get(), &permissions, &bad_message, &error)); - ASSERT_TRUE(bad_message); - bad_message = false; + { + Permissions permissions_object; + value->Clear(); + scoped_ptr<ListValue> invalid_apis(apis->DeepCopy()); + invalid_apis->Append(Value::CreateIntegerValue(3)); + value->Set("permissions", invalid_apis->DeepCopy()); + EXPECT_FALSE(Permissions::Populate(*value, &permissions_object)); + } // Throw errors for non-string origins. - value->Clear(); - scoped_ptr<ListValue> invalid_origins(origins->DeepCopy()); - invalid_origins->Append(Value::CreateIntegerValue(3)); - value->Set("origins", invalid_origins->DeepCopy()); - ASSERT_FALSE(UnpackPermissionsFromValue( - value.get(), &permissions, &bad_message, &error)); - ASSERT_TRUE(bad_message); - bad_message = false; + { + Permissions permissions_object; + value->Clear(); + scoped_ptr<ListValue> invalid_origins(origins->DeepCopy()); + invalid_origins->Append(Value::CreateIntegerValue(3)); + value->Set("origins", invalid_origins->DeepCopy()); + EXPECT_FALSE(Permissions::Populate(*value, &permissions_object)); + } // Throw errors when "origins" or "permissions" are not list values. - value->Clear(); - value->Set("origins", Value::CreateIntegerValue(2)); - ASSERT_FALSE(UnpackPermissionsFromValue( - value.get(), &permissions, &bad_message, &error)); - ASSERT_TRUE(bad_message); - bad_message = false; - - value->Clear(); - value->Set("permissions", Value::CreateIntegerValue(2)); - ASSERT_FALSE(UnpackPermissionsFromValue( - value.get(), &permissions, &bad_message, &error)); - ASSERT_TRUE(bad_message); - bad_message = false; + { + Permissions permissions_object; + value->Clear(); + value->Set("origins", Value::CreateIntegerValue(2)); + EXPECT_FALSE(Permissions::Populate(*value, &permissions_object)); + } + + { + Permissions permissions_object; + value->Clear(); + value->Set("permissions", Value::CreateIntegerValue(2)); + EXPECT_FALSE(Permissions::Populate(*value, &permissions_object)); + } // Additional fields should be allowed. - value->Clear(); - value->Set("origins", origins->DeepCopy()); - value->Set("random", Value::CreateIntegerValue(3)); - ASSERT_TRUE(UnpackPermissionsFromValue( - value.get(), &permissions, &bad_message, &error)); - ASSERT_FALSE(bad_message); - ASSERT_TRUE(error.empty()); - ASSERT_TRUE(permissions->HasExplicitAccessToOrigin(GURL("http://a.com/"))); - - // Unknown permissions should throw an error but not set the bad_message bit. - value->Clear(); - invalid_apis.reset(apis->DeepCopy()); - invalid_apis->Append(Value::CreateStringValue("unknown_permission")); - value->Set("permissions", invalid_apis->DeepCopy()); - ASSERT_FALSE(UnpackPermissionsFromValue( - value.get(), &permissions, &bad_message, &error)); - ASSERT_FALSE(bad_message); - ASSERT_FALSE(error.empty()); - ASSERT_EQ(error, "'unknown_permission' is not a recognized permission."); + { + Permissions permissions_object; + value->Clear(); + value->Set("origins", origins->DeepCopy()); + value->Set("random", Value::CreateIntegerValue(3)); + EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); + permissions = UnpackPermissionSet(permissions_object, &error); + EXPECT_TRUE(permissions); + EXPECT_TRUE(error.empty()); + EXPECT_TRUE(permissions->HasExplicitAccessToOrigin(GURL("http://a.com/"))); + } + + // Unknown permissions should throw an error. + { + Permissions permissions_object; + value->Clear(); + scoped_ptr<ListValue> invalid_apis(apis->DeepCopy()); + invalid_apis->Append(Value::CreateStringValue("unknown_permission")); + value->Set("permissions", invalid_apis->DeepCopy()); + EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); + permissions = UnpackPermissionSet(permissions_object, &error); + EXPECT_FALSE(permissions); + EXPECT_FALSE(error.empty()); + EXPECT_EQ(error, "'unknown_permission' is not a recognized permission."); + } } diff --git a/chrome/browser/extensions/permissions_updater.cc b/chrome/browser/extensions/permissions_updater.cc index 9fdc2a7..b15352a 100644 --- a/chrome/browser/extensions/permissions_updater.cc +++ b/chrome/browser/extensions/permissions_updater.cc @@ -12,6 +12,7 @@ #include "chrome/browser/extensions/extension_prefs.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/profiles/profile.h" +#include "chrome/common/extensions/api/permissions.h" #include "chrome/common/chrome_notification_types.h" #include "chrome/common/extensions/extension.h" #include "chrome/common/extensions/extension_messages.h" @@ -20,7 +21,7 @@ #include "content/public/browser/render_process_host.h" using content::RenderProcessHost; -using extensions::permissions_api::PackPermissionsToValue; +using extensions::permissions_api_helpers::PackPermissionSet; namespace extensions { @@ -96,7 +97,9 @@ void PermissionsUpdater::DispatchEvent( return; ListValue value; - value.Append(PackPermissionsToValue(changed_permissions)); + scoped_ptr<api::permissions::Permissions> permissions = + PackPermissionSet(changed_permissions); + value.Append(permissions->ToValue()); std::string json_value; base::JSONWriter::Write(&value, false, &json_value); profile_->GetExtensionEventRouter()->DispatchEventToExtension( diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index bd2f142..6939ab9 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -19,6 +19,7 @@ 'chrome_resources.gyp:platform_locale_settings', 'chrome_resources.gyp:theme_resources', 'common', + 'common/extensions/api/api.gyp:api', 'common_net', 'debugger', 'in_memory_url_index_cache_proto', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 392303a..d0b099b 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1215,6 +1215,7 @@ '../ui/ui.gyp:ui_resources_standard', '../ui/ui.gyp:ui_test_support', '../v8/tools/gyp/v8.gyp:v8', + 'common/extensions/api/api.gyp:api', 'chrome_resources.gyp:chrome_resources', 'chrome_resources.gyp:chrome_strings', ], diff --git a/chrome/common/extensions/api/api.gyp b/chrome/common/extensions/api/api.gyp new file mode 100644 index 0000000..b0f1065 --- /dev/null +++ b/chrome/common/extensions/api/api.gyp @@ -0,0 +1,24 @@ +# Copyright (c) 2012 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. + +{ + 'variables': { + 'chromium_code': 1, + 'json_schema_files': [ + 'permissions.json', + ], + 'cc_dir': 'chrome/common/extensions/api', + 'root_namespace': 'extensions::api', + }, + 'targets': [ + { + 'target_name': 'api', + 'type': 'static_library', + 'sources': [ + '<@(json_schema_files)', + ], + 'includes': ['../../../../build/json_schema_compile.gypi'], + }, + ], +} diff --git a/chrome/common/extensions/api/permissions.json b/chrome/common/extensions/api/permissions.json index 377a43f..e51cc38 100644 --- a/chrome/common/extensions/api/permissions.json +++ b/chrome/common/extensions/api/permissions.json @@ -1,6 +1,7 @@ [ { "namespace": "permissions", + "compile": true, "types": [ { "id": "Permissions", diff --git a/tools/json_schema_compiler/api_gen_util.gyp b/tools/json_schema_compiler/api_gen_util.gyp new file mode 100644 index 0000000..54966cc --- /dev/null +++ b/tools/json_schema_compiler/api_gen_util.gyp @@ -0,0 +1,20 @@ +# Copyright (c) 2012 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. + +{ + 'variables': { + 'chromium_code': 1, + }, + 'targets': [{ + 'target_name': 'api_gen_util', + 'type': 'static_library', + 'sources': [ + 'util.cc', + ], + 'dependencies': ['<(DEPTH)/base/base.gyp:base'], + 'include_dirs': [ + '<(DEPTH)', + ], + }], +} diff --git a/tools/json_schema_compiler/cc_generator.py b/tools/json_schema_compiler/cc_generator.py new file mode 100644 index 0000000..d0c57de --- /dev/null +++ b/tools/json_schema_compiler/cc_generator.py @@ -0,0 +1,294 @@ +# Copyright (c) 2012 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. + +from model import PropertyType +import code +import cpp_util + +class CCGenerator(object): + """A .cc generator for a namespace. + """ + def __init__(self, namespace, cpp_type_generator): + self._cpp_type_generator = cpp_type_generator + self._namespace = namespace + self._target_namespace = ( + self._cpp_type_generator.GetCppNamespaceName(self._namespace)) + + def Generate(self): + """Generates a code.Code object with the .cc for a single namespace. + """ + c = code.Code() + (c.Append(cpp_util.CHROMIUM_LICENSE) + .Append() + .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) + .Append() + .Append('#include "tools/json_schema_compiler/util.h"') + .Append('#include "%s/%s.h"' % + (self._namespace.source_file_dir, self._target_namespace)) + .Append() + .Concat(self._cpp_type_generator.GetCppNamespaceStart()) + .Append() + .Append('//') + .Append('// Types') + .Append('//') + .Append() + ) + for type_ in self._namespace.types.values(): + (c.Concat(self._GenerateType(type_)) + .Append() + ) + (c.Append('//') + .Append('// Functions') + .Append('//') + .Append() + ) + for function in self._namespace.functions.values(): + (c.Concat(self._GenerateFunction(function)) + .Append() + ) + (c.Concat(self._cpp_type_generator.GetCppNamespaceEnd()) + .Append() + ) + # TODO(calamity): Events + return c + + def _GenerateType(self, type_): + """Generates the function definitions for a type. + """ + c = code.Code() + + (c.Append('%(classname)s::%(classname)s() {}') + .Append('%(classname)s::~%(classname)s() {}') + .Append() + ) + c.Substitute({'classname': type_.name}) + + c.Concat(self._GenerateTypePopulate(type_)) + c.Append() + # TODO(calamity): deal with non-serializable + c.Concat(self._GenerateTypeTovalue(type_)) + c.Append() + + return c + + def _GenerateTypePopulate(self, type_): + """Generates the function for populating a type given a pointer to it. + """ + c = code.Code() + (c.Append('// static') + .Sblock('bool %(name)s::Populate(const Value& value, %(name)s* out) {') + .Append('if (!value.IsType(Value::TYPE_DICTIONARY))') + .Append(' return false;') + .Append('const DictionaryValue* dict = ' + 'static_cast<const DictionaryValue*>(&value);') + .Append() + ) + c.Substitute({'name': type_.name}) + + # TODO(calamity): this handle single PropertyType.REF properties. + # add ALL the types + for prop in type_.properties.values(): + sub = {'name': prop.name} + if prop.type_ == PropertyType.ARRAY: + if prop.item_type.type_ == PropertyType.REF: + if prop.optional: + (c.Append('if (!json_schema_compiler::util::' + 'GetOptionalTypes<%(type)s>(*dict,') + .Append(' "%(name)s", &out->%(name)s))') + .Append(' return false;') + ) + else: + (c.Append('if (!json_schema_compiler::util::' + 'GetTypes<%(type)s>(*dict,') + .Append(' "%(name)s", &out->%(name)s))') + .Append(' return false;') + ) + sub['type'] = self._cpp_type_generator.GetType(prop.item_type, + pad_for_generics=True) + elif prop.item_type.type_ == PropertyType.STRING: + if prop.optional: + (c.Append('if (!json_schema_compiler::util::GetOptionalStrings' + '(*dict, "%(name)s", &out->%(name)s))') + .Append(' return false;') + ) + else: + (c.Append('if (!json_schema_compiler::util::GetStrings' + '(*dict, "%(name)s", &out->%(name)s))') + .Append(' return false;') + ) + else: + raise NotImplementedError(prop.item_type.type_) + elif prop.type_.is_fundamental: + c.Append('if (!dict->%s)' % + cpp_util.GetFundamentalValue(prop, '&out->%s' % prop.name)) + c.Append(' return false;') + else: + raise NotImplementedError(prop.type_) + c.Substitute(sub) + (c.Append('return true;') + .Eblock('}') + ) + return c + + def _GenerateTypeTovalue(self, type_): + """Generates a function that serializes the type into a |DictionaryValue|. + """ + c = code.Code() + (c.Sblock('DictionaryValue* %s::ToValue() const {' % type_.name) + .Append('DictionaryValue* value = new DictionaryValue();') + .Append() + ) + name = type_.name.lower() + for prop in type_.properties.values(): + prop_name = name + '_' + prop.name if name else prop.name + this_var = prop.name + c.Concat(self._CreateValueFromProperty(prop_name, prop, this_var)) + (c.Append() + .Append('return value;') + .Eblock('}') + ) + return c + + # TODO(calamity): object and choices prop types + def _CreateValueFromProperty(self, name, prop, var): + """Generates code to serialize a single property in a type. + """ + c = code.Code() + if prop.type_.is_fundamental: + c.Append('Value* %s_value = %s;' % + (name, cpp_util.CreateFundamentalValue(prop, var))) + elif prop.type_ == PropertyType.ARRAY: + if prop.item_type.type_ == PropertyType.STRING: + if prop.optional: + c.Append('json_schema_compiler::util::' + 'SetOptionalStrings(%s, "%s", value);' % (var, prop.name)) + else: + c.Append('json_schema_compiler::util::' + 'SetStrings(%s, "%s", value);' % (var, prop.name)) + else: + item_name = name + '_single' + (c.Append('ListValue* %(name)s_value = new ListValue();') + .Append('for (%(it_type)s::iterator it = %(var)s->begin();') + .Sblock(' it != %(var)s->end(); ++it) {') + .Concat(self._CreateValueFromProperty(item_name, prop.item_type, + '*it')) + .Append('%(name)s_value->Append(%(prop_val)s_value);') + .Eblock('}') + ) + c.Substitute( + {'it_type': self._cpp_type_generator.GetType(prop), + 'name': name, 'var': var, 'prop_val': item_name}) + elif prop.type_ == PropertyType.REF: + c.Append('Value* %s_value = %s.ToValue();' % (name, var)) + else: + raise NotImplementedError + return c + + def _GenerateFunction(self, function): + """Generates the definitions for function structs. + """ + classname = cpp_util.CppName(function.name) + c = code.Code() + + # Params::Populate function + if function.params: + (c.Append('%(name)s::Params::Params() {}') + .Append('%(name)s::Params::~Params() {}') + .Append() + .Concat(self._GenerateFunctionParamsCreate(function)) + .Append() + ) + + # Result::Create function + c.Concat(self._GenerateFunctionResultCreate(function)) + + c.Substitute({'name': classname}) + + return c + + def _GenerateFunctionParamsCreate(self, function): + """Generate function to create an instance of Params given a pointer. + """ + classname = cpp_util.CppName(function.name) + c = code.Code() + (c.Append('// static') + .Sblock('scoped_ptr<%(classname)s::Params> %(classname)s::Params::Create' + '(const ListValue& args) {') + .Append('if (args.GetSize() != %d)' % len(function.params)) + .Append(' return scoped_ptr<Params>();') + .Append() + .Append('scoped_ptr<Params> params(new Params());') + ) + c.Substitute({'classname': classname}) + + # TODO(calamity): generalize, needs to move to function to do populates for + # wider variety of args + for i, param in enumerate(function.params): + sub = {'name': param.name, 'pos': i} + c.Append() + # TODO(calamity): Make valid for not just objects + c.Append('DictionaryValue* %(name)s_param = NULL;') + c.Append('if (!args.GetDictionary(%(pos)d, &%(name)s_param))') + c.Append(' return scoped_ptr<Params>();') + if param.type_ == PropertyType.REF: + c.Append('if (!%(ctype)s::Populate(*%(name)s_param, ' + '¶ms->%(name)s))') + c.Append(' return scoped_ptr<Params>();') + sub['ctype'] = self._cpp_type_generator.GetType(param) + elif param.type_.is_fundamental: + raise NotImplementedError('Fundamental types are unimplemented') + elif param.type_ == PropertyType.OBJECT: + c.Append('if (!%(ctype)s::Populate(*%(name)s_param, ' + '¶ms->%(name)s))') + c.Append(' return scoped_ptr<Params>();') + sub['ctype'] = self._cpp_type_generator.GetType(param) + elif param.type_ == PropertyType.CHOICES: + raise NotImplementedError('Choices is unimplemented') + else: + raise NotImplementedError(param.type_) + c.Substitute(sub) + c.Append() + c.Append('return params.Pass();') + c.Eblock('}') + + return c + + def _GenerateFunctionResultCreate(self, function): + """Generate function to create a Result given the return value. + """ + classname = cpp_util.CppName(function.name) + c = code.Code() + c.Append('// static') + param = function.callback.param + arg = '' + if param: + if param.type_ == PropertyType.REF: + arg = 'const %(type)s& %(name)s' + else: + arg = 'const %(type)s %(name)s' + arg = arg % { + 'type': self._cpp_type_generator.GetType(param), + 'name': param.name + } + c.Sblock('Value* %(classname)s::Result::Create(%(arg)s) {') + sub = {'classname': classname, 'arg': arg} + # TODO(calamity): Choices + if not param: + c.Append('return Value::CreateNullValue();') + else: + sub['argname'] = param.name + if param.type_.is_fundamental: + c.Append('return %s;' % + cpp_util.CreateFundamentalValue(param, param.name)) + elif param.type_ == PropertyType.REF: + c.Append('return %(argname)s.ToValue();') + elif param.type_ == PropertyType.OBJECT: + raise NotImplementedError('Objects not implemented') + elif param.type_ == PropertyType.ARRAY: + raise NotImplementedError('Arrays not implemented') + else: + raise NotImplementedError(param.type_) + c.Substitute(sub) + c.Eblock('}') + return c diff --git a/tools/json_schema_compiler/code.py b/tools/json_schema_compiler/code.py new file mode 100644 index 0000000..4f2a64b --- /dev/null +++ b/tools/json_schema_compiler/code.py @@ -0,0 +1,112 @@ +# Copyright (c) 2012 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. + +class Code(object): + """A convenience object for constructing code. + + Logically each object should be a block of code. All methods except |Render| + and |IsEmpty| return self. + """ + def __init__(self, indent_size=2, comment_length=80): + self._code = [] + self._indent_level = 0 + self._indent_size = indent_size + self._comment_length = comment_length + + def Append(self, line=''): + """Appends a line of code at the current indent level or just a newline if + line is not specified. Trailing whitespace is stripped. + """ + self._code.append(((' ' * self._indent_level) + line).rstrip()) + return self + + def IsEmpty(self): + """Returns True if the Code object is empty. + """ + return not bool(self._code) + + def Concat(self, obj): + """Concatenate another Code object onto this one. Trailing whitespace is + stripped. + + Appends the code at the current indent level. Will fail if there are any + un-interpolated format specifiers eg %s, %(something)s which helps + isolate any strings that haven't been substituted. + """ + if not isinstance(obj, Code): + raise TypeError() + assert self is not obj + for line in obj._code: + # line % () will fail if any substitution tokens are left in line + self._code.append(((' ' * self._indent_level) + line % ()).rstrip()) + + return self + + def Sblock(self, line=''): + """Starts a code block. + + Appends a line of code and then increases the indent level. + """ + self.Append(line) + self._indent_level += self._indent_size + return self + + def Eblock(self, line=''): + """Ends a code block by decreasing and then appending a line (or a blank + line if not given). + """ + # TODO(calamity): Decide if type checking is necessary + #if not isinstance(line, basestring): + # raise TypeError + self._indent_level -= self._indent_size + self.Append(line) + return self + + # TODO(calamity): Make comment its own class or something and Render at + # self.Render() time + def Comment(self, comment): + """Adds the given string as a comment. + + Will split the comment if it's too long. Use mainly for variable length + comments. Otherwise just use code.Append('// ...') for comments. + """ + comment_symbol = '// ' + max_len = self._comment_length - self._indent_level - len(comment_symbol) + while len(comment) >= max_len: + line = comment[0:max_len] + last_space = line.rfind(' ') + if last_space != -1: + line = line[0:last_space] + comment = comment[last_space + 1:] + else: + comment = comment[max_len:] + self.Append(comment_symbol + line) + self.Append(comment_symbol + comment) + return self + + def Substitute(self, d): + """Goes through each line and interpolates using the given dict. + + Raises type error if passed something that isn't a dict + + Use for long pieces of code using interpolation with the same variables + repeatedly. This will reduce code and allow for named placeholders which + are more clear. + """ + if not isinstance(d, dict): + raise TypeError('Passed argument is not a dictionary: ' + d) + for i, line in enumerate(self._code): + # Only need to check %s because arg is a dict and python will allow + # '%s %(named)s' but just about nothing else + if '%s' in self._code[i] or '%r' in self._code[i]: + raise TypeError('"%s" or "%r" found in substitution. ' + 'Named arguments only. Use "%" to escape') + self._code[i] = line % d + return self + + def Render(self): + """Renders Code as a string. + """ + return '\n'.join(self._code) + diff --git a/tools/json_schema_compiler/code_test.py b/tools/json_schema_compiler/code_test.py new file mode 100644 index 0000000..8431d95 --- /dev/null +++ b/tools/json_schema_compiler/code_test.py @@ -0,0 +1,152 @@ +# Copyright (c) 2012 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. + +from code import Code +import unittest + +class CodeTest(unittest.TestCase): + def testAppend(self): + c = Code() + c.Append('line') + self.assertEquals('line', c.Render()) + + def testBlock(self): + c = Code() + (c.Append('line') + .Sblock('sblock') + .Append('inner') + .Append('moreinner') + .Sblock('moresblock') + .Append('inner') + .Eblock('out') + .Append('inner') + .Eblock('out') + ) + self.assertEquals( + 'line\n' + 'sblock\n' + ' inner\n' + ' moreinner\n' + ' moresblock\n' + ' inner\n' + ' out\n' + ' inner\n' + 'out', + c.Render()) + + def testConcat(self): + b = Code() + (b.Sblock('2') + .Append('2') + .Eblock('2') + ) + c = Code() + (c.Sblock('1') + .Concat(b) + .Append('1') + .Eblock('1') + ) + self.assertEquals( + '1\n' + ' 2\n' + ' 2\n' + ' 2\n' + ' 1\n' + '1', + c.Render()) + d = Code() + a = Code() + a.Concat(d) + self.assertEquals('', a.Render()) + a.Concat(c) + self.assertEquals( + '1\n' + ' 2\n' + ' 2\n' + ' 2\n' + ' 1\n' + '1', + a.Render()) + + def testConcatErrors(self): + c = Code() + d = Code() + d.Append('%s') + self.assertRaises(TypeError, c.Concat, d) + d = Code() + d.Append('%(classname)s') + self.assertRaises(TypeError, c.Concat, d) + d = 'line of code' + self.assertRaises(TypeError, c.Concat, d) + + def testSubstitute(self): + c = Code() + c.Append('%(var1)s %(var2)s %(var1)s') + c.Substitute({'var1': 'one', 'var2': 'two'}) + self.assertEquals('one two one', c.Render()) + c.Append('%(var1)s %(var2)s %(var3)s') + c.Append('%(var2)s %(var1)s %(var3)s') + c.Substitute({'var1': 'one', 'var2': 'two', 'var3': 'three'}) + self.assertEquals( + 'one two one\n' + 'one two three\n' + 'two one three', + c.Render()) + + def testSubstituteErrors(self): + # No unnamed placeholders allowed when substitute is run + c = Code() + c.Append('%s %s') + self.assertRaises(TypeError, c.Substitute, ('var1', 'one')) + c = Code() + c.Append('%s %(var1)s') + self.assertRaises(TypeError, c.Substitute, {'var1': 'one'}) + c = Code() + c.Append('%s %(var1)s') + self.assertRaises(TypeError, c.Substitute, {'var1': 'one'}) + c = Code() + c.Append('%(var1)s') + self.assertRaises(KeyError, c.Substitute, {'clearlynotvar1': 'one'}) + + def testIsEmpty(self): + c = Code() + self.assertTrue(c.IsEmpty()) + c.Append('asdf') + self.assertFalse(c.IsEmpty()) + + def testComment(self): + long_comment = ('This comment is eighty nine characters in longness, ' + 'that is, to use another word, length') + c = Code() + c.Comment(long_comment) + self.assertEquals( + '// This comment is eighty nine characters ' + 'in longness, that is, to use another\n' + '// word, length', + c.Render()) + c = Code() + c.Sblock('sblock') + c.Comment(long_comment) + c.Eblock('eblock') + c.Comment(long_comment) + self.assertEquals( + 'sblock\n' + ' // This comment is eighty nine characters ' + 'in longness, that is, to use\n' + ' // another word, length\n' + 'eblock\n' + '// This comment is eighty nine characters in ' + 'longness, that is, to use another\n' + '// word, length', + c.Render()) + long_word = 'x' * 100 + c = Code() + c.Comment(long_word) + self.assertEquals( + '// ' + 'x' * 77 + '\n' + '// ' + 'x' * 23, + c.Render()) + +if __name__ == '__main__': + unittest.main() diff --git a/tools/json_schema_compiler/compiler.py b/tools/json_schema_compiler/compiler.py new file mode 100644 index 0000000..afd499d --- /dev/null +++ b/tools/json_schema_compiler/compiler.py @@ -0,0 +1,101 @@ +#!/usr/bin/env python +# Copyright (c) 2012 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. +"""Generator for C++ structs from api json files. + +The purpose of this tool is to remove the need for hand-written code that +converts to and from base::Value types when receiving javascript api calls. +Originally written for generating code for extension apis. Reference schemas +are in chrome/common/extensions/api. + +Usage example: + compiler.py --root /home/Work/src --namespace extensions windows.json + tabs.json + compiler.py --destdir gen --suffix _api --root /home/Work/src + --namespace extensions windows.json tabs.json +""" + +import cpp_util +import cc_generator +import cpp_type_generator +import h_generator +import json +import model +import optparse +import os.path +import sys + +if __name__ == '__main__': + parser = optparse.OptionParser( + description='Generates a C++ model of an API from JSON schema', + usage='usage: %prog [option]... schema [referenced_schema]...') + parser.add_option('-r', '--root', default='.', + help='logical include root directory. Path to schema files from specified' + 'dir will be the include path.') + parser.add_option('-d', '--destdir', + help='root directory to output generated files.') + parser.add_option('-n', '--namespace', default='generated_api_schemas', + help='C++ namespace for generated files. e.g extensions::api.') + parser.add_option('-s', '--suffix', default='', + help='Filename and C++ namespace suffix for generated files.') + + (opts, args) = parser.parse_args() + if not args: + sys.exit(parser.get_usage()) + dest_dir = opts.destdir + root_namespace = opts.namespace + filename_suffix = opts.suffix + + schema = os.path.normpath(args[0]) + referenced_schemas = args[1:] + + api_model = model.Model() + + # Load type dependencies into the model. + for referenced_schema_path in referenced_schemas: + with open(referenced_schema_path, 'r') as referenced_schema_file: + referenced_api_defs = json.loads(referenced_schema_file.read()) + + for namespace in referenced_api_defs: + api_model.AddNamespace(namespace, + os.path.relpath(referenced_schema_path, opts.root)) + + # Actually generate for source file. + with open(schema, 'r') as schema_file: + api_defs = json.loads(schema_file.read()) + + for target_namespace in api_defs: + # Gets the relative path from opts.root to the schema to correctly determine + # the include path. + relpath = os.path.relpath(schema, opts.root) + namespace = api_model.AddNamespace(target_namespace, relpath) + if not namespace: + continue + + out_file = cpp_util.CppName(namespace.name).lower() + filename_suffix + type_generator = cpp_type_generator.CppTypeGenerator(root_namespace, + namespace, out_file) + for referenced_namespace in api_model.namespaces.values(): + type_generator.AddNamespace(referenced_namespace, + cpp_util.CppName(referenced_namespace.name).lower() + filename_suffix) + cc_generator = cc_generator.CCGenerator(namespace, type_generator) + cc_code = cc_generator.Generate().Render() + h_generator = h_generator.HGenerator(namespace, type_generator) + h_code = h_generator.Generate().Render() + + if dest_dir: + with open(os.path.join(dest_dir, namespace.source_file_dir, + out_file + '.cc'), 'w') as cc_file: + cc_file.write(cc_code) + with open(os.path.join(dest_dir, namespace.source_file_dir, + out_file + '.h'), 'w') as h_file: + h_file.write(h_code) + else: + print '%s.h' % out_file + print + print h_code + print + print '%s.cc' % out_file + print + print cc_code diff --git a/tools/json_schema_compiler/cpp_type_generator.py b/tools/json_schema_compiler/cpp_type_generator.py new file mode 100644 index 0000000..a60b6e5 --- /dev/null +++ b/tools/json_schema_compiler/cpp_type_generator.py @@ -0,0 +1,135 @@ +# Copyright (c) 2012 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. + +from code import Code +from model import PropertyType +import cpp_util + +class CppTypeGenerator(object): + """Manages the types of properties and provides utilities for getting the + C++ type out of a model.Property + """ + def __init__(self, root_namespace, namespace, cpp_namespace): + """Creates a cpp_type_generator. The given root_namespace should be of the + format extensions::api::sub. The generator will generate code suitable for + use in the given namespace. + """ + self._type_namespaces = {} + self._namespace = namespace + self._root_namespace = root_namespace.split('::') + self._cpp_namespaces = {} + self.AddNamespace(namespace, cpp_namespace) + + def AddNamespace(self, namespace, cpp_namespace): + """Maps a model.Namespace to its C++ namespace name. All mappings are + beneath the root namespace. + """ + for type_ in namespace.types: + self._type_namespaces[type_] = namespace + self._cpp_namespaces[namespace] = cpp_namespace + + def GetCppNamespaceName(self, namespace): + """Gets the mapped C++ namespace name for the given namespace relative to + the root namespace. + """ + return self._cpp_namespaces[namespace] + + def GetCppNamespaceStart(self): + """Get opening namespace declarations. + """ + c = Code() + for n in self._root_namespace: + c.Append('namespace %s {' % n) + c.Append('namespace %s {' % self.GetCppNamespaceName(self._namespace)) + return c + + def GetCppNamespaceEnd(self): + """Get closing namespace declarations. + """ + c = Code() + c.Append('} // %s' % self.GetCppNamespaceName(self._namespace)) + for n in reversed(self._root_namespace): + c.Append('} // %s' % n) + return c + + # TODO(calamity): Handle ANY + def GetType(self, prop, pad_for_generics=False): + """Translates a model.Property into its C++ type. + + If REF types from different namespaces are referenced, will resolve + using self._type_namespaces. + + Use pad_for_generics when using as a generic to avoid operator ambiguity. + """ + cpp_type = None + if prop.type_ == PropertyType.REF: + dependency_namespace = self._type_namespaces.get(prop.ref_type) + if not dependency_namespace: + raise KeyError('Cannot find referenced type: %s' % prop.ref_type) + if self._namespace != dependency_namespace: + cpp_type = '%s::%s' % (self._cpp_namespaces[dependency_namespace], + prop.ref_type) + else: + cpp_type = '%s' % prop.ref_type + elif prop.type_ == PropertyType.BOOLEAN: + cpp_type = 'bool' + elif prop.type_ == PropertyType.INTEGER: + cpp_type = 'int' + elif prop.type_ == PropertyType.DOUBLE: + cpp_type = 'double' + elif prop.type_ == PropertyType.STRING: + cpp_type = 'std::string' + elif prop.type_ == PropertyType.ARRAY: + cpp_type = 'std::vector<%s>' % self.GetType( + prop.item_type, pad_for_generics=True) + elif prop.type_ == PropertyType.OBJECT: + cpp_type = cpp_util.CppName(prop.name) + # TODO(calamity): choices + else: + raise NotImplementedError + + # Add a space to prevent operator ambiguity + if pad_for_generics and cpp_type[-1] == '>': + return '%s ' % cpp_type + return '%s' % cpp_type + + def GenerateCppIncludes(self): + """Returns the #include lines for self._namespace using the other + namespaces in self._model. + """ + dependencies = set() + for function in self._namespace.functions.values(): + for param in function.params: + dependencies |= self._TypeDependencies(param) + dependencies |= self._TypeDependencies(function.callback.param) + for type_ in self._namespace.types.values(): + for prop in type_.properties.values(): + dependencies |= self._TypeDependencies(prop) + + dependency_namespaces = set() + for dependency in dependencies: + dependency_namespace = self._type_namespaces[dependency] + if dependency_namespace != self._namespace: + dependency_namespaces.add(dependency_namespace) + + includes = Code() + for dependency_namespace in sorted(dependency_namespaces): + includes.Append('#include "%s/%s.h"' % ( + dependency_namespace.source_file_dir, + self._cpp_namespaces[dependency_namespace])) + return includes + + def _TypeDependencies(self, prop): + """Recursively gets all the type dependencies of a property. + """ + deps = set() + if prop: + if prop.type_ == PropertyType.REF: + deps.add(prop.ref_type) + elif prop.type_ == PropertyType.ARRAY: + deps = self._TypeDependencies(prop.item_type) + elif prop.type_ == PropertyType.OBJECT: + for p in prop.properties.values(): + deps |= self._TypeDependencies(p) + return deps diff --git a/tools/json_schema_compiler/cpp_type_generator_test.py b/tools/json_schema_compiler/cpp_type_generator_test.py new file mode 100644 index 0000000..155b788 --- /dev/null +++ b/tools/json_schema_compiler/cpp_type_generator_test.py @@ -0,0 +1,125 @@ +# Copyright (c) 2012 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. + +from cpp_type_generator import CppTypeGenerator +import json +import model +import unittest + +class CppTypeGeneratorTest(unittest.TestCase): + def setUp(self): + self.model = model.Model() + self.permissions_json = json.loads(open('test/permissions.json').read()) + self.model.AddNamespace(self.permissions_json[0], + 'path/to/permissions.json') + self.permissions = self.model.namespaces.get('permissions') + self.windows_json = json.loads(open('test/windows.json').read()) + self.model.AddNamespace(self.windows_json[0], + 'path/to/window.json') + self.windows = self.model.namespaces.get('windows') + self.tabs_json = json.loads(open('test/tabs.json').read()) + self.model.AddNamespace(self.tabs_json[0], + 'path/to/tabs.json') + self.tabs = self.model.namespaces.get('tabs') + + def testGenerateCppIncludes(self): + manager = CppTypeGenerator('', self.windows, 'windows_api') + manager.AddNamespace(self.tabs, 'tabs_api') + self.assertEquals('#include "path/to/tabs_api.h"', + manager.GenerateCppIncludes().Render()) + manager = CppTypeGenerator('', self.permissions, 'permissions_api') + manager.AddNamespace(self.permissions, 'permissions_api') + self.assertEquals('', manager.GenerateCppIncludes().Render()) + + def testGenerateCppIncludesMultipleTypes(self): + m = model.Model() + self.tabs_json[0]['types'].append(self.permissions_json[0]['types'][0]) + tabs_namespace = m.AddNamespace(self.tabs_json[0], + 'path/to/tabs.json') + self.windows_json[0]['functions'].append( + self.permissions_json[0]['functions'][1]) + windows = m.AddNamespace(self.windows_json[0], + 'path/to/windows.json') + manager = CppTypeGenerator('', windows, 'windows_api') + manager.AddNamespace(tabs_namespace, 'tabs_api') + self.assertEquals('#include "path/to/tabs_api.h"', + manager.GenerateCppIncludes().Render()) + + def testGetTypeSimple(self): + manager = CppTypeGenerator('', self.tabs, 'tabs_api') + self.assertEquals('int', + manager.GetType( + self.tabs.types['Tab'].properties['id'])) + self.assertEquals('std::string', + manager.GetType( + self.tabs.types['Tab'].properties['status'])) + self.assertEquals('bool', + manager.GetType( + self.tabs.types['Tab'].properties['selected'])) + + def testGetTypeArray(self): + manager = CppTypeGenerator('', self.windows, 'windows_api') + self.assertEquals('std::vector<Window>', + manager.GetType( + self.windows.functions['getAll'].callback.param)) + manager = CppTypeGenerator('', self.permissions, 'permissions_api') + self.assertEquals('std::vector<std::string>', + manager.GetType( + self.permissions.types['Permissions'].properties['origins'])) + + def testGetTypeLocalRef(self): + manager = CppTypeGenerator('', self.tabs, 'tabs_api') + self.assertEquals('Tab', + manager.GetType( + self.tabs.functions['get'].callback.param)) + + def testGetTypeIncludedRef(self): + manager = CppTypeGenerator('', self.windows, 'windows_api') + manager.AddNamespace(self.tabs, 'tabs_api') + self.assertEquals('std::vector<tabs_api::Tab>', + manager.GetType( + self.windows.types['Window'].properties['tabs'])) + + def testGetTypeNotfound(self): + prop = self.windows.types['Window'].properties['tabs'].item_type + prop.ref_type = 'Something' + manager = CppTypeGenerator('', self.windows, 'windows_api') + self.assertRaises(KeyError, manager.GetType, prop) + + def testGetTypeNotimplemented(self): + prop = self.windows.types['Window'].properties['tabs'].item_type + prop.type_ = 10 + manager = CppTypeGenerator('', self.windows, 'windows_api') + self.assertRaises(NotImplementedError, manager.GetType, prop) + + def testGetTypeWithPadForGeneric(self): + manager = CppTypeGenerator('', self.permissions, 'permissions_api') + self.assertEquals('std::vector<std::string> ', + manager.GetType( + self.permissions.types['Permissions'].properties['origins'], + pad_for_generics=True)) + self.assertEquals('bool', + manager.GetType( + self.permissions.functions['contains'].callback.param, + pad_for_generics=True)) + + def testNamespaceDeclaration(self): + manager = CppTypeGenerator('extensions', self.permissions, + 'permissions_api') + self.assertEquals( + 'namespace extensions {\n' + 'namespace permissions_api {', + manager.GetCppNamespaceStart().Render()) + + manager = CppTypeGenerator('extensions::gen::api', self.permissions, + 'permissions_api') + self.assertEquals( + 'namespace extensions {\n' + 'namespace gen {\n' + 'namespace api {\n' + 'namespace permissions_api {', + manager.GetCppNamespaceStart().Render()) + +if __name__ == '__main__': + unittest.main() diff --git a/tools/json_schema_compiler/cpp_util.py b/tools/json_schema_compiler/cpp_util.py new file mode 100644 index 0000000..760f01d --- /dev/null +++ b/tools/json_schema_compiler/cpp_util.py @@ -0,0 +1,51 @@ +# Copyright (c) 2012 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. +"""Utilies and constants specific to Chromium C++ code. +""" + +from datetime import datetime +from model import PropertyType + +CHROMIUM_LICENSE = ( +"""// Copyright (c) %d 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.""" % datetime.now().year +) +GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN +// %s +// DO NOT EDIT. +""" + + +def CppName(s): + """Translates a namespace name or function name into something more + suited to C++. + + eg experimental.downloads -> Experimental_Downloads + updateAll -> UpdateAll. + """ + return '_'.join([x[0].upper() + x[1:] for x in s.split('.')]) + +def CreateFundamentalValue(prop, var): + """Returns the C++ code for creating a value of the given property type + using the given variable. + """ + return { + PropertyType.STRING: 'Value::CreateStringValue(%s)', + PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', + PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)', + PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', + }[prop.type_] % var + + +def GetFundamentalValue(prop, var): + """Returns the C++ code for retrieving a fundamental type from a Value + into a variable. + """ + return { + PropertyType.STRING: 'GetAsString(%s)', + PropertyType.BOOLEAN: 'GetAsBoolean(%s)', + PropertyType.INTEGER: 'GetAsInteger(%s)', + PropertyType.DOUBLE: 'GetAsDouble(%s)', + }[prop.type_] % var diff --git a/tools/json_schema_compiler/cpp_util_test.py b/tools/json_schema_compiler/cpp_util_test.py new file mode 100644 index 0000000..56f83d3 --- /dev/null +++ b/tools/json_schema_compiler/cpp_util_test.py @@ -0,0 +1,16 @@ +# Copyright (c) 2012 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. + +import cpp_util +import unittest + +class CppUtilTest(unittest.TestCase): + def testCppName(self): + self.assertEquals('Permissions', cpp_util.CppName('permissions')) + self.assertEquals('UpdateAllTheThings', + cpp_util.CppName('updateAllTheThings')) + self.assertEquals('Aa_Bb_Cc', cpp_util.CppName('aa.bb.cc')) + +if __name__ == '__main__': + unittest.main() diff --git a/tools/json_schema_compiler/h_generator.py b/tools/json_schema_compiler/h_generator.py new file mode 100644 index 0000000..619763b --- /dev/null +++ b/tools/json_schema_compiler/h_generator.py @@ -0,0 +1,214 @@ +# Copyright (c) 2012 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. + +from model import PropertyType +import code +import cpp_util +import os + +class HGenerator(object): + """A .h generator for a namespace. + """ + def __init__(self, namespace, cpp_type_generator): + self._cpp_type_generator = cpp_type_generator + self._namespace = namespace + self._target_namespace = ( + self._cpp_type_generator.GetCppNamespaceName(self._namespace)) + + def Generate(self): + """Generates a code.Code object with the .h for a single namespace. + """ + c = code.Code() + (c.Append(cpp_util.CHROMIUM_LICENSE) + .Append() + .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) + .Append() + ) + + ifndef_name = self._GenerateIfndefName() + (c.Append('#ifndef %s' % ifndef_name) + .Append('#define %s' % ifndef_name) + .Append('#pragma once') + .Append() + .Append('#include <string>') + .Append('#include <vector>') + .Append() + .Append('#include "base/basictypes.h"') + .Append('#include "base/memory/scoped_ptr.h"') + .Append('#include "base/values.h"') + .Append() + .Append('using base::Value;') + .Append('using base::DictionaryValue;') + .Append('using base::ListValue;') + .Append() + ) + + includes = self._cpp_type_generator.GenerateCppIncludes() + if not includes.IsEmpty(): + (c.Concat(includes) + .Append() + ) + + (c.Concat(self._cpp_type_generator.GetCppNamespaceStart()) + .Append() + .Append('//') + .Append('// Types') + .Append('//') + .Append() + ) + for type_ in self._namespace.types.values(): + (c.Concat(self._GenerateType(type_)) + .Append() + ) + (c.Append('//') + .Append('// Functions') + .Append('//') + .Append() + ) + for function in self._namespace.functions.values(): + (c.Concat(self._GenerateFunction(function)) + .Append() + ) + (c.Append() + .Append() + .Concat(self._cpp_type_generator.GetCppNamespaceEnd()) + .Append() + .Append('#endif // %s' % ifndef_name) + .Append() + ) + return c + + def _GenerateType(self, type_, serializable=True): + """Generates a struct for a type. + """ + classname = cpp_util.CppName(type_.name) + c = code.Code() + if type_.description: + c.Comment(type_.description) + (c.Sblock('struct %(classname)s {') + .Append('~%(classname)s();') + .Append('%(classname)s();') + .Append() + ) + + for prop in type_.properties.values(): + if prop.description: + c.Comment(prop.description) + if prop.optional: + c.Append('scoped_ptr<%s> %s;' % + (self._cpp_type_generator.GetType(prop, pad_for_generics=True), + prop.name)) + else: + c.Append('%s %s;' % + (self._cpp_type_generator.GetType(prop), prop.name)) + c.Append() + + (c.Comment('Populates a %(classname)s object from a Value. Returns' + ' whether |out| was successfully populated.') + .Append('static bool Populate(const Value& value, %(classname)s* out);') + .Append() + ) + + if serializable: + (c.Comment('Returns a new DictionaryValue representing the' + ' serialized form of this %(classname)s object. Passes' + 'ownership to caller.') + .Append('DictionaryValue* ToValue() const;') + ) + (c.Eblock() + .Sblock(' private:') + .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') + .Eblock('};') + ) + c.Substitute({'classname': classname}) + return c + + def _GenerateFunction(self, function): + """Generates the structs for a function. + """ + c = code.Code() + (c.Sblock('namespace %s {' % cpp_util.CppName(function.name)) + .Concat(self._GenerateFunctionParams(function)) + .Append() + .Concat(self._GenerateFunctionResult(function)) + .Append() + .Eblock('};') + ) + return c + + def _GenerateFunctionParams(self, function): + """Generates the struct for passing parameters into a function. + """ + c = code.Code() + + if function.params: + c.Sblock('struct Params {') + for param in function.params: + if param.description: + c.Comment(param.description) + if param.type_ == PropertyType.OBJECT: + c.Concat(self._GenerateType(param, serializable=False)) + c.Append() + for param in function.params: + c.Append('%s %s;' % + (self._cpp_type_generator.GetType(param), param.name)) + + (c.Append() + .Append('~Params();') + .Append() + .Append('static scoped_ptr<Params> Create(const ListValue& args);') + .Eblock() + .Sblock(' private:') + .Append('Params();') + .Append() + .Append('DISALLOW_COPY_AND_ASSIGN(Params);') + ) + + c.Eblock('};') + + return c + + def _GenerateFunctionResult(self, function): + """Generates the struct for passing a function's result back. + """ + # TODO(calamity): Handle returning an object + c = code.Code() + + param = function.callback.param + # TODO(calamity): Put this description comment in less stupid place + if param.description: + c.Comment(param.description) + (c.Append('class Result {') + .Sblock(' public:') + ) + arg = '' + # TODO(calamity): handle object + if param: + if param.type_ == PropertyType.REF: + arg = 'const %(type)s& %(name)s' + else: + arg = 'const %(type)s %(name)s' + arg = arg % { + 'type': self._cpp_type_generator.GetType(param), + 'name': param.name + } + (c.Append('static Value* Create(%s);' % arg) + .Eblock() + .Sblock(' private:') + .Append('Result() {};') + .Append('DISALLOW_COPY_AND_ASSIGN(Result);') + .Eblock('};') + ) + + return c + + def _GenerateIfndefName(self): + """Formats a path and filename as a #define name. + + e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. + """ + return (('%s_%s_H__' % + (self._namespace.source_file_dir, self._target_namespace)) + .upper().replace(os.sep, '_')) + diff --git a/tools/json_schema_compiler/model.py b/tools/json_schema_compiler/model.py new file mode 100644 index 0000000..2c356ff --- /dev/null +++ b/tools/json_schema_compiler/model.py @@ -0,0 +1,134 @@ +# Copyright (c) 2012 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. + +import os.path + +class Model(object): + """Model of all namespaces that comprise an API. + """ + def __init__(self): + self.namespaces = {} + + def AddNamespace(self, json, source_file): + """Add a namespace's json to the model if it has a "compile" property set + to true. Returns the new namespace or None if a namespace wasn't added. + """ + if not json.get('compile'): + return None + namespace = Namespace(json, source_file) + self.namespaces[namespace.name] = namespace + return namespace + +class Namespace(object): + """An API namespace. + """ + def __init__(self, json, source_file): + self.name = json['namespace'] + self.source_file = source_file + self.source_file_dir, self.source_file_filename = os.path.split(source_file) + self.type_dependencies = {} + self.types = {} + self.functions = {} + for type_json in json['types']: + type_ = Type(type_json) + self.types[type_.name] = type_ + for function_json in json['functions']: + if not function_json.get('nocompile'): + function = Function(function_json) + self.functions[function.name] = function + +class Type(object): + """A Type defined in the json. + """ + def __init__(self, json): + self.name = json['id'] + self.description = json.get('description') + self.properties = {} + for prop_name, prop_json in json['properties'].items(): + self.properties[prop_name] = Property(prop_name, prop_json) + +class Callback(object): + """A callback parameter to a Function. + """ + def __init__(self, json): + params = json['parameters'] + if len(params) == 0: + self.param = None + elif len(params) == 1: + param = params[0] + self.param = Property(param['name'], param) + else: + raise AssertionError("Callbacks can have at most a single parameter") + +class Function(object): + """A Function defined in the API. + """ + def __init__(self, json): + self.name = json['name'] + self.params = [] + self.description = json['description'] + self.callback = None + self.type_dependencies = {} + for param in json['parameters']: + if param.get('type') == 'function': + assert (not self.callback), "Function has more than one callback" + self.callback = Callback(param) + else: + self.params.append(Property(param['name'], param)) + assert (self.callback), "Function does not support callback" + +# TODO(calamity): handle Enum/choices +class Property(object): + """A property of a type OR a parameter to a function. + + Members will change based on PropertyType. Check self.type_ to determine which + members actually exist. + """ + def __init__(self, name, json): + self.name = name + self.optional = json.get('optional', False) + self.description = json.get('description') + # TODO(calamity) maybe check for circular refs? could that be a problem? + if '$ref' in json: + self.ref_type = json['$ref'] + self.type_ = PropertyType.REF + elif 'type' in json: + json_type = json['type'] + if json_type == 'string': + self.type_ = PropertyType.STRING + elif json_type == 'boolean': + self.type_ = PropertyType.BOOLEAN + elif json_type == 'integer': + self.type_ = PropertyType.INTEGER + elif json_type == 'double': + self.type_ = PropertyType.DOUBLE + elif json_type == 'array': + self.item_type = Property(name + "_inner", json['items']) + self.type_ = PropertyType.ARRAY + elif json_type == 'object': + self.properties = {} + self.type_ = PropertyType.OBJECT + for key, val in json['properties'].items(): + self.properties[key] = Property(key, val) + else: + raise NotImplementedError(json_type) + elif 'choices' in json: + self.type_ = PropertyType.CHOICES + self.choices = {} + +class PropertyType(object): + """Enum of different types of properties/parameters. + """ + class _Info(object): + def __init__(self, is_fundamental): + self.is_fundamental = is_fundamental + + INTEGER = _Info(True) + DOUBLE = _Info(True) + BOOLEAN = _Info(True) + STRING = _Info(True) + ARRAY = _Info(False) + REF = _Info(False) + CHOICES = _Info(False) + OBJECT = _Info(False) diff --git a/tools/json_schema_compiler/model_test.py b/tools/json_schema_compiler/model_test.py new file mode 100644 index 0000000..61a65e7 --- /dev/null +++ b/tools/json_schema_compiler/model_test.py @@ -0,0 +1,95 @@ +# Copyright (c) 2012 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. + +import json +import model +import unittest + +class ModelTest(unittest.TestCase): + def setUp(self): + self.model = model.Model() + self.permissions_json = json.loads(open('test/permissions.json').read()) + self.model.AddNamespace(self.permissions_json[0], + 'path/to/permissions.json') + self.permissions = self.model.namespaces.get('permissions') + self.windows_json = json.loads(open('test/windows.json').read()) + self.model.AddNamespace(self.windows_json[0], + 'path/to/window.json') + self.windows = self.model.namespaces.get('windows') + self.tabs_json = json.loads(open('test/tabs.json').read()) + self.model.AddNamespace(self.tabs_json[0], + 'path/to/tabs.json') + self.tabs = self.model.namespaces.get('tabs') + + def testNamespaces(self): + self.assertEquals(3, len(self.model.namespaces)) + self.assertTrue(self.permissions) + + def testNamespaceNocompile(self): + self.permissions_json[0]['namespace'] = 'something' + del self.permissions_json[0]['compile'] + self.model.AddNamespace(self.permissions_json[0], + 'path/to/something.json') + self.assertEquals(3, len(self.model.namespaces)) + + def testHasFunctions(self): + self.assertEquals(["contains", "getAll", "remove", "request"], + sorted(self.permissions.functions.keys())) + + def testFunctionNoCallback(self): + del (self.permissions_json[0]['functions'][0]['parameters'][0]) + self.assertRaises(AssertionError, self.model.AddNamespace, + self.permissions_json[0], 'path/to/something.json') + + def testFunctionNocompile(self): + # tabs.json has 2 functions marked as nocompile (connect, sendRequest) + self.assertEquals(["captureVisibleTab", "create", "detectLanguage", + "executeScript", "get", "getAllInWindow", "getCurrent", + "getSelected", "highlight", "insertCSS", "move", "query", "reload", + "remove", "update"], + sorted(self.tabs.functions.keys()) + ) + + def testHasTypes(self): + self.assertEquals(['Tab'], self.tabs.types.keys()) + self.assertEquals(['Permissions'], self.permissions.types.keys()) + self.assertEquals(['Window'], self.windows.types.keys()) + + def testHasProperties(self): + self.assertEquals(["active", "favIconUrl", "highlighted", "id", + "incognito", "index", "pinned", "selected", "status", "title", "url", + "windowId"], + sorted(self.tabs.types['Tab'].properties.keys())) + + def testProperties(self): + string_prop = self.tabs.types['Tab'].properties['status'] + self.assertEquals(model.PropertyType.STRING, string_prop.type_) + integer_prop = self.tabs.types['Tab'].properties['id'] + self.assertEquals(model.PropertyType.INTEGER, integer_prop.type_) + array_prop = self.windows.types['Window'].properties['tabs'] + self.assertEquals(model.PropertyType.ARRAY, array_prop.type_) + self.assertEquals(model.PropertyType.REF, array_prop.item_type.type_) + self.assertEquals('Tab', array_prop.item_type.ref_type) + object_prop = self.tabs.functions['query'].params[0] + self.assertEquals(model.PropertyType.OBJECT, object_prop.type_) + self.assertEquals( + ["active", "highlighted", "pinned", "status", "title", "url", + "windowId", "windowType"], + sorted(object_prop.properties.keys())) + # TODO(calamity): test choices + + def testPropertyNotImplemented(self): + (self.permissions_json[0]['types'][0] + ['properties']['permissions']['type']) = 'something' + self.assertRaises(NotImplementedError, self.model.AddNamespace, + self.permissions_json[0], 'path/to/something.json') + + def testDescription(self): + self.assertFalse( + self.permissions.functions['contains'].params[0].description) + self.assertEquals('True if the extension has the specified permissions.', + self.permissions.functions['contains'].callback.param.description) + +if __name__ == '__main__': + unittest.main() diff --git a/tools/json_schema_compiler/test/permissions.json b/tools/json_schema_compiler/test/permissions.json new file mode 100644 index 0000000..e51cc38 --- /dev/null +++ b/tools/json_schema_compiler/test/permissions.json @@ -0,0 +1,140 @@ +[ + { + "namespace": "permissions", + "compile": true, + "types": [ + { + "id": "Permissions", + "type": "object", + "properties": { + "permissions": { + "type": "array", + "items": {"type": "string"}, + "optional": true, + "description": "List of named permissions (does not include hosts or origins)." + }, + "origins": { + "type": "array", + "items": {"type": "string"}, + "optional": true, + "description": "List of origin permissions." + } + } + } + ], + "events": [ + { + "name": "onAdded", + "type": "function", + "description": "Fired when the extension acquires new permissions.", + "parameters": [ + { + "$ref": "Permissions", + "name": "permissions", + "description": "The newly acquired permissions." + } + ] + }, + { + "name": "onRemoved", + "type": "function", + "description": "Fired when access to permissions has been removed from the extension.", + "parameters": [ + { + "$ref": "Permissions", + "name": "permissions", + "description": "The permissions that have been removed." + } + ] + } + ], + "functions": [ + { + "name": "getAll", + "type": "function", + "description": "Gets the extension's current set of permissions.", + "parameters": [ + { + "name": "callback", + "type": "function", + "parameters": [ + { + "name": "permissions", + "$ref": "Permissions", + "description": "The extension's active permissions." + } + ] + } + ] + }, + { + "name": "contains", + "type": "function", + "description": "Checks if the extension has the specified permissions.", + "parameters": [ + { + "name": "permissions", + "$ref": "Permissions" + }, + { + "name": "callback", + "type": "function", + "parameters": [ + { + "name": "result", + "type": "boolean", + "description": "True if the extension has the specified permissions." + } + ] + } + ] + }, + { + "name": "request", + "type": "function", + "description": "Requests access to the specified permissions. These permissions must be defined in the optional_permissions field of the manifest. If there are any problems requesting the permissions, <a href='extension.html#property-lastError'>chrome.extension.lastError</a> will be set.", + "parameters": [ + { + "name": "permissions", + "$ref": "Permissions" + }, + { + "name": "callback", + "type": "function", + "optional": true, + "parameters": [ + { + "name": "granted", + "type": "boolean", + "description": "True if the user granted the specified permissions." + } + ] + } + ] + }, + { + "name": "remove", + "type": "function", + "description": "Removes access to the specified permissions. If there are any problems removing the permissions, <a href='extension.html#property-lastError'>chrome.extension.lastError</a> will be set.", + "parameters": [ + { + "name": "permissions", + "$ref": "Permissions" + }, + { + "name": "callback", + "type": "function", + "optional": true, + "parameters": [ + { + "name": "removed", + "type": "boolean", + "description": "True if the permissions were removed." + } + ] + } + ] + } + ] + } +] diff --git a/tools/json_schema_compiler/test/tabs.json b/tools/json_schema_compiler/test/tabs.json new file mode 100644 index 0000000..0bd6ae6 --- /dev/null +++ b/tools/json_schema_compiler/test/tabs.json @@ -0,0 +1,769 @@ +[ + { + "namespace": "tabs", + "compile": true, + "types": [ + { + "id": "Tab", + "type": "object", + "properties": { + "id": {"type": "integer", "minimum": 0, "description": "The ID of the tab. Tab IDs are unique within a browser session."}, + "index": {"type": "integer", "minimum": 0, "description": "The zero-based index of the tab within its window."}, + "windowId": {"type": "integer", "minimum": 0, "description": "The ID of the window the tab is contained within."}, + "selected": {"type": "boolean", "description": "Whether the tab is selected.", "nodoc": true}, + "highlighted": {"type": "boolean", "description": "Whether the tab is highlighted."}, + "active": {"type": "boolean", "description": "Whether the tab is active in its window."}, + "pinned": {"type": "boolean", "description": "Whether the tab is pinned."}, + "url": {"type": "string", "description": "The URL the tab is displaying."}, + "title": {"type": "string", "optional": true, "description": "The title of the tab. This may not be available if the tab is loading."}, + "favIconUrl": {"type": "string", "optional": true, "description": "The URL of the tab's favicon. This may not be available if the tab is loading."}, + "status": {"type": "string", "optional": true, "description": "Either <em>loading</em> or <em>complete</em>."}, + "incognito": {"type": "boolean", "description": "Whether the tab is in an incognito window."} + } + } + ], + "functions": [ + { + "name": "get", + "type": "function", + "description": "Retrieves details about the specified tab.", + "parameters": [ + { + "type": "integer", + "name": "tabId", + "minimum": 0 + }, + { + "type": "function", + "name": "callback", + "parameters": [ + {"name": "tab", "$ref": "Tab"} + ] + } + ] + }, + { + "name": "getCurrent", + "type": "function", + "description": "Gets the tab that this script call is being made from. May be undefined if called from a non-tab context (for example: a background page or popup view).", + "parameters": [ + { + "type": "function", + "name": "callback", + "parameters": [ + { + "name": "tab", + "$ref": "Tab", + "optional": true + } + ] + } + ] + }, + { + "name": "connect", + "nocompile": true, + "type": "function", + "description": "Connects to the content script(s) in the specified tab. The <a href='extension.html#event-onConnect'>chrome.extension.onConnect</a> event is fired in each content script running in the specified tab for the current extension. For more details, see <a href='content_scripts.html#messaging'>Content Script Messaging</a>.", + "parameters": [ + { + "type": "integer", + "name": "tabId", + "minimum": 0 + }, + { + "type": "object", + "name": "connectInfo", + "properties": { + "name": { "type": "string", "optional": true, "description": "Will be passed into onConnect for content scripts that are listening for the connection event." } + }, + "optional": true + } + ], + "returns": { + "$ref": "Port", + "description": "A port that can be used to communicate with the content scripts running in the specified tab. The port's <a href='extension.html#type-Port'>onDisconnect</a> event is fired if the tab closes or does not exist. " + } + }, + { + "name": "sendRequest", + "nocompile": true, + "type": "function", + "description": "Sends a single request to the content script(s) in the specified tab, with an optional callback to run when a response is sent back. The <a href='extension.html#event-onRequest'>chrome.extension.onRequest</a> event is fired in each content script running in the specified tab for the current extension.", + "parameters": [ + { + "type": "integer", + "name": "tabId", + "minimum": 0 + }, + { + "type": "any", + "name": "request" + }, + { + "type": "function", + "name": "responseCallback", + "optional": true, + "parameters": [ + { + "name": "response", + "type": "any", + "description": "The JSON response object sent by the handler of the request. If an error occurs while connecting to the specified tab, the callback will be called with no arguments and <a href='extension.html#property-lastError'>chrome.extension.lastError</a> will be set to the error message." + } + ] + } + ] + }, + { + "name": "getSelected", + "nodoc": true, + "type": "function", + "description": "Deprecated. Please use query({'active': true}). Gets the tab that is selected in the specified window.", + "parameters": [ + { + "type": "integer", + "name": "windowId", + "minimum": 0, + "optional": true, + "description": "Defaults to the <a href='windows.html#current-window'>current window</a>." + }, + { + "type": "function", + "name": "callback", + "parameters": [ + {"name": "tab", "$ref": "Tab"} + ] + } + ] + }, + { + "name": "getAllInWindow", + "type": "function", + "nodoc": true, + "description": "Deprecated. Please use query({'windowId': windowId}). Gets details about all tabs in the specified window.", + "parameters": [ + { + "type": "integer", + "name": "windowId", + "minimum": 0, + "optional": true, + "description": "Defaults to the <a href='windows.html#current-window'>current window</a>." + }, + { + "type": "function", + "name": "callback", + "parameters": [ + {"name": "tabs", "type": "array", "items": { "$ref": "Tab" } } + ] + } + ] + }, + { + "name": "create", + "type": "function", + "description": "Creates a new tab. Note: This function can be used without requesting the 'tabs' permission in the manifest.", + "parameters": [ + { + "type": "object", + "name": "createProperties", + "properties": { + "windowId": { + "type": "integer", + "minimum": 0, + "optional": true, + "description": "The window to create the new tab in. Defaults to the <a href='windows.html#current-window'>current window</a>." + }, + "index": { + "type": "integer", + "minimum": 0, + "optional": true, + "description": "The position the tab should take in the window. The provided value will be clamped to between zero and the number of tabs in the window." + }, + "url": { + "type": "string", + "optional": true, + "description": "The URL to navigate the tab to initially. Fully-qualified URLs must include a scheme (i.e. 'http://www.google.com', not 'www.google.com'). Relative URLs will be relative to the current page within the extension. Defaults to the New Tab Page." + }, + "active": { + "type": "boolean", + "optional": true, + "description": "Whether the tab should become the active tab in the window. Defaults to <var>true</var>" + }, + "selected": { + "nodoc": true, + "type": "boolean", + "optional": true, + "description": "Whether the tab should become the selected tab in the window. Defaults to <var>true</var>" + }, + "pinned": { + "type": "boolean", + "optional": true, + "description": "Whether the tab should be pinned. Defaults to <var>false</var>" + } + } + }, + { + "type": "function", + "name": "callback", + "optional": true, + "parameters": [ + { + "name": "tab", + "$ref": "Tab", + "description": "Details about the created tab. Will contain the ID of the new tab." + } + ] + } + ] + }, + { + "name": "query", + "type": "function", + "description": "Gets all tabs that have the specified properties, or all tabs if no properties are specified.", + "parameters": [ + { + "type": "object", + "name": "queryInfo", + "properties": { + "active": { + "type": "boolean", + "optional": true, + "description": "Whether the tabs are active in their windows." + }, + "pinned": { + "type": "boolean", + "optional": true, + "description": "Whether the tabs are pinned." + }, + "highlighted": { + "type": "boolean", + "optional": true, + "description": "Whether the tabs are highlighted." + }, + "status": { + "type": "string", + "optional": true, + "enum": ["loading", "complete"], + "description": "Whether the tabs have completed loading." + }, + "title": { + "type": "string", + "optional": true, + "description": "Match page titles against a pattern." + }, + "url": { + "type": "string", + "optional": true, + "description": "Match tabs against a URL pattern." + }, + "windowId": { + "type": "integer", + "optional": true, + "minimum": 0, + "description": "The ID of the parent window." + }, + "windowType": { + "type": "string", + "optional": true, + "enum": ["normal", "popup", "panel", "app"], + "description": "The type of window the tabs are in." + } + } + }, + { + "type": "function", + "name": "callback", + "parameters": [ + { + "name": "result", + "type": "array", + "items": { + "$ref": "Tab" + } + } + ] + } + ] + }, + { + "name": "highlight", + "type": "function", + "description": "Highlights the given tabs.", + "parameters": [ + { + "type": "object", + "name": "highlightInfo", + "properties": { + "windowId": { + "type": "integer", + "description": "The window that contains the tabs." + }, + "tabs": { + "description": "One or more tab indices to highlight.", + "choices": [ + {"type": "array", "items": {"type": "integer", "minimum": 0}}, + {"type": "integer"} + ] + } + } + }, + { + "type": "function", + "name": "callback", + "parameters": [ + { + "name": "window", + "$ref": "Window", + "description": "Contains details about the window whose tabs were highlighted." + } + ] + } + ] + }, + { + "name": "update", + "type": "function", + "description": "Modifies the properties of a tab. Properties that are not specified in <var>updateProperties</var> are not modified. Note: This function can be used without requesting the 'tabs' permission in the manifest.", + "parameters": [ + { + "type": "integer", + "name": "tabId", + "minimum": 0, + "optional": true, + "description": "Defaults to the selected tab of the <a href='windows.html#current-window'>current window</a>." + }, + { + "type": "object", + "name": "updateProperties", + "properties": { + "url": { + "optional": true, + "description": "A URL to navigate the tab to." + }, + "active": { + "type": "boolean", + "optional": true, + "description": "Whether the tab should be active." + }, + "highlighted": { + "type": "boolean", + "optional": true, + "description": "Adds or removes the tab from the current selection." + }, + "selected": { + "nodoc": true, + "type": "boolean", + "optional": true, + "description": "Whether the tab should be selected." + }, + "pinned": { + "type": "boolean", + "optional": true, + "description": "Whether the tab should be pinned." + } + } + }, + { + "type": "function", + "name": "callback", + "optional": true, + "parameters": [ + { + "name": "tab", + "$ref": "Tab", + "optional": true, + "description": "Details about the updated tab, or <code>null</code> if the 'tabs' permission has not been requested." + } + ] + } + ] + }, + { + "name": "move", + "type": "function", + "description": "Moves one or more tabs to a new position within its window, or to a new window. Note that tabs can only be moved to and from normal (window.type === \"normal\") windows.", + "parameters": [ + { + "name": "tabIds", + "description": "The tab or list of tabs to move.", + "choices": [ + {"type": "integer", "minimum": 0}, + {"type": "array", "items": {"type": "integer", "minimum": 0}} + ] + }, + { + "type": "object", + "name": "moveProperties", + "properties": { + "windowId": { + "type": "integer", + "minimum": 0, + "optional": true, + "description": "Defaults to the window the tab is currently in." + }, + "index": { + "type": "integer", + "minimum": 0, + "description": "The position to move the window to. The provided value will be clamped to between zero and the number of tabs in the window." + } + } + }, + { + "type": "function", + "name": "callback", + "optional": true, + "parameters": [ + { + "name": "tabs", + "description": "Details about the moved tabs.", + "choices": [ + {"$ref": "Tab"}, + {"type": "array", "items": {"$ref": "Tab"}} + ] + } + ] + } + ] + }, + { + "name": "reload", + "type": "function", + "description": "Reload a tab.", + "parameters": [ + {"type": "integer", "name": "tabId", "optional": true, "description": "The ID of the tab to reload; defaults to the selected tab of the current window."}, + { + "type": "object", + "name": "reloadProperties", + "optional": true, + "properties": { + "bypassCache": { + "type": "boolean", + "optional": true, + "description": "Whether using any local cache. Default is false." + } + } + }, + {"type": "function", "name": "callback", "optional": true, "parameters": []} + ] + }, + { + "name": "remove", + "type": "function", + "description": "Closes one or more tabs. Note: This function can be used without requesting the 'tabs' permission in the manifest.", + "parameters": [ + { + "name": "tabIds", + "description": "The tab or list of tabs to close.", + "choices": [ + {"type": "integer", "minimum": 0}, + {"type": "array", "items": {"type": "integer", "minimum": 0}} + ] + }, + {"type": "function", "name": "callback", "optional": true, "parameters": []} + ] + }, + { + "name": "detectLanguage", + "type": "function", + "description": "Detects the primary language of the content in a tab.", + "parameters": [ + { + "type": "integer", + "name": "tabId", + "minimum": 0, + "optional": true, + "description": "Defaults to the active tab of the <a href='windows.html#current-window'>current window</a>." + }, + { + "type": "function", + "name": "callback", + "parameters": [ + { + "type": "string", + "name": "language", + "description": "An ISO language code such as <code>en</code> or <code>fr</code>. For a complete list of languages supported by this method, see <a href='http://src.chromium.org/viewvc/chrome/trunk/src/third_party/cld/languages/internal/languages.cc'>kLanguageInfoTable</a>. The 2nd to 4th columns will be checked and the first non-NULL value will be returned except for Simplified Chinese for which zh-CN will be returned. For an unknown language, <code>und</code> will be returned." + } + ] + } + ] + }, + { + "name": "captureVisibleTab", + "type": "function", + "description": "Captures the visible area of the currently active tab in the specified window. You must have <a href='manifest.html#permissions'>host permission</a> for the URL displayed by the tab.", + "parameters": [ + { + "type": "integer", + "name": "windowId", + "minimum": 0, + "optional": true, + "description": "The target window. Defaults to the <a href='windows.html#current-window'>current window</a>." + }, + { + "type": "object", + "name": "options", + "optional": true, + "description": "Set parameters of image capture, such as the format of the resulting image.", + "properties": { + "format": { + "type": "string", + "optional": true, + "enum": ["jpeg", "png"], + "description": "The format of the resulting image. Default is jpeg." + }, + "quality": { + "type": "integer", + "name": "quality", + "optional": true, + "minimum": 0, + "maximum": 100, + "description": "When format is 'jpeg', controls the quality of the resulting image. This value is ignored for PNG images. As quality is decreased, the resulting image will have more visual artifacts, and the number of bytes needed to store it will decrease." + } + } + }, + { + "type": "function", "name": "callback", "parameters": [ + {"type": "string", "name": "dataUrl", "description": "A data URL which encodes an image of the visible area of the captured tab. May be assigned to the 'src' property of an HTML Image element for display."} + ] + } + ] + }, + { + "name": "executeScript", + "type": "function", + "description": "Injects JavaScript code into a page. For details, see the <a href='content_scripts.html#pi'>programmatic injection</a> section of the content scripts doc.", + "parameters": [ + {"type": "integer", "name": "tabId", "optional": true, "description": "The ID of the tab in which to run the script; defaults to the active tab of the current window."}, + { + "type": "object", + "name": "details", + "description": "Details of the script to run. Either the code or the file property must be set, but both may not be set at the same time.", + "properties": { + "code": {"type": "string", "optional": true, "description": "JavaScript code to execute."}, + "file": {"type": "string", "optional": true, "description": "JavaScript file to execute."}, + "allFrames": {"type": "boolean", "optional": true, "description": "If allFrames is true, this function injects script into all frames of current page. By default, it's false and script is injected only into the top main frame."} + } + }, + { + "type": "function", + "name": "callback", + "optional": true, + "description": "Called after all the JavaScript has been executed.", + "parameters": [] + } + ] + }, + { + "name": "insertCSS", + "type": "function", + "description": "Injects CSS into a page. For details, see the <a href='content_scripts.html#pi'>programmatic injection</a> section of the content scripts doc.", + "parameters": [ + {"type": "integer", "name": "tabId", "optional": true, "description": "The ID of the tab in which to insert the CSS; defaults to the active tab of the current window."}, + { + "type": "object", + "name": "details", + "description": "Details of the CSS text to insert. Either the code or the file property must be set, but both may not be set at the same time.", + "properties": { + "code": {"type": "string", "optional": true, "description": "CSS code to be injected."}, + "file": {"type": "string", "optional": true, "description": "CSS file to be injected."}, + "allFrames": {"type": "boolean", "optional": true, "description": "If allFrames is true, this function injects CSS text into all frames of current page. By default, it's false and CSS is injected only into the top main frame."} + } + }, + { + "type": "function", + "name": "callback", + "optional": true, + "description": "Called when all the CSS has been inserted.", + "parameters": [] + } + ] + } + ], + "events": [ + { + "name": "onCreated", + "type": "function", + "description": "Fired when a tab is created. Note that the tab's URL may not be set at the time this event fired, but you can listen to onUpdated events to be notified when a URL is set.", + "parameters": [ + { + "$ref": "Tab", + "name": "tab", + "description": "Details of the tab that was created." + } + ] + }, + { + "name": "onUpdated", + "type": "function", + "description": "Fired when a tab is updated.", + "parameters": [ + {"type": "integer", "name": "tabId", "minimum": 0}, + { + "type": "object", + "name": "changeInfo", + "description": "Lists the changes to the state of the tab that was updated.", + "properties": { + "status": { + "type": "string", + "optional": true, + "description": "The status of the tab. Can be either <em>loading</em> or <em>complete</em>." + }, + "url": { + "type": "string", + "optional": true, + "description": "The tab's URL if it has changed." + }, + "pinned": { + "type": "boolean", + "optional": true, + "description": "The tab's new pinned state." + } + } + }, + { + "$ref": "Tab", + "name": "tab", + "description": "Gives the state of the tab that was updated." + } + ] + }, + { + "name": "onMoved", + "type": "function", + "description": "Fired when a tab is moved within a window. Only one move event is fired, representing the tab the user directly moved. Move events are not fired for the other tabs that must move in response. This event is not fired when a tab is moved between windows. For that, see <a href='#event-onDetached'>onDetached</a>.", + "parameters": [ + {"type": "integer", "name": "tabId", "minimum": 0}, + { + "type": "object", + "name": "moveInfo", + "properties": { + "windowId": {"type": "integer", "minimum": 0}, + "fromIndex": {"type": "integer", "minimum": 0}, + "toIndex": {"type": "integer", "minimum": 0} + } + } + ] + }, + { + "name": "onSelectionChanged", + "nodoc": true, + "type": "function", + "description": "Deprecated. Please use onActiveChanged.", + "parameters": [ + { + "type": "integer", + "name": "tabId", + "minimum": 0, + "description": "The ID of the tab that has become active." + }, + { + "type": "object", + "name": "selectInfo", + "properties": { + "windowId": { + "type": "integer", + "minimum": 0, + "description": "The ID of the window the selected tab changed inside of." + } + } + } + ] + }, + { + "name": "onActiveChanged", + "type": "function", + "description": "Fires when the selected tab in a window changes.", + "parameters": [ + { + "type": "integer", + "name": "tabId", + "minimum": 0, + "description": "The ID of the tab that has become active." + }, + { + "type": "object", + "name": "selectInfo", + "properties": { + "windowId": { + "type": "integer", + "minimum": 0, + "description": "The ID of the window the selected tab changed inside of." + } + } + } + ] + }, + { + "name": "onHighlightChanged", + "type": "function", + "description": "Fired when the highlighted or selected tabs in a window changes.", + "parameters": [ + { + "type": "object", + "name": "selectInfo", + "properties": { + "windowId": { + "type": "integer", + "minimum": 0, + "description": "The window whose tabs changed." + }, + "tabIds": { + "type": "array", + "name": "tabIds", + "items": {"type": "integer", "minimum": 0}, + "description": "All highlighted tabs in the window." + } + } + } + ] + }, + { + "name": "onDetached", + "type": "function", + "description": "Fired when a tab is detached from a window, for example because it is being moved between windows.", + "parameters": [ + {"type": "integer", "name": "tabId", "minimum": 0}, + { + "type": "object", + "name": "detachInfo", + "properties": { + "oldWindowId": {"type": "integer", "minimum": 0}, + "oldPosition": {"type": "integer", "minimum": 0} + } + } + ] + }, + { + "name": "onAttached", + "type": "function", + "description": "Fired when a tab is attached to a window, for example because it was moved between windows.", + "parameters": [ + {"type": "integer", "name": "tabId", "minimum": 0}, + { + "type": "object", + "name": "attachInfo", + "properties": { + "newWindowId": {"type": "integer", "minimum": 0}, + "newPosition": {"type": "integer", "minimum": 0} + } + } + ] + }, + { + "name": "onRemoved", + "type": "function", + "description": "Fired when a tab is closed. Note: A listener can be registered for this event without requesting the 'tabs' permission in the manifest.", + "parameters": [ + {"type": "integer", "name": "tabId", "minimum": 0}, + { + "type": "object", + "name": "removeInfo", + "properties": { + "isWindowClosing": {"type": "boolean", "description": "True when the tab is being closed because its window is being closed." } + } + } + ] + } + ] + } +] diff --git a/tools/json_schema_compiler/test/windows.json b/tools/json_schema_compiler/test/windows.json new file mode 100644 index 0000000..c7711db --- /dev/null +++ b/tools/json_schema_compiler/test/windows.json @@ -0,0 +1,264 @@ +[ + { + "namespace": "windows", + "compile": true, + "types": [ + { + "id": "Window", + "type": "object", + "properties": { + "id": {"type": "integer", "minimum": 0, "description": "The ID of the window. Window IDs are unique within a browser session."}, + "focused": {"type": "boolean", "description": "Whether the window is currently the focused window."}, + "top": {"type": "integer", "description": "The offset of the window from the top edge of the screen in pixels."}, + "left": {"type": "integer", "description": "The offset of the window from the left edge of the screen in pixels."}, + "width": {"type": "integer", "description": "The width of the window in pixels."}, + "height": {"type": "integer", "description": "The height of the window in pixels."}, + "tabs": {"type": "array", "items": { "$ref": "Tab" }, "optional": true, "description": "Array of $ref:Tab objects representing the current tabs in the window."}, + "incognito": {"type": "boolean", "description": "Whether the window is incognito."}, + "type": { + "type": "string", + "description": "The type of browser window this is.", + "enum": ["normal", "popup", "panel", "app"] + }, + "state": { + "type": "string", + "description": "The state of this browser window.", + "enum": ["normal", "minimized", "maximized"] + } + } + } + ], + "properties": { + "WINDOW_ID_NONE": { + "type": "integer", + "value": "-1", + "description": "The windowId value that represents the absence of a chrome browser window." + } + }, + "functions": [ + { + "name": "get", + "type": "function", + "description": "Gets details about a window.", + "parameters": [ + {"type": "integer", "name": "windowId", "minimum": 0}, + { + "type": "object", + "name": "getInfo", + "optional": true, + "description": "", + "properties": { + "populate": {"type": "boolean", "optional": true, "description": "If true, the window object will have a <var>tabs</var> property that contains a list of the $ref:Tab objects" } + } + }, + { + "type": "function", + "name": "callback", + "parameters": [ + { + "name": "window", "$ref": "Window" + } + ] + } + ] + }, + { + "name": "getCurrent", + "type": "function", + "description": "Gets the <a href='#current-window'>current window</a>.", + "parameters": [ + { + "type": "object", + "name": "getInfo", + "optional": true, + "description": "", + "properties": { + "populate": {"type": "boolean", "optional": true, "description": "If true, the window object will have a <var>tabs</var> property that contains a list of the $ref:Tab objects" } + } + }, + { + "type": "function", + "name": "callback", + "parameters": [ + { + "name": "window", "$ref": "Window" + } + ] + } + ] + }, + { + "name": "getLastFocused", + "type": "function", + "description": "Gets the window that was most recently focused — typically the window 'on top'.", + "parameters": [ + { + "type": "object", + "name": "getInfo", + "optional": true, + "description": "", + "properties": { + "populate": {"type": "boolean", "optional": true, "description": "If true, the window object will have a <var>tabs</var> property that contains a list of the $ref:Tab objects" } + } + }, + { + "type": "function", + "name": "callback", + "parameters": [ + { + "name": "window", "$ref": "Window" + } + ] + } + ] + }, + { + "name": "getAll", + "type": "function", + "description": "Gets all windows.", + "parameters": [ + { + "type": "object", + "name": "getInfo", + "optional": true, + "description": "", + "properties": { + "populate": {"type": "boolean", "optional": true, "description": "If true, each window object will have a <var>tabs</var> property that contains a list of the $ref:Tab objects for that window." } + } + }, + { + "type": "function", + "name": "callback", + "parameters": [ + { + "name": "windows", "type": "array", "items": { "$ref": "Window" } + } + ] + } + ] + }, + { + "name": "create", + "type": "function", + "description": "Creates (opens) a new browser with any optional sizing, position or default URL provided.", + "parameters": [ + { + "type": "object", + "name": "createData", + "properties": { + "url": { + "type": "string", + "description": "A URL or list of URLs to open as tabs in the window. Fully-qualified URLs must include a scheme (i.e. 'http://www.google.com', not 'www.google.com'). Relative URLs will be relative to the current page within the extension. Defaults to the New Tab Page.", + "optional": true, + "choices": [ + {"type": "string"}, + {"type": "array", "items": {"type": "string"}} + ] + }, + "tabId": {"type": "integer", "minimum": 0, "optional": true, "description": "The id of the tab for which you want to adopt to the new window."}, + "left": {"type": "integer", "optional": true, "description": "The number of pixels to position the new window from the left edge of the screen. If not specified, the new window is offset naturally from the last focusd window. This value is ignored for panels."}, + "top": {"type": "integer", "optional": true, "description": "The number of pixels to position the new window from the top edge of the screen. If not specified, the new window is offset naturally from the last focusd window. This value is ignored for panels."}, + "width": {"type": "integer", "minimum": 0, "optional": true, "description": "The width in pixels of the new window. If not specified defaults to a natural width."}, + "height": {"type": "integer", "minimum": 0, "optional": true, "description": "The height in pixels of the new window. If not specified defaults to a natural height."}, + "focused": {"type": "boolean", "optional": true, "description": "If true, opens an active window. If false, opens an inactive window."}, + "incognito": {"type": "boolean", "optional": true, "description": "Whether the new window should be an incognito window."}, + "type": { + "type": "string", + "optional": true, + "description": "Specifies what type of browser window to create. The 'panel' type creates a popup unless the '--enable-panels' flag is set.", + "enum": ["normal", "popup", "panel"] + } + }, + "optional": true + }, + { + "type": "function", + "name": "callback", + "optional": true, + "parameters": [ + { + "name": "window", "$ref": "Window", "description": "Contains details about the created window.", + "optional": true + } + ] + } + ] + }, + { + "name": "update", + "type": "function", + "description": "Updates the properties of a window. Specify only the properties that you want to change; unspecified properties will be left unchanged.", + "parameters": [ + {"type": "integer", "name": "windowId", "minimum": 0}, + { + "type": "object", + "name": "updateInfo", + "properties": { + "left": {"type": "integer", "optional": true, "description": "The offset from the left edge of the screen to move the window to in pixels. This value is ignored for panels."}, + "top": {"type": "integer", "optional": true, "description": "The offset from the top edge of the screen to move the window to in pixels. This value is ignored for panels."}, + "width": {"type": "integer", "minimum": 0, "optional": true, "description": "The width to resize the window to in pixels. This value is ignored for panels."}, + "height": {"type": "integer", "minimum": 0, "optional": true, "description": "The height to resize the window to in pixels. This value is ignored for panels."}, + "focused": {"type": "boolean", "optional": true, "description": "If true, brings the window to the front. If false, brings the next window in the z-order to the front."}, + "drawAttention": {"type": "boolean", "optional": true, "description": "If true, causes the window to be displayed in a manner that draws the user's attention to the window, without changing the focused window. The effect lasts until the user changes focus to the window. This option has no effect if set to false or if the window already has focus."}, + "state": { + "type": "string", + "optional": true, + "description": "The new state of the window. The 'minimized' and 'maximized' states cannot be combined with 'left', 'top', 'width' or 'height'.", + "enum": ["normal", "minimized", "maximized"] + } + } + }, + { + "type": "function", + "name": "callback", + "optional": true, + "parameters": [ + { + "name": "window", "$ref": "Window" + } + ] + } + ] + }, + { + "name": "remove", + "type": "function", + "description": "Removes (closes) a window, and all the tabs inside it.", + "parameters": [ + {"type": "integer", "name": "windowId", "minimum": 0}, + {"type": "function", "name": "callback", "optional": true, "parameters": []} + ] + } + ], + "events": [ + { + "name": "onCreated", + "type": "function", + "description": "Fired when a window is created.", + "parameters": [ + { + "$ref": "Window", + "name": "window", + "description": "Details of the window that was created." + } + ] + }, + { + "name": "onRemoved", + "type": "function", + "description": "Fired when a window is removed (closed).", + "parameters": [ + {"type": "integer", "name": "windowId", "minimum": 0, "description": "ID of the removed window."} + ] + }, + { + "name": "onFocusChanged", + "type": "function", + "description": "Fired when the currently focused window changes. Will be chrome.windows.WINDOW_ID_NONE if all chrome windows have lost focus. Note: On some Linux window managers, WINDOW_ID_NONE will always be sent immediately preceding a switch from one chrome window to another.", + "parameters": [ + {"type": "integer", "name": "windowId", "minimum": -1, "description": "ID of the newly focused window."} + ] + } + ] + } +] diff --git a/tools/json_schema_compiler/util.cc b/tools/json_schema_compiler/util.cc new file mode 100644 index 0000000..38e6b30 --- /dev/null +++ b/tools/json_schema_compiler/util.cc @@ -0,0 +1,82 @@ +// Copyright (c) 2012 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 "tools/json_schema_compiler/util.h" + +#include "base/values.h" + +namespace json_schema_compiler { +namespace util { + +bool GetStrings( + const base::DictionaryValue& from, + const std::string& name, + std::vector<std::string>* out) { + base::ListValue* list = NULL; + if (!from.GetListWithoutPathExpansion(name, &list)) + return false; + + std::string string; + for (size_t i = 0; i < list->GetSize(); ++i) { + if (!list->GetString(i, &string)) + return false; + out->push_back(string); + } + + return true; +} + +bool GetOptionalStrings( + const base::DictionaryValue& from, + const std::string& name, + scoped_ptr<std::vector<std::string> >* out) { + base::ListValue* list = NULL; + { + base::Value* maybe_list = NULL; + // Since |name| is optional, its absence is acceptable. However, anything + // other than a ListValue is not. + if (!from.GetWithoutPathExpansion(name, &maybe_list)) + return true; + if (!maybe_list->IsType(base::Value::TYPE_LIST)) + return false; + list = static_cast<base::ListValue*>(maybe_list); + } + + out->reset(new std::vector<std::string>()); + std::string string; + for (size_t i = 0; i < list->GetSize(); ++i) { + if (!list->GetString(i, &string)) { + out->reset(); + return false; + } + (*out)->push_back(string); + } + + return true; +} + +void SetStrings( + const std::vector<std::string>& from, + const std::string& name, + base::DictionaryValue* out) { + base::ListValue* list = new base::ListValue(); + out->SetWithoutPathExpansion(name, list); + for (std::vector<std::string>::const_iterator it = from.begin(); + it != from.end(); ++it) { + list->Append(base::Value::CreateStringValue(*it)); + } +} + +void SetOptionalStrings( + const scoped_ptr<std::vector<std::string> >& from, + const std::string& name, + base::DictionaryValue* out) { + if (!from.get()) + return; + + SetStrings(*from, name, out); +} + +} // namespace api_util +} // namespace extensions diff --git a/tools/json_schema_compiler/util.h b/tools/json_schema_compiler/util.h new file mode 100644 index 0000000..e94b518 --- /dev/null +++ b/tools/json_schema_compiler/util.h @@ -0,0 +1,53 @@ +// Copyright (c) 2012 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. + +#ifndef TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ +#define TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ +#pragma once + +#include <string> +#include <vector> + +#include "base/memory/scoped_ptr.h" + +namespace base { +class DictionaryValue; +} + +namespace json_schema_compiler { +namespace util { + +// Creates a new vector containing the strings |from|.|name| at |out|. Returns +// false if there is no list at the specified key or if the list has anything +// other than strings. +bool GetStrings( + const base::DictionaryValue& from, + const std::string& name, + std::vector<std::string>* out); + +// Creates a new vector containing the strings |from|.|name| at |out|. Returns +// true on success or if there is nothing at the specified key. Returns false +// if anything other than a list of strings is at the specified key. +bool GetOptionalStrings( + const base::DictionaryValue& from, + const std::string& name, + scoped_ptr<std::vector<std::string> >* out); + +// Puts the each string in |from| into a new ListValue at |out|.|name|. +void SetStrings( + const std::vector<std::string>& from, + const std::string& name, + base::DictionaryValue* out); + +// If from is non-NULL, puts each string in |from| into a new ListValue at +// |out|.|name|. +void SetOptionalStrings( + const scoped_ptr<std::vector<std::string> >& from, + const std::string& name, + base::DictionaryValue* out); + +} // namespace api_util +} // namespace extensions + +#endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ |