OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_utils.h" | 5 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_utils.h" |
6 | 6 |
7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/scoped_nsobject.h" |
11 #include "components/browser_sync/profile_sync_service.h" | 11 #include "components/browser_sync/profile_sync_service.h" |
12 #include "components/sync/driver/sync_service.h" | 12 #include "components/sync/driver/sync_service.h" |
13 #include "components/sync_sessions/open_tabs_ui_delegate.h" | 13 #include "components/sync_sessions/open_tabs_ui_delegate.h" |
14 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 14 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
15 #import "ios/chrome/browser/favicon/favicon_loader.h" | 15 #import "ios/chrome/browser/favicon/favicon_loader.h" |
16 #include "ios/chrome/browser/favicon/ios_chrome_favicon_loader_factory.h" | 16 #include "ios/chrome/browser/favicon/ios_chrome_favicon_loader_factory.h" |
17 #include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h" | 17 #include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h" |
18 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 18 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
19 #include "ios/chrome/grit/ios_theme_resources.h" | 19 #include "ios/chrome/grit/ios_theme_resources.h" |
20 | 20 |
21 namespace ios_internal { | 21 namespace { |
22 | 22 |
23 UIImage* DefaultFaviconImage() { | 23 UIImage* DefaultFaviconImage() { |
24 return NativeImage(IDR_IOS_OMNIBOX_HTTP); | 24 return NativeImage(IDR_IOS_OMNIBOX_HTTP); |
25 } | 25 } |
26 | 26 |
27 void GetFavicon(GURL const& url, | 27 enum BacktrackOperation { NOTHING, SUBSTITUTION, DELETION, INSERTION }; |
28 ios::ChromeBrowserState* browser_state, | 28 |
29 ios_internal::FaviconGetterCompletionBlock block) { | 29 BacktrackOperation BacktrackOperationInCostMatrix( |
30 std::vector<std::vector<int>> const& costMatrix, | |
sdefresne
2017/01/03 16:04:30
nit: this is a C++ function so it should use cpp_n
rohitrao (ping after 24h)
2017/01/04 13:36:38
Acknowledged.
| |
31 size_t finalIndex, | |
32 size_t initialIndex) { | |
33 DCHECK(finalIndex || initialIndex); | |
34 DCHECK(initialIndex < costMatrix.size()); | |
35 DCHECK(finalIndex < costMatrix[initialIndex].size()); | |
36 | |
37 if (finalIndex == 0) | |
38 return DELETION; | |
39 if (initialIndex == 0) | |
40 return INSERTION; | |
41 | |
42 int currentCost = costMatrix[initialIndex][finalIndex]; | |
43 | |
44 int costBeforeInsertion = costMatrix[initialIndex][finalIndex - 1]; | |
45 if (costBeforeInsertion + 1 == currentCost) | |
46 return INSERTION; | |
47 | |
48 int costBeforeDeletion = costMatrix[initialIndex - 1][finalIndex]; | |
49 if (costBeforeDeletion + 1 == currentCost) | |
50 return DELETION; | |
51 | |
52 int costBeforeSubstitution = costMatrix[initialIndex - 1][finalIndex - 1]; | |
53 if (costBeforeSubstitution == currentCost) | |
54 return NOTHING; | |
55 | |
56 return SUBSTITUTION; | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 void TabSwitcherGetFavicon(GURL const& url, | |
62 ios::ChromeBrowserState* browser_state, | |
63 TabSwitcherFaviconGetterCompletionBlock block) { | |
30 DCHECK(browser_state); | 64 DCHECK(browser_state); |
31 syncer::SyncService* sync_service = | 65 syncer::SyncService* sync_service = |
32 IOSChromeProfileSyncServiceFactory::GetForBrowserState(browser_state); | 66 IOSChromeProfileSyncServiceFactory::GetForBrowserState(browser_state); |
33 sync_sessions::OpenTabsUIDelegate* open_tabs = | 67 sync_sessions::OpenTabsUIDelegate* open_tabs = |
34 sync_service ? sync_service->GetOpenTabsUIDelegate() : NULL; | 68 sync_service ? sync_service->GetOpenTabsUIDelegate() : NULL; |
35 scoped_refptr<base::RefCountedMemory> favicon; | 69 scoped_refptr<base::RefCountedMemory> favicon; |
36 if (open_tabs && | 70 if (open_tabs && |
37 open_tabs->GetSyncedFaviconForPageURL(url.spec(), &favicon)) { | 71 open_tabs->GetSyncedFaviconForPageURL(url.spec(), &favicon)) { |
38 dispatch_queue_t queue = | 72 dispatch_queue_t queue = |
39 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0); | 73 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0); |
(...skipping 23 matching lines...) Expand all Loading... | |
63 favicon_base::TOUCH_PRECOMPOSED_ICON; | 97 favicon_base::TOUCH_PRECOMPOSED_ICON; |
64 UIImage* image = loader->ImageForURL(url, types, block); | 98 UIImage* image = loader->ImageForURL(url, types, block); |
65 DCHECK(image); | 99 DCHECK(image); |
66 block(image); | 100 block(image); |
67 return; | 101 return; |
68 } | 102 } |
69 // Finally returns a default image. | 103 // Finally returns a default image. |
70 block(DefaultFaviconImage()); | 104 block(DefaultFaviconImage()); |
71 } | 105 } |
72 | 106 |
73 enum BacktrackOperation { NOTHING, SUBSTITUTION, DELETION, INSERTION }; | 107 void TabSwitcherMinimalReplacementOperations(std::vector<size_t> const& initial, |
74 | 108 std::vector<size_t> const& final, |
75 BacktrackOperation BacktrackOperationInCostMatrix( | 109 std::vector<size_t>* substitutions, |
76 std::vector<std::vector<int>> const& costMatrix, | 110 std::vector<size_t>* deletions, |
77 size_t finalIndex, | 111 std::vector<size_t>* insertions) { |
78 size_t initialIndex) { | |
79 DCHECK(finalIndex || initialIndex); | |
80 DCHECK(initialIndex < costMatrix.size()); | |
81 DCHECK(finalIndex < costMatrix[initialIndex].size()); | |
82 | |
83 if (finalIndex == 0) | |
84 return DELETION; | |
85 if (initialIndex == 0) | |
86 return INSERTION; | |
87 | |
88 int currentCost = costMatrix[initialIndex][finalIndex]; | |
89 | |
90 int costBeforeInsertion = costMatrix[initialIndex][finalIndex - 1]; | |
91 if (costBeforeInsertion + 1 == currentCost) | |
92 return INSERTION; | |
93 | |
94 int costBeforeDeletion = costMatrix[initialIndex - 1][finalIndex]; | |
95 if (costBeforeDeletion + 1 == currentCost) | |
96 return DELETION; | |
97 | |
98 int costBeforeSubstitution = costMatrix[initialIndex - 1][finalIndex - 1]; | |
99 if (costBeforeSubstitution == currentCost) | |
100 return NOTHING; | |
101 | |
102 return SUBSTITUTION; | |
103 } | |
104 | |
105 void MinimalReplacementOperations(std::vector<size_t> const& initial, | |
106 std::vector<size_t> const& final, | |
107 std::vector<size_t>* substitutions, | |
108 std::vector<size_t>* deletions, | |
109 std::vector<size_t>* insertions) { | |
110 DCHECK(substitutions); | 112 DCHECK(substitutions); |
111 DCHECK(deletions); | 113 DCHECK(deletions); |
112 DCHECK(insertions); | 114 DCHECK(insertions); |
113 DCHECK_EQ(substitutions->size(), 0UL); | 115 DCHECK_EQ(substitutions->size(), 0UL); |
114 DCHECK_EQ(deletions->size(), 0UL); | 116 DCHECK_EQ(deletions->size(), 0UL); |
115 DCHECK_EQ(insertions->size(), 0UL); | 117 DCHECK_EQ(insertions->size(), 0UL); |
116 | 118 |
117 // The substitutions/deletions/insertions are computed using the Levenshtein | 119 // The substitutions/deletions/insertions are computed using the Levenshtein |
118 // algorithm. | 120 // algorithm. |
119 // https://en.wikipedia.org/wiki/Levenshtein_distance | 121 // https://en.wikipedia.org/wiki/Levenshtein_distance |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 } | 180 } |
179 } | 181 } |
180 | 182 |
181 // The backtracking results in the indexes of the operations being stored in | 183 // The backtracking results in the indexes of the operations being stored in |
182 // decreasing order. | 184 // decreasing order. |
183 // For readability, order them in ascending value. | 185 // For readability, order them in ascending value. |
184 std::reverse(std::begin(*substitutions), std::end(*substitutions)); | 186 std::reverse(std::begin(*substitutions), std::end(*substitutions)); |
185 std::reverse(std::begin(*deletions), std::end(*deletions)); | 187 std::reverse(std::begin(*deletions), std::end(*deletions)); |
186 std::reverse(std::begin(*insertions), std::end(*insertions)); | 188 std::reverse(std::begin(*insertions), std::end(*insertions)); |
187 } | 189 } |
188 | |
189 } // namespace ios_internal | |
OLD | NEW |