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 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h" | 5 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h" |
6 | 6 |
7 #include "chrome/browser/bookmarks/bookmark_model.h" | 7 #include "chrome/browser/bookmarks/bookmark_model.h" |
8 #include "chrome/browser/bookmarks/bookmark_node_data.h" | 8 #include "chrome/browser/bookmarks/bookmark_node_data.h" |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper_delegate.h" | 10 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper_delegate.h" |
11 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 11 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
12 #include "chrome/browser/ui/webui/chrome_web_ui.h" | 12 #include "chrome/browser/ui/webui/chrome_web_ui.h" |
13 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
14 #include "content/browser/tab_contents/navigation_controller.h" | 14 #include "content/browser/tab_contents/navigation_controller.h" |
15 #include "content/browser/tab_contents/tab_contents.h" | 15 #include "content/browser/tab_contents/tab_contents.h" |
16 #include "content/common/notification_service.h" | 16 #include "content/common/notification_service.h" |
17 | 17 |
18 static bool ForceBookmarkBarVisible(WebUI* ui) { | 18 namespace { |
19 return ui && static_cast<ChromeWebUI*>(ui)->force_bookmark_bar_visible(); | 19 |
| 20 bool CanShowBookmarkBar(WebUI* ui) { |
| 21 return ui && static_cast<ChromeWebUI*>(ui)->CanShowBookmarkBar(); |
20 } | 22 } |
21 | 23 |
| 24 } // namespace |
| 25 |
22 BookmarkTabHelper::BookmarkTabHelper(TabContentsWrapper* tab_contents) | 26 BookmarkTabHelper::BookmarkTabHelper(TabContentsWrapper* tab_contents) |
23 : TabContentsObserver(tab_contents->tab_contents()), | 27 : TabContentsObserver(tab_contents->tab_contents()), |
24 is_starred_(false), | 28 is_starred_(false), |
25 tab_contents_wrapper_(tab_contents), | 29 tab_contents_wrapper_(tab_contents), |
26 delegate_(NULL), | 30 delegate_(NULL), |
27 bookmark_drag_(NULL) { | 31 bookmark_drag_(NULL) { |
28 // Register for notifications about URL starredness changing on any profile. | 32 // Register for notifications about URL starredness changing on any profile. |
29 registrar_.Add(this, chrome::NOTIFICATION_URLS_STARRED, | 33 registrar_.Add(this, chrome::NOTIFICATION_URLS_STARRED, |
30 NotificationService::AllBrowserContextsAndSources()); | 34 NotificationService::AllBrowserContextsAndSources()); |
31 registrar_.Add(this, chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED, | 35 registrar_.Add(this, chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED, |
32 NotificationService::AllBrowserContextsAndSources()); | 36 NotificationService::AllBrowserContextsAndSources()); |
33 } | 37 } |
34 | 38 |
35 BookmarkTabHelper::~BookmarkTabHelper() { | 39 BookmarkTabHelper::~BookmarkTabHelper() { |
36 // We don't want any notifications while we're running our destructor. | 40 // We don't want any notifications while we're running our destructor. |
37 registrar_.RemoveAll(); | 41 registrar_.RemoveAll(); |
38 } | 42 } |
39 | 43 |
40 bool BookmarkTabHelper::ShouldShowBookmarkBar() { | 44 bool BookmarkTabHelper::ShouldShowBookmarkBar() { |
41 if (tab_contents()->showing_interstitial_page()) | 45 if (tab_contents()->showing_interstitial_page()) |
42 return false; | 46 return false; |
43 | 47 |
44 // See TabContents::GetWebUIForCurrentState() comment for more info. This case | 48 // See TabContents::GetWebUIForCurrentState() comment for more info. This case |
45 // is very similar, but for non-first loads, we want to use the committed | 49 // is very similar, but for non-first loads, we want to use the committed |
46 // entry. This is so the bookmarks bar disappears at the same time the page | 50 // entry. This is so the bookmarks bar disappears at the same time the page |
47 // does. | 51 // does. |
48 if (tab_contents()->controller().GetLastCommittedEntry()) { | 52 if (tab_contents()->controller().GetLastCommittedEntry()) { |
49 // Not the first load, always use the committed Web UI. | 53 // Not the first load, always use the committed Web UI. |
50 return ForceBookmarkBarVisible(tab_contents()->committed_web_ui()); | 54 return CanShowBookmarkBar(tab_contents()->committed_web_ui()); |
51 } | 55 } |
52 | 56 |
53 // When it's the first load, we know either the pending one or the committed | 57 // When it's the first load, we know either the pending one or the committed |
54 // one will have the Web UI in it (see GetWebUIForCurrentState), and only one | 58 // one will have the Web UI in it (see GetWebUIForCurrentState), and only one |
55 // of them will be valid, so we can just check both. | 59 // of them will be valid, so we can just check both. |
56 return ForceBookmarkBarVisible(tab_contents()->web_ui()); | 60 return CanShowBookmarkBar(tab_contents()->web_ui()); |
57 } | 61 } |
58 | 62 |
59 void BookmarkTabHelper::DidNavigateMainFramePostCommit( | 63 void BookmarkTabHelper::DidNavigateMainFramePostCommit( |
60 const content::LoadCommittedDetails& /*details*/, | 64 const content::LoadCommittedDetails& /*details*/, |
61 const ViewHostMsg_FrameNavigate_Params& /*params*/) { | 65 const ViewHostMsg_FrameNavigate_Params& /*params*/) { |
62 UpdateStarredStateForCurrentURL(); | 66 UpdateStarredStateForCurrentURL(); |
63 } | 67 } |
64 | 68 |
65 void BookmarkTabHelper::Observe(int type, | 69 void BookmarkTabHelper::Observe(int type, |
66 const NotificationSource& source, | 70 const NotificationSource& source, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 void BookmarkTabHelper::UpdateStarredStateForCurrentURL() { | 102 void BookmarkTabHelper::UpdateStarredStateForCurrentURL() { |
99 Profile* profile = | 103 Profile* profile = |
100 Profile::FromBrowserContext(tab_contents()->browser_context()); | 104 Profile::FromBrowserContext(tab_contents()->browser_context()); |
101 BookmarkModel* model = profile->GetBookmarkModel(); | 105 BookmarkModel* model = profile->GetBookmarkModel(); |
102 const bool old_state = is_starred_; | 106 const bool old_state = is_starred_; |
103 is_starred_ = (model && model->IsBookmarked(tab_contents()->GetURL())); | 107 is_starred_ = (model && model->IsBookmarked(tab_contents()->GetURL())); |
104 | 108 |
105 if (is_starred_ != old_state && delegate()) | 109 if (is_starred_ != old_state && delegate()) |
106 delegate()->URLStarredChanged(tab_contents_wrapper_, is_starred_); | 110 delegate()->URLStarredChanged(tab_contents_wrapper_, is_starred_); |
107 } | 111 } |
OLD | NEW |