| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/api/web_request/web_request_api_utils.h" | 5 #include "extensions/browser/api/web_request/web_request_api_utils.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 | 10 |
| 11 namespace extension_web_request_api_utils { | 11 namespace extension_web_request_api_utils { |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 static const char* kResourceTypeStrings[] = { | 15 static const char* kResourceTypeStrings[] = { |
| 16 "main_frame", | 16 "main_frame", |
| 17 "sub_frame", | 17 "sub_frame", |
| 18 "stylesheet", | 18 "stylesheet", |
| 19 "script", | 19 "script", |
| 20 "image", | 20 "image", |
| 21 "object", | 21 "object", |
| 22 "xmlhttprequest", | 22 "xmlhttprequest", |
| 23 "other", | 23 "other", |
| 24 "other", | 24 "other", |
| 25 }; | 25 }; |
| 26 | 26 |
| 27 const size_t kResourceTypeStringsLength = arraysize(kResourceTypeStrings); |
| 28 |
| 27 static ResourceType kResourceTypeValues[] = { | 29 static ResourceType kResourceTypeValues[] = { |
| 28 content::RESOURCE_TYPE_MAIN_FRAME, | 30 content::RESOURCE_TYPE_MAIN_FRAME, |
| 29 content::RESOURCE_TYPE_SUB_FRAME, | 31 content::RESOURCE_TYPE_SUB_FRAME, |
| 30 content::RESOURCE_TYPE_STYLESHEET, | 32 content::RESOURCE_TYPE_STYLESHEET, |
| 31 content::RESOURCE_TYPE_SCRIPT, | 33 content::RESOURCE_TYPE_SCRIPT, |
| 32 content::RESOURCE_TYPE_IMAGE, | 34 content::RESOURCE_TYPE_IMAGE, |
| 33 content::RESOURCE_TYPE_OBJECT, | 35 content::RESOURCE_TYPE_OBJECT, |
| 34 content::RESOURCE_TYPE_XHR, | 36 content::RESOURCE_TYPE_XHR, |
| 35 content::RESOURCE_TYPE_LAST_TYPE, // represents "other" | 37 content::RESOURCE_TYPE_LAST_TYPE, // represents "other" |
| 36 // TODO(jochen): We duplicate the last entry, so the array's size is not a | 38 // TODO(jochen): We duplicate the last entry, so the array's size is not a |
| 37 // power of two. If it is, this triggers a bug in gcc 4.4 in Release builds | 39 // power of two. If it is, this triggers a bug in gcc 4.4 in Release builds |
| 38 // (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43949). Once we use a version | 40 // (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43949). Once we use a version |
| 39 // of gcc with this bug fixed, or the array is changed so this duplicate | 41 // of gcc with this bug fixed, or the array is changed so this duplicate |
| 40 // entry is no longer required, this should be removed. | 42 // entry is no longer required, this should be removed. |
| 41 content::RESOURCE_TYPE_LAST_TYPE, | 43 content::RESOURCE_TYPE_LAST_TYPE, |
| 42 }; | 44 }; |
| 43 | 45 |
| 46 const size_t kResourceTypeValuesLength = arraysize(kResourceTypeValues); |
| 47 |
| 44 } | 48 } |
| 45 | 49 |
| 46 #define ARRAYEND(array) (array + arraysize(array)) | 50 #define ARRAYEND(array) (array + arraysize(array)) |
| 47 | 51 |
| 48 bool IsRelevantResourceType(ResourceType type) { | 52 bool IsRelevantResourceType(ResourceType type) { |
| 49 ResourceType* iter = | 53 ResourceType* iter = |
| 50 std::find(kResourceTypeValues, ARRAYEND(kResourceTypeValues), type); | 54 std::find(kResourceTypeValues, |
| 51 return iter != ARRAYEND(kResourceTypeValues); | 55 kResourceTypeValues + kResourceTypeValuesLength, |
| 56 type); |
| 57 return iter != (kResourceTypeValues + kResourceTypeValuesLength); |
| 52 } | 58 } |
| 53 | 59 |
| 54 const char* ResourceTypeToString(ResourceType type) { | 60 const char* ResourceTypeToString(ResourceType type) { |
| 55 ResourceType* iter = | 61 ResourceType* iter = |
| 56 std::find(kResourceTypeValues, ARRAYEND(kResourceTypeValues), type); | 62 std::find(kResourceTypeValues, |
| 57 if (iter == ARRAYEND(kResourceTypeValues)) | 63 kResourceTypeValues + kResourceTypeValuesLength, |
| 64 type); |
| 65 if (iter == (kResourceTypeValues + kResourceTypeValuesLength)) |
| 58 return "other"; | 66 return "other"; |
| 59 | 67 |
| 60 return kResourceTypeStrings[iter - kResourceTypeValues]; | 68 return kResourceTypeStrings[iter - kResourceTypeValues]; |
| 61 } | 69 } |
| 62 | 70 |
| 63 bool ParseResourceType(const std::string& type_str, | 71 bool ParseResourceType(const std::string& type_str, |
| 64 ResourceType* type) { | 72 ResourceType* type) { |
| 65 const char** iter = | 73 const char** iter = |
| 66 std::find(kResourceTypeStrings, ARRAYEND(kResourceTypeStrings), type_str); | 74 std::find(kResourceTypeStrings, |
| 67 if (iter == ARRAYEND(kResourceTypeStrings)) | 75 kResourceTypeStrings + kResourceTypeStringsLength, |
| 76 type_str); |
| 77 if (iter == (kResourceTypeStrings + kResourceTypeStringsLength)) |
| 68 return false; | 78 return false; |
| 69 *type = kResourceTypeValues[iter - kResourceTypeStrings]; | 79 *type = kResourceTypeValues[iter - kResourceTypeStrings]; |
| 70 return true; | 80 return true; |
| 71 } | 81 } |
| 72 | 82 |
| 73 } // namespace extension_web_request_api_utils | 83 } // namespace extension_web_request_api_utils |
| OLD | NEW |