OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 #include "chrome/browser/google/google_url_tracker_map_entry.h" | 5 #include "chrome/browser/google/google_url_tracker_map_entry.h" |
6 | 6 |
7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
8 #include "chrome/browser/google/google_url_tracker.h" | 8 #include "chrome/browser/google/google_url_tracker.h" |
9 #include "components/infobars/core/infobar.h" | 9 #include "components/infobars/core/infobar.h" |
10 #include "content/public/browser/notification_details.h" | |
11 #include "content/public/browser/notification_source.h" | |
12 | 10 |
13 GoogleURLTrackerMapEntry::GoogleURLTrackerMapEntry( | 11 GoogleURLTrackerMapEntry::GoogleURLTrackerMapEntry( |
14 GoogleURLTracker* google_url_tracker, | 12 GoogleURLTracker* google_url_tracker, |
15 infobars::InfoBarManager* infobar_manager, | 13 infobars::InfoBarManager* infobar_manager, |
16 scoped_ptr<GoogleURLTrackerNavigationHelper> navigation_helper) | 14 scoped_ptr<GoogleURLTrackerNavigationHelper> navigation_helper) |
17 : google_url_tracker_(google_url_tracker), | 15 : google_url_tracker_(google_url_tracker), |
18 infobar_manager_(infobar_manager), | 16 infobar_manager_(infobar_manager), |
19 infobar_delegate_(NULL), | 17 infobar_delegate_(NULL), |
20 navigation_helper_(navigation_helper.Pass()) { | 18 navigation_helper_(navigation_helper.Pass()), |
| 19 observing_(false) { |
| 20 DCHECK(infobar_manager_); |
21 } | 21 } |
22 | 22 |
23 GoogleURLTrackerMapEntry::~GoogleURLTrackerMapEntry() { | 23 GoogleURLTrackerMapEntry::~GoogleURLTrackerMapEntry() { |
24 } | 24 if (observing_) |
25 | 25 infobar_manager_->RemoveObserver(this); |
26 void GoogleURLTrackerMapEntry::Observe( | |
27 int type, | |
28 const content::NotificationSource& source, | |
29 const content::NotificationDetails& details) { | |
30 DCHECK(infobar_delegate_); | |
31 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type); | |
32 DCHECK_EQ(infobar_manager_, | |
33 content::Source<infobars::InfoBarManager>(source).ptr()); | |
34 if (content::Details<infobars::InfoBar::RemovedDetails>( | |
35 details)->first->delegate() == infobar_delegate_) { | |
36 google_url_tracker_->DeleteMapEntryForManager(infobar_manager_); | |
37 // WARNING: At this point |this| has been deleted! | |
38 } | |
39 } | 26 } |
40 | 27 |
41 void GoogleURLTrackerMapEntry::SetInfoBarDelegate( | 28 void GoogleURLTrackerMapEntry::SetInfoBarDelegate( |
42 GoogleURLTrackerInfoBarDelegate* infobar_delegate) { | 29 GoogleURLTrackerInfoBarDelegate* infobar_delegate) { |
43 DCHECK(!infobar_delegate_); | 30 DCHECK(!infobar_delegate_); |
44 | 31 |
45 // Transfer ownership of |navigation_helper_| to the infobar delegate as the | 32 // Transfer ownership of |navigation_helper_| to the infobar delegate as the |
46 // infobar delegate has need of it after this object has been destroyed in | 33 // infobar delegate has need of it after this object has been destroyed in |
47 // the case where the user accepts the new Google URL. | 34 // the case where the user accepts the new Google URL. |
48 infobar_delegate->set_navigation_helper(navigation_helper_.Pass()); | 35 infobar_delegate->set_navigation_helper(navigation_helper_.Pass()); |
49 infobar_delegate_ = infobar_delegate; | 36 infobar_delegate_ = infobar_delegate; |
50 registrar_.Add(this, | 37 infobar_manager_->AddObserver(this); |
51 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | 38 observing_ = true; |
52 content::Source<infobars::InfoBarManager>(infobar_manager_)); | |
53 } | 39 } |
54 | 40 |
55 void GoogleURLTrackerMapEntry::Close(bool redo_search) { | 41 void GoogleURLTrackerMapEntry::Close(bool redo_search) { |
56 if (infobar_delegate_) { | 42 if (infobar_delegate_) { |
57 infobar_delegate_->Close(redo_search); | 43 infobar_delegate_->Close(redo_search); |
58 } else { | 44 } else { |
59 // WARNING: |infobar_manager_| may point to a deleted object. Do not | 45 // WARNING: |infobar_manager_| may point to a deleted object. Do not |
60 // dereference it! See GoogleURLTracker::OnTabClosed(). | 46 // dereference it! See GoogleURLTracker::OnTabClosed(). |
61 google_url_tracker_->DeleteMapEntryForManager(infobar_manager_); | 47 google_url_tracker_->DeleteMapEntryForManager(infobar_manager_); |
62 } | 48 } |
63 // WARNING: At this point |this| has been deleted! | 49 // WARNING: At this point |this| has been deleted! |
64 } | 50 } |
| 51 |
| 52 void GoogleURLTrackerMapEntry::OnInfoBarRemoved(infobars::InfoBar* infobar, |
| 53 bool animate) { |
| 54 DCHECK(infobar_delegate_); |
| 55 if (infobar->delegate() == infobar_delegate_) { |
| 56 google_url_tracker_->DeleteMapEntryForManager(infobar_manager_); |
| 57 // WARNING: At this point |this| has been deleted! |
| 58 } |
| 59 } |
| 60 |
| 61 void GoogleURLTrackerMapEntry::OnManagerShuttingDown( |
| 62 infobars::InfoBarManager* manager) { |
| 63 DCHECK(observing_); |
| 64 DCHECK_EQ(infobar_manager_, manager); |
| 65 manager->RemoveObserver(this); |
| 66 observing_ = false; |
| 67 } |
OLD | NEW |