Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1165)

Side by Side Diff: chrome/browser/extensions/api/web_request/web_request_api.cc

Issue 10012004: Implemented proper support for checking schemes and requested resource types. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/web_request/web_request_api.h" 5 #include "chrome/browser/extensions/api/web_request/web_request_api.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/json/json_writer.h" 10 #include "base/json/json_writer.h"
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 keys::kOnBeforeRequest, 63 keys::kOnBeforeRequest,
64 keys::kOnBeforeSendHeaders, 64 keys::kOnBeforeSendHeaders,
65 keys::kOnCompleted, 65 keys::kOnCompleted,
66 keys::kOnErrorOccurred, 66 keys::kOnErrorOccurred,
67 keys::kOnSendHeaders, 67 keys::kOnSendHeaders,
68 keys::kOnAuthRequired, 68 keys::kOnAuthRequired,
69 keys::kOnResponseStarted, 69 keys::kOnResponseStarted,
70 keys::kOnHeadersReceived, 70 keys::kOnHeadersReceived,
71 }; 71 };
72 72
73 static const char* kResourceTypeStrings[] = {
battre 2012/04/05 15:01:11 moved this into web_request_api_helpers.cc so that
74 "main_frame",
75 "sub_frame",
76 "stylesheet",
77 "script",
78 "image",
79 "object",
80 "xmlhttprequest",
81 "other",
82 "other",
83 };
84
85 static ResourceType::Type kResourceTypeValues[] = {
86 ResourceType::MAIN_FRAME,
87 ResourceType::SUB_FRAME,
88 ResourceType::STYLESHEET,
89 ResourceType::SCRIPT,
90 ResourceType::IMAGE,
91 ResourceType::OBJECT,
92 ResourceType::XHR,
93 ResourceType::LAST_TYPE, // represents "other"
94 // TODO(jochen): We duplicate the last entry, so the array's size is not a
95 // power of two. If it is, this triggers a bug in gcc 4.4 in Release builds
96 // (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43949). Once we use a version
97 // of gcc with this bug fixed, or the array is changed so this duplicate
98 // entry is no longer required, this should be removed.
99 ResourceType::LAST_TYPE,
100 };
101
102 COMPILE_ASSERT(
103 arraysize(kResourceTypeStrings) == arraysize(kResourceTypeValues),
104 keep_resource_types_in_sync);
105
106 #define ARRAYEND(array) (array + arraysize(array)) 73 #define ARRAYEND(array) (array + arraysize(array))
107 74
108 // Returns the frame ID as it will be passed to the extension: 75 // Returns the frame ID as it will be passed to the extension:
109 // 0 if the navigation happens in the main frame, or the frame ID 76 // 0 if the navigation happens in the main frame, or the frame ID
110 // modulo 32 bits otherwise. 77 // modulo 32 bits otherwise.
111 // Keep this in sync with the GetFrameId() function in 78 // Keep this in sync with the GetFrameId() function in
112 // extension_webnavigation_api.cc. 79 // extension_webnavigation_api.cc.
113 int GetFrameId(bool is_main_frame, int64 frame_id) { 80 int GetFrameId(bool is_main_frame, int64 frame_id) {
114 return is_main_frame ? 0 : static_cast<int>(frame_id); 81 return is_main_frame ? 0 : static_cast<int>(frame_id);
115 } 82 }
(...skipping 21 matching lines...) Expand all
137 return extension_info_map->process_map().Contains(info->GetChildID()); 104 return extension_info_map->process_map().Contains(info->GetChildID());
138 } 105 }
139 106
140 bool CanExtensionAccessURL(const Extension* extension, const GURL& url) { 107 bool CanExtensionAccessURL(const Extension* extension, const GURL& url) {
141 // about: URLs are not covered in host permissions, but are allowed anyway. 108 // about: URLs are not covered in host permissions, but are allowed anyway.
142 return (url.SchemeIs(chrome::kAboutScheme) || 109 return (url.SchemeIs(chrome::kAboutScheme) ||
143 extension->HasHostPermission(url) || 110 extension->HasHostPermission(url) ||
144 url.GetOrigin() == extension->url()); 111 url.GetOrigin() == extension->url());
145 } 112 }
146 113
147 const char* ResourceTypeToString(ResourceType::Type type) {
148 ResourceType::Type* iter =
149 std::find(kResourceTypeValues, ARRAYEND(kResourceTypeValues), type);
150 if (iter == ARRAYEND(kResourceTypeValues))
151 return "other";
152
153 return kResourceTypeStrings[iter - kResourceTypeValues];
154 }
155
156 bool ParseResourceType(const std::string& type_str,
157 ResourceType::Type* type) {
158 const char** iter =
159 std::find(kResourceTypeStrings, ARRAYEND(kResourceTypeStrings), type_str);
160 if (iter == ARRAYEND(kResourceTypeStrings))
161 return false;
162 *type = kResourceTypeValues[iter - kResourceTypeStrings];
163 return true;
164 }
165
166 void ExtractRequestInfoDetails(net::URLRequest* request, 114 void ExtractRequestInfoDetails(net::URLRequest* request,
167 bool* is_main_frame, 115 bool* is_main_frame,
168 int64* frame_id, 116 int64* frame_id,
169 bool* parent_is_main_frame, 117 bool* parent_is_main_frame,
170 int64* parent_frame_id, 118 int64* parent_frame_id,
171 int* tab_id, 119 int* tab_id,
172 int* window_id, 120 int* window_id,
173 ResourceType::Type* resource_type) { 121 ResourceType::Type* resource_type) {
174 if (!request->GetUserData(NULL)) 122 if (!request->GetUserData(NULL))
175 return; 123 return;
176 124
177 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); 125 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
178 ExtensionTabIdMap::GetInstance()->GetTabAndWindowId( 126 ExtensionTabIdMap::GetInstance()->GetTabAndWindowId(
179 info->GetChildID(), info->GetRouteID(), tab_id, window_id); 127 info->GetChildID(), info->GetRouteID(), tab_id, window_id);
180 *frame_id = info->GetFrameID(); 128 *frame_id = info->GetFrameID();
181 *is_main_frame = info->IsMainFrame(); 129 *is_main_frame = info->IsMainFrame();
182 *parent_frame_id = info->GetParentFrameID(); 130 *parent_frame_id = info->GetParentFrameID();
183 *parent_is_main_frame = info->ParentIsMainFrame(); 131 *parent_is_main_frame = info->ParentIsMainFrame();
184 132
185 // Restrict the resource type to the values we care about. 133 // Restrict the resource type to the values we care about.
186 ResourceType::Type* iter = 134 if (helpers::IsRelevantResourceType(info->GetResourceType()))
187 std::find(kResourceTypeValues, ARRAYEND(kResourceTypeValues), 135 *resource_type = info->GetResourceType();
188 info->GetResourceType()); 136 else
189 *resource_type = (iter != ARRAYEND(kResourceTypeValues)) ? 137 *resource_type = ResourceType::LAST_TYPE;
190 *iter : ResourceType::LAST_TYPE;
191 } 138 }
192 139
193 // Extracts from |request| information for the keys requestId, url, method, 140 // Extracts from |request| information for the keys requestId, url, method,
194 // frameId, tabId, type, and timeStamp and writes these into |out| to be passed 141 // frameId, tabId, type, and timeStamp and writes these into |out| to be passed
195 // on to extensions. 142 // on to extensions.
196 void ExtractRequestInfo(net::URLRequest* request, DictionaryValue* out) { 143 void ExtractRequestInfo(net::URLRequest* request, DictionaryValue* out) {
197 bool is_main_frame = false; 144 bool is_main_frame = false;
198 int64 frame_id = -1; 145 int64 frame_id = -1;
199 bool parent_is_main_frame = false; 146 bool parent_is_main_frame = false;
200 int64 parent_frame_id = -1; 147 int64 parent_frame_id = -1;
201 int frame_id_for_extension = -1; 148 int frame_id_for_extension = -1;
202 int parent_frame_id_for_extension = -1; 149 int parent_frame_id_for_extension = -1;
203 int tab_id = -1; 150 int tab_id = -1;
204 int window_id = -1; 151 int window_id = -1;
205 ResourceType::Type resource_type = ResourceType::LAST_TYPE; 152 ResourceType::Type resource_type = ResourceType::LAST_TYPE;
206 ExtractRequestInfoDetails(request, &is_main_frame, &frame_id, 153 ExtractRequestInfoDetails(request, &is_main_frame, &frame_id,
207 &parent_is_main_frame, &parent_frame_id, &tab_id, 154 &parent_is_main_frame, &parent_frame_id, &tab_id,
208 &window_id, &resource_type); 155 &window_id, &resource_type);
209 frame_id_for_extension = GetFrameId(is_main_frame, frame_id); 156 frame_id_for_extension = GetFrameId(is_main_frame, frame_id);
210 parent_frame_id_for_extension = GetFrameId(parent_is_main_frame, 157 parent_frame_id_for_extension = GetFrameId(parent_is_main_frame,
211 parent_frame_id); 158 parent_frame_id);
212 159
213 out->SetString(keys::kRequestIdKey, 160 out->SetString(keys::kRequestIdKey,
214 base::Uint64ToString(request->identifier())); 161 base::Uint64ToString(request->identifier()));
215 out->SetString(keys::kUrlKey, request->url().spec()); 162 out->SetString(keys::kUrlKey, request->url().spec());
216 out->SetString(keys::kMethodKey, request->method()); 163 out->SetString(keys::kMethodKey, request->method());
217 out->SetInteger(keys::kFrameIdKey, frame_id_for_extension); 164 out->SetInteger(keys::kFrameIdKey, frame_id_for_extension);
218 out->SetInteger(keys::kParentFrameIdKey, parent_frame_id_for_extension); 165 out->SetInteger(keys::kParentFrameIdKey, parent_frame_id_for_extension);
219 out->SetInteger(keys::kTabIdKey, tab_id); 166 out->SetInteger(keys::kTabIdKey, tab_id);
220 out->SetString(keys::kTypeKey, ResourceTypeToString(resource_type)); 167 out->SetString(keys::kTypeKey, helpers::ResourceTypeToString(resource_type));
221 out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000); 168 out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000);
222 } 169 }
223 170
224 // Converts a HttpHeaders dictionary to a |name|, |value| pair. Returns 171 // Converts a HttpHeaders dictionary to a |name|, |value| pair. Returns
225 // true if successful. 172 // true if successful.
226 bool FromHeaderDictionary(const DictionaryValue* header_value, 173 bool FromHeaderDictionary(const DictionaryValue* header_value,
227 std::string* name, 174 std::string* name,
228 std::string* value) { 175 std::string* value) {
229 if (!header_value->GetString(keys::kHeaderNameKey, name)) 176 if (!header_value->GetString(keys::kHeaderNameKey, name))
230 return false; 177 return false;
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 urls.AddPattern(pattern); 374 urls.AddPattern(pattern);
428 } 375 }
429 } else if (*key == "types") { 376 } else if (*key == "types") {
430 ListValue* types_value = NULL; 377 ListValue* types_value = NULL;
431 if (!value.GetList("types", &types_value)) 378 if (!value.GetList("types", &types_value))
432 return false; 379 return false;
433 for (size_t i = 0; i < types_value->GetSize(); ++i) { 380 for (size_t i = 0; i < types_value->GetSize(); ++i) {
434 std::string type_str; 381 std::string type_str;
435 ResourceType::Type type; 382 ResourceType::Type type;
436 if (!types_value->GetString(i, &type_str) || 383 if (!types_value->GetString(i, &type_str) ||
437 !ParseResourceType(type_str, &type)) 384 !helpers::ParseResourceType(type_str, &type))
438 return false; 385 return false;
439 types.push_back(type); 386 types.push_back(type);
440 } 387 }
441 } else if (*key == "tabId") { 388 } else if (*key == "tabId") {
442 if (!value.GetInteger("tabId", &tab_id)) 389 if (!value.GetInteger("tabId", &tab_id))
443 return false; 390 return false;
444 } else if (*key == "windowId") { 391 } else if (*key == "windowId") {
445 if (!value.GetInteger("windowId", &window_id)) 392 if (!value.GetInteger("windowId", &window_id))
446 return false; 393 return false;
447 } else { 394 } else {
(...skipping 1313 matching lines...) Expand 10 before | Expand all | Expand 10 after
1761 } else if ((*it)->name().find("AdBlock") != std::string::npos) { 1708 } else if ((*it)->name().find("AdBlock") != std::string::npos) {
1762 adblock = true; 1709 adblock = true;
1763 } else { 1710 } else {
1764 other = true; 1711 other = true;
1765 } 1712 }
1766 } 1713 }
1767 } 1714 }
1768 1715
1769 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other)); 1716 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other));
1770 } 1717 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698