| 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* 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*>(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 uint64 request_id) { | 62 uint64 request_id) { |
| 62 ListValue args; | 63 ListValue args; |
| 63 DictionaryValue* dict = new DictionaryValue(); | 64 DictionaryValue* dict = new DictionaryValue(); |
| 64 dict->SetInteger(keys::kTabIdKey, | 65 dict->SetInteger(keys::kTabIdKey, |
| 65 ExtensionTabUtil::GetTabId(tab_contents)); | 66 ExtensionTabUtil::GetTabId(tab_contents)); |
| 66 dict->SetString(keys::kUrlKey, validated_url.spec()); | 67 dict->SetString(keys::kUrlKey, validated_url.spec()); |
| 67 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 68 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
| 68 dict->SetString(keys::kRequestIdKey, | 69 dict->SetString(keys::kRequestIdKey, |
| 69 base::Uint64ToString(request_id)); | 70 base::Uint64ToString(request_id)); |
| 70 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 71 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 71 args.Append(dict); | 72 args.Append(dict); |
| 72 | 73 |
| 73 std::string json_args; | 74 std::string json_args; |
| 74 base::JSONWriter::Write(&args, false, &json_args); | 75 base::JSONWriter::Write(&args, false, &json_args); |
| 75 DispatchEvent(tab_contents->profile(), keys::kOnBeforeNavigate, json_args); | 76 DispatchEvent(tab_contents->context(), keys::kOnBeforeNavigate, json_args); |
| 76 } | 77 } |
| 77 | 78 |
| 78 // Constructs and dispatches an onCommitted event. | 79 // Constructs and dispatches an onCommitted event. |
| 79 void DispatchOnCommitted(TabContents* tab_contents, | 80 void DispatchOnCommitted(TabContents* tab_contents, |
| 80 int64 frame_id, | 81 int64 frame_id, |
| 81 bool is_main_frame, | 82 bool is_main_frame, |
| 82 const GURL& url, | 83 const GURL& url, |
| 83 PageTransition::Type transition_type) { | 84 PageTransition::Type transition_type) { |
| 84 ListValue args; | 85 ListValue args; |
| 85 DictionaryValue* dict = new DictionaryValue(); | 86 DictionaryValue* dict = new DictionaryValue(); |
| 86 dict->SetInteger(keys::kTabIdKey, | 87 dict->SetInteger(keys::kTabIdKey, |
| 87 ExtensionTabUtil::GetTabId(tab_contents)); | 88 ExtensionTabUtil::GetTabId(tab_contents)); |
| 88 dict->SetString(keys::kUrlKey, url.spec()); | 89 dict->SetString(keys::kUrlKey, url.spec()); |
| 89 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 90 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
| 90 dict->SetString(keys::kTransitionTypeKey, | 91 dict->SetString(keys::kTransitionTypeKey, |
| 91 PageTransition::CoreTransitionString(transition_type)); | 92 PageTransition::CoreTransitionString(transition_type)); |
| 92 ListValue* qualifiers = new ListValue(); | 93 ListValue* qualifiers = new ListValue(); |
| 93 if (transition_type & PageTransition::CLIENT_REDIRECT) | 94 if (transition_type & PageTransition::CLIENT_REDIRECT) |
| 94 qualifiers->Append(Value::CreateStringValue("client_redirect")); | 95 qualifiers->Append(Value::CreateStringValue("client_redirect")); |
| 95 if (transition_type & PageTransition::SERVER_REDIRECT) | 96 if (transition_type & PageTransition::SERVER_REDIRECT) |
| 96 qualifiers->Append(Value::CreateStringValue("server_redirect")); | 97 qualifiers->Append(Value::CreateStringValue("server_redirect")); |
| 97 if (transition_type & PageTransition::FORWARD_BACK) | 98 if (transition_type & PageTransition::FORWARD_BACK) |
| 98 qualifiers->Append(Value::CreateStringValue("forward_back")); | 99 qualifiers->Append(Value::CreateStringValue("forward_back")); |
| 99 dict->Set(keys::kTransitionQualifiersKey, qualifiers); | 100 dict->Set(keys::kTransitionQualifiersKey, qualifiers); |
| 100 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 101 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 101 args.Append(dict); | 102 args.Append(dict); |
| 102 | 103 |
| 103 std::string json_args; | 104 std::string json_args; |
| 104 base::JSONWriter::Write(&args, false, &json_args); | 105 base::JSONWriter::Write(&args, false, &json_args); |
| 105 DispatchEvent(tab_contents->profile(), keys::kOnCommitted, json_args); | 106 DispatchEvent(tab_contents->context(), keys::kOnCommitted, json_args); |
| 106 } | 107 } |
| 107 | 108 |
| 108 // Constructs and dispatches an onDOMContentLoaded event. | 109 // Constructs and dispatches an onDOMContentLoaded event. |
| 109 void DispatchOnDOMContentLoaded(TabContents* tab_contents, | 110 void DispatchOnDOMContentLoaded(TabContents* tab_contents, |
| 110 const GURL& url, | 111 const GURL& url, |
| 111 bool is_main_frame, | 112 bool is_main_frame, |
| 112 int64 frame_id) { | 113 int64 frame_id) { |
| 113 ListValue args; | 114 ListValue args; |
| 114 DictionaryValue* dict = new DictionaryValue(); | 115 DictionaryValue* dict = new DictionaryValue(); |
| 115 dict->SetInteger(keys::kTabIdKey, | 116 dict->SetInteger(keys::kTabIdKey, |
| 116 ExtensionTabUtil::GetTabId(tab_contents)); | 117 ExtensionTabUtil::GetTabId(tab_contents)); |
| 117 dict->SetString(keys::kUrlKey, url.spec()); | 118 dict->SetString(keys::kUrlKey, url.spec()); |
| 118 dict->SetInteger(keys::kFrameIdKey, | 119 dict->SetInteger(keys::kFrameIdKey, |
| 119 is_main_frame ? 0 : static_cast<int>(frame_id)); | 120 is_main_frame ? 0 : static_cast<int>(frame_id)); |
| 120 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 121 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 121 args.Append(dict); | 122 args.Append(dict); |
| 122 | 123 |
| 123 std::string json_args; | 124 std::string json_args; |
| 124 base::JSONWriter::Write(&args, false, &json_args); | 125 base::JSONWriter::Write(&args, false, &json_args); |
| 125 DispatchEvent(tab_contents->profile(), keys::kOnDOMContentLoaded, json_args); | 126 DispatchEvent(tab_contents->context(), keys::kOnDOMContentLoaded, json_args); |
| 126 } | 127 } |
| 127 | 128 |
| 128 // Constructs and dispatches an onCompleted event. | 129 // Constructs and dispatches an onCompleted event. |
| 129 void DispatchOnCompleted(TabContents* tab_contents, | 130 void DispatchOnCompleted(TabContents* tab_contents, |
| 130 const GURL& url, | 131 const GURL& url, |
| 131 bool is_main_frame, | 132 bool is_main_frame, |
| 132 int64 frame_id) { | 133 int64 frame_id) { |
| 133 ListValue args; | 134 ListValue args; |
| 134 DictionaryValue* dict = new DictionaryValue(); | 135 DictionaryValue* dict = new DictionaryValue(); |
| 135 dict->SetInteger(keys::kTabIdKey, | 136 dict->SetInteger(keys::kTabIdKey, |
| 136 ExtensionTabUtil::GetTabId(tab_contents)); | 137 ExtensionTabUtil::GetTabId(tab_contents)); |
| 137 dict->SetString(keys::kUrlKey, url.spec()); | 138 dict->SetString(keys::kUrlKey, url.spec()); |
| 138 dict->SetInteger(keys::kFrameIdKey, | 139 dict->SetInteger(keys::kFrameIdKey, |
| 139 is_main_frame ? 0 : static_cast<int>(frame_id)); | 140 is_main_frame ? 0 : static_cast<int>(frame_id)); |
| 140 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 141 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 141 args.Append(dict); | 142 args.Append(dict); |
| 142 | 143 |
| 143 std::string json_args; | 144 std::string json_args; |
| 144 base::JSONWriter::Write(&args, false, &json_args); | 145 base::JSONWriter::Write(&args, false, &json_args); |
| 145 DispatchEvent(tab_contents->profile(), keys::kOnCompleted, json_args); | 146 DispatchEvent(tab_contents->context(), keys::kOnCompleted, json_args); |
| 146 } | 147 } |
| 147 | 148 |
| 148 // Constructs and dispatches an onBeforeRetarget event. | 149 // Constructs and dispatches an onBeforeRetarget event. |
| 149 void DispatchOnBeforeRetarget(TabContents* tab_contents, | 150 void DispatchOnBeforeRetarget(TabContents* tab_contents, |
| 150 Profile* profile, | 151 Profile* profile, |
| 151 const GURL& opener_url, | 152 const GURL& opener_url, |
| 152 const GURL& target_url) { | 153 const GURL& target_url) { |
| 153 ListValue args; | 154 ListValue args; |
| 154 DictionaryValue* dict = new DictionaryValue(); | 155 DictionaryValue* dict = new DictionaryValue(); |
| 155 dict->SetInteger(keys::kSourceTabIdKey, | 156 dict->SetInteger(keys::kSourceTabIdKey, |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 break; | 273 break; |
| 273 | 274 |
| 274 default: | 275 default: |
| 275 NOTREACHED(); | 276 NOTREACHED(); |
| 276 } | 277 } |
| 277 } | 278 } |
| 278 | 279 |
| 279 void ExtensionWebNavigationEventRouter::CreatingNewWindow( | 280 void ExtensionWebNavigationEventRouter::CreatingNewWindow( |
| 280 TabContents* tab_contents, | 281 TabContents* tab_contents, |
| 281 const ViewHostMsg_CreateWindow_Params* details) { | 282 const ViewHostMsg_CreateWindow_Params* details) { |
| 282 if (profile_->IsSameProfile(tab_contents->profile())) { | 283 Profile* tab_profile = static_cast<Profile*>(tab_contents->context()); |
| 284 if (profile_->IsSameProfile(tab_profile)) { |
| 283 DispatchOnBeforeRetarget(tab_contents, | 285 DispatchOnBeforeRetarget(tab_contents, |
| 284 tab_contents->profile(), | 286 tab_profile, |
| 285 details->opener_url, | 287 details->opener_url, |
| 286 details->target_url); | 288 details->target_url); |
| 287 } | 289 } |
| 288 } | 290 } |
| 289 | 291 |
| 290 | 292 |
| 291 // ExtensionWebNavigationTabObserver ------------------------------------------ | 293 // ExtensionWebNavigationTabObserver ------------------------------------------ |
| 292 | 294 |
| 293 ExtensionWebNavigationTabObserver::ExtensionWebNavigationTabObserver( | 295 ExtensionWebNavigationTabObserver::ExtensionWebNavigationTabObserver( |
| 294 TabContents* tab_contents) | 296 TabContents* tab_contents) |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 dict->SetString(keys::kUrlKey, validated_url.spec()); | 347 dict->SetString(keys::kUrlKey, validated_url.spec()); |
| 346 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 348 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
| 347 dict->SetString(keys::kErrorKey, | 349 dict->SetString(keys::kErrorKey, |
| 348 std::string(net::ErrorToString(error_code))); | 350 std::string(net::ErrorToString(error_code))); |
| 349 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 351 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 350 args.Append(dict); | 352 args.Append(dict); |
| 351 | 353 |
| 352 std::string json_args; | 354 std::string json_args; |
| 353 base::JSONWriter::Write(&args, false, &json_args); | 355 base::JSONWriter::Write(&args, false, &json_args); |
| 354 navigation_state_.ErrorOccurredInFrame(frame_id); | 356 navigation_state_.ErrorOccurredInFrame(frame_id); |
| 355 DispatchEvent(tab_contents()->profile(), keys::kOnErrorOccurred, json_args); | 357 DispatchEvent(tab_contents()->context(), keys::kOnErrorOccurred, json_args); |
| 356 } | 358 } |
| 357 | 359 |
| 358 void ExtensionWebNavigationTabObserver::DocumentLoadedInFrame( | 360 void ExtensionWebNavigationTabObserver::DocumentLoadedInFrame( |
| 359 int64 frame_id) { | 361 int64 frame_id) { |
| 360 if (!navigation_state_.CanSendEvents(frame_id)) | 362 if (!navigation_state_.CanSendEvents(frame_id)) |
| 361 return; | 363 return; |
| 362 DispatchOnDOMContentLoaded(tab_contents(), | 364 DispatchOnDOMContentLoaded(tab_contents(), |
| 363 navigation_state_.GetUrl(frame_id), | 365 navigation_state_.GetUrl(frame_id), |
| 364 navigation_state_.IsMainFrame(frame_id), | 366 navigation_state_.IsMainFrame(frame_id), |
| 365 frame_id); | 367 frame_id); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 384 const GURL& url, | 386 const GURL& url, |
| 385 const GURL& referrer, | 387 const GURL& referrer, |
| 386 WindowOpenDisposition disposition, | 388 WindowOpenDisposition disposition, |
| 387 PageTransition::Type transition) { | 389 PageTransition::Type transition) { |
| 388 if (disposition != NEW_FOREGROUND_TAB && | 390 if (disposition != NEW_FOREGROUND_TAB && |
| 389 disposition != NEW_BACKGROUND_TAB && | 391 disposition != NEW_BACKGROUND_TAB && |
| 390 disposition != NEW_WINDOW && | 392 disposition != NEW_WINDOW && |
| 391 disposition != OFF_THE_RECORD) { | 393 disposition != OFF_THE_RECORD) { |
| 392 return; | 394 return; |
| 393 } | 395 } |
| 394 Profile* profile = tab_contents()->profile(); | 396 Profile* profile = static_cast<Profile*>(tab_contents()->context()); |
| 395 if (disposition == OFF_THE_RECORD) { | 397 if (disposition == OFF_THE_RECORD) { |
| 396 if (!profile->HasOffTheRecordProfile()) { | 398 if (!profile->HasOffTheRecordProfile()) { |
| 397 NOTREACHED(); | 399 NOTREACHED(); |
| 398 return; | 400 return; |
| 399 } | 401 } |
| 400 profile = profile->GetOffTheRecordProfile(); | 402 profile = profile->GetOffTheRecordProfile(); |
| 401 } | 403 } |
| 402 DispatchOnBeforeRetarget(tab_contents(), | 404 DispatchOnBeforeRetarget(tab_contents(), |
| 403 profile, | 405 profile, |
| 404 tab_contents()->GetURL(), | 406 tab_contents()->GetURL(), |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 transition_type); | 444 transition_type); |
| 443 DispatchOnDOMContentLoaded(tab_contents(), | 445 DispatchOnDOMContentLoaded(tab_contents(), |
| 444 url, | 446 url, |
| 445 is_main_frame, | 447 is_main_frame, |
| 446 frame_id); | 448 frame_id); |
| 447 DispatchOnCompleted(tab_contents(), | 449 DispatchOnCompleted(tab_contents(), |
| 448 url, | 450 url, |
| 449 is_main_frame, | 451 is_main_frame, |
| 450 frame_id); | 452 frame_id); |
| 451 } | 453 } |
| OLD | NEW |