| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 int GetFrameId(bool is_main_frame, int64 frame_id) { | 37 int GetFrameId(bool is_main_frame, int64 frame_id) { |
| 38 return is_main_frame ? 0 : static_cast<int>(frame_id); | 38 return is_main_frame ? 0 : static_cast<int>(frame_id); |
| 39 } | 39 } |
| 40 | 40 |
| 41 // Returns |time| as milliseconds since the epoch. | 41 // Returns |time| as milliseconds since the epoch. |
| 42 double MilliSecondsFromTime(const base::Time& time) { | 42 double MilliSecondsFromTime(const base::Time& time) { |
| 43 return 1000 * time.ToDoubleT(); | 43 return 1000 * time.ToDoubleT(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Dispatches events to the extension message service. | 46 // Dispatches events to the extension message service. |
| 47 void DispatchEvent(Profile* profile, | 47 void DispatchEvent(content::BrowserContext* browser_context, |
| 48 const char* event_name, | 48 const char* event_name, |
| 49 const std::string& json_args) { | 49 const std::string& json_args) { |
| 50 Profile* profile = static_cast<Profile*>(browser_context); |
| 50 if (profile && profile->GetExtensionEventRouter()) { | 51 if (profile && profile->GetExtensionEventRouter()) { |
| 51 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | 52 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 52 event_name, json_args, profile, GURL()); | 53 event_name, json_args, profile, GURL()); |
| 53 } | 54 } |
| 54 } | 55 } |
| 55 | 56 |
| 56 // Constructs and dispatches an onBeforeNavigate event. | 57 // Constructs and dispatches an onBeforeNavigate event. |
| 57 void DispatchOnBeforeNavigate(TabContents* tab_contents, | 58 void DispatchOnBeforeNavigate(TabContents* tab_contents, |
| 58 int64 frame_id, | 59 int64 frame_id, |
| 59 bool is_main_frame, | 60 bool is_main_frame, |
| 60 const GURL& validated_url) { | 61 const GURL& validated_url) { |
| 61 ListValue args; | 62 ListValue args; |
| 62 DictionaryValue* dict = new DictionaryValue(); | 63 DictionaryValue* dict = new DictionaryValue(); |
| 63 dict->SetInteger(keys::kTabIdKey, | 64 dict->SetInteger(keys::kTabIdKey, |
| 64 ExtensionTabUtil::GetTabId(tab_contents)); | 65 ExtensionTabUtil::GetTabId(tab_contents)); |
| 65 dict->SetString(keys::kUrlKey, validated_url.spec()); | 66 dict->SetString(keys::kUrlKey, validated_url.spec()); |
| 66 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 67 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
| 67 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 68 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 68 args.Append(dict); | 69 args.Append(dict); |
| 69 | 70 |
| 70 std::string json_args; | 71 std::string json_args; |
| 71 base::JSONWriter::Write(&args, false, &json_args); | 72 base::JSONWriter::Write(&args, false, &json_args); |
| 72 DispatchEvent(tab_contents->profile(), keys::kOnBeforeNavigate, json_args); | 73 DispatchEvent(tab_contents->browser_context(), |
| 74 keys::kOnBeforeNavigate, |
| 75 json_args); |
| 73 } | 76 } |
| 74 | 77 |
| 75 // Constructs and dispatches an onCommitted event. | 78 // Constructs and dispatches an onCommitted event. |
| 76 void DispatchOnCommitted(TabContents* tab_contents, | 79 void DispatchOnCommitted(TabContents* tab_contents, |
| 77 int64 frame_id, | 80 int64 frame_id, |
| 78 bool is_main_frame, | 81 bool is_main_frame, |
| 79 const GURL& url, | 82 const GURL& url, |
| 80 PageTransition::Type transition_type) { | 83 PageTransition::Type transition_type) { |
| 81 ListValue args; | 84 ListValue args; |
| 82 DictionaryValue* dict = new DictionaryValue(); | 85 DictionaryValue* dict = new DictionaryValue(); |
| 83 dict->SetInteger(keys::kTabIdKey, | 86 dict->SetInteger(keys::kTabIdKey, |
| 84 ExtensionTabUtil::GetTabId(tab_contents)); | 87 ExtensionTabUtil::GetTabId(tab_contents)); |
| 85 dict->SetString(keys::kUrlKey, url.spec()); | 88 dict->SetString(keys::kUrlKey, url.spec()); |
| 86 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 89 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
| 87 dict->SetString(keys::kTransitionTypeKey, | 90 dict->SetString(keys::kTransitionTypeKey, |
| 88 PageTransition::CoreTransitionString(transition_type)); | 91 PageTransition::CoreTransitionString(transition_type)); |
| 89 ListValue* qualifiers = new ListValue(); | 92 ListValue* qualifiers = new ListValue(); |
| 90 if (transition_type & PageTransition::CLIENT_REDIRECT) | 93 if (transition_type & PageTransition::CLIENT_REDIRECT) |
| 91 qualifiers->Append(Value::CreateStringValue("client_redirect")); | 94 qualifiers->Append(Value::CreateStringValue("client_redirect")); |
| 92 if (transition_type & PageTransition::SERVER_REDIRECT) | 95 if (transition_type & PageTransition::SERVER_REDIRECT) |
| 93 qualifiers->Append(Value::CreateStringValue("server_redirect")); | 96 qualifiers->Append(Value::CreateStringValue("server_redirect")); |
| 94 if (transition_type & PageTransition::FORWARD_BACK) | 97 if (transition_type & PageTransition::FORWARD_BACK) |
| 95 qualifiers->Append(Value::CreateStringValue("forward_back")); | 98 qualifiers->Append(Value::CreateStringValue("forward_back")); |
| 96 dict->Set(keys::kTransitionQualifiersKey, qualifiers); | 99 dict->Set(keys::kTransitionQualifiersKey, qualifiers); |
| 97 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 100 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 98 args.Append(dict); | 101 args.Append(dict); |
| 99 | 102 |
| 100 std::string json_args; | 103 std::string json_args; |
| 101 base::JSONWriter::Write(&args, false, &json_args); | 104 base::JSONWriter::Write(&args, false, &json_args); |
| 102 DispatchEvent(tab_contents->profile(), keys::kOnCommitted, json_args); | 105 DispatchEvent(tab_contents->browser_context(), keys::kOnCommitted, json_args); |
| 103 } | 106 } |
| 104 | 107 |
| 105 // Constructs and dispatches an onDOMContentLoaded event. | 108 // Constructs and dispatches an onDOMContentLoaded event. |
| 106 void DispatchOnDOMContentLoaded(TabContents* tab_contents, | 109 void DispatchOnDOMContentLoaded(TabContents* tab_contents, |
| 107 const GURL& url, | 110 const GURL& url, |
| 108 bool is_main_frame, | 111 bool is_main_frame, |
| 109 int64 frame_id) { | 112 int64 frame_id) { |
| 110 ListValue args; | 113 ListValue args; |
| 111 DictionaryValue* dict = new DictionaryValue(); | 114 DictionaryValue* dict = new DictionaryValue(); |
| 112 dict->SetInteger(keys::kTabIdKey, | 115 dict->SetInteger(keys::kTabIdKey, |
| 113 ExtensionTabUtil::GetTabId(tab_contents)); | 116 ExtensionTabUtil::GetTabId(tab_contents)); |
| 114 dict->SetString(keys::kUrlKey, url.spec()); | 117 dict->SetString(keys::kUrlKey, url.spec()); |
| 115 dict->SetInteger(keys::kFrameIdKey, | 118 dict->SetInteger(keys::kFrameIdKey, |
| 116 is_main_frame ? 0 : static_cast<int>(frame_id)); | 119 is_main_frame ? 0 : static_cast<int>(frame_id)); |
| 117 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 120 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 118 args.Append(dict); | 121 args.Append(dict); |
| 119 | 122 |
| 120 std::string json_args; | 123 std::string json_args; |
| 121 base::JSONWriter::Write(&args, false, &json_args); | 124 base::JSONWriter::Write(&args, false, &json_args); |
| 122 DispatchEvent(tab_contents->profile(), keys::kOnDOMContentLoaded, json_args); | 125 DispatchEvent(tab_contents->browser_context(), |
| 126 keys::kOnDOMContentLoaded, |
| 127 json_args); |
| 123 } | 128 } |
| 124 | 129 |
| 125 // Constructs and dispatches an onCompleted event. | 130 // Constructs and dispatches an onCompleted event. |
| 126 void DispatchOnCompleted(TabContents* tab_contents, | 131 void DispatchOnCompleted(TabContents* tab_contents, |
| 127 const GURL& url, | 132 const GURL& url, |
| 128 bool is_main_frame, | 133 bool is_main_frame, |
| 129 int64 frame_id) { | 134 int64 frame_id) { |
| 130 ListValue args; | 135 ListValue args; |
| 131 DictionaryValue* dict = new DictionaryValue(); | 136 DictionaryValue* dict = new DictionaryValue(); |
| 132 dict->SetInteger(keys::kTabIdKey, | 137 dict->SetInteger(keys::kTabIdKey, |
| 133 ExtensionTabUtil::GetTabId(tab_contents)); | 138 ExtensionTabUtil::GetTabId(tab_contents)); |
| 134 dict->SetString(keys::kUrlKey, url.spec()); | 139 dict->SetString(keys::kUrlKey, url.spec()); |
| 135 dict->SetInteger(keys::kFrameIdKey, | 140 dict->SetInteger(keys::kFrameIdKey, |
| 136 is_main_frame ? 0 : static_cast<int>(frame_id)); | 141 is_main_frame ? 0 : static_cast<int>(frame_id)); |
| 137 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 142 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 138 args.Append(dict); | 143 args.Append(dict); |
| 139 | 144 |
| 140 std::string json_args; | 145 std::string json_args; |
| 141 base::JSONWriter::Write(&args, false, &json_args); | 146 base::JSONWriter::Write(&args, false, &json_args); |
| 142 DispatchEvent(tab_contents->profile(), keys::kOnCompleted, json_args); | 147 DispatchEvent(tab_contents->browser_context(), keys::kOnCompleted, json_args); |
| 143 } | 148 } |
| 144 | 149 |
| 145 // Constructs and dispatches an onBeforeRetarget event. | 150 // Constructs and dispatches an onBeforeRetarget event. |
| 146 void DispatchOnBeforeRetarget(TabContents* tab_contents, | 151 void DispatchOnBeforeRetarget(TabContents* tab_contents, |
| 147 Profile* profile, | 152 Profile* profile, |
| 148 const GURL& opener_url, | 153 const GURL& opener_url, |
| 149 const GURL& target_url) { | 154 const GURL& target_url) { |
| 150 ListValue args; | 155 ListValue args; |
| 151 DictionaryValue* dict = new DictionaryValue(); | 156 DictionaryValue* dict = new DictionaryValue(); |
| 152 dict->SetInteger(keys::kSourceTabIdKey, | 157 dict->SetInteger(keys::kSourceTabIdKey, |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 break; | 274 break; |
| 270 | 275 |
| 271 default: | 276 default: |
| 272 NOTREACHED(); | 277 NOTREACHED(); |
| 273 } | 278 } |
| 274 } | 279 } |
| 275 | 280 |
| 276 void ExtensionWebNavigationEventRouter::CreatingNewWindow( | 281 void ExtensionWebNavigationEventRouter::CreatingNewWindow( |
| 277 TabContents* tab_contents, | 282 TabContents* tab_contents, |
| 278 const ViewHostMsg_CreateWindow_Params* details) { | 283 const ViewHostMsg_CreateWindow_Params* details) { |
| 279 if (profile_->IsSameProfile(tab_contents->profile())) { | 284 Profile* tab_profile = static_cast<Profile*>(tab_contents->browser_context()); |
| 285 if (profile_->IsSameProfile(tab_profile)) { |
| 280 DispatchOnBeforeRetarget(tab_contents, | 286 DispatchOnBeforeRetarget(tab_contents, |
| 281 tab_contents->profile(), | 287 tab_profile, |
| 282 details->opener_url, | 288 details->opener_url, |
| 283 details->target_url); | 289 details->target_url); |
| 284 } | 290 } |
| 285 } | 291 } |
| 286 | 292 |
| 287 | 293 |
| 288 // ExtensionWebNavigationTabObserver ------------------------------------------ | 294 // ExtensionWebNavigationTabObserver ------------------------------------------ |
| 289 | 295 |
| 290 ExtensionWebNavigationTabObserver::ExtensionWebNavigationTabObserver( | 296 ExtensionWebNavigationTabObserver::ExtensionWebNavigationTabObserver( |
| 291 TabContents* tab_contents) | 297 TabContents* tab_contents) |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 dict->SetString(keys::kUrlKey, validated_url.spec()); | 348 dict->SetString(keys::kUrlKey, validated_url.spec()); |
| 343 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 349 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
| 344 dict->SetString(keys::kErrorKey, | 350 dict->SetString(keys::kErrorKey, |
| 345 std::string(net::ErrorToString(error_code))); | 351 std::string(net::ErrorToString(error_code))); |
| 346 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 352 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 347 args.Append(dict); | 353 args.Append(dict); |
| 348 | 354 |
| 349 std::string json_args; | 355 std::string json_args; |
| 350 base::JSONWriter::Write(&args, false, &json_args); | 356 base::JSONWriter::Write(&args, false, &json_args); |
| 351 navigation_state_.ErrorOccurredInFrame(frame_id); | 357 navigation_state_.ErrorOccurredInFrame(frame_id); |
| 352 DispatchEvent(tab_contents()->profile(), keys::kOnErrorOccurred, json_args); | 358 DispatchEvent(tab_contents()->browser_context(), |
| 359 keys::kOnErrorOccurred, |
| 360 json_args); |
| 353 } | 361 } |
| 354 | 362 |
| 355 void ExtensionWebNavigationTabObserver::DocumentLoadedInFrame( | 363 void ExtensionWebNavigationTabObserver::DocumentLoadedInFrame( |
| 356 int64 frame_id) { | 364 int64 frame_id) { |
| 357 if (!navigation_state_.CanSendEvents(frame_id)) | 365 if (!navigation_state_.CanSendEvents(frame_id)) |
| 358 return; | 366 return; |
| 359 DispatchOnDOMContentLoaded(tab_contents(), | 367 DispatchOnDOMContentLoaded(tab_contents(), |
| 360 navigation_state_.GetUrl(frame_id), | 368 navigation_state_.GetUrl(frame_id), |
| 361 navigation_state_.IsMainFrame(frame_id), | 369 navigation_state_.IsMainFrame(frame_id), |
| 362 frame_id); | 370 frame_id); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 381 const GURL& url, | 389 const GURL& url, |
| 382 const GURL& referrer, | 390 const GURL& referrer, |
| 383 WindowOpenDisposition disposition, | 391 WindowOpenDisposition disposition, |
| 384 PageTransition::Type transition) { | 392 PageTransition::Type transition) { |
| 385 if (disposition != NEW_FOREGROUND_TAB && | 393 if (disposition != NEW_FOREGROUND_TAB && |
| 386 disposition != NEW_BACKGROUND_TAB && | 394 disposition != NEW_BACKGROUND_TAB && |
| 387 disposition != NEW_WINDOW && | 395 disposition != NEW_WINDOW && |
| 388 disposition != OFF_THE_RECORD) { | 396 disposition != OFF_THE_RECORD) { |
| 389 return; | 397 return; |
| 390 } | 398 } |
| 391 Profile* profile = tab_contents()->profile(); | 399 Profile* profile = static_cast<Profile*>(tab_contents()->browser_context()); |
| 392 if (disposition == OFF_THE_RECORD) { | 400 if (disposition == OFF_THE_RECORD) { |
| 393 if (!profile->HasOffTheRecordProfile()) { | 401 if (!profile->HasOffTheRecordProfile()) { |
| 394 NOTREACHED(); | 402 NOTREACHED(); |
| 395 return; | 403 return; |
| 396 } | 404 } |
| 397 profile = profile->GetOffTheRecordProfile(); | 405 profile = profile->GetOffTheRecordProfile(); |
| 398 } | 406 } |
| 399 DispatchOnBeforeRetarget(tab_contents(), | 407 DispatchOnBeforeRetarget(tab_contents(), |
| 400 profile, | 408 profile, |
| 401 tab_contents()->GetURL(), | 409 tab_contents()->GetURL(), |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 transition_type); | 446 transition_type); |
| 439 DispatchOnDOMContentLoaded(tab_contents(), | 447 DispatchOnDOMContentLoaded(tab_contents(), |
| 440 url, | 448 url, |
| 441 is_main_frame, | 449 is_main_frame, |
| 442 frame_id); | 450 frame_id); |
| 443 DispatchOnCompleted(tab_contents(), | 451 DispatchOnCompleted(tab_contents(), |
| 444 url, | 452 url, |
| 445 is_main_frame, | 453 is_main_frame, |
| 446 frame_id); | 454 frame_id); |
| 447 } | 455 } |
| OLD | NEW |