Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: chrome/browser/resources/ntp_search/mock/mock.js

Issue 10829131: Refactoring NTP5: new implementation of TilePage and MostVisitedPage (which now inherits from Thumb… (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 var __chrome__ = chrome;
6 var chrome = (function() {
7
8 var shouldRegisterData = !!window.chrome && !!window.chrome.send;
9
10 var callbackMap = {
11 'metricsHandler:logEventTime': 1,
12 'metricsHandler:recordInHistogram': 1,
13 'getApps': 'ntp.getAppsCallback',
14 'getRecentlyClosedTabs': 'ntp.setRecentlyClosedTabs',
15 'getMostVisited': 'ntp.setMostVisitedPages'
16 };
17
18 // TODO include automatically in the recorded data
19 var mockedThumbnailUrls = [
20 "http---www.wikipedia.org-",
21 "http---www.deviantart.com-",
22 "http---wefunkradio.com-",
23 "http---youtube.com-",
24 "http---amazon.com-",
25 "http---nytimes.com-",
26 "http---news.ycombinator.com-",
27 "http---cnn.com-",
28 "http---ebay.com-",
29 "http---www.google.com-chrome-intl-en-welcome.html",
30 "https---chrome.google.com-webstore-hl-en"
31 ];
32
33
34 //----------------------------------------------------------------------------
35 // Internals
36 //----------------------------------------------------------------------------
37
38 var dataMap = {};
39 var recordedDataMap = {};
40 var isInitialized = false;
41 var thumbnailUrlList = [];
42
43 function initialize() {
44 if (shouldRegisterData || !namespace('ntp')) {
45 return;
46 }
47 isInitialized = true;
48 namespace('ntp.getThumbnailUrl', mockGetThumbnailUrl);
49 }
50
51 function namespace(str, data) {
52 var ns = str.split('.'), name, object = window;
53 for (var i = 0, l = ns.length; i < l; i++) {
54 name = ns[i];
55 if (data && i == (l - 1)) {
56 object = object[name] = data;
57 } else {
58 object = object[name];
59 }
60 }
61 return object == window ? null : object;
62 }
63
64 function copyArray(arr) {
65 return Array.prototype.slice.call(arr, 0);
66 }
67
68 function interceptCallback(message, callbackNamespace) {
69 var original = namespace(callbackNamespace);
70 namespace(callbackNamespace, function() {
71 recordedDataMap[message] = copyArray(arguments);
72 var result = original.apply(window, arguments);
73 namespace(callbackNamespace, original);
74 return result;
75 });
76 }
77
78 function interceptLoadData() {
79 window.addEventListener('load', function() {
80 recordedDataMap['__loadTimeData__'] = loadTimeData.data_;
81 });
82 }
83
84 function mockGetThumbnailUrl(url) {
85 url = url.replace(/[\:\/\?\=]/g, '-');
86
87 if (thumbnailUrlList.length == 0) {
88 thumbnailUrlList = copyArray(mockedThumbnailUrls);
89 }
90 var mockUrl;
91 var index = thumbnailUrlList.indexOf(url);
92 if (index) {
jeremycho_google 2012/08/02 03:00:49 if (index != -1)
pedrosimonetti2 2012/08/03 18:14:12 Done.
93 // Remove an element from a particular index.
94 mockUrl = thumbnailUrlList.splice(index, 0);
jeremycho_google 2012/08/02 03:00:49 This doesn't remove anything. splice(index, 1) in
pedrosimonetti2 2012/08/03 18:14:12 You're right. The bug wasn't visible for me becaus
95 } else {
96 // Remove the first element.
97 mockUrl = thumbnailUrlList.shift();
98 }
99
100 mockUrl = 'mock/images/thumb/' + mockUrl + '.png';
101 return mockUrl;
102 }
103
104 function mockLoadData() {
105 if (loadTimeData) {
106 loadTimeData.data = dataMap['__loadTimeData__'];
107 }
108 }
109
110
111 //----------------------------------------------------------------------------
112 // ChromeMock implementation
113 //----------------------------------------------------------------------------
114
115 ChromeMock = {
116
117 mock: function(newDataMap) {
118 if (newDataMap) {
119 dataMap = newDataMap;
120 if (!shouldRegisterData) {
121 mockLoadData();
122 }
123 } else {
124 return recordedDataMap;
125 }
126 },
127
128 send: function() {
129 //console.log(message, arguments.length, arguments);
130 if (!isInitialized) {
131 initialize();
132 }
133
134 var message = arguments[0];
135 var shouldCallChromeSend = false;
136
137 var data;
138 var callback;
139 var callbackNamespace;
140
141 if (callbackMap.hasOwnProperty(message)) {
142
143 callbackNamespace = callbackMap[message];
144
145 if (shouldRegisterData) {
146 if (callbackNamespace !== 1) {
147 interceptCallback(message, callbackNamespace);
148 }
149 } else {
150 if (dataMap.hasOwnProperty(message)) {
151 data = dataMap[message];
152 callback = namespace(callbackNamespace);
153 setTimeout(function() {
154 callback.apply(window, data);
155 }, 0);
156 } else {
157 if (callbackNamespace !== 1) {
158 console.warn('No mock registered for message "%s".', message);
159 }
160 }
161 }
162
163 } else {
164 shouldCallChromeSend = true;
165 console.warn('No callback data registered for message "%s".', message);
166 }
167
168 shouldCallChromeSend = shouldCallChromeSend || shouldRegisterData;
169 if (shouldCallChromeSend) {
170 if (__chrome__ && __chrome__.send) {
171 __chrome__.send(message);
172 }
173 }
174 },
175 };
176
177 //----------------------------------------------------------------------------
178 // ChromeMock initialization
179 //----------------------------------------------------------------------------
180
181 if (shouldRegisterData) {
182 interceptLoadData();
183 }
184
185 return ChromeMock;
186 })();
187
jeremycho_google 2012/08/02 03:00:49 Extra newline.
pedrosimonetti2 2012/08/03 18:14:12 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698