OLD | NEW |
---|---|
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/browser.h" | |
19 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 20 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
20 #include "chrome/browser/view_type_utils.h" | 21 #include "chrome/browser/view_type_utils.h" |
21 #include "chrome/common/chrome_notification_types.h" | 22 #include "chrome/common/chrome_notification_types.h" |
22 #include "chrome/common/extensions/api/web_navigation.h" | 23 #include "chrome/common/extensions/api/web_navigation.h" |
23 #include "chrome/common/extensions/event_filtering_info.h" | 24 #include "chrome/common/extensions/event_filtering_info.h" |
24 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
25 #include "content/public/browser/resource_request_details.h" | 26 #include "content/public/browser/resource_request_details.h" |
26 #include "content/public/browser/navigation_details.h" | 27 #include "content/public/browser/navigation_details.h" |
27 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
28 #include "content/public/browser/notification_types.h" | 29 #include "content/public/browser/notification_types.h" |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
199 dict->SetInteger(keys::kSourceTabIdKey, | 200 dict->SetInteger(keys::kSourceTabIdKey, |
200 ExtensionTabUtil::GetTabId(web_contents)); | 201 ExtensionTabUtil::GetTabId(web_contents)); |
201 dict->SetInteger(keys::kSourceFrameIdKey, | 202 dict->SetInteger(keys::kSourceFrameIdKey, |
202 GetFrameId(source_frame_is_main_frame, source_frame_id)); | 203 GetFrameId(source_frame_is_main_frame, source_frame_id)); |
203 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); | 204 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); |
204 dict->SetInteger(keys::kTabIdKey, | 205 dict->SetInteger(keys::kTabIdKey, |
205 ExtensionTabUtil::GetTabId(target_web_contents)); | 206 ExtensionTabUtil::GetTabId(target_web_contents)); |
206 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 207 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
207 args.Append(dict); | 208 args.Append(dict); |
208 | 209 |
210 // TODO(koz): Why not web_contents->GetURL() instead of target_url or both? | |
209 DispatchEvent(browser_context, keys::kOnCreatedNavigationTarget, args, | 211 DispatchEvent(browser_context, keys::kOnCreatedNavigationTarget, args, |
210 target_url); | 212 target_url); |
211 } | 213 } |
212 | 214 |
213 // Constructs and dispatches an onErrorOccurred event. | 215 // Constructs and dispatches an onErrorOccurred event. |
214 void DispatchOnErrorOccurred(WebContents* web_contents, | 216 void DispatchOnErrorOccurred(WebContents* web_contents, |
215 const GURL& url, | 217 const GURL& url, |
216 int64 frame_id, | 218 int64 frame_id, |
217 bool is_main_frame, | 219 bool is_main_frame, |
218 int error_code) { | 220 int error_code) { |
219 ListValue args; | 221 ListValue args; |
220 DictionaryValue* dict = new DictionaryValue(); | 222 DictionaryValue* dict = new DictionaryValue(); |
221 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); | 223 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); |
222 dict->SetString(keys::kUrlKey, url.spec()); | 224 dict->SetString(keys::kUrlKey, url.spec()); |
223 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 225 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
224 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); | 226 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); |
225 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 227 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
226 args.Append(dict); | 228 args.Append(dict); |
227 | 229 |
228 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnErrorOccurred, | 230 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnErrorOccurred, |
229 args, url); | 231 args, url); |
230 } | 232 } |
231 | 233 |
234 // Constructs and dispatches an onTabReplaced event. | |
235 void DispatchOnTabReplaced( | |
236 WebContents* old_web_contents, | |
237 BrowserContext* browser_context, | |
238 WebContents* new_web_contents) { | |
239 ListValue args; | |
240 DictionaryValue* dict = new DictionaryValue(); | |
241 dict->SetInteger(keys::kSourceTabIdKey, | |
242 ExtensionTabUtil::GetTabId(old_web_contents)); | |
243 dict->SetInteger(keys::kTabIdKey, | |
244 ExtensionTabUtil::GetTabId(new_web_contents)); | |
245 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | |
246 args.Append(dict); | |
247 | |
248 // TODO(koz): Why not old_web_contents->GetURL() or both? | |
249 DispatchEvent(browser_context, keys::kOnTabReplaced, args, | |
250 new_web_contents->GetURL()); | |
251 } | |
252 | |
232 } // namespace | 253 } // namespace |
233 | 254 |
234 | 255 |
235 // FrameNavigationState ------------------------------------------------------- | 256 // FrameNavigationState ------------------------------------------------------- |
236 | 257 |
237 // static | 258 // static |
238 bool FrameNavigationState::allow_extension_scheme_ = false; | 259 bool FrameNavigationState::allow_extension_scheme_ = false; |
239 | 260 |
240 FrameNavigationState::FrameNavigationState() | 261 FrameNavigationState::FrameNavigationState() |
241 : main_frame_id_(-1) { | 262 : main_frame_id_(-1) { |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
390 source_frame_is_main_frame(source_frame_is_main_frame), | 411 source_frame_is_main_frame(source_frame_is_main_frame), |
391 target_web_contents(target_web_contents), | 412 target_web_contents(target_web_contents), |
392 target_url(target_url) { | 413 target_url(target_url) { |
393 } | 414 } |
394 | 415 |
395 WebNavigationEventRouter::PendingWebContents::~PendingWebContents() {} | 416 WebNavigationEventRouter::PendingWebContents::~PendingWebContents() {} |
396 | 417 |
397 WebNavigationEventRouter::WebNavigationEventRouter(Profile* profile) | 418 WebNavigationEventRouter::WebNavigationEventRouter(Profile* profile) |
398 : profile_(profile) {} | 419 : profile_(profile) {} |
399 | 420 |
400 WebNavigationEventRouter::~WebNavigationEventRouter() {} | 421 WebNavigationEventRouter::~WebNavigationEventRouter() {} |
battre
2012/07/05 15:38:46
The WebNavigationEventRouter is owned by the Exten
jochen (gone - plz use gerrit)
2012/07/06 11:13:50
Done.
| |
401 | 422 |
402 void WebNavigationEventRouter::Init() { | 423 void WebNavigationEventRouter::Init() { |
403 if (registrar_.IsEmpty()) { | 424 if (!registrar_.IsEmpty()) |
404 registrar_.Add(this, | 425 return; |
405 chrome::NOTIFICATION_RETARGETING, | 426 registrar_.Add(this, |
406 content::NotificationService::AllSources()); | 427 chrome::NOTIFICATION_RETARGETING, |
407 registrar_.Add(this, | 428 content::NotificationService::AllSources()); |
408 chrome::NOTIFICATION_TAB_ADDED, | 429 registrar_.Add(this, |
409 content::NotificationService::AllSources()); | 430 chrome::NOTIFICATION_TAB_ADDED, |
410 registrar_.Add(this, | 431 content::NotificationService::AllSources()); |
411 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 432 registrar_.Add(this, |
412 content::NotificationService::AllSources()); | 433 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
434 content::NotificationService::AllSources()); | |
435 | |
436 BrowserList::AddObserver(this); | |
437 for (BrowserList::const_iterator iter = BrowserList::begin(); | |
438 iter != BrowserList::end(); ++iter) { | |
439 OnBrowserAdded(*iter); | |
413 } | 440 } |
414 } | 441 } |
415 | 442 |
443 void WebNavigationEventRouter::OnBrowserAdded(Browser* browser) { | |
444 if (!profile_->IsSameProfile(browser->profile())) | |
445 return; | |
446 browser->tab_strip_model()->AddObserver(this); | |
447 } | |
448 | |
449 void WebNavigationEventRouter::OnBrowserRemoved(Browser* browser) { | |
450 if (!profile_->IsSameProfile(browser->profile())) | |
451 return; | |
452 browser->tab_strip_model()->RemoveObserver(this); | |
453 } | |
454 | |
455 void WebNavigationEventRouter::TabReplacedAt( | |
456 TabStripModel* tab_strip_model, | |
457 TabContents* old_contents, | |
458 TabContents* new_contents, | |
459 int index) { | |
460 WebNavigationTabObserver* tab_observer = | |
461 WebNavigationTabObserver::Get(old_contents->web_contents()); | |
462 if (!tab_observer) { | |
463 // If you hit this DCHECK(), please add reproduction steps to | |
464 // http://crbug.com/109464. | |
465 DCHECK(chrome::GetViewType(old_contents->web_contents()) != | |
466 chrome::VIEW_TYPE_TAB_CONTENTS); | |
467 return; | |
468 } | |
469 const FrameNavigationState& frame_navigation_state = | |
470 tab_observer->frame_navigation_state(); | |
471 | |
472 if (!frame_navigation_state.IsValidUrl( | |
473 old_contents->web_contents()->GetURL()) || | |
474 !frame_navigation_state.IsValidUrl( | |
475 new_contents->web_contents()->GetURL())) | |
476 return; | |
477 | |
478 DispatchOnTabReplaced( | |
479 old_contents->web_contents(), | |
480 profile_, | |
481 new_contents->web_contents()); | |
482 } | |
483 | |
416 void WebNavigationEventRouter::Observe( | 484 void WebNavigationEventRouter::Observe( |
417 int type, | 485 int type, |
418 const content::NotificationSource& source, | 486 const content::NotificationSource& source, |
419 const content::NotificationDetails& details) { | 487 const content::NotificationDetails& details) { |
420 switch (type) { | 488 switch (type) { |
421 case chrome::NOTIFICATION_RETARGETING: { | 489 case chrome::NOTIFICATION_RETARGETING: { |
422 Profile* profile = content::Source<Profile>(source).ptr(); | 490 Profile* profile = content::Source<Profile>(source).ptr(); |
423 if (profile->GetOriginalProfile() == profile_) { | 491 if (profile->GetOriginalProfile() == profile_) { |
424 Retargeting( | 492 Retargeting( |
425 content::Details<const RetargetingDetails>(details).ptr()); | 493 content::Details<const RetargetingDetails>(details).ptr()); |
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
801 frame->frame_id = GetFrameId(navigation_state.IsMainFrame(frame_id), | 869 frame->frame_id = GetFrameId(navigation_state.IsMainFrame(frame_id), |
802 frame_id); | 870 frame_id); |
803 frame->error_occurred = navigation_state.GetErrorOccurredInFrame(frame_id); | 871 frame->error_occurred = navigation_state.GetErrorOccurredInFrame(frame_id); |
804 result_list.push_back(frame); | 872 result_list.push_back(frame); |
805 } | 873 } |
806 result_.reset(GetAllFrames::Result::Create(result_list)); | 874 result_.reset(GetAllFrames::Result::Create(result_list)); |
807 return true; | 875 return true; |
808 } | 876 } |
809 | 877 |
810 } // namespace extensions | 878 } // namespace extensions |
OLD | NEW |