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

Side by Side Diff: chrome/browser/extensions/api/web_navigation/web_navigation_api.cc

Issue 10514013: Filtered events. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: split out event_filter changes Created 8 years, 6 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 // Implements the Chrome Extensions WebNavigation API. 5 // Implements the Chrome Extensions WebNavigation API.
6 6
7 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h" 7 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h"
8 8
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
11 #include "base/string_number_conversions.h" 11 #include "base/string_number_conversions.h"
12 #include "base/time.h" 12 #include "base/time.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_consta nts.h" 14 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_consta nts.h"
15 #include "chrome/browser/extensions/extension_event_router.h" 15 #include "chrome/browser/extensions/extension_event_router.h"
16 #include "chrome/browser/extensions/extension_tab_util.h" 16 #include "chrome/browser/extensions/extension_tab_util.h"
17 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/tab_contents/retargeting_details.h" 18 #include "chrome/browser/tab_contents/retargeting_details.h"
19 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 19 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
20 #include "chrome/browser/view_type_utils.h" 20 #include "chrome/browser/view_type_utils.h"
21 #include "chrome/common/chrome_notification_types.h" 21 #include "chrome/common/chrome_notification_types.h"
22 #include "chrome/common/extensions/event_filtering_info.h"
22 #include "chrome/common/url_constants.h" 23 #include "chrome/common/url_constants.h"
23 #include "content/public/browser/resource_request_details.h" 24 #include "content/public/browser/resource_request_details.h"
24 #include "content/public/browser/navigation_details.h" 25 #include "content/public/browser/navigation_details.h"
25 #include "content/public/browser/notification_service.h" 26 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/notification_types.h" 27 #include "content/public/browser/notification_types.h"
27 #include "content/public/browser/render_view_host.h" 28 #include "content/public/browser/render_view_host.h"
28 #include "content/public/browser/web_contents.h" 29 #include "content/public/browser/web_contents.h"
29 #include "net/base/net_errors.h" 30 #include "net/base/net_errors.h"
30 31
31 using content::BrowserContext; 32 using content::BrowserContext;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 } 64 }
64 65
65 // Returns |time| as milliseconds since the epoch. 66 // Returns |time| as milliseconds since the epoch.
66 double MilliSecondsFromTime(const base::Time& time) { 67 double MilliSecondsFromTime(const base::Time& time) {
67 return 1000 * time.ToDoubleT(); 68 return 1000 * time.ToDoubleT();
68 } 69 }
69 70
70 // Dispatches events to the extension message service. 71 // Dispatches events to the extension message service.
71 void DispatchEvent(BrowserContext* browser_context, 72 void DispatchEvent(BrowserContext* browser_context,
72 const char* event_name, 73 const char* event_name,
73 const std::string& json_args) { 74 const ListValue& args) {
75 std::string json_args;
76 base::JSONWriter::Write(&args, &json_args);
77
78 DictionaryValue* dict;
79 args.GetDictionary(0, &dict);
80 std::string url_string;
81 dict->GetString(std::string(keys::kUrlKey), &url_string);
82 extensions::EventFilteringInfo info;
83 info.SetURL(GURL(url_string));
Matt Perry 2012/06/13 01:24:27 This feels dirty. Let's just pass the URL in as a
koz (OOO until 15th September) 2012/06/14 02:15:55 Cool, done.
84
74 Profile* profile = Profile::FromBrowserContext(browser_context); 85 Profile* profile = Profile::FromBrowserContext(browser_context);
75 if (profile && profile->GetExtensionEventRouter()) { 86 if (profile && profile->GetExtensionEventRouter()) {
76 profile->GetExtensionEventRouter()->DispatchEventToRenderers( 87 profile->GetExtensionEventRouter()->DispatchEventToRenderers(
77 event_name, json_args, profile, GURL()); 88 event_name, json_args, profile, GURL(), info);
78 } 89 }
79 } 90 }
80 91
81 // Constructs and dispatches an onBeforeNavigate event. 92 // Constructs and dispatches an onBeforeNavigate event.
82 void DispatchOnBeforeNavigate(WebContents* web_contents, 93 void DispatchOnBeforeNavigate(WebContents* web_contents,
83 int64 frame_id, 94 int64 frame_id,
84 bool is_main_frame, 95 bool is_main_frame,
85 const GURL& validated_url) { 96 const GURL& validated_url) {
86 ListValue args; 97 ListValue args;
87 DictionaryValue* dict = new DictionaryValue(); 98 DictionaryValue* dict = new DictionaryValue();
88 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); 99 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents));
89 dict->SetString(keys::kUrlKey, validated_url.spec()); 100 dict->SetString(keys::kUrlKey, validated_url.spec());
90 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); 101 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
91 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 102 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
92 args.Append(dict); 103 args.Append(dict);
93 104
94 std::string json_args;
95 base::JSONWriter::Write(&args, &json_args);
96 DispatchEvent(web_contents->GetBrowserContext(), 105 DispatchEvent(web_contents->GetBrowserContext(),
97 keys::kOnBeforeNavigate, 106 keys::kOnBeforeNavigate,
98 json_args); 107 args);
99 } 108 }
100 109
101 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated 110 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated
102 // event. 111 // event.
103 void DispatchOnCommitted(const char* event_name, 112 void DispatchOnCommitted(const char* event_name,
104 WebContents* web_contents, 113 WebContents* web_contents,
105 int64 frame_id, 114 int64 frame_id,
106 bool is_main_frame, 115 bool is_main_frame,
107 const GURL& url, 116 const GURL& url,
108 content::PageTransition transition_type) { 117 content::PageTransition transition_type) {
(...skipping 11 matching lines...) Expand all
120 if (transition_type & content::PAGE_TRANSITION_SERVER_REDIRECT) 129 if (transition_type & content::PAGE_TRANSITION_SERVER_REDIRECT)
121 qualifiers->Append(Value::CreateStringValue("server_redirect")); 130 qualifiers->Append(Value::CreateStringValue("server_redirect"));
122 if (transition_type & content::PAGE_TRANSITION_FORWARD_BACK) 131 if (transition_type & content::PAGE_TRANSITION_FORWARD_BACK)
123 qualifiers->Append(Value::CreateStringValue("forward_back")); 132 qualifiers->Append(Value::CreateStringValue("forward_back"));
124 if (transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR) 133 if (transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR)
125 qualifiers->Append(Value::CreateStringValue("from_address_bar")); 134 qualifiers->Append(Value::CreateStringValue("from_address_bar"));
126 dict->Set(keys::kTransitionQualifiersKey, qualifiers); 135 dict->Set(keys::kTransitionQualifiersKey, qualifiers);
127 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 136 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
128 args.Append(dict); 137 args.Append(dict);
129 138
130 std::string json_args; 139 DispatchEvent(web_contents->GetBrowserContext(), event_name, args);
131 base::JSONWriter::Write(&args, &json_args);
132 DispatchEvent(web_contents->GetBrowserContext(), event_name, json_args);
133 } 140 }
134 141
135 // Constructs and dispatches an onDOMContentLoaded event. 142 // Constructs and dispatches an onDOMContentLoaded event.
136 void DispatchOnDOMContentLoaded(WebContents* web_contents, 143 void DispatchOnDOMContentLoaded(WebContents* web_contents,
137 const GURL& url, 144 const GURL& url,
138 bool is_main_frame, 145 bool is_main_frame,
139 int64 frame_id) { 146 int64 frame_id) {
140 ListValue args; 147 ListValue args;
141 DictionaryValue* dict = new DictionaryValue(); 148 DictionaryValue* dict = new DictionaryValue();
142 dict->SetInteger(keys::kTabIdKey, 149 dict->SetInteger(keys::kTabIdKey,
143 ExtensionTabUtil::GetTabId(web_contents)); 150 ExtensionTabUtil::GetTabId(web_contents));
144 dict->SetString(keys::kUrlKey, url.spec()); 151 dict->SetString(keys::kUrlKey, url.spec());
145 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); 152 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
146 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 153 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
147 args.Append(dict); 154 args.Append(dict);
148 155
149 std::string json_args;
150 base::JSONWriter::Write(&args, &json_args);
151 DispatchEvent(web_contents->GetBrowserContext(), 156 DispatchEvent(web_contents->GetBrowserContext(),
152 keys::kOnDOMContentLoaded, 157 keys::kOnDOMContentLoaded,
153 json_args); 158 args);
154 } 159 }
155 160
156 // Constructs and dispatches an onCompleted event. 161 // Constructs and dispatches an onCompleted event.
157 void DispatchOnCompleted(WebContents* web_contents, 162 void DispatchOnCompleted(WebContents* web_contents,
158 const GURL& url, 163 const GURL& url,
159 bool is_main_frame, 164 bool is_main_frame,
160 int64 frame_id) { 165 int64 frame_id) {
161 ListValue args; 166 ListValue args;
162 DictionaryValue* dict = new DictionaryValue(); 167 DictionaryValue* dict = new DictionaryValue();
163 dict->SetInteger(keys::kTabIdKey, 168 dict->SetInteger(keys::kTabIdKey,
164 ExtensionTabUtil::GetTabId(web_contents)); 169 ExtensionTabUtil::GetTabId(web_contents));
165 dict->SetString(keys::kUrlKey, url.spec()); 170 dict->SetString(keys::kUrlKey, url.spec());
166 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); 171 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
167 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 172 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
168 args.Append(dict); 173 args.Append(dict);
169 174
170 std::string json_args;
171 base::JSONWriter::Write(&args, &json_args);
172 DispatchEvent(web_contents->GetBrowserContext(), 175 DispatchEvent(web_contents->GetBrowserContext(),
173 keys::kOnCompleted, json_args); 176 keys::kOnCompleted, args);
174 } 177 }
175 178
176 // Constructs and dispatches an onCreatedNavigationTarget event. 179 // Constructs and dispatches an onCreatedNavigationTarget event.
177 void DispatchOnCreatedNavigationTarget( 180 void DispatchOnCreatedNavigationTarget(
178 WebContents* web_contents, 181 WebContents* web_contents,
179 BrowserContext* browser_context, 182 BrowserContext* browser_context,
180 int64 source_frame_id, 183 int64 source_frame_id,
181 bool source_frame_is_main_frame, 184 bool source_frame_is_main_frame,
182 WebContents* target_web_contents, 185 WebContents* target_web_contents,
183 const GURL& target_url) { 186 const GURL& target_url) {
184 // Check that the tab is already inserted into a tab strip model. This code 187 // Check that the tab is already inserted into a tab strip model. This code
185 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. 188 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab.
186 DCHECK(ExtensionTabUtil::GetTabById( 189 DCHECK(ExtensionTabUtil::GetTabById(
187 ExtensionTabUtil::GetTabId(target_web_contents), 190 ExtensionTabUtil::GetTabId(target_web_contents),
188 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()), 191 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()),
189 false, NULL, NULL, NULL, NULL)); 192 false, NULL, NULL, NULL, NULL));
190 193
191 ListValue args; 194 ListValue args;
192 DictionaryValue* dict = new DictionaryValue(); 195 DictionaryValue* dict = new DictionaryValue();
193 dict->SetInteger(keys::kSourceTabIdKey, 196 dict->SetInteger(keys::kSourceTabIdKey,
194 ExtensionTabUtil::GetTabId(web_contents)); 197 ExtensionTabUtil::GetTabId(web_contents));
195 dict->SetInteger(keys::kSourceFrameIdKey, 198 dict->SetInteger(keys::kSourceFrameIdKey,
196 GetFrameId(source_frame_is_main_frame, source_frame_id)); 199 GetFrameId(source_frame_is_main_frame, source_frame_id));
197 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); 200 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec());
198 dict->SetInteger(keys::kTabIdKey, 201 dict->SetInteger(keys::kTabIdKey,
199 ExtensionTabUtil::GetTabId(target_web_contents)); 202 ExtensionTabUtil::GetTabId(target_web_contents));
200 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 203 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
201 args.Append(dict); 204 args.Append(dict);
202 205
203 std::string json_args;
204 base::JSONWriter::Write(&args, &json_args);
205 DispatchEvent( 206 DispatchEvent(
206 browser_context, keys::kOnCreatedNavigationTarget, json_args); 207 browser_context, keys::kOnCreatedNavigationTarget, args);
207 } 208 }
208 209
209 // Constructs and dispatches an onErrorOccurred event. 210 // Constructs and dispatches an onErrorOccurred event.
210 void DispatchOnErrorOccurred(WebContents* web_contents, 211 void DispatchOnErrorOccurred(WebContents* web_contents,
211 const GURL& url, 212 const GURL& url,
212 int64 frame_id, 213 int64 frame_id,
213 bool is_main_frame, 214 bool is_main_frame,
214 int error_code) { 215 int error_code) {
215 ListValue args; 216 ListValue args;
216 DictionaryValue* dict = new DictionaryValue(); 217 DictionaryValue* dict = new DictionaryValue();
217 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); 218 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents));
218 dict->SetString(keys::kUrlKey, url.spec()); 219 dict->SetString(keys::kUrlKey, url.spec());
219 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); 220 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
220 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); 221 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code));
221 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 222 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
222 args.Append(dict); 223 args.Append(dict);
223 224
224 std::string json_args;
225 base::JSONWriter::Write(&args, &json_args);
226 DispatchEvent(web_contents->GetBrowserContext(), 225 DispatchEvent(web_contents->GetBrowserContext(),
227 keys::kOnErrorOccurred, 226 keys::kOnErrorOccurred,
228 json_args); 227 args);
229 } 228 }
230 229
231 } // namespace 230 } // namespace
232 231
233 232
234 // FrameNavigationState ------------------------------------------------------- 233 // FrameNavigationState -------------------------------------------------------
235 234
236 // static 235 // static
237 bool FrameNavigationState::allow_extension_scheme_ = false; 236 bool FrameNavigationState::allow_extension_scheme_ = false;
238 237
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after
803 frameDict->SetBoolean( 802 frameDict->SetBoolean(
804 keys::kErrorOccurredKey, 803 keys::kErrorOccurredKey,
805 navigation_state.GetErrorOccurredInFrame(*frame)); 804 navigation_state.GetErrorOccurredInFrame(*frame));
806 resultList->Append(frameDict); 805 resultList->Append(frameDict);
807 } 806 }
808 result_.reset(resultList); 807 result_.reset(resultList);
809 return true; 808 return true;
810 } 809 }
811 810
812 } // namespace extensions 811 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698