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 #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_model_factory.h" | 8 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 9 #include "chrome/browser/defaults.h" |
| 10 #include "chrome/browser/prefs/pref_service_syncable.h" |
9 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper_delegate.h" | 12 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper_delegate.h" |
11 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 13 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
| 14 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/common/url_constants.h" |
| 16 #include "content/public/browser/navigation_entry.h" |
12 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
13 | 18 |
14 DEFINE_WEB_CONTENTS_USER_DATA_KEY(BookmarkTabHelper); | 19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(BookmarkTabHelper); |
15 | 20 |
16 namespace { | |
17 | |
18 bool CanShowBookmarkBar(content::WebUI* ui) { | |
19 if (!ui) | |
20 return false; | |
21 NewTabUI* new_tab = NewTabUI::FromWebUIController(ui->GetController()); | |
22 return new_tab && new_tab->CanShowBookmarkBar(); | |
23 } | |
24 | |
25 } // namespace | |
26 | |
27 BookmarkTabHelper::~BookmarkTabHelper() { | 21 BookmarkTabHelper::~BookmarkTabHelper() { |
28 if (bookmark_model_) | 22 if (bookmark_model_) |
29 bookmark_model_->RemoveObserver(this); | 23 bookmark_model_->RemoveObserver(this); |
30 } | 24 } |
31 | 25 |
32 bool BookmarkTabHelper::ShouldShowBookmarkBar() const { | 26 bool BookmarkTabHelper::ShouldShowBookmarkBar() const { |
33 if (web_contents()->ShowingInterstitialPage()) | 27 if (web_contents()->ShowingInterstitialPage()) |
34 return false; | 28 return false; |
35 | 29 |
36 // See WebContents::GetWebUIForCurrentState() comment for more info. This case | 30 // For non-first loads, we want to use the committed entry. This is so the |
37 // is very similar, but for non-first loads, we want to use the committed | 31 // bookmarks bar disappears at the same time the page does. |
38 // entry. This is so the bookmarks bar disappears at the same time the page | 32 content::NavigationEntry* entry = |
39 // does. | 33 web_contents()->GetController().GetLastCommittedEntry(); |
40 if (web_contents()->GetController().GetLastCommittedEntry()) { | 34 if (!entry) |
41 // Not the first load, always use the committed Web UI. | 35 entry = web_contents()->GetController().GetVisibleEntry(); |
42 return CanShowBookmarkBar(web_contents()->GetCommittedWebUI()); | 36 if (!entry) |
| 37 return false; |
| 38 |
| 39 GURL url = entry->GetVirtualURL(); |
| 40 if (url != GURL(chrome::kChromeUINewTabURL) || |
| 41 url.SchemeIs(chrome::kViewSourceScheme)) { |
| 42 return false; |
43 } | 43 } |
44 | 44 |
45 // When it's the first load, we know either the pending one or the committed | 45 Profile* profile = |
46 // one will have the Web UI in it (see GetWebUIForCurrentState), and only one | 46 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
47 // of them will be valid, so we can just check both. | 47 PrefServiceSyncable* prefs = profile->GetPrefs(); |
48 return CanShowBookmarkBar(web_contents()->GetWebUI()); | 48 bool disabled_by_policy = |
| 49 prefs->IsManagedPreference(prefs::kShowBookmarkBar) && |
| 50 !prefs->GetBoolean(prefs::kShowBookmarkBar); |
| 51 return browser_defaults::bookmarks_enabled && !disabled_by_policy; |
49 } | 52 } |
50 | 53 |
51 BookmarkTabHelper::BookmarkTabHelper(content::WebContents* web_contents) | 54 BookmarkTabHelper::BookmarkTabHelper(content::WebContents* web_contents) |
52 : content::WebContentsObserver(web_contents), | 55 : content::WebContentsObserver(web_contents), |
53 is_starred_(false), | 56 is_starred_(false), |
54 bookmark_model_(NULL), | 57 bookmark_model_(NULL), |
55 delegate_(NULL), | 58 delegate_(NULL), |
56 bookmark_drag_(NULL) { | 59 bookmark_drag_(NULL) { |
57 Profile* profile = | 60 Profile* profile = |
58 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 61 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 void BookmarkTabHelper::BookmarkNodeChanged(BookmarkModel* model, | 96 void BookmarkTabHelper::BookmarkNodeChanged(BookmarkModel* model, |
94 const BookmarkNode* node) { | 97 const BookmarkNode* node) { |
95 UpdateStarredStateForCurrentURL(); | 98 UpdateStarredStateForCurrentURL(); |
96 } | 99 } |
97 | 100 |
98 void BookmarkTabHelper::DidNavigateMainFrame( | 101 void BookmarkTabHelper::DidNavigateMainFrame( |
99 const content::LoadCommittedDetails& /*details*/, | 102 const content::LoadCommittedDetails& /*details*/, |
100 const content::FrameNavigateParams& /*params*/) { | 103 const content::FrameNavigateParams& /*params*/) { |
101 UpdateStarredStateForCurrentURL(); | 104 UpdateStarredStateForCurrentURL(); |
102 } | 105 } |
OLD | NEW |