File: bookmark_folder_applescript.mm

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (240 lines) | stat: -rw-r--r-- 8,614 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/ui/cocoa/applescript/bookmark_folder_applescript.h"

#include "base/strings/sys_string_conversions.h"
#import "chrome/browser/ui/cocoa/applescript/bookmark_item_applescript.h"
#import "chrome/browser/ui/cocoa/applescript/constants_applescript.h"
#include "chrome/browser/ui/cocoa/applescript/error_applescript.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/common/bookmark_metrics.h"
#include "url/gurl.h"

using bookmarks::BookmarkModel;
using bookmarks::BookmarkNode;

// /!\ Warning
//
// The design of the AppleScript dictionary with regards to bookmarks is
// deficient. The ideal design would mirror the design of the actual bookmark
// system, where a bookmark element could be either a folder or an item, and
// bookmark folders could hold any number of them, in any order.
//
// However, that's not the design that is implemented. The current design is
// that bookmark folders and bookmark items are separate and unrelated things,
// and that bookmark folders have a list of bookmark folders they contain, as
// well as a list of bookmark items they contain.
//
// That is, there are _two separate lists_.
//
// Translating from the real bookmarks system, where the children of a folder
// are a list of intermingled folders and items, is easy: walk the list of
// children, and filter out the wrong kind. (See `-bookmarkFolders` and
// `-bookmarkItems`.)
//
// Translating _to_ the real bookmarks system cannot be done in the general
// case. There is no control over the mingling of folders and items, and there
// is only vague control over the ordering of items that share a type.
//
// All this is to explain the difference in terminology between "index" and
// "bookmark manager position". An "index" value is relative to the two separate
// child lists that exist from the AppleScript perspective. The "bookmark
// manager position" is relative to the true list of (both types of) children of
// a folder in the bookmarks system.
//
// Because AppleScript maintains no state, no translation ever needs to be done
// from positions to indexes. AppleScript keeps object identifiers, and if it
// ever needs to update the index, it will re-request the list and find the item
// again by matching its ID.
//
// However, when a script requests a folder or bookmark to be moved around or
// inserted, AppleScript will request a change in index. That index will need to
// be converted into a position in the real list of children of a folder, and
// that's what the `-bookmarkManagerPositionOf[Folder|Item]At:` methods are for.

@interface BookmarkFolderAppleScript ()

// Does the actual insertion of a bookmark folder at an absolute position.
- (void)insertFolder:(BookmarkFolderAppleScript*)bookmarkFolder
    atBookmarkManagerPosition:(size_t)position;

// Does the actual insertion of a bookmark item at an absolute position.
- (void)insertItem:(BookmarkItemAppleScript*)bookmarkItem
    atBookmarkManagerPosition:(size_t)position;

// Returns the position of a bookmark folder within the current bookmark folder
// which consists of bookmark folders as well as bookmark items.
- (size_t)bookmarkManagerPositionOfFolderAt:(size_t)index;

// Returns the position of a bookmark item within the current bookmark folder
// which consists of bookmark folders as well as bookmark items.
- (size_t)bookmarkManagerPositionOfItemAt:(size_t)index;

@end

@implementation BookmarkFolderAppleScript

- (NSArray<BookmarkFolderAppleScript*>*)bookmarkFolders {
  NSMutableArray<BookmarkFolderAppleScript*>* bookmarkFolders =
      [NSMutableArray arrayWithCapacity:self.bookmarkNode->children().size()];

  for (const auto& node : self.bookmarkNode->children()) {
    if (!node->is_folder()) {
      continue;
    }

    BookmarkFolderAppleScript* bookmarkFolder =
        [[BookmarkFolderAppleScript alloc] initWithBookmarkNode:node.get()];
    [bookmarkFolder setContainer:self
                        property:AppleScript::kBookmarkFoldersProperty];
    [bookmarkFolders addObject:bookmarkFolder];
  }

  return bookmarkFolders;
}

- (NSArray<BookmarkItemAppleScript*>*)bookmarkItems {
  NSMutableArray<BookmarkItemAppleScript*>* bookmarkItems =
      [NSMutableArray arrayWithCapacity:self.bookmarkNode->children().size()];

  for (const auto& node : self.bookmarkNode->children()) {
    if (!node->is_url()) {
      continue;
    }

    BookmarkItemAppleScript* bookmarkItem =
        [[BookmarkItemAppleScript alloc] initWithBookmarkNode:node.get()];
    [bookmarkItem setContainer:self
                      property:AppleScript::kBookmarkItemsProperty];
    [bookmarkItems addObject:bookmarkItem];
  }

  return bookmarkItems;
}

- (void)insertInBookmarkFolders:(BookmarkFolderAppleScript*)bookmarkFolder {
  [self insertFolder:bookmarkFolder
      atBookmarkManagerPosition:self.bookmarkNode->children().size()];
}

- (void)insertInBookmarkFolders:(BookmarkFolderAppleScript*)bookmarkFolder
                        atIndex:(size_t)index {
  [self insertFolder:bookmarkFolder
      atBookmarkManagerPosition:[self bookmarkManagerPositionOfFolderAt:index]];
}

- (void)insertFolder:(BookmarkFolderAppleScript*)bookmarkFolder
    atBookmarkManagerPosition:(size_t)position {
  [bookmarkFolder setContainer:self
                      property:AppleScript::kBookmarkFoldersProperty];

  BookmarkModel* model = self.bookmarkModel;
  if (!model) {
    return;
  }

  const BookmarkNode* node = model->AddFolder(
      self.bookmarkNode, position,
      /*title=*/std::u16string(), /*meta_info=*/nullptr,
      /*creation_time=*/std::nullopt, bookmarkFolder.bookmarkGUID);
  if (!node) {
    AppleScript::SetError(AppleScript::Error::kCreateBookmarkFolder);
    return;
  }

  [bookmarkFolder didCreateBookmarkNode:node];
}

- (void)removeFromBookmarkFoldersAtIndex:(size_t)index {
  size_t position = [self bookmarkManagerPositionOfFolderAt:index];

  BookmarkModel* model = self.bookmarkModel;
  if (!model) {
    return;
  }

  model->Remove(self.bookmarkNode->children()[position].get(),
                bookmarks::metrics::BookmarkEditSource::kUser, FROM_HERE);
}

- (void)insertInBookmarkItems:(BookmarkItemAppleScript*)bookmarkItem {
  [self insertItem:bookmarkItem
      atBookmarkManagerPosition:self.bookmarkNode->children().size()];
}

- (void)insertInBookmarkItems:(BookmarkItemAppleScript*)bookmarkItem
                      atIndex:(size_t)index {
  [self insertItem:bookmarkItem
      atBookmarkManagerPosition:[self bookmarkManagerPositionOfItemAt:index]];
}

- (void)insertItem:(BookmarkItemAppleScript*)bookmarkItem
    atBookmarkManagerPosition:(size_t)position {
  [bookmarkItem setContainer:self property:AppleScript::kBookmarkItemsProperty];

  BookmarkModel* model = self.bookmarkModel;
  if (!model) {
    return;
  }

  GURL url(base::SysNSStringToUTF8(bookmarkItem.URL));
  if (!url.is_valid()) {
    AppleScript::SetError(AppleScript::Error::kInvalidURL);
    return;
  }

  const BookmarkNode* node = model->AddURL(
      self.bookmarkNode, position, /*title=*/std::u16string(), url,
      /*meta_info=*/nullptr, /*creation_time=*/std::nullopt,
      bookmarkItem.bookmarkGUID, /*added_by_user=*/true);
  if (!node) {
    AppleScript::SetError(AppleScript::Error::kCreateBookmarkItem);
    return;
  }

  [bookmarkItem didCreateBookmarkNode:node];
}

- (void)removeFromBookmarkItemsAtIndex:(size_t)index {
  size_t position = [self bookmarkManagerPositionOfItemAt:index];

  BookmarkModel* model = self.bookmarkModel;
  if (!model) {
    return;
  }

  model->Remove(self.bookmarkNode->children()[position].get(),
                bookmarks::metrics::BookmarkEditSource::kUser, FROM_HERE);
}

- (size_t)bookmarkManagerPositionOfFolderAt:(size_t)index {
  // Traverse through all the child nodes until the required node is found and
  // return its position.
  // AppleScript is 1-based therefore index is incremented by 1.
  ++index;
  size_t count = 0;
  while (index) {
    if (self.bookmarkNode->children()[count++]->is_folder()) {
      --index;
    }
  }
  return count - 1;
}

- (size_t)bookmarkManagerPositionOfItemAt:(size_t)index {
  // Traverse through all the child nodes until the required node is found and
  // return its position.
  // AppleScript is 1-based therefore index is incremented by 1.
  ++index;
  size_t count = 0;
  while (index) {
    if (self.bookmarkNode->children()[count++]->is_url()) {
      --index;
    }
  }
  return count - 1;
}

@end