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 | |
7 #include "chrome/browser/extensions/api/bookmarks/bookmarks_api.h" | 5 #include "chrome/browser/extensions/api/bookmarks/bookmarks_api.h" |
8 | 6 |
9 #include "base/bind.h" | 7 #include "base/bind.h" |
10 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
11 #include "base/i18n/file_util_icu.h" | 9 #include "base/i18n/file_util_icu.h" |
12 #include "base/i18n/time_formatting.h" | 10 #include "base/i18n/time_formatting.h" |
13 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
14 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
15 #include "base/path_service.h" | 13 #include "base/path_service.h" |
16 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
17 #include "base/rand_util.h" | |
18 #include "base/sha1.h" | 15 #include "base/sha1.h" |
19 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
20 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
21 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
22 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
23 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
24 #include "base/time/time.h" | 21 #include "base/time/time.h" |
25 #include "chrome/browser/bookmarks/bookmark_html_writer.h" | 22 #include "chrome/browser/bookmarks/bookmark_html_writer.h" |
26 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 23 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
27 #include "chrome/browser/bookmarks/chrome_bookmark_client.h" | 24 #include "chrome/browser/bookmarks/chrome_bookmark_client.h" |
(...skipping 12 matching lines...) Expand all Loading... |
40 #include "chrome/common/pref_names.h" | 37 #include "chrome/common/pref_names.h" |
41 #include "chrome/grit/generated_resources.h" | 38 #include "chrome/grit/generated_resources.h" |
42 #include "components/bookmarks/browser/bookmark_model.h" | 39 #include "components/bookmarks/browser/bookmark_model.h" |
43 #include "components/bookmarks/browser/bookmark_utils.h" | 40 #include "components/bookmarks/browser/bookmark_utils.h" |
44 #include "components/user_prefs/user_prefs.h" | 41 #include "components/user_prefs/user_prefs.h" |
45 #include "content/public/browser/browser_context.h" | 42 #include "content/public/browser/browser_context.h" |
46 #include "content/public/browser/notification_service.h" | 43 #include "content/public/browser/notification_service.h" |
47 #include "content/public/browser/web_contents.h" | 44 #include "content/public/browser/web_contents.h" |
48 #include "extensions/browser/event_router.h" | 45 #include "extensions/browser/event_router.h" |
49 #include "extensions/browser/extension_function_dispatcher.h" | 46 #include "extensions/browser/extension_function_dispatcher.h" |
50 #include "extensions/browser/extension_registry.h" | |
51 #include "extensions/browser/notification_types.h" | 47 #include "extensions/browser/notification_types.h" |
52 #include "extensions/common/permissions/permissions_data.h" | |
53 #include "ui/base/l10n/l10n_util.h" | 48 #include "ui/base/l10n/l10n_util.h" |
54 | 49 |
55 #if defined(OS_WIN) | 50 #if defined(OS_WIN) |
56 #include "ui/aura/remote_window_tree_host_win.h" | 51 #include "ui/aura/remote_window_tree_host_win.h" |
57 #endif | 52 #endif |
58 | 53 |
59 namespace extensions { | 54 namespace extensions { |
60 | 55 |
61 namespace keys = bookmark_api_constants; | 56 namespace keys = bookmark_api_constants; |
62 namespace bookmarks = api::bookmarks; | 57 namespace bookmarks = api::bookmarks; |
(...skipping 22 matching lines...) Expand all Loading... |
85 base::TimeFormatShortDateNumeric(time)); | 80 base::TimeFormatShortDateNumeric(time)); |
86 #endif | 81 #endif |
87 | 82 |
88 base::i18n::ReplaceIllegalCharactersInPath(&filename, '_'); | 83 base::i18n::ReplaceIllegalCharactersInPath(&filename, '_'); |
89 | 84 |
90 base::FilePath default_path; | 85 base::FilePath default_path; |
91 PathService::Get(chrome::DIR_USER_DOCUMENTS, &default_path); | 86 PathService::Get(chrome::DIR_USER_DOCUMENTS, &default_path); |
92 return default_path.Append(filename); | 87 return default_path.Append(filename); |
93 } | 88 } |
94 | 89 |
95 bool IsEnhancedBookmarksExtensionActive(Profile* profile) { | |
96 static const char *enhanced_extension_hashes[] = { | |
97 "D5736E4B5CF695CB93A2FB57E4FDC6E5AFAB6FE2", // http://crbug.com/312900 | |
98 "D57DE394F36DC1C3220E7604C575D29C51A6C495", // http://crbug.com/319444 | |
99 "3F65507A3B39259B38C8173C6FFA3D12DF64CCE9" // http://crbug.com/371562 | |
100 }; | |
101 const ExtensionSet& extensions = | |
102 ExtensionRegistry::Get(profile)->enabled_extensions(); | |
103 for (ExtensionSet::const_iterator it = extensions.begin(); | |
104 it != extensions.end(); ++it) { | |
105 const Extension* extension = it->get(); | |
106 if (extension->permissions_data()->HasAPIPermission( | |
107 APIPermission::kBookmarkManagerPrivate)) { | |
108 std::string hash = base::SHA1HashString(extension->id()); | |
109 hash = base::HexEncode(hash.c_str(), hash.length()); | |
110 for (size_t i = 0; i < arraysize(enhanced_extension_hashes); i++) | |
111 if (hash == enhanced_extension_hashes[i]) | |
112 return true; | |
113 } | |
114 } | |
115 return false; | |
116 } | |
117 | |
118 std::string ToBase36(int64 value) { | |
119 DCHECK(value >= 0); | |
120 std::string str; | |
121 while (value > 0) { | |
122 int digit = value % 36; | |
123 value /= 36; | |
124 str += (digit < 10 ? '0' + digit : 'a' + digit - 10); | |
125 } | |
126 std::reverse(str.begin(), str.end()); | |
127 return str; | |
128 } | |
129 | |
130 // Generate a metadata ID based on a the current time and a random number for | |
131 // enhanced bookmarks, to be assigned pre-sync. | |
132 std::string GenerateEnhancedBookmarksID(bool is_folder) { | |
133 static const char bookmark_prefix[] = "cc_"; | |
134 static const char folder_prefix[] = "cf_"; | |
135 // Use [0..range_mid) for bookmarks, [range_mid..2*range_mid) for folders. | |
136 int range_mid = 36*36*36*36 / 2; | |
137 int rand = base::RandInt(0, range_mid - 1); | |
138 int64 unix_epoch_time_in_ms = | |
139 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds(); | |
140 return std::string(is_folder ? folder_prefix : bookmark_prefix) + | |
141 ToBase36(is_folder ? range_mid + rand : rand) + | |
142 ToBase36(unix_epoch_time_in_ms); | |
143 } | |
144 | |
145 } // namespace | 90 } // namespace |
146 | 91 |
147 bool BookmarksFunction::RunAsync() { | 92 bool BookmarksFunction::RunAsync() { |
148 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 93 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); |
149 if (!model->loaded()) { | 94 if (!model->loaded()) { |
150 // Bookmarks are not ready yet. We'll wait. | 95 // Bookmarks are not ready yet. We'll wait. |
151 model->AddObserver(this); | 96 model->AddObserver(this); |
152 AddRef(); // Balanced in Loaded(). | 97 AddRef(); // Balanced in Loaded(). |
153 return true; | 98 return true; |
154 } | 99 } |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 move_info.parent_id = base::Int64ToString(new_parent->id()); | 276 move_info.parent_id = base::Int64ToString(new_parent->id()); |
332 move_info.index = new_index; | 277 move_info.index = new_index; |
333 move_info.old_parent_id = base::Int64ToString(old_parent->id()); | 278 move_info.old_parent_id = base::Int64ToString(old_parent->id()); |
334 move_info.old_index = old_index; | 279 move_info.old_index = old_index; |
335 | 280 |
336 DispatchEvent( | 281 DispatchEvent( |
337 bookmarks::OnMoved::kEventName, | 282 bookmarks::OnMoved::kEventName, |
338 bookmarks::OnMoved::Create(base::Int64ToString(node->id()), move_info)); | 283 bookmarks::OnMoved::Create(base::Int64ToString(node->id()), move_info)); |
339 } | 284 } |
340 | 285 |
341 void BookmarkEventRouter::OnWillAddBookmarkNode(BookmarkModel* model, | |
342 BookmarkNode* node) { | |
343 // TODO(wittman): Remove this once extension hooks are in place to allow the | |
344 // enhanced bookmarks extension to manage all bookmark creation code | |
345 // paths. See http://crbug.com/383557. | |
346 if (IsEnhancedBookmarksExtensionActive(Profile::FromBrowserContext( | |
347 browser_context_))) { | |
348 static const char key[] = "stars.id"; | |
349 std::string value; | |
350 if (!node->GetMetaInfo(key, &value)) | |
351 node->SetMetaInfo(key, GenerateEnhancedBookmarksID(node->is_folder())); | |
352 } | |
353 } | |
354 | |
355 void BookmarkEventRouter::BookmarkNodeAdded(BookmarkModel* model, | 286 void BookmarkEventRouter::BookmarkNodeAdded(BookmarkModel* model, |
356 const BookmarkNode* parent, | 287 const BookmarkNode* parent, |
357 int index) { | 288 int index) { |
358 const BookmarkNode* node = parent->GetChild(index); | 289 const BookmarkNode* node = parent->GetChild(index); |
359 scoped_ptr<BookmarkTreeNode> tree_node( | 290 scoped_ptr<BookmarkTreeNode> tree_node( |
360 bookmark_api_helpers::GetBookmarkTreeNode(client_, node, false, false)); | 291 bookmark_api_helpers::GetBookmarkTreeNode(client_, node, false, false)); |
361 DispatchEvent(bookmarks::OnCreated::kEventName, | 292 DispatchEvent(bookmarks::OnCreated::kEventName, |
362 bookmarks::OnCreated::Create(base::Int64ToString(node->id()), | 293 bookmarks::OnCreated::Create(base::Int64ToString(node->id()), |
363 *tree_node)); | 294 *tree_node)); |
364 } | 295 } |
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
906 } | 837 } |
907 | 838 |
908 void BookmarksExportFunction::FileSelected(const base::FilePath& path, | 839 void BookmarksExportFunction::FileSelected(const base::FilePath& path, |
909 int index, | 840 int index, |
910 void* params) { | 841 void* params) { |
911 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); | 842 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); |
912 Release(); // Balanced in BookmarksIOFunction::SelectFile() | 843 Release(); // Balanced in BookmarksIOFunction::SelectFile() |
913 } | 844 } |
914 | 845 |
915 } // namespace extensions | 846 } // namespace extensions |
OLD | NEW |