Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/common/extensions/webview_handler.h" | |
| 6 | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "base/strings/string_number_conversions.h" | |
| 9 #include "base/strings/string_util.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "base/values.h" | |
| 12 #include "extensions/common/error_utils.h" | |
| 13 #include "extensions/common/manifest.h" | |
| 14 #include "extensions/common/manifest_constants.h" | |
| 15 | |
| 16 namespace extensions { | |
| 17 | |
| 18 namespace keys = extensions::manifest_keys; | |
| 19 namespace errors = extensions::manifest_errors; | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 const WebviewInfo* GetResourcesInfo( | |
| 24 const Extension* extension) { | |
| 25 return static_cast<WebviewInfo*>( | |
| 26 extension->GetManifestData(keys::kWebviewAccessibleResources)); | |
| 27 } | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 WebviewInfo::WebviewInfo() { | |
| 32 } | |
| 33 | |
| 34 WebviewInfo::~WebviewInfo() { | |
| 35 } | |
| 36 | |
| 37 // static | |
| 38 bool WebviewInfo::IsResourceWebviewAccessible( | |
| 39 const Extension* extension, | |
| 40 const std::string& partition_id, | |
| 41 const std::string& relative_path) { | |
| 42 if (extension->manifest_version() < 2) | |
|
Matt Perry
2013/09/25 01:13:54
Rather than checking this here, let's make webview
Fady Samuel
2013/09/25 03:29:31
Done.
| |
| 43 return false; | |
| 44 | |
| 45 const WebviewInfo* info = GetResourcesInfo(extension); | |
| 46 if (!info) | |
| 47 return false; | |
| 48 | |
| 49 bool partition_is_privileged = false; | |
| 50 for (unsigned int i = 0; | |
|
Matt Perry
2013/09/25 01:13:54
use size_t
Fady Samuel
2013/09/25 03:29:31
Done.
| |
| 51 i < info->webview_privileged_partitions_.size(); | |
| 52 ++i) { | |
| 53 if (MatchPattern(partition_id, info->webview_privileged_partitions_[i])) { | |
| 54 partition_is_privileged = true; | |
| 55 break; | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 return partition_is_privileged && extension->ResourceMatches( | |
| 60 info->webview_accessible_resources_, relative_path); | |
| 61 } | |
| 62 | |
| 63 // static | |
| 64 bool WebviewInfo::HasWebviewAccessibleResources(const Extension* extension) { | |
| 65 const WebviewInfo* info = GetResourcesInfo(extension); | |
| 66 return info && info->webview_accessible_resources_.size() > 0; | |
| 67 } | |
| 68 | |
| 69 WebviewHandler::WebviewHandler() { | |
| 70 } | |
| 71 | |
| 72 WebviewHandler::~WebviewHandler() { | |
| 73 } | |
| 74 | |
| 75 bool WebviewHandler::Parse(Extension* extension, string16* error) { | |
| 76 scoped_ptr<WebviewInfo> info(new WebviewInfo()); | |
| 77 | |
| 78 const base::DictionaryValue* dict_value = NULL; | |
| 79 if (!extension->manifest()->GetDictionary(keys::kWebview, | |
| 80 &dict_value)) { | |
| 81 *error = ASCIIToUTF16(errors::kInvalidWebview); | |
| 82 return false; | |
| 83 } | |
| 84 | |
| 85 const base::ListValue* url_list = NULL; | |
| 86 if (!dict_value->GetList(keys::kWebviewAccessibleResources, | |
| 87 &url_list)) { | |
| 88 *error = ASCIIToUTF16(errors::kInvalidWebviewAccessibleResourcesList); | |
| 89 return false; | |
| 90 } | |
| 91 | |
| 92 for (size_t i = 0; i < url_list->GetSize(); ++i) { | |
| 93 std::string relative_path; | |
| 94 if (!url_list->GetString(i, &relative_path)) { | |
| 95 *error = ErrorUtils::FormatErrorMessageUTF16( | |
| 96 errors::kInvalidWebviewAccessibleResource, base::IntToString(i)); | |
| 97 return false; | |
| 98 } | |
| 99 URLPattern pattern(URLPattern::SCHEME_EXTENSION); | |
| 100 if (pattern.Parse(extension->url().spec()) != URLPattern::PARSE_SUCCESS) { | |
| 101 *error = ErrorUtils::FormatErrorMessageUTF16( | |
| 102 errors::kInvalidURLPatternError, extension->url().spec()); | |
| 103 return false; | |
| 104 } | |
| 105 while (relative_path[0] == '/') | |
| 106 relative_path = relative_path.substr(1, relative_path.length() - 1); | |
| 107 pattern.SetPath(pattern.path() + relative_path); | |
| 108 info->webview_accessible_resources_.AddPattern(pattern); | |
| 109 } | |
| 110 | |
| 111 const base::ListValue* partition_list = NULL; | |
| 112 if (!dict_value->GetList(keys::kWebviewPrivilegedPartitions, | |
| 113 &partition_list)) { | |
| 114 *error = ASCIIToUTF16(errors::kInvalidWebviewPrivilegedPartitionList); | |
| 115 return false; | |
| 116 } | |
| 117 for (size_t i = 0; i < partition_list->GetSize(); ++i) { | |
| 118 std::string partition_wildcard; | |
| 119 if (!partition_list->GetString(i, &partition_wildcard)) { | |
| 120 *error = ErrorUtils::FormatErrorMessageUTF16( | |
| 121 errors::kInvalidWebviewPrivilegedPartition, base::IntToString(i)); | |
| 122 return false; | |
| 123 } | |
| 124 info->webview_privileged_partitions_.push_back(partition_wildcard); | |
| 125 } | |
| 126 extension->SetManifestData(keys::kWebviewAccessibleResources, info.release()); | |
| 127 return true; | |
| 128 } | |
| 129 | |
| 130 const std::vector<std::string> WebviewHandler::Keys() const { | |
| 131 return SingleKey(keys::kWebview); | |
| 132 } | |
| 133 | |
| 134 } // namespace extensions | |
| OLD | NEW |