Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / bookmarks / bookmark_bar_bridge.mm
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/cocoa/bookmarks/bookmark_bar_bridge.h"
6
7 #include "base/bind.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h"
11 #include "chrome/common/pref_names.h"
12 #include "components/bookmarks/core/browser/bookmark_model.h"
13
14
15 BookmarkBarBridge::BookmarkBarBridge(Profile* profile,
16                                      BookmarkBarController* controller,
17                                      BookmarkModel* model)
18     : controller_(controller),
19       model_(model),
20       batch_mode_(false) {
21   model_->AddObserver(this);
22
23   // Bookmark loading is async; it may may not have happened yet.
24   // We will be notified when that happens with the AddObserver() call.
25   if (model->loaded())
26     BookmarkModelLoaded(model, false);
27
28   profile_pref_registrar_.Init(profile->GetPrefs());
29   profile_pref_registrar_.Add(
30       prefs::kShowAppsShortcutInBookmarkBar,
31       base::Bind(&BookmarkBarBridge::OnAppsPageShortcutVisibilityPrefChanged,
32                  base::Unretained(this)));
33
34   [controller_ updateAppsPageShortcutButtonVisibility];
35 }
36
37 BookmarkBarBridge::~BookmarkBarBridge() {
38   model_->RemoveObserver(this);
39 }
40
41 void BookmarkBarBridge::BookmarkModelLoaded(BookmarkModel* model,
42                                             bool ids_reassigned) {
43   [controller_ loaded:model];
44 }
45
46 void BookmarkBarBridge::BookmarkModelBeingDeleted(BookmarkModel* model) {
47   [controller_ beingDeleted:model];
48 }
49
50 void BookmarkBarBridge::BookmarkNodeMoved(BookmarkModel* model,
51                                           const BookmarkNode* old_parent,
52                                           int old_index,
53                                           const BookmarkNode* new_parent,
54                                           int new_index) {
55   if (!batch_mode_) {
56     [controller_ nodeMoved:model
57                  oldParent:old_parent oldIndex:old_index
58                  newParent:new_parent newIndex:new_index];
59   }
60 }
61
62 void BookmarkBarBridge::BookmarkNodeAdded(BookmarkModel* model,
63                                           const BookmarkNode* parent,
64                                           int index) {
65   if (!batch_mode_)
66     [controller_ nodeAdded:model parent:parent index:index];
67 }
68
69 void BookmarkBarBridge::BookmarkNodeRemoved(
70     BookmarkModel* model,
71     const BookmarkNode* parent,
72     int old_index,
73     const BookmarkNode* node,
74     const std::set<GURL>& removed_urls) {
75   if (!batch_mode_)
76     [controller_ nodeRemoved:model parent:parent index:old_index];
77 }
78
79 void BookmarkBarBridge::BookmarkAllNodesRemoved(
80     BookmarkModel* model,
81     const std::set<GURL>& removed_urls) {
82   [controller_ loaded:model];
83 }
84
85 void BookmarkBarBridge::BookmarkNodeChanged(BookmarkModel* model,
86                                             const BookmarkNode* node) {
87   if (!batch_mode_)
88     [controller_ nodeChanged:model node:node];
89 }
90
91 void BookmarkBarBridge::BookmarkNodeFaviconChanged(BookmarkModel* model,
92                                                    const BookmarkNode* node) {
93   if (!batch_mode_)
94     [controller_ nodeFaviconLoaded:model node:node];
95 }
96
97 void BookmarkBarBridge::BookmarkNodeChildrenReordered(
98     BookmarkModel* model, const BookmarkNode* node) {
99   if (!batch_mode_)
100     [controller_ nodeChildrenReordered:model node:node];
101 }
102
103 void BookmarkBarBridge::ExtensiveBookmarkChangesBeginning(
104     BookmarkModel* model) {
105   batch_mode_ = true;
106 }
107
108 void BookmarkBarBridge::ExtensiveBookmarkChangesEnded(BookmarkModel* model) {
109   batch_mode_ = false;
110   [controller_ loaded:model];
111 }
112
113 void BookmarkBarBridge::OnAppsPageShortcutVisibilityPrefChanged() {
114   [controller_ updateAppsPageShortcutButtonVisibility];
115 }