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

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: Manually fixing mock_data_autogen.js lines with more than 80 chars. 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 // TODO(pedrosimonetti): document how to generate the data pseudo-automatically.
6 !/^chrome:\/\/./.test(location.href) && (function() {
7
8 var __chrome__ = chrome;
9 var shouldRegisterData = !!window.chrome && !!window.chrome.send;
10
11 var NO_CALLBACK = 1;
12
13 // Only messages registered in the callback map will be intercepted.
14 var callbackMap = {
15 'metricsHandler:logEventTime': NO_CALLBACK,
16 'metricsHandler:recordInHistogram': NO_CALLBACK,
17 'getApps': 'ntp.getAppsCallback',
18 'getRecentlyClosedTabs': 'ntp.setRecentlyClosedTabs',
19 'getMostVisited': 'ntp.setMostVisitedPages'
20 };
21
22 // TODO(pedrosimonetti): include automatically in the recorded data
23 // TODO(pedrosimonetti): document the special char replacement
24 var mockedThumbnailUrls = [
25 'http---www.wikipedia.org-',
26 'http---www.deviantart.com-',
27 'http---wefunkradio.com-',
28 'http---youtube.com-',
29 'http---amazon.com-',
30 'http---nytimes.com-',
31 'http---news.ycombinator.com-',
32 'http---cnn.com-',
33 'http---ebay.com-',
34 'http---www.google.com-chrome-intl-en-welcome.html',
35 'https---chrome.google.com-webstore-hl-en'
36 ];
37
38
39 //----------------------------------------------------------------------------
40 // Internals
41 //----------------------------------------------------------------------------
42
43 var dataMap = {};
44 var recordedDataMap = {};
45 var isInitialized = false;
46 var thumbnailUrlList = [];
47
48 function initialize() {
49 if (shouldRegisterData || !namespace('ntp')) {
50 return;
51 }
52 isInitialized = true;
53 namespace('ntp.getThumbnailUrl', mockGetThumbnailUrl);
54
55 var data = loadTimeData.data_;
56 document.documentElement.dir = data.textdirection;
57 document.body.style.fontSize = data.fontsize;
58 document.body.style.fontFamily = data.fontfamily;
59 }
60
61 function namespace(str, data) {
62 var ns = str.split('.'), name, object = window;
63 for (var i = 0, l = ns.length; i < l; i++) {
64 name = ns[i];
65 if (data && i == (l - 1)) {
66 object = object[name] = data;
67 } else {
68 object = object[name];
69 }
70 }
71 return object == window ? null : object;
72 }
73
74 function copyArray(arr) {
75 return Array.prototype.slice.call(arr, 0);
76 }
77
78 function interceptCallback(message, callbackNamespace) {
79 var original = namespace(callbackNamespace);
80 namespace(callbackNamespace, function() {
81 recordedDataMap[message] = copyArray(arguments);
82 var result = original.apply(window, arguments);
83 namespace(callbackNamespace, original);
84 return result;
85 });
86 }
87
88 function interceptLoadData() {
89 window.addEventListener('load', function() {
90 recordedDataMap['__loadTimeData__'] = loadTimeData.data_;
91 });
92 }
93
94 function mockGetThumbnailUrl(url) {
95 url = url.replace(/[\:\/\?\=]/g, '-');
96
97 if (thumbnailUrlList.length == 0) {
98 thumbnailUrlList = copyArray(mockedThumbnailUrls);
99 }
100 var mockUrl;
101 var index = thumbnailUrlList.indexOf(url);
102 if (index != -1) {
103 // Remove an element from a particular index.
104 mockUrl = thumbnailUrlList.splice(index, 1);
105 } else {
106 // Remove the first element.
107 mockUrl = thumbnailUrlList.shift();
108 }
109
110 mockUrl = 'mock/images/' + mockUrl + '.jpg';
111 return mockUrl;
112 }
113
114 function mockLoadData() {
115 if (loadTimeData) {
116 loadTimeData.data = dataMap['__loadTimeData__'];
117 }
118 }
119
120
121 //----------------------------------------------------------------------------
122 // ChromeMock implementation
123 //----------------------------------------------------------------------------
124
125 ChromeMock = {
126 mock: function(newDataMap) {
127 if (newDataMap) {
128 dataMap = newDataMap;
129 if (!shouldRegisterData) {
130 mockLoadData();
131 }
132 } else {
133 return recordedDataMap;
134 }
135 },
136
137 send: function() {
138 if (!isInitialized) {
139 initialize();
140 }
141
142 var message = arguments[0];
143 var shouldCallChromeSend = false;
144
145 var data;
146 var callback;
147 var callbackNamespace;
148
149 if (callbackMap.hasOwnProperty(message)) {
150 callbackNamespace = callbackMap[message];
151
152 if (shouldRegisterData) {
153 if (callbackNamespace !== NO_CALLBACK) {
154 interceptCallback(message, callbackNamespace);
155 }
156 } else {
157 if (dataMap.hasOwnProperty(message)) {
158 data = dataMap[message];
159 callback = namespace(callbackNamespace);
160 setTimeout(function() {
161 callback.apply(window, data);
162 }, 0);
163 } else {
164 if (callbackNamespace !== NO_CALLBACK) {
165 console.warn('No mock registered for message "%s".', message);
166 }
167 }
168 }
169 } else {
170 shouldCallChromeSend = true;
171 console.warn('No callback data registered for message "%s".', message);
172 }
173
174 shouldCallChromeSend = shouldCallChromeSend || shouldRegisterData;
175 if (shouldCallChromeSend) {
176 if (__chrome__ && __chrome__.send) {
177 __chrome__.send(message);
178 }
179 }
180 },
181 };
182
183 //----------------------------------------------------------------------------
184 // Debug
185 //----------------------------------------------------------------------------
186
187 var debugArgs = {};
188 var debugStylesheet = null;
189 var animationSelectorSpeedMap = {
190 '#page-list': 200,
191 '.tile-grid': 200,
192 '.tile-grid-content': 200,
193 '.tile-row': 200,
194 '.animate-tile .tile-cell': 200,
195 '.debug .animate-tile .tile-cell': 200
196 };
197
198 function adjustAnimationSpeed(slownessFactor) {
199 slownessFactor = slownessFactor || 1;
200
201 var animationRules = [];
202 for (var selector in animationSelectorSpeedMap) {
203 if (animationSelectorSpeedMap.hasOwnProperty(selector)) {
204 animationRules.push(selector + ' { -webkit-transition-duration: ' +
205 Math.round(animationSelectorSpeedMap[selector] * slownessFactor) +
206 'ms !important; }\n');
207 }
208 }
209
210 var doc = document;
211 debugStylesheet = doc.getElementById('debugStylesheet');
212 if (debugStylesheet) {
213 debugStylesheet.parentElement.removeChild(debugStylesheet);
214 }
215 debugStylesheet = doc.createElement('style');
216 debugStylesheet.id = 'debugStylesheet';
217 debugStylesheet.textContent = animationRules.join('');
218 doc.querySelector('head').appendChild(debugStylesheet);
219 }
220
221 function getArgs() {
222 var url = location.href;
223 var parts = url.split('?');
224 var args = parts[1];
225 if (args) {
226 var pairs = args.split('&');
227 for (var i = 0, l = pairs.length; i < l; i++) {
228 var pair = pairs[i];
229 var data = pair.split('=');
230 var key = data[0];
231 var value = data[1];
232 debugArgs[key] = typeof value == 'undefined' ? true :
233 parseInt(value) ? parseInt(value) : value;
234 }
235 }
236 }
237
238 window.addEventListener('load', function() {
239 getArgs();
240
241 if (debugArgs.debug)
242 document.body.classList.add('debug');
243
244 if (debugArgs.slownessFactor)
245 adjustAnimationSpeed(debugArgs.slownessFactor);
246 });
247
248 //----------------------------------------------------------------------------
249 // ChromeMock initialization
250 //----------------------------------------------------------------------------
251
252 if (shouldRegisterData) {
253 interceptLoadData();
254 }
255
256 window.chrome = ChromeMock;
257 })();
OLDNEW
« no previous file with comments | « chrome/browser/resources/ntp_search/mock/debug.css ('k') | chrome/browser/resources/ntp_search/mock/mock_data_autogen.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698