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