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

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: rebase, reland Created 8 years, 5 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.h" 19 #include "chrome/browser/ui/tab_contents/tab_contents.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/api/web_navigation.h" 22 #include "chrome/common/extensions/api/web_navigation.h"
23 #include "chrome/common/extensions/event_filtering_info.h"
23 #include "chrome/common/url_constants.h" 24 #include "chrome/common/url_constants.h"
24 #include "content/public/browser/resource_request_details.h" 25 #include "content/public/browser/resource_request_details.h"
25 #include "content/public/browser/navigation_details.h" 26 #include "content/public/browser/navigation_details.h"
26 #include "content/public/browser/notification_service.h" 27 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/notification_types.h" 28 #include "content/public/browser/notification_types.h"
28 #include "content/public/browser/render_view_host.h" 29 #include "content/public/browser/render_view_host.h"
29 #include "content/public/browser/web_contents.h" 30 #include "content/public/browser/web_contents.h"
30 #include "net/base/net_errors.h" 31 #include "net/base/net_errors.h"
31 32
32 namespace GetFrame = extensions::api::web_navigation::GetFrame; 33 namespace GetFrame = extensions::api::web_navigation::GetFrame;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 } 68 }
68 69
69 // Returns |time| as milliseconds since the epoch. 70 // Returns |time| as milliseconds since the epoch.
70 double MilliSecondsFromTime(const base::Time& time) { 71 double MilliSecondsFromTime(const base::Time& time) {
71 return 1000 * time.ToDoubleT(); 72 return 1000 * time.ToDoubleT();
72 } 73 }
73 74
74 // Dispatches events to the extension message service. 75 // Dispatches events to the extension message service.
75 void DispatchEvent(BrowserContext* browser_context, 76 void DispatchEvent(BrowserContext* browser_context,
76 const char* event_name, 77 const char* event_name,
77 const std::string& json_args) { 78 const ListValue& args,
79 const GURL& url) {
80 std::string json_args;
81 base::JSONWriter::Write(&args, &json_args);
82
83 extensions::EventFilteringInfo info;
84 info.SetURL(url);
85
78 Profile* profile = Profile::FromBrowserContext(browser_context); 86 Profile* profile = Profile::FromBrowserContext(browser_context);
79 if (profile && profile->GetExtensionEventRouter()) { 87 if (profile && profile->GetExtensionEventRouter()) {
80 profile->GetExtensionEventRouter()->DispatchEventToRenderers( 88 profile->GetExtensionEventRouter()->DispatchEventToRenderers(
81 event_name, json_args, profile, GURL()); 89 event_name, json_args, profile, GURL(), info);
82 } 90 }
83 } 91 }
84 92
85 // Constructs and dispatches an onBeforeNavigate event. 93 // Constructs and dispatches an onBeforeNavigate event.
86 void DispatchOnBeforeNavigate(WebContents* web_contents, 94 void DispatchOnBeforeNavigate(WebContents* web_contents,
87 int64 frame_id, 95 int64 frame_id,
88 bool is_main_frame, 96 bool is_main_frame,
89 const GURL& validated_url) { 97 const GURL& validated_url) {
90 ListValue args; 98 ListValue args;
91 DictionaryValue* dict = new DictionaryValue(); 99 DictionaryValue* dict = new DictionaryValue();
92 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); 100 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents));
93 dict->SetString(keys::kUrlKey, validated_url.spec()); 101 dict->SetString(keys::kUrlKey, validated_url.spec());
94 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); 102 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
95 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 103 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
96 args.Append(dict); 104 args.Append(dict);
97 105
98 std::string json_args;
99 base::JSONWriter::Write(&args, &json_args);
100 DispatchEvent(web_contents->GetBrowserContext(), 106 DispatchEvent(web_contents->GetBrowserContext(),
101 keys::kOnBeforeNavigate, 107 keys::kOnBeforeNavigate,
102 json_args); 108 args,
109 validated_url);
103 } 110 }
104 111
105 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated 112 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated
106 // event. 113 // event.
107 void DispatchOnCommitted(const char* event_name, 114 void DispatchOnCommitted(const char* event_name,
108 WebContents* web_contents, 115 WebContents* web_contents,
109 int64 frame_id, 116 int64 frame_id,
110 bool is_main_frame, 117 bool is_main_frame,
111 const GURL& url, 118 const GURL& url,
112 content::PageTransition transition_type) { 119 content::PageTransition transition_type) {
(...skipping 11 matching lines...) Expand all
124 if (transition_type & content::PAGE_TRANSITION_SERVER_REDIRECT) 131 if (transition_type & content::PAGE_TRANSITION_SERVER_REDIRECT)
125 qualifiers->Append(Value::CreateStringValue("server_redirect")); 132 qualifiers->Append(Value::CreateStringValue("server_redirect"));
126 if (transition_type & content::PAGE_TRANSITION_FORWARD_BACK) 133 if (transition_type & content::PAGE_TRANSITION_FORWARD_BACK)
127 qualifiers->Append(Value::CreateStringValue("forward_back")); 134 qualifiers->Append(Value::CreateStringValue("forward_back"));
128 if (transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR) 135 if (transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR)
129 qualifiers->Append(Value::CreateStringValue("from_address_bar")); 136 qualifiers->Append(Value::CreateStringValue("from_address_bar"));
130 dict->Set(keys::kTransitionQualifiersKey, qualifiers); 137 dict->Set(keys::kTransitionQualifiersKey, qualifiers);
131 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 138 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
132 args.Append(dict); 139 args.Append(dict);
133 140
134 std::string json_args; 141 DispatchEvent(web_contents->GetBrowserContext(), event_name, args, url);
135 base::JSONWriter::Write(&args, &json_args);
136 DispatchEvent(web_contents->GetBrowserContext(), event_name, json_args);
137 } 142 }
138 143
139 // Constructs and dispatches an onDOMContentLoaded event. 144 // Constructs and dispatches an onDOMContentLoaded event.
140 void DispatchOnDOMContentLoaded(WebContents* web_contents, 145 void DispatchOnDOMContentLoaded(WebContents* web_contents,
141 const GURL& url, 146 const GURL& url,
142 bool is_main_frame, 147 bool is_main_frame,
143 int64 frame_id) { 148 int64 frame_id) {
144 ListValue args; 149 ListValue args;
145 DictionaryValue* dict = new DictionaryValue(); 150 DictionaryValue* dict = new DictionaryValue();
146 dict->SetInteger(keys::kTabIdKey, 151 dict->SetInteger(keys::kTabIdKey,
147 ExtensionTabUtil::GetTabId(web_contents)); 152 ExtensionTabUtil::GetTabId(web_contents));
148 dict->SetString(keys::kUrlKey, url.spec()); 153 dict->SetString(keys::kUrlKey, url.spec());
149 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); 154 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
150 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 155 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
151 args.Append(dict); 156 args.Append(dict);
152 157
153 std::string json_args;
154 base::JSONWriter::Write(&args, &json_args);
155 DispatchEvent(web_contents->GetBrowserContext(), 158 DispatchEvent(web_contents->GetBrowserContext(),
156 keys::kOnDOMContentLoaded, 159 keys::kOnDOMContentLoaded,
157 json_args); 160 args,
161 url);
158 } 162 }
159 163
160 // Constructs and dispatches an onCompleted event. 164 // Constructs and dispatches an onCompleted event.
161 void DispatchOnCompleted(WebContents* web_contents, 165 void DispatchOnCompleted(WebContents* web_contents,
162 const GURL& url, 166 const GURL& url,
163 bool is_main_frame, 167 bool is_main_frame,
164 int64 frame_id) { 168 int64 frame_id) {
165 ListValue args; 169 ListValue args;
166 DictionaryValue* dict = new DictionaryValue(); 170 DictionaryValue* dict = new DictionaryValue();
167 dict->SetInteger(keys::kTabIdKey, 171 dict->SetInteger(keys::kTabIdKey,
168 ExtensionTabUtil::GetTabId(web_contents)); 172 ExtensionTabUtil::GetTabId(web_contents));
169 dict->SetString(keys::kUrlKey, url.spec()); 173 dict->SetString(keys::kUrlKey, url.spec());
170 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); 174 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
171 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 175 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
172 args.Append(dict); 176 args.Append(dict);
173 177
174 std::string json_args; 178 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnCompleted, args,
175 base::JSONWriter::Write(&args, &json_args); 179 url);
176 DispatchEvent(web_contents->GetBrowserContext(),
177 keys::kOnCompleted, json_args);
178 } 180 }
179 181
180 // Constructs and dispatches an onCreatedNavigationTarget event. 182 // Constructs and dispatches an onCreatedNavigationTarget event.
181 void DispatchOnCreatedNavigationTarget( 183 void DispatchOnCreatedNavigationTarget(
182 WebContents* web_contents, 184 WebContents* web_contents,
183 BrowserContext* browser_context, 185 BrowserContext* browser_context,
184 int64 source_frame_id, 186 int64 source_frame_id,
185 bool source_frame_is_main_frame, 187 bool source_frame_is_main_frame,
186 WebContents* target_web_contents, 188 WebContents* target_web_contents,
187 const GURL& target_url) { 189 const GURL& target_url) {
188 // Check that the tab is already inserted into a tab strip model. This code 190 // Check that the tab is already inserted into a tab strip model. This code
189 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. 191 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab.
190 DCHECK(ExtensionTabUtil::GetTabById( 192 DCHECK(ExtensionTabUtil::GetTabById(
191 ExtensionTabUtil::GetTabId(target_web_contents), 193 ExtensionTabUtil::GetTabId(target_web_contents),
192 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()), 194 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()),
193 false, NULL, NULL, NULL, NULL)); 195 false, NULL, NULL, NULL, NULL));
194 196
195 ListValue args; 197 ListValue args;
196 DictionaryValue* dict = new DictionaryValue(); 198 DictionaryValue* dict = new DictionaryValue();
197 dict->SetInteger(keys::kSourceTabIdKey, 199 dict->SetInteger(keys::kSourceTabIdKey,
198 ExtensionTabUtil::GetTabId(web_contents)); 200 ExtensionTabUtil::GetTabId(web_contents));
199 dict->SetInteger(keys::kSourceFrameIdKey, 201 dict->SetInteger(keys::kSourceFrameIdKey,
200 GetFrameId(source_frame_is_main_frame, source_frame_id)); 202 GetFrameId(source_frame_is_main_frame, source_frame_id));
201 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); 203 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec());
202 dict->SetInteger(keys::kTabIdKey, 204 dict->SetInteger(keys::kTabIdKey,
203 ExtensionTabUtil::GetTabId(target_web_contents)); 205 ExtensionTabUtil::GetTabId(target_web_contents));
204 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 206 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
205 args.Append(dict); 207 args.Append(dict);
206 208
207 std::string json_args; 209 DispatchEvent(browser_context, keys::kOnCreatedNavigationTarget, args,
208 base::JSONWriter::Write(&args, &json_args); 210 target_url);
209 DispatchEvent(
210 browser_context, keys::kOnCreatedNavigationTarget, json_args);
211 } 211 }
212 212
213 // Constructs and dispatches an onErrorOccurred event. 213 // Constructs and dispatches an onErrorOccurred event.
214 void DispatchOnErrorOccurred(WebContents* web_contents, 214 void DispatchOnErrorOccurred(WebContents* web_contents,
215 const GURL& url, 215 const GURL& url,
216 int64 frame_id, 216 int64 frame_id,
217 bool is_main_frame, 217 bool is_main_frame,
218 int error_code) { 218 int error_code) {
219 ListValue args; 219 ListValue args;
220 DictionaryValue* dict = new DictionaryValue(); 220 DictionaryValue* dict = new DictionaryValue();
221 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); 221 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents));
222 dict->SetString(keys::kUrlKey, url.spec()); 222 dict->SetString(keys::kUrlKey, url.spec());
223 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); 223 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
224 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); 224 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code));
225 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 225 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
226 args.Append(dict); 226 args.Append(dict);
227 227
228 std::string json_args; 228 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnErrorOccurred,
229 base::JSONWriter::Write(&args, &json_args); 229 args, url);
230 DispatchEvent(web_contents->GetBrowserContext(),
231 keys::kOnErrorOccurred,
232 json_args);
233 } 230 }
234 231
235 } // namespace 232 } // namespace
236 233
237 234
238 // FrameNavigationState ------------------------------------------------------- 235 // FrameNavigationState -------------------------------------------------------
239 236
240 // static 237 // static
241 bool FrameNavigationState::allow_extension_scheme_ = false; 238 bool FrameNavigationState::allow_extension_scheme_ = false;
242 239
(...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after
804 frame->frame_id = GetFrameId(navigation_state.IsMainFrame(frame_id), 801 frame->frame_id = GetFrameId(navigation_state.IsMainFrame(frame_id),
805 frame_id); 802 frame_id);
806 frame->error_occurred = navigation_state.GetErrorOccurredInFrame(frame_id); 803 frame->error_occurred = navigation_state.GetErrorOccurredInFrame(frame_id);
807 result_list.push_back(frame); 804 result_list.push_back(frame);
808 } 805 }
809 result_.reset(GetAllFrames::Result::Create(result_list)); 806 result_.reset(GetAllFrames::Result::Create(result_list));
810 return true; 807 return true;
811 } 808 }
812 809
813 } // namespace extensions 810 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698