summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authorfeldstein@chromium.org <feldstein@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-28 00:04:56 +0000
committerfeldstein@chromium.org <feldstein@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-28 00:04:56 +0000
commitcb6cf79cacd881bcbc4f00bb449d7044619aeced (patch)
tree1b265453e2ad2a33beda3d6630444e76da3321a0 /chrome/browser
parent69b3f1eab57725dd68502c19f4eed1fa9c7f401f (diff)
downloadchromium_src-cb6cf79cacd881bcbc4f00bb449d7044619aeced.zip
chromium_src-cb6cf79cacd881bcbc4f00bb449d7044619aeced.tar.gz
chromium_src-cb6cf79cacd881bcbc4f00bb449d7044619aeced.tar.bz2
Add import/export extension apis
This is a duplicate of 543094. That code review seems to have gotten corrupted as i can no longer publish anything from their web UI. It was LGTM'd+nits by arv, which i've fixed here. BUG=32194 TEST=Import and export bookmarks from the extension Review URL: http://codereview.chromium.org/548167 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@37351 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/bookmarks/bookmark_model.cc10
-rw-r--r--chrome/browser/bookmarks/bookmark_model.h6
-rw-r--r--chrome/browser/bookmarks/bookmark_model_observer.h11
-rw-r--r--chrome/browser/cocoa/bookmark_bar_bridge.h4
-rw-r--r--chrome/browser/cocoa/bookmark_bar_bridge.mm16
-rw-r--r--chrome/browser/extensions/extension_bookmark_manager_api.cc63
-rw-r--r--chrome/browser/extensions/extension_bookmark_manager_api.h36
-rw-r--r--chrome/browser/extensions/extension_bookmarks_module.cc19
-rw-r--r--chrome/browser/extensions/extension_bookmarks_module.h2
-rw-r--r--chrome/browser/extensions/extension_bookmarks_module_constants.cc5
-rw-r--r--chrome/browser/extensions/extension_bookmarks_module_constants.h2
-rw-r--r--chrome/browser/extensions/extension_function_dispatcher.cc2
-rw-r--r--chrome/browser/importer/importer.cc9
13 files changed, 181 insertions, 4 deletions
diff --git a/chrome/browser/bookmarks/bookmark_model.cc b/chrome/browser/bookmarks/bookmark_model.cc
index 434fde0..5385810 100644
--- a/chrome/browser/bookmarks/bookmark_model.cc
+++ b/chrome/browser/bookmarks/bookmark_model.cc
@@ -609,6 +609,16 @@ void BookmarkModel::RemoveAndDeleteNode(BookmarkNode* delete_me) {
Details<history::URLsStarredDetails>(&details));
}
+void BookmarkModel::BeginImportMode() {
+ FOR_EACH_OBSERVER(BookmarkModelObserver, observers_,
+ BookmarkImportBeginning(this));
+}
+
+void BookmarkModel::EndImportMode() {
+ FOR_EACH_OBSERVER(BookmarkModelObserver, observers_,
+ BookmarkImportEnding(this));
+}
+
BookmarkNode* BookmarkModel::AddNode(BookmarkNode* parent,
int index,
BookmarkNode* node,
diff --git a/chrome/browser/bookmarks/bookmark_model.h b/chrome/browser/bookmarks/bookmark_model.h
index fcea7c7..1ffdcbe 100644
--- a/chrome/browser/bookmarks/bookmark_model.h
+++ b/chrome/browser/bookmarks/bookmark_model.h
@@ -205,6 +205,12 @@ class BookmarkModel : public NotificationObserver, public BookmarkService {
observers_.RemoveObserver(observer);
}
+ // Notify the observes that an import is about to happen, so they can
+ // delay any expensive UI updates until it is finished.
+ void BeginImportMode();
+ void EndImportMode();
+
+
// Unstars or deletes the specified entry. Removing a group entry recursively
// unstars all nodes. Observers are notified immediately.
void Remove(const BookmarkNode* parent, int index);
diff --git a/chrome/browser/bookmarks/bookmark_model_observer.h b/chrome/browser/bookmarks/bookmark_model_observer.h
index e850cb3..2136475 100644
--- a/chrome/browser/bookmarks/bookmark_model_observer.h
+++ b/chrome/browser/bookmarks/bookmark_model_observer.h
@@ -52,6 +52,17 @@ class BookmarkModelObserver {
virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
const BookmarkNode* node) = 0;
+ // Invoked before a batch import begins. This tells UI intensive observers
+ // to wait until the updates finish to update themselves.
+ // These methods should only be used for imports. Observers should still
+ // respond to BookmarkNodeRemoved immediately, to avoid holding onto
+ // stale node pointers.
+ virtual void BookmarkImportBeginning(BookmarkModel* model) {}
+
+ // Invoked after a batch import finishes. This tells observers to update
+ // themselves if they were waiting for the update to finish.
+ virtual void BookmarkImportEnding(BookmarkModel* model) {}
+
protected:
virtual ~BookmarkModelObserver() {}
};
diff --git a/chrome/browser/cocoa/bookmark_bar_bridge.h b/chrome/browser/cocoa/bookmark_bar_bridge.h
index 9c09e4a..23c6c78 100644
--- a/chrome/browser/cocoa/bookmark_bar_bridge.h
+++ b/chrome/browser/cocoa/bookmark_bar_bridge.h
@@ -45,9 +45,13 @@ class BookmarkBarBridge : public BookmarkModelObserver {
virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
const BookmarkNode* node);
+ virtual void BookmarkImportBeginning(BookmarkModel* model);
+ virtual void BookmarkImportEnding(BookmarkModel* model);
+
private:
BookmarkBarController* controller_; // weak; owns me
BookmarkModel* model_; // weak; it is owned by a Profile.
+ bool batch_mode_;
DISALLOW_COPY_AND_ASSIGN(BookmarkBarBridge);
};
diff --git a/chrome/browser/cocoa/bookmark_bar_bridge.mm b/chrome/browser/cocoa/bookmark_bar_bridge.mm
index e7ace5b..354897f 100644
--- a/chrome/browser/cocoa/bookmark_bar_bridge.mm
+++ b/chrome/browser/cocoa/bookmark_bar_bridge.mm
@@ -10,7 +10,8 @@
BookmarkBarBridge::BookmarkBarBridge(BookmarkBarController* controller,
BookmarkModel* model)
: controller_(controller),
- model_(model) {
+ model_(model),
+ batch_mode_(false) {
model_->AddObserver(this);
// Bookmark loading is async; it may may not have happened yet.
@@ -44,7 +45,9 @@ void BookmarkBarBridge::BookmarkNodeMoved(BookmarkModel* model,
void BookmarkBarBridge::BookmarkNodeAdded(BookmarkModel* model,
const BookmarkNode* parent,
int index) {
- [controller_ nodeAdded:model parent:parent index:index];
+ if (!batch_mode_) {
+ [controller_ nodeAdded:model parent:parent index:index];
+ }
}
void BookmarkBarBridge::BookmarkNodeRemoved(BookmarkModel* model,
@@ -68,3 +71,12 @@ void BookmarkBarBridge::BookmarkNodeChildrenReordered(
BookmarkModel* model, const BookmarkNode* node) {
[controller_ nodeChildrenReordered:model node:node];
}
+
+void BookmarkBarBridge::BookmarkImportBeginning(BookmarkModel* model) {
+ batch_mode_ = true;
+}
+
+void BookmarkBarBridge::BookmarkImportEnding(BookmarkModel* model) {
+ batch_mode_ = false;
+ [controller_ loaded:model];
+}
diff --git a/chrome/browser/extensions/extension_bookmark_manager_api.cc b/chrome/browser/extensions/extension_bookmark_manager_api.cc
index 58986a8..a6b3e08 100644
--- a/chrome/browser/extensions/extension_bookmark_manager_api.cc
+++ b/chrome/browser/extensions/extension_bookmark_manager_api.cc
@@ -7,6 +7,9 @@
#include "app/l10n_util.h"
#include "base/values.h"
#include "grit/generated_resources.h"
+#include "chrome/browser/profile.h"
+#include "chrome/browser/bookmarks/bookmark_html_writer.h"
+#include "chrome/browser/importer/importer.h"
bool CopyBookmarkManagerFunction::RunImpl() {
NOTIMPLEMENTED();
@@ -25,6 +28,66 @@ bool PasteBookmarkManagerFunction::RunImpl() {
return true;
}
+void BookmarkManagerIOFunction::SelectFile(SelectFileDialog::Type type) {
+ // Balanced in one of the three callbacks of SelectFileDialog:
+ // either FileSelectionCanceled, MultiFilesSelected, or FileSelected
+ AddRef();
+ select_file_dialog_ = SelectFileDialog::Create(this);
+ SelectFileDialog::FileTypeInfo file_type_info;
+ file_type_info.extensions.resize(1);
+ file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("html"));
+
+ select_file_dialog_->SelectFile(type,
+ string16(),
+ FilePath(),
+ &file_type_info,
+ 0,
+ FILE_PATH_LITERAL(""),
+ NULL,
+ NULL);
+}
+
+void BookmarkManagerIOFunction::FileSelectionCanceled(void* params) {
+ Release(); //Balanced in BookmarkManagerIOFunction::SelectFile()
+}
+
+void BookmarkManagerIOFunction::MultiFilesSelected(
+ const std::vector<FilePath>& files, void* params) {
+ Release(); //Balanced in BookmarkManagerIOFunction::SelectFile()
+ NOTREACHED() << "Should not be able to select multiple files";
+}
+
+bool ImportBookmarksFunction::RunImpl() {
+ SelectFile(SelectFileDialog::SELECT_OPEN_FILE);
+ return true;
+}
+
+void ImportBookmarksFunction::FileSelected(const FilePath& path,
+ int index,
+ void* params) {
+ ImporterHost* host = new ImporterHost();
+ ProfileInfo profile_info;
+ profile_info.browser_type = BOOKMARKS_HTML;
+ profile_info.source_path = path.ToWStringHack();
+ host->StartImportSettings(profile_info,
+ profile(),
+ FAVORITES,
+ new ProfileWriter(profile()),
+ true);
+ Release(); //Balanced in BookmarkManagerIOFunction::SelectFile()
+}
+
+bool ExportBookmarksFunction::RunImpl() {
+ SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE);
+ return true;
+}
+
+void ExportBookmarksFunction::FileSelected(const FilePath& path,
+ int index,
+ void* params) {
+ bookmark_html_writer::WriteBookmarks(profile()->GetBookmarkModel(), path);
+ Release(); //Balanced in BookmarkManagerIOFunction::SelectFile()
+}
bool BookmarkManagerGetStringsFunction::RunImpl() {
DictionaryValue* localized_strings = new DictionaryValue();
diff --git a/chrome/browser/extensions/extension_bookmark_manager_api.h b/chrome/browser/extensions/extension_bookmark_manager_api.h
index 2ab6be6c..f0bf1a3 100644
--- a/chrome/browser/extensions/extension_bookmark_manager_api.h
+++ b/chrome/browser/extensions/extension_bookmark_manager_api.h
@@ -6,6 +6,8 @@
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_BOOKMARK_MANAGER_API_H_
#include "chrome/browser/extensions/extension_function.h"
+#include "chrome/browser/shell_dialogs.h"
+#include "chrome/browser/extensions/extension_bookmarks_module.h"
class CopyBookmarkManagerFunction : public AsyncExtensionFunction {
public:
@@ -34,6 +36,39 @@ class PasteBookmarkManagerFunction : public AsyncExtensionFunction {
DECLARE_EXTENSION_FUNCTION_NAME("experimental.bookmarkManager.paste");
};
+class BookmarkManagerIOFunction : public BookmarksFunction,
+ public SelectFileDialog::Listener {
+ public:
+ // Overridden from SelectFileDialog::Listener:
+ virtual void FileSelected(const FilePath& path, int index, void* params) = 0;
+ void MultiFilesSelected(const std::vector<FilePath>& files, void* params);
+ void FileSelectionCanceled(void* params);
+ void SelectFile(SelectFileDialog::Type type);
+
+ protected:
+ scoped_refptr<SelectFileDialog> select_file_dialog_;
+};
+
+class ImportBookmarksFunction : public BookmarkManagerIOFunction {
+ public:
+ // Override BookmarkManagerFunction.
+ bool RunImpl();
+ void FileSelected(const FilePath& path, int index, void* params);
+
+ private:
+ DECLARE_EXTENSION_FUNCTION_NAME("experimental.bookmarkManager.import");
+};
+
+class ExportBookmarksFunction : public BookmarkManagerIOFunction {
+ public:
+ // Override BookmarkManagerFunction.
+ bool RunImpl();
+ void FileSelected(const FilePath& path, int index, void* params);
+
+ private:
+ DECLARE_EXTENSION_FUNCTION_NAME("experimental.bookmarkManager.export");
+};
+
class BookmarkManagerGetStringsFunction : public AsyncExtensionFunction {
public:
// Override BookmarkManagerFunction.
@@ -43,5 +78,4 @@ class BookmarkManagerGetStringsFunction : public AsyncExtensionFunction {
DECLARE_EXTENSION_FUNCTION_NAME("experimental.bookmarkManager.getStrings");
};
-
#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_BOOKMARK_MANAGER_API_H_
diff --git a/chrome/browser/extensions/extension_bookmarks_module.cc b/chrome/browser/extensions/extension_bookmarks_module.cc
index 568391c..33abc39 100644
--- a/chrome/browser/extensions/extension_bookmarks_module.cc
+++ b/chrome/browser/extensions/extension_bookmarks_module.cc
@@ -279,6 +279,25 @@ void ExtensionBookmarkEventRouter::BookmarkNodeChildrenReordered(
json_args);
}
+void ExtensionBookmarkEventRouter::
+ BookmarkImportBeginning(BookmarkModel* model) {
+ ListValue args;
+ std::string json_args;
+ base::JSONWriter::Write(&args, false, &json_args);
+ DispatchEvent(model->profile(),
+ keys::kOnBookmarkImportBegan,
+ json_args);
+}
+
+void ExtensionBookmarkEventRouter::BookmarkImportEnding(BookmarkModel* model) {
+ ListValue args;
+ std::string json_args;
+ base::JSONWriter::Write(&args, false, &json_args);
+ DispatchEvent(model->profile(),
+ keys::kOnBookmarkImportEnded,
+ json_args);
+}
+
bool GetBookmarksFunction::RunImpl() {
BookmarkModel* model = profile()->GetBookmarkModel();
scoped_ptr<ListValue> json(new ListValue());
diff --git a/chrome/browser/extensions/extension_bookmarks_module.h b/chrome/browser/extensions/extension_bookmarks_module.h
index 4caece2..b8989aa 100644
--- a/chrome/browser/extensions/extension_bookmarks_module.h
+++ b/chrome/browser/extensions/extension_bookmarks_module.h
@@ -45,6 +45,8 @@ class ExtensionBookmarkEventRouter : public BookmarkModelObserver {
const BookmarkNode* node);
virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
const BookmarkNode* node);
+ virtual void BookmarkImportBeginning(BookmarkModel* model);
+ virtual void BookmarkImportEnding(BookmarkModel* model);
private:
ExtensionBookmarkEventRouter();
diff --git a/chrome/browser/extensions/extension_bookmarks_module_constants.cc b/chrome/browser/extensions/extension_bookmarks_module_constants.cc
index 6f72669..9440f06 100644
--- a/chrome/browser/extensions/extension_bookmarks_module_constants.cc
+++ b/chrome/browser/extensions/extension_bookmarks_module_constants.cc
@@ -33,5 +33,8 @@ const char kOnBookmarkRemoved[] = "bookmarks.onRemoved";
const char kOnBookmarkChanged[] = "bookmarks.onChanged";
const char kOnBookmarkMoved[] = "bookmarks.onMoved";
const char kOnBookmarkChildrenReordered[] = "bookmarks.onChildrenReordered";
-
+const char kOnBookmarkImportBegan[] =
+ "experimental.bookmarkManager.onImportBegan";
+const char kOnBookmarkImportEnded[] =
+ "experimental.bookmarkManager.onImportEnded";
} // namespace extension_bookmarks_module_constants
diff --git a/chrome/browser/extensions/extension_bookmarks_module_constants.h b/chrome/browser/extensions/extension_bookmarks_module_constants.h
index 159dc7f..7214e80 100644
--- a/chrome/browser/extensions/extension_bookmarks_module_constants.h
+++ b/chrome/browser/extensions/extension_bookmarks_module_constants.h
@@ -38,6 +38,8 @@ extern const char kOnBookmarkRemoved[];
extern const char kOnBookmarkChanged[];
extern const char kOnBookmarkMoved[];
extern const char kOnBookmarkChildrenReordered[];
+extern const char kOnBookmarkImportBegan[];
+extern const char kOnBookmarkImportEnded[];
}; // namespace extension_bookmarks_module_constants
diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc
index ba3e80e..6e719fc 100644
--- a/chrome/browser/extensions/extension_function_dispatcher.cc
+++ b/chrome/browser/extensions/extension_function_dispatcher.cc
@@ -136,6 +136,8 @@ void FactoryRegistry::ResetFunctions() {
RegisterFunction<CopyBookmarkManagerFunction>();
RegisterFunction<CutBookmarkManagerFunction>();
RegisterFunction<PasteBookmarkManagerFunction>();
+ RegisterFunction<ImportBookmarksFunction>();
+ RegisterFunction<ExportBookmarksFunction>();
RegisterFunction<BookmarkManagerGetStringsFunction>();
// History
diff --git a/chrome/browser/importer/importer.cc b/chrome/browser/importer/importer.cc
index eddc493..359790b 100644
--- a/chrome/browser/importer/importer.cc
+++ b/chrome/browser/importer/importer.cc
@@ -85,6 +85,11 @@ void ProfileWriter::AddBookmarkEntry(
bool show_bookmark_toolbar = false;
std::set<const BookmarkNode*> groups_added_to;
+ bool import_mode = false;
+ if (bookmark.size() > 1) {
+ model->BeginImportMode();
+ import_mode = true;
+ }
for (std::vector<BookmarkEntry>::const_iterator it = bookmark.begin();
it != bookmark.end(); ++it) {
// Don't insert this url if it isn't valid.
@@ -142,6 +147,10 @@ void ProfileWriter::AddBookmarkEntry(
model->ResetDateGroupModified(*i);
}
+ if (import_mode) {
+ model->EndImportMode();
+ }
+
if (show_bookmark_toolbar)
ShowBookmarkBar();
}