Home | History | Annotate | Download | only in bookmarks
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
      6 
      7 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
      8 #include "chrome/browser/defaults.h"
      9 #include "chrome/browser/prefs/pref_service_syncable.h"
     10 #include "chrome/browser/profiles/profile.h"
     11 #include "chrome/browser/search/search.h"
     12 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper_delegate.h"
     13 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
     14 #include "chrome/browser/ui/sad_tab.h"
     15 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
     16 #include "chrome/common/pref_names.h"
     17 #include "components/bookmarks/browser/bookmark_model.h"
     18 #include "content/public/browser/navigation_entry.h"
     19 #include "content/public/browser/web_contents.h"
     20 
     21 namespace {
     22 
     23 bool IsNTPWebUI(content::WebContents* web_contents) {
     24   content::WebUI* web_ui = NULL;
     25   // Use the committed entry so the bookmarks bar disappears at the same time
     26   // the page does.
     27   if (web_contents->GetController().GetLastCommittedEntry())
     28     web_ui = web_contents->GetCommittedWebUI();
     29   else
     30     web_ui = web_contents->GetWebUI();
     31   return web_ui && NewTabUI::FromWebUIController(web_ui->GetController());
     32 }
     33 
     34 bool IsInstantNTP(content::WebContents* web_contents) {
     35   // Use the committed entry so the bookmarks bar disappears at the same time
     36   // the page does.
     37   const content::NavigationEntry* entry =
     38       web_contents->GetController().GetLastCommittedEntry();
     39   if (!entry)
     40     entry = web_contents->GetController().GetVisibleEntry();
     41   return chrome::NavEntryIsInstantNTP(web_contents, entry);
     42 }
     43 
     44 }  // namespace
     45 
     46 DEFINE_WEB_CONTENTS_USER_DATA_KEY(BookmarkTabHelper);
     47 
     48 BookmarkTabHelper::~BookmarkTabHelper() {
     49   if (bookmark_model_)
     50     bookmark_model_->RemoveObserver(this);
     51 }
     52 
     53 bool BookmarkTabHelper::ShouldShowBookmarkBar() const {
     54   if (web_contents()->ShowingInterstitialPage())
     55     return false;
     56 
     57   if (chrome::SadTab::ShouldShow(web_contents()->GetCrashedStatus()))
     58     return false;
     59 
     60   if (!browser_defaults::bookmarks_enabled)
     61     return false;
     62 
     63   Profile* profile =
     64       Profile::FromBrowserContext(web_contents()->GetBrowserContext());
     65 
     66 #if !defined(OS_CHROMEOS)
     67   if (profile->IsGuestSession())
     68     return false;
     69 #endif
     70 
     71   PrefService* prefs = profile->GetPrefs();
     72   if (prefs->IsManagedPreference(prefs::kShowBookmarkBar) &&
     73       !prefs->GetBoolean(prefs::kShowBookmarkBar))
     74     return false;
     75 
     76   return IsNTPWebUI(web_contents()) || IsInstantNTP(web_contents());
     77 }
     78 
     79 BookmarkTabHelper::BookmarkTabHelper(content::WebContents* web_contents)
     80     : content::WebContentsObserver(web_contents),
     81       is_starred_(false),
     82       bookmark_model_(NULL),
     83       delegate_(NULL),
     84       bookmark_drag_(NULL) {
     85   Profile* profile =
     86       Profile::FromBrowserContext(web_contents->GetBrowserContext());
     87   bookmark_model_= BookmarkModelFactory::GetForProfile(profile);
     88   if (bookmark_model_)
     89     bookmark_model_->AddObserver(this);
     90 }
     91 
     92 void BookmarkTabHelper::UpdateStarredStateForCurrentURL() {
     93   const bool old_state = is_starred_;
     94   is_starred_ = (bookmark_model_ &&
     95       bookmark_model_->IsBookmarked(chrome::GetURLToBookmark(web_contents())));
     96 
     97   if (is_starred_ != old_state && delegate_)
     98     delegate_->URLStarredChanged(web_contents(), is_starred_);
     99 }
    100 
    101 void BookmarkTabHelper::BookmarkModelChanged() {
    102 }
    103 
    104 void BookmarkTabHelper::BookmarkModelLoaded(BookmarkModel* model,
    105                                             bool ids_reassigned) {
    106   UpdateStarredStateForCurrentURL();
    107 }
    108 
    109 void BookmarkTabHelper::BookmarkNodeAdded(BookmarkModel* model,
    110                                           const BookmarkNode* parent,
    111                                           int index) {
    112   UpdateStarredStateForCurrentURL();
    113 }
    114 
    115 void BookmarkTabHelper::BookmarkNodeRemoved(
    116     BookmarkModel* model,
    117     const BookmarkNode* parent,
    118     int old_index,
    119     const BookmarkNode* node,
    120     const std::set<GURL>& removed_urls) {
    121   UpdateStarredStateForCurrentURL();
    122 }
    123 
    124 void BookmarkTabHelper::BookmarkAllUserNodesRemoved(
    125     BookmarkModel* model,
    126     const std::set<GURL>& removed_urls) {
    127   UpdateStarredStateForCurrentURL();
    128 }
    129 
    130 void BookmarkTabHelper::BookmarkNodeChanged(BookmarkModel* model,
    131                                             const BookmarkNode* node) {
    132   UpdateStarredStateForCurrentURL();
    133 }
    134 
    135 void BookmarkTabHelper::DidNavigateMainFrame(
    136     const content::LoadCommittedDetails& /*details*/,
    137     const content::FrameNavigateParams& /*params*/) {
    138   UpdateStarredStateForCurrentURL();
    139 }
    140