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

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: Fixing error during initialization 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 !/CrOs/.test(navigator.userAgent) && (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
56 function namespace(str, data) {
57 var ns = str.split('.'), name, object = window;
58 for (var i = 0, l = ns.length; i < l; i++) {
59 name = ns[i];
60 if (data && i == (l - 1)) {
61 object = object[name] = data;
62 } else {
63 object = object[name];
64 }
65 }
66 return object == window ? null : object;
67 }
68
69 function copyArray(arr) {
70 return Array.prototype.slice.call(arr, 0);
71 }
72
73 function interceptCallback(message, callbackNamespace) {
74 var original = namespace(callbackNamespace);
75 namespace(callbackNamespace, function() {
76 recordedDataMap[message] = copyArray(arguments);
77 var result = original.apply(window, arguments);
78 namespace(callbackNamespace, original);
79 return result;
80 });
81 }
82
83 function interceptLoadData() {
84 window.addEventListener('load', function() {
85 recordedDataMap['__loadTimeData__'] = loadTimeData.data_;
86 });
87 }
88
89 function mockGetThumbnailUrl(url) {
90 url = url.replace(/[\:\/\?\=]/g, '-');
91
92 if (thumbnailUrlList.length == 0) {
93 thumbnailUrlList = copyArray(mockedThumbnailUrls);
94 }
95 var mockUrl;
96 var index = thumbnailUrlList.indexOf(url);
97 if (index != -1) {
98 // Remove an element from a particular index.
99 mockUrl = thumbnailUrlList.splice(index, 1);
100 } else {
101 // Remove the first element.
102 mockUrl = thumbnailUrlList.shift();
103 }
104
105 mockUrl = 'mock/images/thumb/' + mockUrl + '.png';
106 return mockUrl;
107 }
108
109 function mockLoadData() {
110 if (loadTimeData) {
111 loadTimeData.data = dataMap['__loadTimeData__'];
112 }
113 }
114
115
116 //----------------------------------------------------------------------------
117 // ChromeMock implementation
118 //----------------------------------------------------------------------------
119
120 ChromeMock = {
121 mock: function(newDataMap) {
122 if (newDataMap) {
123 dataMap = newDataMap;
124 if (!shouldRegisterData) {
125 mockLoadData();
126 }
127 } else {
128 return recordedDataMap;
129 }
130 },
131
132 send: function() {
133 if (!isInitialized) {
134 initialize();
135 }
136
137 var message = arguments[0];
138 var shouldCallChromeSend = false;
139
140 var data;
141 var callback;
142 var callbackNamespace;
143
144 if (callbackMap.hasOwnProperty(message)) {
145 callbackNamespace = callbackMap[message];
146
147 if (shouldRegisterData) {
148 if (callbackNamespace !== NO_CALLBACK) {
149 interceptCallback(message, callbackNamespace);
150 }
151 } else {
152 if (dataMap.hasOwnProperty(message)) {
153 data = dataMap[message];
154 callback = namespace(callbackNamespace);
155 setTimeout(function() {
156 callback.apply(window, data);
157 }, 0);
158 } else {
159 if (callbackNamespace !== NO_CALLBACK) {
160 console.warn('No mock registered for message "%s".', message);
161 }
162 }
163 }
164 } else {
165 shouldCallChromeSend = true;
166 console.warn('No callback data registered for message "%s".', message);
167 }
168
169 shouldCallChromeSend = shouldCallChromeSend || shouldRegisterData;
170 if (shouldCallChromeSend) {
171 if (__chrome__ && __chrome__.send) {
172 __chrome__.send(message);
173 }
174 }
175 },
176 };
177
178 //----------------------------------------------------------------------------
179 // ChromeMock initialization
180 //----------------------------------------------------------------------------
181
182 if (shouldRegisterData) {
183 interceptLoadData();
184 }
185
186 window.chrome = ChromeMock;
187 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698