OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 #ifndef EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_EVENT_DETAILS_H_ | |
6 #define EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_EVENT_DETAILS_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/callback_forward.h" | |
11 #include "base/macros.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/values.h" | |
14 | |
15 namespace net { | |
16 class AuthChallengeInfo; | |
17 class HttpRequestHeaders; | |
18 class HttpResponseHeaders; | |
19 class URLRequest; | |
20 } | |
21 | |
22 namespace extensions { | |
23 | |
24 // This helper class is used to construct the details for a webRequest event | |
25 // dictionary. Some keys are present on every event, others are only relevant | |
26 // for a few events. And some keys must only be added to the event details if | |
27 // requested at the registration of the event listener (in ExtraInfoSpec). | |
28 // This class provides setters that are aware of these rules. | |
29 // | |
30 // Not all keys are managed by this class. Keys that do not require a special | |
31 // treatment can be set using the generic SetBoolean / SetInteger / SetString | |
32 // methods (e.g. to set "error", "message", "redirectUrl", "stage" or "tabId"). | |
33 // | |
34 // This class should be constructed on the IO thread. It can safely be used on | |
35 // other threads, as long as there is no concurrent access. | |
36 class WebRequestEventDetails { | |
37 public: | |
38 using DeterminedFrameIdCallback = | |
39 base::Callback<void(scoped_ptr<WebRequestEventDetails>)>; | |
40 | |
41 // Create a WebRequestEventDetails with the following keys: | |
42 // - method | |
43 // - requestId | |
44 // - tabId | |
45 // - timeStamp | |
46 // - type | |
47 // - url | |
48 WebRequestEventDetails(const net::URLRequest* request, int extra_info_spec); | |
49 ~WebRequestEventDetails(); | |
50 | |
51 // Sets the following key: | |
52 // - requestBody (on demand) | |
53 void SetRequestBody(const net::URLRequest* request); | |
54 | |
55 // Sets the following key: | |
56 // - requestHeaders (on demand) | |
57 void SetRequestHeaders(const net::HttpRequestHeaders& request_headers); | |
58 | |
59 // Sets the following keys: | |
60 // - challenger | |
61 // - isProxy | |
62 // - realm | |
63 // - scheme | |
64 void SetAuthInfo(const net::AuthChallengeInfo& auth_info); | |
65 | |
66 // Sets the following keys: | |
67 // - responseHeaders (on demand) | |
68 // - statusCode | |
69 // - statusLine | |
70 void SetResponseHeaders(const net::URLRequest* request, | |
71 const net::HttpResponseHeaders* response_headers); | |
72 | |
73 // Sets the following key: | |
74 // - fromCache | |
75 // - ip | |
76 void SetResponseSource(const net::URLRequest* request); | |
77 | |
78 void SetBoolean(const std::string& key, bool value) { | |
battre
2016/01/12 13:08:03
I think the style guide still asks to put the body
robwu
2016/01/12 16:33:05
Trivial methods may be inlined. Since these SetBoo
battre
2016/01/12 19:05:21
Ok, SGTM to leave it as is.
| |
79 dict_.SetBoolean(key, value); | |
80 } | |
81 | |
82 void SetInteger(const std::string& key, int value) { | |
83 dict_.SetInteger(key, value); | |
84 } | |
85 | |
86 void SetString(const std::string& key, const std::string& value) { | |
87 dict_.SetString(key, value); | |
88 } | |
89 | |
90 // Sets the following keys using information from constructor. | |
91 // - frameId | |
92 // - parentFrameId | |
93 // This must be called from the UI thread. | |
94 void DetermineFrameIdOnUI(); | |
95 | |
96 // Sets the following keys using information from constructor. | |
97 // - frameId | |
98 // - parentFrameId | |
99 // | |
100 // This method is more expensive than DetermineFrameIdOnUI because it may | |
101 // involve thread hops, so prefer using DetermineFrameIdOnUI() when possible. | |
102 // The callback is called as soon as these IDs are determined, which can be | |
103 // synchronous or asynchronous. | |
104 // | |
105 // The caller must not use or delete this WebRequestEventDetails instance | |
106 // after calling this method. Ownership of this instance is transferred to | |
107 // |callback|. | |
108 void DetermineFrameIdOnIO(const DeterminedFrameIdCallback& callback); | |
109 | |
110 // Create an event dictionary that contains all required keys, and also the | |
111 // extra keys as specified by the |extra_info_spec| filter. | |
112 // This can be called from any thread. | |
113 scoped_ptr<base::DictionaryValue> GetFilteredDict(int extra_info_spec) const; | |
114 | |
115 // Get the internal dictionary, unfiltered. After this call, the internal | |
116 // dictionary is empty. | |
117 scoped_ptr<base::DictionaryValue> GetAndClearDict(); | |
118 | |
119 private: | |
120 void OnDeterminedFrameId(scoped_ptr<WebRequestEventDetails> self, | |
121 const DeterminedFrameIdCallback& callback, | |
122 int extension_api_frame_id, | |
123 int extension_api_parent_frame_id); | |
124 | |
125 // The details that are always included in a webRequest event object. | |
126 base::DictionaryValue dict_; | |
127 | |
128 // Extra event details: Only included when |extra_info_spec_| matches. | |
129 scoped_ptr<base::DictionaryValue> request_body_; | |
130 scoped_ptr<base::ListValue> request_headers_; | |
131 scoped_ptr<base::ListValue> response_headers_; | |
132 | |
133 int extra_info_spec_; | |
134 | |
135 // Used to determine the frameId and parentFrameId. | |
136 int render_process_id_; | |
137 int render_frame_id_; | |
138 | |
139 DISALLOW_COPY_AND_ASSIGN(WebRequestEventDetails); | |
140 }; | |
141 | |
142 } // namespace extensions | |
143 | |
144 #endif // EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_EVENT_DETAILS_H_ | |
OLD | NEW |