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 static ResourceType kResourceTypeValues[] = { |
| 28 content::RESOURCE_TYPE_MAIN_FRAME, |
| 29 content::RESOURCE_TYPE_SUB_FRAME, |
| 30 content::RESOURCE_TYPE_STYLESHEET, |
| 31 content::RESOURCE_TYPE_SCRIPT, |
| 32 content::RESOURCE_TYPE_IMAGE, |
| 33 content::RESOURCE_TYPE_OBJECT, |
| 34 content::RESOURCE_TYPE_XHR, |
| 35 content::RESOURCE_TYPE_LAST_TYPE, // represents "other" |
| 36 // 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 |
| 38 // (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 |
| 40 // entry is no longer required, this should be removed. |
| 41 content::RESOURCE_TYPE_LAST_TYPE, |
| 42 }; |
| 43 |
| 44 } |
| 45 |
| 46 #define ARRAYEND(array) (array + arraysize(array)) |
| 47 |
| 48 bool IsRelevantResourceType(ResourceType type) { |
| 49 ResourceType* iter = |
| 50 std::find(kResourceTypeValues, ARRAYEND(kResourceTypeValues), type); |
| 51 return iter != ARRAYEND(kResourceTypeValues); |
| 52 } |
| 53 |
| 54 const char* ResourceTypeToString(ResourceType type) { |
| 55 ResourceType* iter = |
| 56 std::find(kResourceTypeValues, ARRAYEND(kResourceTypeValues), type); |
| 57 if (iter == ARRAYEND(kResourceTypeValues)) |
| 58 return "other"; |
| 59 |
| 60 return kResourceTypeStrings[iter - kResourceTypeValues]; |
| 61 } |
| 62 |
| 63 bool ParseResourceType(const std::string& type_str, |
| 64 ResourceType* type) { |
| 65 const char** iter = |
| 66 std::find(kResourceTypeStrings, ARRAYEND(kResourceTypeStrings), type_str); |
| 67 if (iter == ARRAYEND(kResourceTypeStrings)) |
| 68 return false; |
| 69 *type = kResourceTypeValues[iter - kResourceTypeStrings]; |
| 70 return true; |
| 71 } |
| 72 |
| 73 } // namespace extension_web_request_api_utils |
OLD | NEW |