Chromium Code Reviews| Index: chrome/browser/extensions/api/messaging/native_messaging_host_manifest.cc |
| diff --git a/chrome/browser/extensions/api/messaging/native_messaging_host_manifest.cc b/chrome/browser/extensions/api/messaging/native_messaging_host_manifest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0402bec32bb05d18e484d4f3c8fe35f84333c726 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/messaging/native_messaging_host_manifest.cc |
| @@ -0,0 +1,93 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest.h" |
| + |
| +#include "base/json/json_file_value_serializer.h" |
| +#include "base/logging.h" |
| +#include "base/values.h" |
| + |
| +namespace extensions { |
| + |
| +NativeMessagingHostManifest::~NativeMessagingHostManifest() {} |
| + |
| +// static |
| +scoped_ptr<NativeMessagingHostManifest> NativeMessagingHostManifest::Load( |
| + const FilePath& file_path, |
| + std::string* error_message) { |
| + DCHECK(error_message); |
| + |
| + JSONFileValueSerializer serializer(file_path); |
| + scoped_ptr<base::Value> parsed(serializer.Deserialize(NULL, error_message)); |
| + if (!parsed) { |
| + return scoped_ptr<NativeMessagingHostManifest>(); |
| + } |
| + |
| + base::DictionaryValue* dictionary; |
| + if (!parsed->GetAsDictionary(&dictionary)) { |
| + *error_message = "Invalid manifest file."; |
| + return scoped_ptr<NativeMessagingHostManifest>(); |
| + } |
| + |
| + scoped_ptr<NativeMessagingHostManifest> result( |
| + new NativeMessagingHostManifest()); |
| + if (!result->Parse(dictionary, error_message)) { |
| + return scoped_ptr<NativeMessagingHostManifest>(); |
| + } |
| + |
| + return result.Pass(); |
| +} |
| + |
| +bool NativeMessagingHostManifest::IsAllowedOrigin(const std::string& origin) { |
| + return allowed_origins_.find(origin) != allowed_origins_.end(); |
| +} |
| + |
| +NativeMessagingHostManifest::NativeMessagingHostManifest() { |
| +} |
| + |
| +bool NativeMessagingHostManifest::Parse(base::DictionaryValue* dictionary, |
| + std::string* error_message) { |
| + scoped_ptr<NativeMessagingHostManifest> result( |
|
Matt Perry
2013/02/16 02:00:51
unused
Sergey Ulanov
2013/02/20 00:05:36
Done.
|
| + new NativeMessagingHostManifest()); |
| + |
| + std::string type; |
| + std::string path; |
| + if (!dictionary->GetString("name", &name_) || |
| + !dictionary->GetString("description", &description_) || |
| + !dictionary->GetString("type", &type) || |
| + !dictionary->GetString("path", &path)) { |
| + *error_message = "Invalid manifest file."; |
|
Matt Perry
2013/02/16 02:00:51
Better: "Invalid value for <key>"
Matt Perry
2013/02/16 02:00:51
Also, maybe description should be optional.
Sergey Ulanov
2013/02/20 00:05:36
I think it's better if it is required. It doesn't
Sergey Ulanov
2013/02/20 00:05:36
Done.
|
| + return false; |
| + } |
| + |
| + if (type == "stdio") { |
| + interface_ = HOST_INTERFACE_STDIO; |
| + } else { |
| + *error_message = "Unknown native messaging host type: " + type; |
|
Matt Perry
2013/02/16 02:00:51
Better for consistency: "Invalid value for type: "
Sergey Ulanov
2013/02/20 00:05:36
Done.
|
| + return false; |
| + } |
| + |
| + // JSON parsed checks that all strings are valid UTF8. |
| + path_ = FilePath::FromUTF8Unsafe(path); |
| + |
| + const base::ListValue* allowed_origins_list; |
| + if (!dictionary->GetList("allowed_origins", &allowed_origins_list)) { |
| + *error_message = "allowed_origins must be list of strings."; |
|
Matt Perry
2013/02/16 02:00:51
Better for consistency: "Invalid value for allowed
Matt Perry
2013/02/16 02:00:51
Isn't allowed_origins optional?
Sergey Ulanov
2013/02/20 00:05:36
Done.
Sergey Ulanov
2013/02/20 00:05:36
No, it's not optional. Access to each native host
|
| + return false; |
| + } |
| + allowed_origins_.clear(); |
| + for (base::ListValue::const_iterator it = allowed_origins_list->begin(); |
| + it != allowed_origins_list->end(); ++it) { |
| + std::string origin; |
| + if (!(*it)->GetAsString(&origin)) { |
| + *error_message = "allowed_origins must be list of strings."; |
| + return false; |
| + } |
| + allowed_origins_.insert(origin); |
|
Matt Perry
2013/02/16 02:00:51
are these strings, or URL patterns? Extensions use
Sergey Ulanov
2013/02/20 00:05:36
Updated this code to use URLPattern and URLPattern
|
| + } |
| + |
| + return true; |
| +} |
| + |
| +} // namespace extensions |