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

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

Issue 10694085: Refactor extension event distribution to use Values instead of JSON strings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase and review changes. 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"
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 } 69 }
70 70
71 // Returns |time| as milliseconds since the epoch. 71 // Returns |time| as milliseconds since the epoch.
72 double MilliSecondsFromTime(const base::Time& time) { 72 double MilliSecondsFromTime(const base::Time& time) {
73 return 1000 * time.ToDoubleT(); 73 return 1000 * time.ToDoubleT();
74 } 74 }
75 75
76 // Dispatches events to the extension message service. 76 // Dispatches events to the extension message service.
77 void DispatchEvent(BrowserContext* browser_context, 77 void DispatchEvent(BrowserContext* browser_context,
78 const char* event_name, 78 const char* event_name,
79 const ListValue& args, 79 base::Value* args,
80 const GURL& url) { 80 const GURL& url) {
81 std::string json_args;
82 base::JSONWriter::Write(&args, &json_args);
83
84 extensions::EventFilteringInfo info; 81 extensions::EventFilteringInfo info;
85 info.SetURL(url); 82 info.SetURL(url);
86 83
84 ListValue *event_args = new ListValue();
85 event_args->Append(args);
86
87 Profile* profile = Profile::FromBrowserContext(browser_context); 87 Profile* profile = Profile::FromBrowserContext(browser_context);
88 if (profile && profile->GetExtensionEventRouter()) { 88 if (profile && profile->GetExtensionEventRouter()) {
89 profile->GetExtensionEventRouter()->DispatchEventToRenderers( 89 profile->GetExtensionEventRouter()->DispatchEventToRenderers(
90 event_name, json_args, profile, GURL(), info); 90 event_name, event_args, profile, GURL(), info);
91 } 91 }
92 } 92 }
93 93
94 // Constructs and dispatches an onBeforeNavigate event. 94 // Constructs and dispatches an onBeforeNavigate event.
95 void DispatchOnBeforeNavigate(WebContents* web_contents, 95 void DispatchOnBeforeNavigate(WebContents* web_contents,
96 int64 frame_id, 96 int64 frame_id,
97 bool is_main_frame, 97 bool is_main_frame,
98 const GURL& validated_url) { 98 const GURL& validated_url) {
99 ListValue args;
100 DictionaryValue* dict = new DictionaryValue(); 99 DictionaryValue* dict = new DictionaryValue();
101 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); 100 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents));
102 dict->SetString(keys::kUrlKey, validated_url.spec()); 101 dict->SetString(keys::kUrlKey, validated_url.spec());
103 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); 102 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
104 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 103 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
105 args.Append(dict);
106 104
107 DispatchEvent(web_contents->GetBrowserContext(), 105 DispatchEvent(web_contents->GetBrowserContext(),
108 keys::kOnBeforeNavigate, 106 keys::kOnBeforeNavigate,
109 args, 107 dict,
110 validated_url); 108 validated_url);
111 } 109 }
112 110
113 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated 111 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated
114 // event. 112 // event.
115 void DispatchOnCommitted(const char* event_name, 113 void DispatchOnCommitted(const char* event_name,
116 WebContents* web_contents, 114 WebContents* web_contents,
117 int64 frame_id, 115 int64 frame_id,
118 bool is_main_frame, 116 bool is_main_frame,
119 const GURL& url, 117 const GURL& url,
120 content::PageTransition transition_type) { 118 content::PageTransition transition_type) {
121 ListValue args;
122 DictionaryValue* dict = new DictionaryValue(); 119 DictionaryValue* dict = new DictionaryValue();
123 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); 120 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents));
124 dict->SetString(keys::kUrlKey, url.spec()); 121 dict->SetString(keys::kUrlKey, url.spec());
125 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); 122 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
126 dict->SetString( 123 dict->SetString(
127 keys::kTransitionTypeKey, 124 keys::kTransitionTypeKey,
128 content::PageTransitionGetCoreTransitionString(transition_type)); 125 content::PageTransitionGetCoreTransitionString(transition_type));
129 ListValue* qualifiers = new ListValue(); 126 ListValue* qualifiers = new ListValue();
130 if (transition_type & content::PAGE_TRANSITION_CLIENT_REDIRECT) 127 if (transition_type & content::PAGE_TRANSITION_CLIENT_REDIRECT)
131 qualifiers->Append(Value::CreateStringValue("client_redirect")); 128 qualifiers->Append(Value::CreateStringValue("client_redirect"));
132 if (transition_type & content::PAGE_TRANSITION_SERVER_REDIRECT) 129 if (transition_type & content::PAGE_TRANSITION_SERVER_REDIRECT)
133 qualifiers->Append(Value::CreateStringValue("server_redirect")); 130 qualifiers->Append(Value::CreateStringValue("server_redirect"));
134 if (transition_type & content::PAGE_TRANSITION_FORWARD_BACK) 131 if (transition_type & content::PAGE_TRANSITION_FORWARD_BACK)
135 qualifiers->Append(Value::CreateStringValue("forward_back")); 132 qualifiers->Append(Value::CreateStringValue("forward_back"));
136 if (transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR) 133 if (transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR)
137 qualifiers->Append(Value::CreateStringValue("from_address_bar")); 134 qualifiers->Append(Value::CreateStringValue("from_address_bar"));
138 dict->Set(keys::kTransitionQualifiersKey, qualifiers); 135 dict->Set(keys::kTransitionQualifiersKey, qualifiers);
139 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 136 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
140 args.Append(dict);
141 137
142 DispatchEvent(web_contents->GetBrowserContext(), event_name, args, url); 138 DispatchEvent(web_contents->GetBrowserContext(), event_name, dict, url);
miket_OOO 2012/07/10 22:33:19 This CL would be prettier if dict were named args
143 } 139 }
144 140
145 // Constructs and dispatches an onDOMContentLoaded event. 141 // Constructs and dispatches an onDOMContentLoaded event.
146 void DispatchOnDOMContentLoaded(WebContents* web_contents, 142 void DispatchOnDOMContentLoaded(WebContents* web_contents,
147 const GURL& url, 143 const GURL& url,
148 bool is_main_frame, 144 bool is_main_frame,
149 int64 frame_id) { 145 int64 frame_id) {
150 ListValue args;
151 DictionaryValue* dict = new DictionaryValue(); 146 DictionaryValue* dict = new DictionaryValue();
152 dict->SetInteger(keys::kTabIdKey, 147 dict->SetInteger(keys::kTabIdKey,
153 ExtensionTabUtil::GetTabId(web_contents)); 148 ExtensionTabUtil::GetTabId(web_contents));
154 dict->SetString(keys::kUrlKey, url.spec()); 149 dict->SetString(keys::kUrlKey, url.spec());
155 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); 150 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
156 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 151 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
157 args.Append(dict);
158 152
159 DispatchEvent(web_contents->GetBrowserContext(), 153 DispatchEvent(web_contents->GetBrowserContext(),
160 keys::kOnDOMContentLoaded, 154 keys::kOnDOMContentLoaded,
161 args, 155 dict,
162 url); 156 url);
163 } 157 }
164 158
165 // Constructs and dispatches an onCompleted event. 159 // Constructs and dispatches an onCompleted event.
166 void DispatchOnCompleted(WebContents* web_contents, 160 void DispatchOnCompleted(WebContents* web_contents,
167 const GURL& url, 161 const GURL& url,
168 bool is_main_frame, 162 bool is_main_frame,
169 int64 frame_id) { 163 int64 frame_id) {
170 ListValue args;
171 DictionaryValue* dict = new DictionaryValue(); 164 DictionaryValue* dict = new DictionaryValue();
172 dict->SetInteger(keys::kTabIdKey, 165 dict->SetInteger(keys::kTabIdKey,
173 ExtensionTabUtil::GetTabId(web_contents)); 166 ExtensionTabUtil::GetTabId(web_contents));
174 dict->SetString(keys::kUrlKey, url.spec()); 167 dict->SetString(keys::kUrlKey, url.spec());
175 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); 168 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
176 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 169 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
177 args.Append(dict);
178 170
179 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnCompleted, args, 171 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnCompleted, dict,
180 url); 172 url);
181 } 173 }
182 174
183 // Constructs and dispatches an onCreatedNavigationTarget event. 175 // Constructs and dispatches an onCreatedNavigationTarget event.
184 void DispatchOnCreatedNavigationTarget( 176 void DispatchOnCreatedNavigationTarget(
185 WebContents* web_contents, 177 WebContents* web_contents,
186 BrowserContext* browser_context, 178 BrowserContext* browser_context,
187 int64 source_frame_id, 179 int64 source_frame_id,
188 bool source_frame_is_main_frame, 180 bool source_frame_is_main_frame,
189 WebContents* target_web_contents, 181 WebContents* target_web_contents,
190 const GURL& target_url) { 182 const GURL& target_url) {
191 // Check that the tab is already inserted into a tab strip model. This code 183 // Check that the tab is already inserted into a tab strip model. This code
192 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. 184 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab.
193 DCHECK(ExtensionTabUtil::GetTabById( 185 DCHECK(ExtensionTabUtil::GetTabById(
194 ExtensionTabUtil::GetTabId(target_web_contents), 186 ExtensionTabUtil::GetTabId(target_web_contents),
195 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()), 187 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()),
196 false, NULL, NULL, NULL, NULL)); 188 false, NULL, NULL, NULL, NULL));
197 189
198 ListValue args;
199 DictionaryValue* dict = new DictionaryValue(); 190 DictionaryValue* dict = new DictionaryValue();
200 dict->SetInteger(keys::kSourceTabIdKey, 191 dict->SetInteger(keys::kSourceTabIdKey,
201 ExtensionTabUtil::GetTabId(web_contents)); 192 ExtensionTabUtil::GetTabId(web_contents));
202 dict->SetInteger(keys::kSourceFrameIdKey, 193 dict->SetInteger(keys::kSourceFrameIdKey,
203 GetFrameId(source_frame_is_main_frame, source_frame_id)); 194 GetFrameId(source_frame_is_main_frame, source_frame_id));
204 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); 195 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec());
205 dict->SetInteger(keys::kTabIdKey, 196 dict->SetInteger(keys::kTabIdKey,
206 ExtensionTabUtil::GetTabId(target_web_contents)); 197 ExtensionTabUtil::GetTabId(target_web_contents));
207 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 198 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
208 args.Append(dict);
209 199
210 DispatchEvent(browser_context, keys::kOnCreatedNavigationTarget, args, 200 DispatchEvent(browser_context, keys::kOnCreatedNavigationTarget, dict,
211 target_url); 201 target_url);
212 } 202 }
213 203
214 // Constructs and dispatches an onErrorOccurred event. 204 // Constructs and dispatches an onErrorOccurred event.
215 void DispatchOnErrorOccurred(WebContents* web_contents, 205 void DispatchOnErrorOccurred(WebContents* web_contents,
216 const GURL& url, 206 const GURL& url,
217 int64 frame_id, 207 int64 frame_id,
218 bool is_main_frame, 208 bool is_main_frame,
219 int error_code) { 209 int error_code) {
220 ListValue args;
221 DictionaryValue* dict = new DictionaryValue(); 210 DictionaryValue* dict = new DictionaryValue();
222 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); 211 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents));
223 dict->SetString(keys::kUrlKey, url.spec()); 212 dict->SetString(keys::kUrlKey, url.spec());
224 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); 213 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id));
225 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); 214 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code));
226 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 215 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
227 args.Append(dict);
228 216
229 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnErrorOccurred, 217 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnErrorOccurred,
230 args, url); 218 dict, url);
231 } 219 }
232 220
233 // Constructs and dispatches an onTabReplaced event. 221 // Constructs and dispatches an onTabReplaced event.
234 void DispatchOnTabReplaced( 222 void DispatchOnTabReplaced(
235 WebContents* old_web_contents, 223 WebContents* old_web_contents,
236 BrowserContext* browser_context, 224 BrowserContext* browser_context,
237 WebContents* new_web_contents) { 225 WebContents* new_web_contents) {
238 ListValue args;
239 DictionaryValue* dict = new DictionaryValue(); 226 DictionaryValue* dict = new DictionaryValue();
240 dict->SetInteger(keys::kReplacedTabIdKey, 227 dict->SetInteger(keys::kReplacedTabIdKey,
241 ExtensionTabUtil::GetTabId(old_web_contents)); 228 ExtensionTabUtil::GetTabId(old_web_contents));
242 dict->SetInteger(keys::kTabIdKey, 229 dict->SetInteger(keys::kTabIdKey,
243 ExtensionTabUtil::GetTabId(new_web_contents)); 230 ExtensionTabUtil::GetTabId(new_web_contents));
244 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); 231 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
245 args.Append(dict);
246 232
247 DispatchEvent(browser_context, keys::kOnTabReplaced, args, GURL()); 233 DispatchEvent(browser_context, keys::kOnTabReplaced, dict, GURL());
248 } 234 }
249 235
250 } // namespace 236 } // namespace
251 237
252 238
253 // FrameNavigationState ------------------------------------------------------- 239 // FrameNavigationState -------------------------------------------------------
254 240
255 // static 241 // static
256 bool FrameNavigationState::allow_extension_scheme_ = false; 242 bool FrameNavigationState::allow_extension_scheme_ = false;
257 243
(...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after
868 frame->frame_id = GetFrameId(navigation_state.IsMainFrame(frame_id), 854 frame->frame_id = GetFrameId(navigation_state.IsMainFrame(frame_id),
869 frame_id); 855 frame_id);
870 frame->error_occurred = navigation_state.GetErrorOccurredInFrame(frame_id); 856 frame->error_occurred = navigation_state.GetErrorOccurredInFrame(frame_id);
871 result_list.push_back(frame); 857 result_list.push_back(frame);
872 } 858 }
873 result_.reset(GetAllFrames::Result::Create(result_list)); 859 result_.reset(GetAllFrames::Result::Create(result_list));
874 return true; 860 return true;
875 } 861 }
876 862
877 } // namespace extensions 863 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698