OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | |
6 | |
5 #include "chrome/browser/extensions/api/bookmarks/bookmarks_api.h" | 7 #include "chrome/browser/extensions/api/bookmarks/bookmarks_api.h" |
6 | 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
9 #include "base/i18n/file_util_icu.h" | 11 #include "base/i18n/file_util_icu.h" |
10 #include "base/i18n/time_formatting.h" | 12 #include "base/i18n/time_formatting.h" |
11 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
12 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
14 #include "base/path_service.h" | 16 #include "base/path_service.h" |
15 #include "base/prefs/pref_service.h" | 17 #include "base/prefs/pref_service.h" |
18 #include "base/rand_util.h" | |
16 #include "base/sha1.h" | 19 #include "base/sha1.h" |
17 #include "base/stl_util.h" | 20 #include "base/stl_util.h" |
18 #include "base/strings/string16.h" | 21 #include "base/strings/string16.h" |
19 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
20 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
21 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
22 #include "base/time/time.h" | 25 #include "base/time/time.h" |
23 #include "chrome/browser/bookmarks/bookmark_html_writer.h" | 26 #include "chrome/browser/bookmarks/bookmark_html_writer.h" |
24 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 27 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
25 #include "chrome/browser/bookmarks/chrome_bookmark_client.h" | 28 #include "chrome/browser/bookmarks/chrome_bookmark_client.h" |
(...skipping 11 matching lines...) Expand all Loading... | |
37 #include "chrome/common/importer/importer_data_types.h" | 40 #include "chrome/common/importer/importer_data_types.h" |
38 #include "chrome/common/pref_names.h" | 41 #include "chrome/common/pref_names.h" |
39 #include "components/bookmarks/browser/bookmark_model.h" | 42 #include "components/bookmarks/browser/bookmark_model.h" |
40 #include "components/bookmarks/browser/bookmark_utils.h" | 43 #include "components/bookmarks/browser/bookmark_utils.h" |
41 #include "components/user_prefs/user_prefs.h" | 44 #include "components/user_prefs/user_prefs.h" |
42 #include "content/public/browser/browser_context.h" | 45 #include "content/public/browser/browser_context.h" |
43 #include "content/public/browser/notification_service.h" | 46 #include "content/public/browser/notification_service.h" |
44 #include "content/public/browser/web_contents.h" | 47 #include "content/public/browser/web_contents.h" |
45 #include "extensions/browser/event_router.h" | 48 #include "extensions/browser/event_router.h" |
46 #include "extensions/browser/extension_function_dispatcher.h" | 49 #include "extensions/browser/extension_function_dispatcher.h" |
50 #include "extensions/browser/extension_registry.h" | |
47 #include "extensions/browser/quota_service.h" | 51 #include "extensions/browser/quota_service.h" |
52 #include "extensions/common/permissions/permissions_data.h" | |
48 #include "grit/generated_resources.h" | 53 #include "grit/generated_resources.h" |
49 #include "ui/base/l10n/l10n_util.h" | 54 #include "ui/base/l10n/l10n_util.h" |
50 | 55 |
51 #if defined(OS_WIN) | 56 #if defined(OS_WIN) |
52 #include "ui/aura/remote_window_tree_host_win.h" | 57 #include "ui/aura/remote_window_tree_host_win.h" |
53 #endif | 58 #endif |
54 | 59 |
55 namespace extensions { | 60 namespace extensions { |
56 | 61 |
57 namespace keys = bookmark_api_constants; | 62 namespace keys = bookmark_api_constants; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 base::TimeFormatShortDateNumeric(time)); | 94 base::TimeFormatShortDateNumeric(time)); |
90 #endif | 95 #endif |
91 | 96 |
92 file_util::ReplaceIllegalCharactersInPath(&filename, '_'); | 97 file_util::ReplaceIllegalCharactersInPath(&filename, '_'); |
93 | 98 |
94 base::FilePath default_path; | 99 base::FilePath default_path; |
95 PathService::Get(chrome::DIR_USER_DOCUMENTS, &default_path); | 100 PathService::Get(chrome::DIR_USER_DOCUMENTS, &default_path); |
96 return default_path.Append(filename); | 101 return default_path.Append(filename); |
97 } | 102 } |
98 | 103 |
104 bool IsEnhancedBookmarksExtensionActive(Profile* profile) { | |
105 static const char *enhanced_extension_hashes[] = { | |
Yoyo Zhou
2014/06/13 23:54:00
Comment what extensions these represent.
Mike Wittman
2014/06/14 00:49:55
Done.
| |
106 "D5736E4B5CF695CB93A2FB57E4FDC6E5AFAB6FE2", | |
107 "D57DE394F36DC1C3220E7604C575D29C51A6C495", | |
108 "3F65507A3B39259B38C8173C6FFA3D12DF64CCE9" | |
109 }; | |
110 const ExtensionSet& extensions = | |
111 ExtensionRegistry::Get(profile)->enabled_extensions(); | |
112 for (ExtensionSet::const_iterator it = extensions.begin(); | |
113 it != extensions.end(); ++it) { | |
114 const Extension* extension = *it; | |
115 if (extension->permissions_data()->HasAPIPermission( | |
116 APIPermission::kBookmarkManagerPrivate)) { | |
117 std::string hash = base::SHA1HashString(extension->id()); | |
118 hash = base::HexEncode(hash.c_str(), hash.length()); | |
119 for (size_t i = 0; i < arraysize(enhanced_extension_hashes); i++) | |
120 if (hash == enhanced_extension_hashes[i]) | |
121 return true; | |
122 } | |
123 } | |
124 return false; | |
125 } | |
126 | |
127 std::string ToBase36(int64 value) { | |
128 DCHECK(value >= 0); | |
129 std::string str; | |
130 while (value > 0) { | |
131 int digit = value % 36; | |
132 value /= 36; | |
133 str += (digit < 10 ? '0' + digit : 'a' + digit - 10); | |
134 } | |
135 std::reverse(str.begin(), str.end()); | |
136 return str; | |
137 } | |
138 | |
139 // Generate a metadata ID based on a the current time and a random number for | |
140 // enhanced bookmarks, to be assigned pre-sync. | |
141 std::string GenerateEnhancedBookmarksID(bool is_folder) { | |
142 static const char bookmark_prefix[] = "cc_"; | |
143 static const char folder_prefix[] = "cf_"; | |
144 // Use [0..range_mid) for bookmarks, [range_mid..2*range_mid) for folders. | |
Yoyo Zhou
2014/06/13 23:54:00
Why is this necessary? They have different prefixe
Mike Wittman
2014/06/14 00:49:55
To match the existing extension and server-side be
| |
145 int range_mid = 36*36*36*36 / 2; | |
146 int rand = base::RandInt(0, range_mid - 1); | |
147 int64 unix_epoch_time_in_ms = | |
148 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds(); | |
149 return std::string(is_folder ? folder_prefix : bookmark_prefix) + | |
150 ToBase36(is_folder ? range_mid + rand : rand) + | |
151 ToBase36(unix_epoch_time_in_ms); | |
152 } | |
153 | |
99 } // namespace | 154 } // namespace |
100 | 155 |
101 bool BookmarksFunction::RunAsync() { | 156 bool BookmarksFunction::RunAsync() { |
102 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 157 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); |
103 if (!model->loaded()) { | 158 if (!model->loaded()) { |
104 // Bookmarks are not ready yet. We'll wait. | 159 // Bookmarks are not ready yet. We'll wait. |
105 model->AddObserver(this); | 160 model->AddObserver(this); |
106 AddRef(); // Balanced in Loaded(). | 161 AddRef(); // Balanced in Loaded(). |
107 return true; | 162 return true; |
108 } | 163 } |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 move_info.parent_id = base::Int64ToString(new_parent->id()); | 336 move_info.parent_id = base::Int64ToString(new_parent->id()); |
282 move_info.index = new_index; | 337 move_info.index = new_index; |
283 move_info.old_parent_id = base::Int64ToString(old_parent->id()); | 338 move_info.old_parent_id = base::Int64ToString(old_parent->id()); |
284 move_info.old_index = old_index; | 339 move_info.old_index = old_index; |
285 | 340 |
286 DispatchEvent( | 341 DispatchEvent( |
287 bookmarks::OnMoved::kEventName, | 342 bookmarks::OnMoved::kEventName, |
288 bookmarks::OnMoved::Create(base::Int64ToString(node->id()), move_info)); | 343 bookmarks::OnMoved::Create(base::Int64ToString(node->id()), move_info)); |
289 } | 344 } |
290 | 345 |
346 void BookmarkEventRouter::BookmarkNodeAdding(BookmarkModel* model, | |
347 BookmarkNode* node) { | |
348 // TODO(wittman): Remove this once extension hooks are in place to allow the | |
349 // enhanced bookmarks extension to manage all bookmark creation code | |
350 // paths. See http://crbug.com/383557. | |
351 if (IsEnhancedBookmarksExtensionActive(Profile::FromBrowserContext( | |
352 browser_context_))) { | |
353 static const char key[] = "stars.id"; | |
354 node->SetMetaInfo(key, GenerateEnhancedBookmarksID(node->is_folder())); | |
355 } | |
356 } | |
357 | |
291 void BookmarkEventRouter::BookmarkNodeAdded(BookmarkModel* model, | 358 void BookmarkEventRouter::BookmarkNodeAdded(BookmarkModel* model, |
292 const BookmarkNode* parent, | 359 const BookmarkNode* parent, |
293 int index) { | 360 int index) { |
294 const BookmarkNode* node = parent->GetChild(index); | 361 const BookmarkNode* node = parent->GetChild(index); |
295 scoped_ptr<BookmarkTreeNode> tree_node( | 362 scoped_ptr<BookmarkTreeNode> tree_node( |
296 bookmark_api_helpers::GetBookmarkTreeNode(client_, node, false, false)); | 363 bookmark_api_helpers::GetBookmarkTreeNode(client_, node, false, false)); |
297 DispatchEvent(bookmarks::OnCreated::kEventName, | 364 DispatchEvent(bookmarks::OnCreated::kEventName, |
298 bookmarks::OnCreated::Create(base::Int64ToString(node->id()), | 365 bookmarks::OnCreated::Create(base::Int64ToString(node->id()), |
299 *tree_node)); | 366 *tree_node)); |
300 } | 367 } |
(...skipping 739 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1040 #if !defined(OS_ANDROID) | 1107 #if !defined(OS_ANDROID) |
1041 // Android does not have support for the standard exporter. | 1108 // Android does not have support for the standard exporter. |
1042 // TODO(jgreenwald): remove ifdef once extensions are no longer built on | 1109 // TODO(jgreenwald): remove ifdef once extensions are no longer built on |
1043 // Android. | 1110 // Android. |
1044 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); | 1111 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); |
1045 #endif | 1112 #endif |
1046 Release(); // Balanced in BookmarksIOFunction::SelectFile() | 1113 Release(); // Balanced in BookmarksIOFunction::SelectFile() |
1047 } | 1114 } |
1048 | 1115 |
1049 } // namespace extensions | 1116 } // namespace extensions |
OLD | NEW |