| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/extension_webnavigation_api.h" | 7 #include "chrome/browser/extensions/extension_webnavigation_api.h" |
| 8 | 8 |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 controller->tab_contents()); | 189 controller->tab_contents()); |
| 190 if (!navigation_state_.CanSendEvents(details->frame_id())) | 190 if (!navigation_state_.CanSendEvents(details->frame_id())) |
| 191 return; | 191 return; |
| 192 ListValue args; | 192 ListValue args; |
| 193 DictionaryValue* dict = new DictionaryValue(); | 193 DictionaryValue* dict = new DictionaryValue(); |
| 194 dict->SetInteger(keys::kTabIdKey, | 194 dict->SetInteger(keys::kTabIdKey, |
| 195 ExtensionTabUtil::GetTabId(controller->tab_contents())); | 195 ExtensionTabUtil::GetTabId(controller->tab_contents())); |
| 196 dict->SetString(keys::kUrlKey, details->url().spec()); | 196 dict->SetString(keys::kUrlKey, details->url().spec()); |
| 197 dict->SetInteger(keys::kFrameIdKey, GetFrameId(details)); | 197 dict->SetInteger(keys::kFrameIdKey, GetFrameId(details)); |
| 198 dict->SetInteger(keys::kRequestIdKey, 0); | 198 dict->SetInteger(keys::kRequestIdKey, 0); |
| 199 dict->SetReal(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 199 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 200 args.Append(dict); | 200 args.Append(dict); |
| 201 | 201 |
| 202 std::string json_args; | 202 std::string json_args; |
| 203 base::JSONWriter::Write(&args, false, &json_args); | 203 base::JSONWriter::Write(&args, false, &json_args); |
| 204 DispatchEvent(controller->profile(), keys::kOnBeforeNavigate, json_args); | 204 DispatchEvent(controller->profile(), keys::kOnBeforeNavigate, json_args); |
| 205 } | 205 } |
| 206 | 206 |
| 207 void ExtensionWebNavigationEventRouter::FrameProvisionalLoadCommitted( | 207 void ExtensionWebNavigationEventRouter::FrameProvisionalLoadCommitted( |
| 208 NavigationController* controller, | 208 NavigationController* controller, |
| 209 ProvisionalLoadDetails* details) { | 209 ProvisionalLoadDetails* details) { |
| 210 if (!navigation_state_.CanSendEvents(details->frame_id())) | 210 if (!navigation_state_.CanSendEvents(details->frame_id())) |
| 211 return; | 211 return; |
| 212 ListValue args; | 212 ListValue args; |
| 213 DictionaryValue* dict = new DictionaryValue(); | 213 DictionaryValue* dict = new DictionaryValue(); |
| 214 dict->SetInteger(keys::kTabIdKey, | 214 dict->SetInteger(keys::kTabIdKey, |
| 215 ExtensionTabUtil::GetTabId(controller->tab_contents())); | 215 ExtensionTabUtil::GetTabId(controller->tab_contents())); |
| 216 dict->SetString(keys::kUrlKey, details->url().spec()); | 216 dict->SetString(keys::kUrlKey, details->url().spec()); |
| 217 dict->SetInteger(keys::kFrameIdKey, GetFrameId(details)); | 217 dict->SetInteger(keys::kFrameIdKey, GetFrameId(details)); |
| 218 dict->SetString(keys::kTransitionTypeKey, | 218 dict->SetString(keys::kTransitionTypeKey, |
| 219 PageTransition::CoreTransitionString( | 219 PageTransition::CoreTransitionString( |
| 220 details->transition_type())); | 220 details->transition_type())); |
| 221 dict->SetString(keys::kTransitionQualifiersKey, | 221 dict->SetString(keys::kTransitionQualifiersKey, |
| 222 PageTransition::QualifierString( | 222 PageTransition::QualifierString( |
| 223 details->transition_type())); | 223 details->transition_type())); |
| 224 dict->SetReal(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 224 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 225 args.Append(dict); | 225 args.Append(dict); |
| 226 | 226 |
| 227 std::string json_args; | 227 std::string json_args; |
| 228 base::JSONWriter::Write(&args, false, &json_args); | 228 base::JSONWriter::Write(&args, false, &json_args); |
| 229 DispatchEvent(controller->profile(), keys::kOnCommitted, json_args); | 229 DispatchEvent(controller->profile(), keys::kOnCommitted, json_args); |
| 230 } | 230 } |
| 231 | 231 |
| 232 void ExtensionWebNavigationEventRouter::FrameDomContentLoaded( | 232 void ExtensionWebNavigationEventRouter::FrameDomContentLoaded( |
| 233 NavigationController* controller, | 233 NavigationController* controller, |
| 234 int64 frame_id) { | 234 int64 frame_id) { |
| 235 if (!navigation_state_.CanSendEvents(frame_id)) | 235 if (!navigation_state_.CanSendEvents(frame_id)) |
| 236 return; | 236 return; |
| 237 ListValue args; | 237 ListValue args; |
| 238 DictionaryValue* dict = new DictionaryValue(); | 238 DictionaryValue* dict = new DictionaryValue(); |
| 239 dict->SetInteger(keys::kTabIdKey, | 239 dict->SetInteger(keys::kTabIdKey, |
| 240 ExtensionTabUtil::GetTabId(controller->tab_contents())); | 240 ExtensionTabUtil::GetTabId(controller->tab_contents())); |
| 241 dict->SetString(keys::kUrlKey, navigation_state_.GetUrl(frame_id).spec()); | 241 dict->SetString(keys::kUrlKey, navigation_state_.GetUrl(frame_id).spec()); |
| 242 dict->SetInteger(keys::kFrameIdKey, | 242 dict->SetInteger(keys::kFrameIdKey, |
| 243 navigation_state_.IsMainFrame(frame_id) ? 0 : static_cast<int>(frame_id)); | 243 navigation_state_.IsMainFrame(frame_id) ? 0 : static_cast<int>(frame_id)); |
| 244 dict->SetReal(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 244 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 245 args.Append(dict); | 245 args.Append(dict); |
| 246 | 246 |
| 247 std::string json_args; | 247 std::string json_args; |
| 248 base::JSONWriter::Write(&args, false, &json_args); | 248 base::JSONWriter::Write(&args, false, &json_args); |
| 249 DispatchEvent(controller->profile(), keys::kOnDOMContentLoaded, json_args); | 249 DispatchEvent(controller->profile(), keys::kOnDOMContentLoaded, json_args); |
| 250 } | 250 } |
| 251 | 251 |
| 252 void ExtensionWebNavigationEventRouter::FrameDidFinishLoad( | 252 void ExtensionWebNavigationEventRouter::FrameDidFinishLoad( |
| 253 NavigationController* controller, | 253 NavigationController* controller, |
| 254 int64 frame_id) { | 254 int64 frame_id) { |
| 255 if (!navigation_state_.CanSendEvents(frame_id)) | 255 if (!navigation_state_.CanSendEvents(frame_id)) |
| 256 return; | 256 return; |
| 257 ListValue args; | 257 ListValue args; |
| 258 DictionaryValue* dict = new DictionaryValue(); | 258 DictionaryValue* dict = new DictionaryValue(); |
| 259 dict->SetInteger(keys::kTabIdKey, | 259 dict->SetInteger(keys::kTabIdKey, |
| 260 ExtensionTabUtil::GetTabId(controller->tab_contents())); | 260 ExtensionTabUtil::GetTabId(controller->tab_contents())); |
| 261 dict->SetString(keys::kUrlKey, navigation_state_.GetUrl(frame_id).spec()); | 261 dict->SetString(keys::kUrlKey, navigation_state_.GetUrl(frame_id).spec()); |
| 262 dict->SetInteger(keys::kFrameIdKey, | 262 dict->SetInteger(keys::kFrameIdKey, |
| 263 navigation_state_.IsMainFrame(frame_id) ? 0 : static_cast<int>(frame_id)); | 263 navigation_state_.IsMainFrame(frame_id) ? 0 : static_cast<int>(frame_id)); |
| 264 dict->SetReal(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 264 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 265 args.Append(dict); | 265 args.Append(dict); |
| 266 | 266 |
| 267 std::string json_args; | 267 std::string json_args; |
| 268 base::JSONWriter::Write(&args, false, &json_args); | 268 base::JSONWriter::Write(&args, false, &json_args); |
| 269 DispatchEvent(controller->profile(), keys::kOnCompleted, json_args); | 269 DispatchEvent(controller->profile(), keys::kOnCompleted, json_args); |
| 270 } | 270 } |
| 271 | 271 |
| 272 void ExtensionWebNavigationEventRouter::FailProvisionalLoadWithError( | 272 void ExtensionWebNavigationEventRouter::FailProvisionalLoadWithError( |
| 273 NavigationController* controller, | 273 NavigationController* controller, |
| 274 ProvisionalLoadDetails* details) { | 274 ProvisionalLoadDetails* details) { |
| 275 if (!navigation_state_.CanSendEvents(details->frame_id())) | 275 if (!navigation_state_.CanSendEvents(details->frame_id())) |
| 276 return; | 276 return; |
| 277 ListValue args; | 277 ListValue args; |
| 278 DictionaryValue* dict = new DictionaryValue(); | 278 DictionaryValue* dict = new DictionaryValue(); |
| 279 dict->SetInteger(keys::kTabIdKey, | 279 dict->SetInteger(keys::kTabIdKey, |
| 280 ExtensionTabUtil::GetTabId(controller->tab_contents())); | 280 ExtensionTabUtil::GetTabId(controller->tab_contents())); |
| 281 dict->SetString(keys::kUrlKey, details->url().spec()); | 281 dict->SetString(keys::kUrlKey, details->url().spec()); |
| 282 dict->SetInteger(keys::kFrameIdKey, GetFrameId(details)); | 282 dict->SetInteger(keys::kFrameIdKey, GetFrameId(details)); |
| 283 dict->SetString(keys::kErrorKey, | 283 dict->SetString(keys::kErrorKey, |
| 284 std::string(net::ErrorToString(details->error_code()))); | 284 std::string(net::ErrorToString(details->error_code()))); |
| 285 dict->SetReal(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 285 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 286 args.Append(dict); | 286 args.Append(dict); |
| 287 | 287 |
| 288 std::string json_args; | 288 std::string json_args; |
| 289 base::JSONWriter::Write(&args, false, &json_args); | 289 base::JSONWriter::Write(&args, false, &json_args); |
| 290 navigation_state_.ErrorOccurredInFrame(details->frame_id()); | 290 navigation_state_.ErrorOccurredInFrame(details->frame_id()); |
| 291 DispatchEvent(controller->profile(), keys::kOnErrorOccurred, json_args); | 291 DispatchEvent(controller->profile(), keys::kOnErrorOccurred, json_args); |
| 292 } | 292 } |
| 293 | 293 |
| 294 void ExtensionWebNavigationEventRouter::CreatingNewWindow( | 294 void ExtensionWebNavigationEventRouter::CreatingNewWindow( |
| 295 TabContents* tab_contents, | 295 TabContents* tab_contents, |
| 296 const ViewHostMsg_CreateWindow_Params* details) { | 296 const ViewHostMsg_CreateWindow_Params* details) { |
| 297 ListValue args; | 297 ListValue args; |
| 298 DictionaryValue* dict = new DictionaryValue(); | 298 DictionaryValue* dict = new DictionaryValue(); |
| 299 dict->SetInteger(keys::kSourceTabIdKey, | 299 dict->SetInteger(keys::kSourceTabIdKey, |
| 300 ExtensionTabUtil::GetTabId(tab_contents)); | 300 ExtensionTabUtil::GetTabId(tab_contents)); |
| 301 dict->SetString(keys::kSourceUrlKey, details->opener_url.spec()); | 301 dict->SetString(keys::kSourceUrlKey, details->opener_url.spec()); |
| 302 dict->SetString(keys::kTargetUrlKey, | 302 dict->SetString(keys::kTargetUrlKey, |
| 303 details->target_url.possibly_invalid_spec()); | 303 details->target_url.possibly_invalid_spec()); |
| 304 dict->SetReal(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 304 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 305 args.Append(dict); | 305 args.Append(dict); |
| 306 | 306 |
| 307 std::string json_args; | 307 std::string json_args; |
| 308 base::JSONWriter::Write(&args, false, &json_args); | 308 base::JSONWriter::Write(&args, false, &json_args); |
| 309 DispatchEvent(tab_contents->profile(), keys::kOnBeforeRetarget, json_args); | 309 DispatchEvent(tab_contents->profile(), keys::kOnBeforeRetarget, json_args); |
| 310 } | 310 } |
| 311 | 311 |
| 312 void ExtensionWebNavigationEventRouter::DispatchEvent( | 312 void ExtensionWebNavigationEventRouter::DispatchEvent( |
| 313 Profile* profile, | 313 Profile* profile, |
| 314 const char* event_name, | 314 const char* event_name, |
| 315 const std::string& json_args) { | 315 const std::string& json_args) { |
| 316 if (profile && profile->GetExtensionEventRouter()) { | 316 if (profile && profile->GetExtensionEventRouter()) { |
| 317 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | 317 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 318 event_name, json_args, profile, GURL()); | 318 event_name, json_args, profile, GURL()); |
| 319 } | 319 } |
| 320 } | 320 } |
| OLD | NEW |