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/bookmarks/bookmark_expanded_state_tracker.h"
      6 
      7 #include "base/prefs/pref_service.h"
      8 #include "base/strings/string_number_conversions.h"
      9 #include "base/values.h"
     10 #include "chrome/browser/bookmarks/bookmark_model.h"
     11 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
     12 #include "chrome/common/pref_names.h"
     13 
     14 BookmarkExpandedStateTracker::BookmarkExpandedStateTracker(
     15     BookmarkModel* bookmark_model,
     16     PrefService* pref_service)
     17     : bookmark_model_(bookmark_model),
     18       pref_service_(pref_service) {
     19   bookmark_model->AddObserver(this);
     20 }
     21 
     22 BookmarkExpandedStateTracker::~BookmarkExpandedStateTracker() {
     23 }
     24 
     25 void BookmarkExpandedStateTracker::SetExpandedNodes(const Nodes& nodes) {
     26   UpdatePrefs(nodes);
     27 }
     28 
     29 BookmarkExpandedStateTracker::Nodes
     30 BookmarkExpandedStateTracker::GetExpandedNodes() {
     31   Nodes nodes;
     32   if (!bookmark_model_->loaded())
     33     return nodes;
     34 
     35   if (!pref_service_)
     36     return nodes;
     37 
     38   const ListValue* value =
     39       pref_service_->GetList(prefs::kBookmarkEditorExpandedNodes);
     40   if (!value)
     41     return nodes;
     42 
     43   bool changed = false;
     44   for (ListValue::const_iterator i = value->begin(); i != value->end(); ++i) {
     45     std::string value;
     46     int64 node_id;
     47     const BookmarkNode* node;
     48     if ((*i)->GetAsString(&value) && base::StringToInt64(value, &node_id) &&
     49         (node = bookmark_model_->GetNodeByID(node_id)) != NULL &&
     50         node->is_folder()) {
     51       nodes.insert(node);
     52     } else {
     53       changed = true;
     54     }
     55   }
     56   if (changed)
     57     UpdatePrefs(nodes);
     58   return nodes;
     59 }
     60 
     61 void BookmarkExpandedStateTracker::Loaded(BookmarkModel* model,
     62                                           bool ids_reassigned) {
     63   if (ids_reassigned) {
     64     // If the ids change we can't trust the value in preferences and need to
     65     // reset it.
     66     SetExpandedNodes(Nodes());
     67   }
     68 }
     69 
     70 void BookmarkExpandedStateTracker::BookmarkModelChanged() {
     71 }
     72 
     73 void BookmarkExpandedStateTracker::BookmarkModelBeingDeleted(
     74     BookmarkModel* model) {
     75   model->RemoveObserver(this);
     76 }
     77 
     78 void BookmarkExpandedStateTracker::BookmarkNodeRemoved(
     79     BookmarkModel* model,
     80     const BookmarkNode* parent,
     81     int old_index,
     82     const BookmarkNode* node) {
     83   if (!node->is_folder())
     84     return;  // Only care about folders.
     85 
     86   // Ask for the nodes again, which removes any nodes that were deleted.
     87   GetExpandedNodes();
     88 }
     89 
     90 void BookmarkExpandedStateTracker::BookmarkAllNodesRemoved(
     91     BookmarkModel* model) {
     92   // Ask for the nodes again, which removes any nodes that were deleted.
     93   GetExpandedNodes();
     94 }
     95 
     96 void BookmarkExpandedStateTracker::UpdatePrefs(const Nodes& nodes) {
     97   if (!pref_service_)
     98     return;
     99 
    100   ListValue values;
    101   for (Nodes::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
    102     values.Set(values.GetSize(),
    103                new StringValue(base::Int64ToString((*i)->id())));
    104   }
    105 
    106   pref_service_->Set(prefs::kBookmarkEditorExpandedNodes, values);
    107 }
    108