| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/extension_bookmark_manager_api.h" | 5 #include "chrome/browser/extensions/extension_bookmark_manager_api.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/bookmarks/bookmark_drag_data.h" | 13 #include "chrome/browser/bookmarks/bookmark_drag_data.h" |
| 14 #include "chrome/browser/bookmarks/bookmark_model.h" | 14 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 15 #include "chrome/browser/bookmarks/bookmark_html_writer.h" | 15 #include "chrome/browser/bookmarks/bookmark_html_writer.h" |
| 16 #include "chrome/browser/bookmarks/bookmark_utils.h" | 16 #include "chrome/browser/bookmarks/bookmark_utils.h" |
| 17 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" | 17 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" |
| 18 #include "chrome/browser/extensions/extension_bookmarks_module_constants.h" | 18 #include "chrome/browser/extensions/extension_bookmarks_module_constants.h" |
| 19 #include "chrome/browser/extensions/extension_dom_ui.h" | 19 #include "chrome/browser/extensions/extension_dom_ui.h" |
| 20 #include "chrome/browser/extensions/extension_message_service.h" | 20 #include "chrome/browser/extensions/extension_message_service.h" |
| 21 #include "chrome/browser/importer/importer.h" | 21 #include "chrome/browser/importer/importer.h" |
| 22 #include "chrome/browser/profile.h" | 22 #include "chrome/browser/profile.h" |
| 23 #include "chrome/browser/tab_contents/tab_contents.h" | 23 #include "chrome/browser/tab_contents/tab_contents.h" |
| 24 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
| 25 | 25 |
| 26 namespace keys = extension_bookmarks_module_constants; | 26 namespace keys = extension_bookmarks_module_constants; |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 typedef RenderViewHostDelegate::BookmarkDrag::DragData DragData; | |
| 31 | |
| 32 // Returns a single bookmark node from the argument ID. | 30 // Returns a single bookmark node from the argument ID. |
| 33 // This returns NULL in case of failure. | 31 // This returns NULL in case of failure. |
| 34 const BookmarkNode* GetNodeFromArguments(BookmarkModel* model, | 32 const BookmarkNode* GetNodeFromArguments(BookmarkModel* model, |
| 35 const Value* args) { | 33 const Value* args) { |
| 36 std::string id_string; | 34 std::string id_string; |
| 37 if (!args->GetAsString(&id_string)) | 35 if (!args->GetAsString(&id_string)) |
| 38 return NULL; | 36 return NULL; |
| 39 int64 id; | 37 int64 id; |
| 40 if (!StringToInt64(id_string, &id)) | 38 if (!StringToInt64(id_string, &id)) |
| 41 return NULL; | 39 return NULL; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 // We do not have an node IDs when the data comes from a different profile. | 127 // We do not have an node IDs when the data comes from a different profile. |
| 130 std::vector<BookmarkDragData::Element> elements = data.elements; | 128 std::vector<BookmarkDragData::Element> elements = data.elements; |
| 131 for (size_t i = 0; i < elements.size(); ++i) | 129 for (size_t i = 0; i < elements.size(); ++i) |
| 132 AddElementToList(list, elements[i]); | 130 AddElementToList(list, elements[i]); |
| 133 } | 131 } |
| 134 value->Set(keys::kElementsKey, list); | 132 value->Set(keys::kElementsKey, list); |
| 135 | 133 |
| 136 args->Append(value); | 134 args->Append(value); |
| 137 } | 135 } |
| 138 | 136 |
| 139 // This is the platform specific function that takes the drag data and creates | |
| 140 // the BookmarkDragData as needed. | |
| 141 bool GetBookmarkDragData(const DragData* data, | |
| 142 BookmarkDragData* bookmark_drag_data) { | |
| 143 #if defined(TOOLKIT_VIEWS) | |
| 144 // On TOOLKIT_VIEWS DragData is OSExchangeData. | |
| 145 return bookmark_drag_data->Read(*data); | |
| 146 #elif defined(OS_MACOSX) | |
| 147 return bookmark_drag_data->ReadFromDragClipboard(); | |
| 148 #else | |
| 149 NOTIMPLEMENTED(); | |
| 150 return false; | |
| 151 #endif | |
| 152 } | |
| 153 | |
| 154 } // namespace | 137 } // namespace |
| 155 | 138 |
| 156 ExtensionBookmarkManagerEventRouter::ExtensionBookmarkManagerEventRouter( | 139 ExtensionBookmarkManagerEventRouter::ExtensionBookmarkManagerEventRouter( |
| 157 Profile* profile, TabContents* tab_contents) | 140 Profile* profile, TabContents* tab_contents) |
| 158 : profile_(profile), | 141 : profile_(profile), |
| 159 tab_contents_(tab_contents) { | 142 tab_contents_(tab_contents) { |
| 160 tab_contents_->SetBookmarkDragDelegate(this); | 143 tab_contents_->SetBookmarkDragDelegate(this); |
| 161 } | 144 } |
| 162 | 145 |
| 163 ExtensionBookmarkManagerEventRouter::~ExtensionBookmarkManagerEventRouter() { | 146 ExtensionBookmarkManagerEventRouter::~ExtensionBookmarkManagerEventRouter() { |
| 164 if (tab_contents_->GetBookmarkDragDelegate() == this) | 147 if (tab_contents_->GetBookmarkDragDelegate() == this) |
| 165 tab_contents_->SetBookmarkDragDelegate(NULL); | 148 tab_contents_->SetBookmarkDragDelegate(NULL); |
| 166 } | 149 } |
| 167 | 150 |
| 168 void ExtensionBookmarkManagerEventRouter::DispatchEvent(const char* event_name, | 151 void ExtensionBookmarkManagerEventRouter::DispatchEvent(const char* event_name, |
| 169 const ListValue* args) { | 152 const ListValue* args) { |
| 170 if (!profile_->GetExtensionMessageService()) | 153 if (!profile_->GetExtensionMessageService()) |
| 171 return; | 154 return; |
| 172 | 155 |
| 173 std::string json_args; | 156 std::string json_args; |
| 174 base::JSONWriter::Write(args, false, &json_args); | 157 base::JSONWriter::Write(args, false, &json_args); |
| 175 profile_->GetExtensionMessageService()->DispatchEventToRenderers( | 158 profile_->GetExtensionMessageService()->DispatchEventToRenderers( |
| 176 event_name, json_args, profile_->IsOffTheRecord()); | 159 event_name, json_args, profile_->IsOffTheRecord()); |
| 177 } | 160 } |
| 178 | 161 |
| 179 void ExtensionBookmarkManagerEventRouter::DispatchDragEvent( | 162 void ExtensionBookmarkManagerEventRouter::DispatchDragEvent( |
| 180 const DragData* data, const char* event_name) { | 163 const BookmarkDragData& data, const char* event_name) { |
| 181 BookmarkDragData bookmark_drag_data; | 164 if (data.size() == 0) |
| 182 if (::GetBookmarkDragData(data, &bookmark_drag_data)) { | 165 return; |
| 183 ListValue args; | 166 |
| 184 BookmarkDragDataToJSON(profile_, bookmark_drag_data, &args); | 167 ListValue args; |
| 185 DispatchEvent(event_name, &args); | 168 BookmarkDragDataToJSON(profile_, data, &args); |
| 186 } | 169 DispatchEvent(event_name, &args); |
| 187 } | 170 } |
| 188 | 171 |
| 189 void ExtensionBookmarkManagerEventRouter::OnDragEnter(const DragData* data) { | 172 void ExtensionBookmarkManagerEventRouter::OnDragEnter( |
| 173 const BookmarkDragData& data) { |
| 190 DispatchDragEvent(data, keys::kOnBookmarkDragEnter); | 174 DispatchDragEvent(data, keys::kOnBookmarkDragEnter); |
| 191 } | 175 } |
| 192 | 176 |
| 193 void ExtensionBookmarkManagerEventRouter::OnDragOver(const DragData* data) { | 177 void ExtensionBookmarkManagerEventRouter::OnDragOver( |
| 178 const BookmarkDragData& data) { |
| 194 // Intentionally empty since these events happens too often and floods the | 179 // Intentionally empty since these events happens too often and floods the |
| 195 // message queue. We do not need this event for the bookmark manager anyway. | 180 // message queue. We do not need this event for the bookmark manager anyway. |
| 196 } | 181 } |
| 197 | 182 |
| 198 void ExtensionBookmarkManagerEventRouter::OnDragLeave(const DragData* data) { | 183 void ExtensionBookmarkManagerEventRouter::OnDragLeave( |
| 184 const BookmarkDragData& data) { |
| 199 DispatchDragEvent(data, keys::kOnBookmarkDragLeave); | 185 DispatchDragEvent(data, keys::kOnBookmarkDragLeave); |
| 200 } | 186 } |
| 201 | 187 |
| 202 void ExtensionBookmarkManagerEventRouter::OnDrop(const DragData* data) { | 188 void ExtensionBookmarkManagerEventRouter::OnDrop( |
| 189 const BookmarkDragData& data) { |
| 203 DispatchDragEvent(data, keys::kOnBookmarkDrop); | 190 DispatchDragEvent(data, keys::kOnBookmarkDrop); |
| 204 | 191 |
| 205 // Make a copy that is owned by this instance. | 192 // Make a copy that is owned by this instance. |
| 206 ClearBookmarkDragData(); | 193 ClearBookmarkDragData(); |
| 207 ::GetBookmarkDragData(data, &bookmark_drag_data_); | 194 bookmark_drag_data_ = data; |
| 208 } | 195 } |
| 209 | 196 |
| 210 const BookmarkDragData* | 197 const BookmarkDragData* |
| 211 ExtensionBookmarkManagerEventRouter::GetBookmarkDragData() { | 198 ExtensionBookmarkManagerEventRouter::GetBookmarkDragData() { |
| 212 if (bookmark_drag_data_.is_valid()) | 199 if (bookmark_drag_data_.is_valid()) |
| 213 return &bookmark_drag_data_; | 200 return &bookmark_drag_data_; |
| 214 return NULL; | 201 return NULL; |
| 215 } | 202 } |
| 216 | 203 |
| 217 void ExtensionBookmarkManagerEventRouter::ClearBookmarkDragData() { | 204 void ExtensionBookmarkManagerEventRouter::ClearBookmarkDragData() { |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 | 436 |
| 450 DCHECK(router); | 437 DCHECK(router); |
| 451 | 438 |
| 452 bookmark_utils::PerformBookmarkDrop(profile(), *router->GetBookmarkDragData(), | 439 bookmark_utils::PerformBookmarkDrop(profile(), *router->GetBookmarkDragData(), |
| 453 drop_parent, drop_index); | 440 drop_parent, drop_index); |
| 454 | 441 |
| 455 router->ClearBookmarkDragData(); | 442 router->ClearBookmarkDragData(); |
| 456 SendResponse(true); | 443 SendResponse(true); |
| 457 return true; | 444 return true; |
| 458 } | 445 } |
| OLD | NEW |