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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_
#define CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_
#include <stdint.h>
#include <list>
#include <string>
#include <vector>
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "components/bookmarks/browser/base_bookmark_model_observer.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/event_router.h"
#include "ui/shell_dialogs/select_file_dialog.h"
namespace base {
class FilePath;
class ListValue;
}
namespace bookmarks {
class BookmarkModel;
class ManagedBookmarkService;
}
namespace content {
class BrowserContext;
}
namespace extensions {
namespace api {
namespace bookmarks {
struct CreateDetails;
}
}
// Observes BookmarkModel and then routes the notifications as events to
// the extension system.
class BookmarkEventRouter : public bookmarks::BookmarkModelObserver {
public:
explicit BookmarkEventRouter(Profile* profile);
~BookmarkEventRouter() override;
// bookmarks::BookmarkModelObserver:
void BookmarkModelLoaded(bookmarks::BookmarkModel* model,
bool ids_reassigned) override;
void BookmarkModelBeingDeleted(bookmarks::BookmarkModel* model) override;
void BookmarkNodeMoved(bookmarks::BookmarkModel* model,
const bookmarks::BookmarkNode* old_parent,
int old_index,
const bookmarks::BookmarkNode* new_parent,
int new_index) override;
void BookmarkNodeAdded(bookmarks::BookmarkModel* model,
const bookmarks::BookmarkNode* parent,
int index) override;
void BookmarkNodeRemoved(bookmarks::BookmarkModel* model,
const bookmarks::BookmarkNode* parent,
int old_index,
const bookmarks::BookmarkNode* node,
const std::set<GURL>& removed_urls) override;
void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model,
const std::set<GURL>& removed_urls) override;
void BookmarkNodeChanged(bookmarks::BookmarkModel* model,
const bookmarks::BookmarkNode* node) override;
void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel* model,
const bookmarks::BookmarkNode* node) override;
void BookmarkNodeChildrenReordered(
bookmarks::BookmarkModel* model,
const bookmarks::BookmarkNode* node) override;
void ExtensiveBookmarkChangesBeginning(
bookmarks::BookmarkModel* model) override;
void ExtensiveBookmarkChangesEnded(bookmarks::BookmarkModel* model) override;
private:
// Helper to actually dispatch an event to extension listeners.
void DispatchEvent(events::HistogramValue histogram_value,
const std::string& event_name,
std::unique_ptr<base::ListValue> event_args);
content::BrowserContext* browser_context_;
bookmarks::BookmarkModel* model_;
bookmarks::ManagedBookmarkService* managed_;
DISALLOW_COPY_AND_ASSIGN(BookmarkEventRouter);
};
class BookmarksAPI : public BrowserContextKeyedAPI,
public EventRouter::Observer {
public:
explicit BookmarksAPI(content::BrowserContext* context);
~BookmarksAPI() override;
// KeyedService implementation.
void Shutdown() override;
// BrowserContextKeyedAPI implementation.
static BrowserContextKeyedAPIFactory<BookmarksAPI>* GetFactoryInstance();
// EventRouter::Observer implementation.
void OnListenerAdded(const EventListenerInfo& details) override;
private:
friend class BrowserContextKeyedAPIFactory<BookmarksAPI>;
content::BrowserContext* browser_context_;
// BrowserContextKeyedAPI implementation.
static const char* service_name() {
return "BookmarksAPI";
}
static const bool kServiceIsNULLWhileTesting = true;
// Created lazily upon OnListenerAdded.
std::unique_ptr<BookmarkEventRouter> bookmark_event_router_;
};
class BookmarksFunction : public ChromeAsyncExtensionFunction,
public bookmarks::BaseBookmarkModelObserver {
public:
// AsyncExtensionFunction:
bool RunAsync() override;
protected:
~BookmarksFunction() override {}
// RunAsync semantic equivalent called when the bookmarks are ready.
virtual bool RunOnReady() = 0;
// Helper to get the BookmarkModel.
bookmarks::BookmarkModel* GetBookmarkModel();
// Helper to get the ManagedBookmarkService.
bookmarks::ManagedBookmarkService* GetManagedBookmarkService();
// Helper to get the bookmark id as int64_t from the given string id.
// Sets error_ to an error string if the given id string can't be parsed
// as an int64_t. In case of error, doesn't change id and returns false.
bool GetBookmarkIdAsInt64(const std::string& id_string, int64_t* id);
// Helper to get the bookmark node from a given string id.
// If the given id can't be parsed or doesn't refer to a valid node, sets
// error_ and returns NULL.
const bookmarks::BookmarkNode* GetBookmarkNodeFromId(
const std::string& id_string);
// Helper to create a bookmark node from a CreateDetails object. If a node
// can't be created based on the given details, sets error_ and returns NULL.
const bookmarks::BookmarkNode* CreateBookmarkNode(
bookmarks::BookmarkModel* model,
const api::bookmarks::CreateDetails& details,
const bookmarks::BookmarkNode::MetaInfoMap* meta_info);
// Helper that checks if bookmark editing is enabled. If it's not, this sets
// error_ to the appropriate error string.
bool EditBookmarksEnabled();
// Helper that checks if |node| can be modified. Returns false if |node|
// is NULL, or a managed node, or the root node. In these cases the node
// can't be edited, can't have new child nodes appended, and its direct
// children can't be moved or reordered.
bool CanBeModified(const bookmarks::BookmarkNode* node);
private:
// bookmarks::BaseBookmarkModelObserver:
void BookmarkModelChanged() override;
void BookmarkModelLoaded(bookmarks::BookmarkModel* model,
bool ids_reassigned) override;
void RunAndSendResponse();
};
class BookmarksGetFunction : public BookmarksFunction {
public:
DECLARE_EXTENSION_FUNCTION("bookmarks.get", BOOKMARKS_GET)
protected:
~BookmarksGetFunction() override {}
// BookmarksFunction:
bool RunOnReady() override;
};
class BookmarksGetChildrenFunction : public BookmarksFunction {
public:
DECLARE_EXTENSION_FUNCTION("bookmarks.getChildren", BOOKMARKS_GETCHILDREN)
protected:
~BookmarksGetChildrenFunction() override {}
// BookmarksFunction:
bool RunOnReady() override;
};
class BookmarksGetRecentFunction : public BookmarksFunction {
public:
DECLARE_EXTENSION_FUNCTION("bookmarks.getRecent", BOOKMARKS_GETRECENT)
protected:
~BookmarksGetRecentFunction() override {}
// BookmarksFunction:
bool RunOnReady() override;
};
class BookmarksGetTreeFunction : public BookmarksFunction {
public:
DECLARE_EXTENSION_FUNCTION("bookmarks.getTree", BOOKMARKS_GETTREE)
protected:
~BookmarksGetTreeFunction() override {}
// BookmarksFunction:
bool RunOnReady() override;
};
class BookmarksGetSubTreeFunction : public BookmarksFunction {
public:
DECLARE_EXTENSION_FUNCTION("bookmarks.getSubTree", BOOKMARKS_GETSUBTREE)
protected:
~BookmarksGetSubTreeFunction() override {}
// BookmarksFunction:
bool RunOnReady() override;
};
class BookmarksSearchFunction : public BookmarksFunction {
public:
DECLARE_EXTENSION_FUNCTION("bookmarks.search", BOOKMARKS_SEARCH)
protected:
~BookmarksSearchFunction() override {}
// BookmarksFunction:
bool RunOnReady() override;
};
class BookmarksRemoveFunction : public BookmarksFunction {
public:
DECLARE_EXTENSION_FUNCTION("bookmarks.remove", BOOKMARKS_REMOVE)
// Returns true on successful parse and sets invalid_id to true if conversion
// from id string to int64_t failed.
static bool ExtractIds(const base::ListValue* args,
std::list<int64_t>* ids,
bool* invalid_id);
protected:
~BookmarksRemoveFunction() override {}
// BookmarksFunction:
bool RunOnReady() override;
};
class BookmarksRemoveTreeFunction : public BookmarksRemoveFunction {
public:
DECLARE_EXTENSION_FUNCTION("bookmarks.removeTree", BOOKMARKS_REMOVETREE)
protected:
~BookmarksRemoveTreeFunction() override {}
};
class BookmarksCreateFunction : public BookmarksFunction {
public:
DECLARE_EXTENSION_FUNCTION("bookmarks.create", BOOKMARKS_CREATE)
protected:
~BookmarksCreateFunction() override {}
// BookmarksFunction:
bool RunOnReady() override;
};
class BookmarksMoveFunction : public BookmarksFunction {
public:
DECLARE_EXTENSION_FUNCTION("bookmarks.move", BOOKMARKS_MOVE)
static bool ExtractIds(const base::ListValue* args,
std::list<int64_t>* ids,
bool* invalid_id);
protected:
~BookmarksMoveFunction() override {}
// BookmarksFunction:
bool RunOnReady() override;
};
class BookmarksUpdateFunction : public BookmarksFunction {
public:
DECLARE_EXTENSION_FUNCTION("bookmarks.update", BOOKMARKS_UPDATE)
static bool ExtractIds(const base::ListValue* args,
std::list<int64_t>* ids,
bool* invalid_id);
protected:
~BookmarksUpdateFunction() override {}
// BookmarksFunction:
bool RunOnReady() override;
};
class BookmarksIOFunction : public BookmarksFunction,
public ui::SelectFileDialog::Listener {
public:
BookmarksIOFunction();
void FileSelected(const base::FilePath& path,
int index,
void* params) override = 0;
// ui::SelectFileDialog::Listener:
void MultiFilesSelected(const std::vector<base::FilePath>& files,
void* params) override;
void FileSelectionCanceled(void* params) override;
void SelectFile(ui::SelectFileDialog::Type type);
protected:
~BookmarksIOFunction() override;
private:
void ShowSelectFileDialog(
ui::SelectFileDialog::Type type,
const base::FilePath& default_path);
protected:
scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
};
class BookmarksImportFunction : public BookmarksIOFunction {
public:
DECLARE_EXTENSION_FUNCTION("bookmarks.import", BOOKMARKS_IMPORT)
// BookmarkManagerIOFunction:
void FileSelected(const base::FilePath& path,
int index,
void* params) override;
private:
~BookmarksImportFunction() override {}
// BookmarksFunction:
bool RunOnReady() override;
};
class BookmarksExportFunction : public BookmarksIOFunction {
public:
DECLARE_EXTENSION_FUNCTION("bookmarks.export", BOOKMARKS_EXPORT)
// BookmarkManagerIOFunction:
void FileSelected(const base::FilePath& path,
int index,
void* params) override;
private:
~BookmarksExportFunction() override {}
// BookmarksFunction:
bool RunOnReady() override;
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_
|