#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/bookmarks/bookmark_html_writer.h"
-#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
-#include "chrome/browser/bookmarks/bookmark_utils.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/api/bookmarks/bookmark_api_constants.h"
#include "chrome/browser/extensions/api/bookmarks/bookmark_api_helpers.h"
-#include "chrome/browser/extensions/event_router.h"
-#include "chrome/browser/extensions/extension_function_dispatcher.h"
-#include "chrome/browser/extensions/extension_system.h"
-#include "chrome/browser/extensions/extensions_quota_service.h"
#include "chrome/browser/importer/external_process_importer_host.h"
#include "chrome/browser/importer/importer_uma.h"
#include "chrome/browser/platform_util.h"
#include "chrome/common/extensions/api/bookmarks.h"
#include "chrome/common/importer/importer_data_types.h"
#include "chrome/common/pref_names.h"
+#include "components/bookmarks/core/browser/bookmark_model.h"
+#include "components/bookmarks/core/browser/bookmark_utils.h"
#include "components/user_prefs/user_prefs.h"
+#include "content/public/browser/browser_context.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/web_contents.h"
-#include "content/public/browser/web_contents_view.h"
+#include "extensions/browser/event_router.h"
+#include "extensions/browser/extension_function_dispatcher.h"
+#include "extensions/browser/quota_service.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
-#if defined(OS_WIN) && defined(USE_AURA)
-#include "ui/aura/remote_root_window_host_win.h"
+#if defined(OS_WIN)
+#include "ui/aura/remote_window_tree_host_win.h"
#endif
namespace extensions {
using base::TimeDelta;
using bookmarks::BookmarkTreeNode;
+using bookmarks::CreateDetails;
+using content::BrowserContext;
using content::BrowserThread;
using content::WebContents;
typedef QuotaLimitHeuristic::Bucket Bucket;
typedef QuotaLimitHeuristic::Config Config;
typedef QuotaLimitHeuristic::BucketList BucketList;
-typedef ExtensionsQuotaService::TimedLimit TimedLimit;
-typedef ExtensionsQuotaService::SustainedLimit SustainedLimit;
+typedef QuotaService::TimedLimit TimedLimit;
+typedef QuotaService::SustainedLimit SustainedLimit;
typedef QuotaLimitHeuristic::BucketMapper BucketMapper;
namespace {
} // namespace
-void BookmarksFunction::Run() {
+bool BookmarksFunction::RunAsync() {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile());
if (!model->loaded()) {
// Bookmarks are not ready yet. We'll wait.
model->AddObserver(this);
AddRef(); // Balanced in Loaded().
- return;
+ return true;
}
- bool success = RunImpl();
+ bool success = RunOnReady();
if (success) {
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_EXTENSION_BOOKMARKS_API_INVOKED,
content::Details<const BookmarksFunction>(this));
}
SendResponse(success);
+ return true;
}
bool BookmarksFunction::GetBookmarkIdAsInt64(const std::string& id_string,
return false;
}
+const BookmarkNode* BookmarksFunction::GetBookmarkNodeFromId(
+ const std::string& id_string) {
+ int64 id;
+ if (!GetBookmarkIdAsInt64(id_string, &id))
+ return NULL;
+
+ const BookmarkNode* node = GetBookmarkNodeByID(
+ BookmarkModelFactory::GetForProfile(GetProfile()), id);
+ if (!node)
+ error_ = keys::kNoNodeError;
+
+ return node;
+}
+
+const BookmarkNode* BookmarksFunction::CreateBookmarkNode(
+ BookmarkModel* model,
+ const CreateDetails& details,
+ const BookmarkNode::MetaInfoMap* meta_info) {
+ int64 parentId;
+
+ if (!details.parent_id.get()) {
+ // Optional, default to "other bookmarks".
+ parentId = model->other_node()->id();
+ } else {
+ if (!GetBookmarkIdAsInt64(*details.parent_id, &parentId))
+ return NULL;
+ }
+ const BookmarkNode* parent = GetBookmarkNodeByID(model, parentId);
+ if (!parent) {
+ error_ = keys::kNoParentError;
+ return NULL;
+ }
+ if (parent->is_root()) { // Can't create children of the root.
+ error_ = keys::kModifySpecialError;
+ return NULL;
+ }
+
+ int index;
+ if (!details.index.get()) { // Optional (defaults to end).
+ index = parent->child_count();
+ } else {
+ index = *details.index;
+ if (index > parent->child_count() || index < 0) {
+ error_ = keys::kInvalidIndexError;
+ return NULL;
+ }
+ }
+
+ base::string16 title; // Optional.
+ if (details.title.get())
+ title = base::UTF8ToUTF16(*details.title.get());
+
+ std::string url_string; // Optional.
+ if (details.url.get())
+ url_string = *details.url.get();
+
+ GURL url(url_string);
+ if (!url_string.empty() && !url.is_valid()) {
+ error_ = keys::kInvalidUrlError;
+ return NULL;
+ }
+
+ const BookmarkNode* node;
+ if (url_string.length())
+ node = model->AddURLWithCreationTimeAndMetaInfo(
+ parent, index, title, url, base::Time::Now(), meta_info);
+ else
+ node = model->AddFolderWithMetaInfo(parent, index, title, meta_info);
+ DCHECK(node);
+ if (!node) {
+ error_ = keys::kNoNodeError;
+ return NULL;
+ }
+
+ return node;
+}
+
bool BookmarksFunction::EditBookmarksEnabled() {
PrefService* prefs = user_prefs::UserPrefs::Get(GetProfile());
if (prefs->GetBoolean(prefs::kEditBookmarksEnabled))
void BookmarksFunction::BookmarkModelChanged() {
}
-void BookmarksFunction::Loaded(BookmarkModel* model, bool ids_reassigned) {
+void BookmarksFunction::BookmarkModelLoaded(BookmarkModel* model,
+ bool ids_reassigned) {
model->RemoveObserver(this);
- Run();
- Release(); // Balanced in Run().
+ RunOnReady();
+ Release(); // Balanced in RunOnReady().
}
-BookmarkEventRouter::BookmarkEventRouter(Profile* profile,
+BookmarkEventRouter::BookmarkEventRouter(BrowserContext* context,
BookmarkModel* model)
- : profile_(profile),
- model_(model) {
+ : browser_context_(context), model_(model) {
model_->AddObserver(this);
}
void BookmarkEventRouter::DispatchEvent(
const std::string& event_name,
scoped_ptr<base::ListValue> event_args) {
- if (extensions::ExtensionSystem::Get(profile_)->event_router()) {
- extensions::ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(
+ EventRouter* event_router = EventRouter::Get(browser_context_);
+ if (event_router) {
+ event_router->BroadcastEvent(
make_scoped_ptr(new extensions::Event(event_name, event_args.Pass())));
}
}
-void BookmarkEventRouter::Loaded(BookmarkModel* model, bool ids_reassigned) {
+void BookmarkEventRouter::BookmarkModelLoaded(BookmarkModel* model,
+ bool ids_reassigned) {
// TODO(erikkay): Perhaps we should send this event down to the extension
// so they know when it's safe to use the API?
}
int old_index,
const BookmarkNode* new_parent,
int new_index) {
- scoped_ptr<base::ListValue> args(new base::ListValue());
const BookmarkNode* node = new_parent->GetChild(new_index);
- args->Append(new base::StringValue(base::Int64ToString(node->id())));
- base::DictionaryValue* object_args = new base::DictionaryValue();
- object_args->SetString(keys::kParentIdKey,
- base::Int64ToString(new_parent->id()));
- object_args->SetInteger(keys::kIndexKey, new_index);
- object_args->SetString(keys::kOldParentIdKey,
- base::Int64ToString(old_parent->id()));
- object_args->SetInteger(keys::kOldIndexKey, old_index);
- args->Append(object_args);
-
- DispatchEvent(bookmarks::OnMoved::kEventName, args.Pass());
+ bookmarks::OnMoved::MoveInfo move_info;
+ move_info.parent_id = base::Int64ToString(new_parent->id());
+ move_info.index = new_index;
+ move_info.old_parent_id = base::Int64ToString(old_parent->id());
+ move_info.old_index = old_index;
+
+ DispatchEvent(
+ bookmarks::OnMoved::kEventName,
+ bookmarks::OnMoved::Create(base::Int64ToString(node->id()), move_info));
}
void BookmarkEventRouter::BookmarkNodeAdded(BookmarkModel* model,
const BookmarkNode* parent,
int index) {
- scoped_ptr<base::ListValue> args(new base::ListValue());
const BookmarkNode* node = parent->GetChild(index);
- args->Append(new base::StringValue(base::Int64ToString(node->id())));
scoped_ptr<BookmarkTreeNode> tree_node(
bookmark_api_helpers::GetBookmarkTreeNode(node, false, false));
- args->Append(tree_node->ToValue().release());
-
- DispatchEvent(bookmarks::OnCreated::kEventName, args.Pass());
+ DispatchEvent(bookmarks::OnCreated::kEventName,
+ bookmarks::OnCreated::Create(base::Int64ToString(node->id()),
+ *tree_node));
}
-void BookmarkEventRouter::BookmarkNodeRemoved(BookmarkModel* model,
- const BookmarkNode* parent,
- int index,
- const BookmarkNode* node) {
- scoped_ptr<base::ListValue> args(new base::ListValue());
- args->Append(new base::StringValue(base::Int64ToString(node->id())));
- base::DictionaryValue* object_args = new base::DictionaryValue();
- object_args->SetString(keys::kParentIdKey,
- base::Int64ToString(parent->id()));
- object_args->SetInteger(keys::kIndexKey, index);
- args->Append(object_args);
-
- DispatchEvent(bookmarks::OnRemoved::kEventName, args.Pass());
+void BookmarkEventRouter::BookmarkNodeRemoved(
+ BookmarkModel* model,
+ const BookmarkNode* parent,
+ int index,
+ const BookmarkNode* node,
+ const std::set<GURL>& removed_urls) {
+ bookmarks::OnRemoved::RemoveInfo remove_info;
+ remove_info.parent_id = base::Int64ToString(parent->id());
+ remove_info.index = index;
+
+ DispatchEvent(bookmarks::OnRemoved::kEventName,
+ bookmarks::OnRemoved::Create(base::Int64ToString(node->id()),
+ remove_info));
}
-void BookmarkEventRouter::BookmarkAllNodesRemoved(BookmarkModel* model) {
+void BookmarkEventRouter::BookmarkAllNodesRemoved(
+ BookmarkModel* model,
+ const std::set<GURL>& removed_urls) {
NOTREACHED();
// TODO(shashishekhar) Currently this notification is only used on Android,
// which does not support extensions. If Desktop needs to support this, add
void BookmarkEventRouter::BookmarkNodeChanged(BookmarkModel* model,
const BookmarkNode* node) {
- scoped_ptr<base::ListValue> args(new base::ListValue());
- args->Append(new base::StringValue(base::Int64ToString(node->id())));
-
// TODO(erikkay) The only three things that BookmarkModel sends this
// notification for are title, url and favicon. Since we're currently
// ignoring favicon and since the notification doesn't say which one anyway,
// for now we only include title and url. The ideal thing would be to change
// BookmarkModel to indicate what changed.
- base::DictionaryValue* object_args = new base::DictionaryValue();
- object_args->SetString(keys::kTitleKey, node->GetTitle());
+ bookmarks::OnChanged::ChangeInfo change_info;
+ change_info.title = base::UTF16ToUTF8(node->GetTitle());
if (node->is_url())
- object_args->SetString(keys::kUrlKey, node->url().spec());
- args->Append(object_args);
+ change_info.url.reset(new std::string(node->url().spec()));
- DispatchEvent(bookmarks::OnChanged::kEventName, args.Pass());
+ DispatchEvent(bookmarks::OnChanged::kEventName,
+ bookmarks::OnChanged::Create(base::Int64ToString(node->id()),
+ change_info));
}
void BookmarkEventRouter::BookmarkNodeFaviconChanged(BookmarkModel* model,
void BookmarkEventRouter::BookmarkNodeChildrenReordered(
BookmarkModel* model,
const BookmarkNode* node) {
- scoped_ptr<base::ListValue> args(new base::ListValue());
- args->Append(new base::StringValue(base::Int64ToString(node->id())));
+ bookmarks::OnChildrenReordered::ReorderInfo reorder_info;
int childCount = node->child_count();
- base::ListValue* children = new base::ListValue();
for (int i = 0; i < childCount; ++i) {
const BookmarkNode* child = node->GetChild(i);
- base::Value* child_id =
- new base::StringValue(base::Int64ToString(child->id()));
- children->Append(child_id);
+ reorder_info.child_ids.push_back(base::Int64ToString(child->id()));
}
- base::DictionaryValue* reorder_info = new base::DictionaryValue();
- reorder_info->Set(keys::kChildIdsKey, children);
- args->Append(reorder_info);
- DispatchEvent(bookmarks::OnChildrenReordered::kEventName, args.Pass());
+ DispatchEvent(bookmarks::OnChildrenReordered::kEventName,
+ bookmarks::OnChildrenReordered::Create(
+ base::Int64ToString(node->id()), reorder_info));
}
void BookmarkEventRouter::ExtensiveBookmarkChangesBeginning(
BookmarkModel* model) {
- scoped_ptr<base::ListValue> args(new base::ListValue());
- DispatchEvent(bookmarks::OnImportBegan::kEventName, args.Pass());
+ DispatchEvent(bookmarks::OnImportBegan::kEventName,
+ bookmarks::OnImportBegan::Create());
}
void BookmarkEventRouter::ExtensiveBookmarkChangesEnded(BookmarkModel* model) {
- scoped_ptr<base::ListValue> args(new base::ListValue());
- DispatchEvent(bookmarks::OnImportEnded::kEventName, args.Pass());
+ DispatchEvent(bookmarks::OnImportEnded::kEventName,
+ bookmarks::OnImportEnded::Create());
}
-BookmarksAPI::BookmarksAPI(Profile* profile) : profile_(profile) {
- ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
- this, bookmarks::OnCreated::kEventName);
- ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
- this, bookmarks::OnRemoved::kEventName);
- ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
- this, bookmarks::OnChanged::kEventName);
- ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
- this, bookmarks::OnMoved::kEventName);
- ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
- this, bookmarks::OnChildrenReordered::kEventName);
- ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
- this, bookmarks::OnImportBegan::kEventName);
- ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
- this, bookmarks::OnImportEnded::kEventName);
+BookmarksAPI::BookmarksAPI(BrowserContext* context)
+ : browser_context_(context) {
+ EventRouter* event_router = EventRouter::Get(browser_context_);
+ event_router->RegisterObserver(this, bookmarks::OnCreated::kEventName);
+ event_router->RegisterObserver(this, bookmarks::OnRemoved::kEventName);
+ event_router->RegisterObserver(this, bookmarks::OnChanged::kEventName);
+ event_router->RegisterObserver(this, bookmarks::OnMoved::kEventName);
+ event_router->RegisterObserver(this,
+ bookmarks::OnChildrenReordered::kEventName);
+ event_router->RegisterObserver(this, bookmarks::OnImportBegan::kEventName);
+ event_router->RegisterObserver(this, bookmarks::OnImportEnded::kEventName);
}
BookmarksAPI::~BookmarksAPI() {
}
void BookmarksAPI::Shutdown() {
- ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
+ EventRouter::Get(browser_context_)->UnregisterObserver(this);
}
-static base::LazyInstance<ProfileKeyedAPIFactory<BookmarksAPI> >
-g_factory = LAZY_INSTANCE_INITIALIZER;
+static base::LazyInstance<BrowserContextKeyedAPIFactory<BookmarksAPI> >
+ g_factory = LAZY_INSTANCE_INITIALIZER;
// static
-ProfileKeyedAPIFactory<BookmarksAPI>* BookmarksAPI::GetFactoryInstance() {
- return &g_factory.Get();
+BrowserContextKeyedAPIFactory<BookmarksAPI>*
+BookmarksAPI::GetFactoryInstance() {
+ return g_factory.Pointer();
}
void BookmarksAPI::OnListenerAdded(const EventListenerInfo& details) {
- bookmark_event_router_.reset(new BookmarkEventRouter(profile_,
- BookmarkModelFactory::GetForProfile(profile_)));
- ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
+ bookmark_event_router_.reset(new BookmarkEventRouter(
+ browser_context_,
+ BookmarkModelFactory::GetForProfile(
+ Profile::FromBrowserContext(browser_context_))));
+ EventRouter::Get(browser_context_)->UnregisterObserver(this);
}
-bool BookmarksGetFunction::RunImpl() {
+bool BookmarksGetFunction::RunOnReady() {
scoped_ptr<bookmarks::Get::Params> params(
bookmarks::Get::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
std::vector<linked_ptr<BookmarkTreeNode> > nodes;
- BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile());
if (params->id_or_id_list.as_strings) {
std::vector<std::string>& ids = *params->id_or_id_list.as_strings;
size_t count = ids.size();
EXTENSION_FUNCTION_VALIDATE(count > 0);
for (size_t i = 0; i < count; ++i) {
- int64 id;
- if (!GetBookmarkIdAsInt64(ids[i], &id))
+ const BookmarkNode* node = GetBookmarkNodeFromId(ids[i]);
+ if (!node)
return false;
- const BookmarkNode* node = model->GetNodeByID(id);
- if (!node) {
- error_ = keys::kNoNodeError;
- return false;
- } else {
- bookmark_api_helpers::AddNode(node, &nodes, false);
- }
+ bookmark_api_helpers::AddNode(node, &nodes, false);
}
} else {
- int64 id;
- if (!GetBookmarkIdAsInt64(*params->id_or_id_list.as_string, &id))
- return false;
- const BookmarkNode* node = model->GetNodeByID(id);
- if (!node) {
- error_ = keys::kNoNodeError;
+ const BookmarkNode* node =
+ GetBookmarkNodeFromId(*params->id_or_id_list.as_string);
+ if (!node)
return false;
- }
bookmark_api_helpers::AddNode(node, &nodes, false);
}
return true;
}
-bool BookmarksGetChildrenFunction::RunImpl() {
+bool BookmarksGetChildrenFunction::RunOnReady() {
scoped_ptr<bookmarks::GetChildren::Params> params(
bookmarks::GetChildren::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
- int64 id;
- if (!GetBookmarkIdAsInt64(params->id, &id))
+ const BookmarkNode* node = GetBookmarkNodeFromId(params->id);
+ if (!node)
return false;
std::vector<linked_ptr<BookmarkTreeNode> > nodes;
- const BookmarkNode* node =
- BookmarkModelFactory::GetForProfile(GetProfile())->GetNodeByID(id);
- if (!node) {
- error_ = keys::kNoNodeError;
- return false;
- }
int child_count = node->child_count();
for (int i = 0; i < child_count; ++i) {
const BookmarkNode* child = node->GetChild(i);
return true;
}
-bool BookmarksGetRecentFunction::RunImpl() {
+bool BookmarksGetRecentFunction::RunOnReady() {
scoped_ptr<bookmarks::GetRecent::Params> params(
bookmarks::GetRecent::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
return true;
}
-bool BookmarksGetTreeFunction::RunImpl() {
+bool BookmarksGetTreeFunction::RunOnReady() {
std::vector<linked_ptr<BookmarkTreeNode> > nodes;
const BookmarkNode* node =
BookmarkModelFactory::GetForProfile(GetProfile())->root_node();
return true;
}
-bool BookmarksGetSubTreeFunction::RunImpl() {
+bool BookmarksGetSubTreeFunction::RunOnReady() {
scoped_ptr<bookmarks::GetSubTree::Params> params(
bookmarks::GetSubTree::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
- int64 id;
- if (!GetBookmarkIdAsInt64(params->id, &id))
- return false;
-
- const BookmarkNode* node =
- BookmarkModelFactory::GetForProfile(GetProfile())->GetNodeByID(id);
- if (!node) {
- error_ = keys::kNoNodeError;
+ const BookmarkNode* node = GetBookmarkNodeFromId(params->id);
+ if (!node)
return false;
- }
std::vector<linked_ptr<BookmarkTreeNode> > nodes;
bookmark_api_helpers::AddNode(node, &nodes, true);
return true;
}
-bool BookmarksSearchFunction::RunImpl() {
+bool BookmarksSearchFunction::RunOnReady() {
scoped_ptr<bookmarks::Search::Params> params(
bookmarks::Search::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
PrefService* prefs = user_prefs::UserPrefs::Get(GetProfile());
std::string lang = prefs->GetString(prefs::kAcceptLanguages);
std::vector<const BookmarkNode*> nodes;
- bookmark_utils::GetBookmarksContainingText(
- BookmarkModelFactory::GetForProfile(GetProfile()),
- UTF8ToUTF16(params->query),
- std::numeric_limits<int>::max(),
- lang,
- &nodes);
+ if (params->query.as_string) {
+ bookmark_utils::QueryFields query;
+ query.word_phrase_query.reset(
+ new base::string16(base::UTF8ToUTF16(*params->query.as_string)));
+ bookmark_utils::GetBookmarksMatchingProperties(
+ BookmarkModelFactory::GetForProfile(GetProfile()),
+ query,
+ std::numeric_limits<int>::max(),
+ lang,
+ &nodes);
+ } else {
+ DCHECK(params->query.as_object);
+ const bookmarks::Search::Params::Query::Object& object =
+ *params->query.as_object;
+ bookmark_utils::QueryFields query;
+ if (object.query) {
+ query.word_phrase_query.reset(
+ new base::string16(base::UTF8ToUTF16(*object.query)));
+ }
+ if (object.url)
+ query.url.reset(new base::string16(base::UTF8ToUTF16(*object.url)));
+ if (object.title)
+ query.title.reset(new base::string16(base::UTF8ToUTF16(*object.title)));
+ bookmark_utils::GetBookmarksMatchingProperties(
+ BookmarkModelFactory::GetForProfile(GetProfile()),
+ query,
+ std::numeric_limits<int>::max(),
+ lang,
+ &nodes);
+ }
std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes;
for (std::vector<const BookmarkNode*>::iterator node_iter = nodes.begin();
return true;
}
-bool BookmarksRemoveFunction::RunImpl() {
+bool BookmarksRemoveFunction::RunOnReady() {
if (!EditBookmarksEnabled())
return false;
EXTENSION_FUNCTION_VALIDATE(params.get());
int64 id;
- if (!base::StringToInt64(params->id, &id)) {
- error_ = keys::kInvalidIdError;
+ if (!GetBookmarkIdAsInt64(params->id, &id))
return false;
- }
bool recursive = false;
if (name() == BookmarksRemoveTreeFunction::function_name())
return true;
}
-bool BookmarksCreateFunction::RunImpl() {
+bool BookmarksCreateFunction::RunOnReady() {
if (!EditBookmarksEnabled())
return false;
EXTENSION_FUNCTION_VALIDATE(params.get());
BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile());
- int64 parentId;
-
- if (!params->bookmark.parent_id.get()) {
- // Optional, default to "other bookmarks".
- parentId = model->other_node()->id();
- } else {
- if (!GetBookmarkIdAsInt64(*params->bookmark.parent_id, &parentId))
- return false;
- }
- const BookmarkNode* parent = model->GetNodeByID(parentId);
- if (!parent) {
- error_ = keys::kNoParentError;
+ const BookmarkNode* node = CreateBookmarkNode(model, params->bookmark, NULL);
+ if (!node)
return false;
- }
- if (parent->is_root()) { // Can't create children of the root.
- error_ = keys::kModifySpecialError;
- return false;
- }
-
- int index;
- if (!params->bookmark.index.get()) { // Optional (defaults to end).
- index = parent->child_count();
- } else {
- index = *params->bookmark.index;
- if (index > parent->child_count() || index < 0) {
- error_ = keys::kInvalidIndexError;
- return false;
- }
- }
-
- string16 title; // Optional.
- if (params->bookmark.title.get())
- title = UTF8ToUTF16(*params->bookmark.title.get());
-
- std::string url_string; // Optional.
- if (params->bookmark.url.get())
- url_string = *params->bookmark.url.get();
-
- GURL url(url_string);
- if (!url_string.empty() && !url.is_valid()) {
- error_ = keys::kInvalidUrlError;
- return false;
- }
-
- const BookmarkNode* node;
- if (url_string.length())
- node = model->AddURL(parent, index, title, url);
- else
- node = model->AddFolder(parent, index, title);
- DCHECK(node);
- if (!node) {
- error_ = keys::kNoNodeError;
- return false;
- }
scoped_ptr<BookmarkTreeNode> ret(
bookmark_api_helpers::GetBookmarkTreeNode(node, false, false));
return BookmarksUpdateFunction::ExtractIds(args, ids, invalid_id);
}
-bool BookmarksMoveFunction::RunImpl() {
+bool BookmarksMoveFunction::RunOnReady() {
if (!EditBookmarksEnabled())
return false;
bookmarks::Move::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
- int64 id;
- if (!base::StringToInt64(params->id, &id)) {
- error_ = keys::kInvalidIdError;
+ const BookmarkNode* node = GetBookmarkNodeFromId(params->id);
+ if (!node)
return false;
- }
BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile());
- const BookmarkNode* node = model->GetNodeByID(id);
- if (!node) {
- error_ = keys::kNoNodeError;
- return false;
- }
if (model->is_permanent_node(node)) {
error_ = keys::kModifySpecialError;
return false;
if (!GetBookmarkIdAsInt64(*params->destination.parent_id, &parentId))
return false;
- parent = model->GetNodeByID(parentId);
+ parent = GetBookmarkNodeByID(model, parentId);
}
if (!parent) {
error_ = keys::kNoParentError;
return BookmarksRemoveFunction::ExtractIds(args, ids, invalid_id);
}
-bool BookmarksUpdateFunction::RunImpl() {
+bool BookmarksUpdateFunction::RunOnReady() {
if (!EditBookmarksEnabled())
return false;
bookmarks::Update::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
- int64 id;
- if (!base::StringToInt64(params->id, &id)) {
- error_ = keys::kInvalidIdError;
- return false;
- }
-
- BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile());
-
// Optional but we need to distinguish non present from an empty title.
- string16 title;
+ base::string16 title;
bool has_title = false;
if (params->changes.title.get()) {
- title = UTF8ToUTF16(*params->changes.title);
+ title = base::UTF8ToUTF16(*params->changes.title);
has_title = true;
}
return false;
}
- const BookmarkNode* node = model->GetNodeByID(id);
- if (!node) {
- error_ = keys::kNoNodeError;
+ const BookmarkNode* node = GetBookmarkNodeFromId(params->id);
+ if (!node)
return false;
- }
+
+ BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile());
if (model->is_permanent_node(node)) {
error_ = keys::kModifySpecialError;
return false;
// unique bucket.
class CreateBookmarkBucketMapper : public BookmarkBucketMapper<std::string> {
public:
- explicit CreateBookmarkBucketMapper(Profile* profile) : profile_(profile) {}
- // TODO(tim): This should share code with BookmarksCreateFunction::RunImpl,
+ explicit CreateBookmarkBucketMapper(BrowserContext* context)
+ : browser_context_(context) {}
+ // TODO(tim): This should share code with BookmarksCreateFunction::RunOnReady,
// but I can't figure out a good way to do that with all the macros.
virtual void GetBucketsForArgs(const base::ListValue* args,
BucketList* buckets) OVERRIDE {
if (!json->GetString(keys::kParentIdKey, &parent_id))
return;
}
- BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_);
+ BookmarkModel* model = BookmarkModelFactory::GetForProfile(
+ Profile::FromBrowserContext(browser_context_));
int64 parent_id_int64;
base::StringToInt64(parent_id, &parent_id_int64);
- const BookmarkNode* parent = model->GetNodeByID(parent_id_int64);
+ const BookmarkNode* parent = GetBookmarkNodeByID(model, parent_id_int64);
if (!parent)
return;
- std::string bucket_id = UTF16ToUTF8(parent->GetTitle());
+ std::string bucket_id = base::UTF16ToUTF8(parent->GetTitle());
std::string title;
json->GetString(keys::kTitleKey, &title);
std::string url_string;
buckets->push_back(GetBucket(base::SHA1HashString(bucket_id)));
}
private:
- Profile* profile_;
+ BrowserContext* browser_context_;
};
// Mapper for 'bookmarks.remove'.
class RemoveBookmarksBucketMapper : public BookmarkBucketMapper<std::string> {
public:
- explicit RemoveBookmarksBucketMapper(Profile* profile) : profile_(profile) {}
+ explicit RemoveBookmarksBucketMapper(BrowserContext* context)
+ : browser_context_(context) {}
virtual void GetBucketsForArgs(const base::ListValue* args,
BucketList* buckets) OVERRIDE {
typedef std::list<int64> IdList;
}
for (IdList::iterator it = ids.begin(); it != ids.end(); ++it) {
- BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_);
- const BookmarkNode* node = model->GetNodeByID(*it);
+ BookmarkModel* model = BookmarkModelFactory::GetForProfile(
+ Profile::FromBrowserContext(browser_context_));
+ const BookmarkNode* node = GetBookmarkNodeByID(model, *it);
if (!node || node->is_root())
return;
std::string bucket_id;
- bucket_id += UTF16ToUTF8(node->parent()->GetTitle());
- bucket_id += UTF16ToUTF8(node->GetTitle());
+ bucket_id += base::UTF16ToUTF8(node->parent()->GetTitle());
+ bucket_id += base::UTF16ToUTF8(node->GetTitle());
bucket_id += node->url().spec();
buckets->push_back(GetBucket(base::SHA1HashString(bucket_id)));
}
}
private:
- Profile* profile_;
+ BrowserContext* browser_context_;
};
// Mapper for any bookmark function accepting bookmark IDs as parameters, where
// For bookmarks.create.
static void BuildForCreate(QuotaLimitHeuristics* heuristics,
- Profile* profile) {
- BuildWithMappers(heuristics, new CreateBookmarkBucketMapper(profile),
- new CreateBookmarkBucketMapper(profile));
+ BrowserContext* context) {
+ BuildWithMappers(heuristics,
+ new CreateBookmarkBucketMapper(context),
+ new CreateBookmarkBucketMapper(context));
}
// For bookmarks.remove.
static void BuildForRemove(QuotaLimitHeuristics* heuristics,
- Profile* profile) {
- BuildWithMappers(heuristics, new RemoveBookmarksBucketMapper(profile),
- new RemoveBookmarksBucketMapper(profile));
+ BrowserContext* context) {
+ BuildWithMappers(heuristics,
+ new RemoveBookmarksBucketMapper(context),
+ new RemoveBookmarksBucketMapper(context));
}
private:
file_type_info.extensions.resize(1);
file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("html"));
gfx::NativeWindow owning_window = web_contents ?
- platform_util::GetTopLevel(web_contents->GetView()->GetNativeView())
+ platform_util::GetTopLevel(web_contents->GetNativeView())
: NULL;
-#if defined(OS_WIN) && defined(USE_AURA)
+#if defined(OS_WIN)
if (!owning_window &&
chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH)
- owning_window = aura::RemoteRootWindowHostWin::Instance()->GetAshWindow();
+ owning_window = aura::RemoteWindowTreeHostWin::Instance()->GetAshWindow();
#endif
// |web_contents| can be NULL (for background pages), which is fine. In such
// a case if file-selection dialogs are forbidden by policy, we will not
// show an InfoBar, which is better than letting one appear out of the blue.
select_file_dialog_->SelectFile(type,
- string16(),
+ base::string16(),
default_path,
&file_type_info,
0,
NOTREACHED() << "Should not be able to select multiple files";
}
-bool BookmarksImportFunction::RunImpl() {
+bool BookmarksImportFunction::RunOnReady() {
if (!EditBookmarksEnabled())
return false;
SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE);
Release(); // Balanced in BookmarksIOFunction::SelectFile()
}
-bool BookmarksExportFunction::RunImpl() {
+bool BookmarksExportFunction::RunOnReady() {
SelectFile(ui::SelectFileDialog::SELECT_SAVEAS_FILE);
return true;
}