| OLD | NEW |
| 1 // Copyright (c) 2009 The chrome Authors. All rights reserved. | 1 // Copyright (c) 2009 The chrome 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 // ----------------------------------------------------------------------------- | 5 // ----------------------------------------------------------------------------- |
| 6 // NOTE: If you change this file you need to touch renderer_resources.grd to | 6 // NOTE: If you change this file you need to touch renderer_resources.grd to |
| 7 // have your change take effect. | 7 // have your change take effect. |
| 8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
| 9 | 9 |
| 10 // This script contains privileged chrome extension related javascript APIs. | 10 // This script contains privileged chrome extension related javascript APIs. |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 } else { | 108 } else { |
| 109 request.callback(); | 109 request.callback(); |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 } finally { | 112 } finally { |
| 113 delete requests[requestId]; | 113 delete requests[requestId]; |
| 114 delete chrome.extension.lastError; | 114 delete chrome.extension.lastError; |
| 115 } | 115 } |
| 116 }; | 116 }; |
| 117 | 117 |
| 118 chromeHidden.setViewType = function(type) { |
| 119 var modeClass = "chrome-" + type; |
| 120 var className = document.documentElement.className; |
| 121 if (className && className.length) { |
| 122 var classes = className.split(" "); |
| 123 var new_classes = []; |
| 124 classes.forEach(function(cls) { |
| 125 if (cls.indexOf("chrome-") != 0) { |
| 126 new_classes.push(cls); |
| 127 } |
| 128 }); |
| 129 new_classes.push(modeClass); |
| 130 document.documentElement.className = new_classes.join(" "); |
| 131 } else { |
| 132 document.documentElement.className = modeClass; |
| 133 } |
| 134 }; |
| 135 |
| 118 function prepareRequest(args, argSchemas) { | 136 function prepareRequest(args, argSchemas) { |
| 119 var request = {}; | 137 var request = {}; |
| 120 var argCount = args.length; | 138 var argCount = args.length; |
| 121 | 139 |
| 122 // Look for callback param. | 140 // Look for callback param. |
| 123 if (argSchemas.length > 0 && | 141 if (argSchemas.length > 0 && |
| 124 args.length == argSchemas.length && | 142 args.length == argSchemas.length && |
| 125 argSchemas[argSchemas.length - 1].type == "function") { | 143 argSchemas[argSchemas.length - 1].type == "function") { |
| 126 request.callback = args[argSchemas.length - 1]; | 144 request.callback = args[argSchemas.length - 1]; |
| 127 request.callbackSchema = argSchemas[argSchemas.length - 1]; | 145 request.callbackSchema = argSchemas[argSchemas.length - 1]; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 149 // JSON.stringify doesn't support a root object which is undefined. | 167 // JSON.stringify doesn't support a root object which is undefined. |
| 150 if (request.args === undefined) | 168 if (request.args === undefined) |
| 151 request.args = null; | 169 request.args = null; |
| 152 var sargs = JSON.stringify(request.args); | 170 var sargs = JSON.stringify(request.args); |
| 153 var requestId = GetNextRequestId(); | 171 var requestId = GetNextRequestId(); |
| 154 requests[requestId] = request; | 172 requests[requestId] = request; |
| 155 return StartRequest(functionName, sargs, requestId, | 173 return StartRequest(functionName, sargs, requestId, |
| 156 request.callback ? true : false); | 174 request.callback ? true : false); |
| 157 } | 175 } |
| 158 | 176 |
| 159 // Using forEach for convenience, and to bind |module|s & |apiDefs|s via | |
| 160 // closures. | |
| 161 function forEach(a, f) { | |
| 162 for (var i = 0; i < a.length; i++) { | |
| 163 f(a[i], i); | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 function bind(obj, func) { | 177 function bind(obj, func) { |
| 168 return function() { | 178 return function() { |
| 169 return func.apply(obj, arguments); | 179 return func.apply(obj, arguments); |
| 170 }; | 180 }; |
| 171 } | 181 } |
| 172 | 182 |
| 173 // --- Setup additional api's not currently handled in common/extensions/api | 183 // --- Setup additional api's not currently handled in common/extensions/api |
| 174 | 184 |
| 175 // Page action events send (pageActionId, {tabId, tabUrl}). | 185 // Page action events send (pageActionId, {tabId, tabUrl}). |
| 176 function setupPageActionEvents(extensionId) { | 186 function setupPageActionEvents(extensionId) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 204 var apiFunctions = {}; | 214 var apiFunctions = {}; |
| 205 | 215 |
| 206 // Read api definitions and setup api functions in the chrome namespace. | 216 // Read api definitions and setup api functions in the chrome namespace. |
| 207 // TODO(rafaelw): Consider defining a json schema for an api definition | 217 // TODO(rafaelw): Consider defining a json schema for an api definition |
| 208 // and validating either here, in a unit_test or both. | 218 // and validating either here, in a unit_test or both. |
| 209 // TODO(rafaelw): Handle synchronous functions. | 219 // TODO(rafaelw): Handle synchronous functions. |
| 210 // TOOD(rafaelw): Consider providing some convenient override points | 220 // TOOD(rafaelw): Consider providing some convenient override points |
| 211 // for api functions that wish to insert themselves into the call. | 221 // for api functions that wish to insert themselves into the call. |
| 212 var apiDefinitions = JSON.parse(GetExtensionAPIDefinition()); | 222 var apiDefinitions = JSON.parse(GetExtensionAPIDefinition()); |
| 213 | 223 |
| 214 forEach(apiDefinitions, function(apiDef) { | 224 apiDefinitions.forEach(function(apiDef) { |
| 215 chrome[apiDef.namespace] = chrome[apiDef.namespace] || {}; | 225 chrome[apiDef.namespace] = chrome[apiDef.namespace] || {}; |
| 216 var module = chrome[apiDef.namespace]; | 226 var module = chrome[apiDef.namespace]; |
| 217 | 227 |
| 218 // Add types to global validationTypes | 228 // Add types to global validationTypes |
| 219 if (apiDef.types) { | 229 if (apiDef.types) { |
| 220 forEach(apiDef.types, function(t) { | 230 apiDef.types.forEach(function(t) { |
| 221 chromeHidden.validationTypes.push(t); | 231 chromeHidden.validationTypes.push(t); |
| 222 }); | 232 }); |
| 223 } | 233 } |
| 224 | 234 |
| 225 // Setup Functions. | 235 // Setup Functions. |
| 226 if (apiDef.functions) { | 236 if (apiDef.functions) { |
| 227 forEach(apiDef.functions, function(functionDef) { | 237 apiDef.functions.forEach(function(functionDef) { |
| 228 // Module functions may have been defined earlier by hand. Don't | 238 // Module functions may have been defined earlier by hand. Don't |
| 229 // clobber them. | 239 // clobber them. |
| 230 if (module[functionDef.name]) | 240 if (module[functionDef.name]) |
| 231 return; | 241 return; |
| 232 | 242 |
| 233 var apiFunction = {}; | 243 var apiFunction = {}; |
| 234 apiFunction.definition = functionDef; | 244 apiFunction.definition = functionDef; |
| 235 apiFunction.name = apiDef.namespace + "." + functionDef.name;; | 245 apiFunction.name = apiDef.namespace + "." + functionDef.name;; |
| 236 apiFunctions[apiFunction.name] = apiFunction; | 246 apiFunctions[apiFunction.name] = apiFunction; |
| 237 | 247 |
| 238 module[functionDef.name] = bind(apiFunction, function() { | 248 module[functionDef.name] = bind(apiFunction, function() { |
| 239 chromeHidden.validate(arguments, this.definition.parameters); | 249 chromeHidden.validate(arguments, this.definition.parameters); |
| 240 | 250 |
| 241 if (this.handleRequest) | 251 if (this.handleRequest) |
| 242 return this.handleRequest.apply(this, arguments); | 252 return this.handleRequest.apply(this, arguments); |
| 243 else | 253 else |
| 244 return sendRequest(this.name, arguments, | 254 return sendRequest(this.name, arguments, |
| 245 this.definition.parameters); | 255 this.definition.parameters); |
| 246 }); | 256 }); |
| 247 }); | 257 }); |
| 248 } | 258 } |
| 249 | 259 |
| 250 // Setup Events | 260 // Setup Events |
| 251 if (apiDef.events) { | 261 if (apiDef.events) { |
| 252 forEach(apiDef.events, function(eventDef) { | 262 apiDef.events.forEach(function(eventDef) { |
| 253 // Module events may have been defined earlier by hand. Don't clobber | 263 // Module events may have been defined earlier by hand. Don't clobber |
| 254 // them. | 264 // them. |
| 255 if (module[eventDef.name]) | 265 if (module[eventDef.name]) |
| 256 return; | 266 return; |
| 257 | 267 |
| 258 var eventName = apiDef.namespace + "." + eventDef.name; | 268 var eventName = apiDef.namespace + "." + eventDef.name; |
| 259 module[eventDef.name] = new chrome.Event(eventName, | 269 module[eventDef.name] = new chrome.Event(eventName, |
| 260 eventDef.parameters); | 270 eventDef.parameters); |
| 261 }); | 271 }); |
| 262 } | 272 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 289 | 299 |
| 290 apiFunctions["extension.getTabContentses"].handleRequest = | 300 apiFunctions["extension.getTabContentses"].handleRequest = |
| 291 function(windowId) { | 301 function(windowId) { |
| 292 if (typeof(windowId) == "undefined") | 302 if (typeof(windowId) == "undefined") |
| 293 windowId = -1; | 303 windowId = -1; |
| 294 return GetExtensionViews(windowId, "TAB"); | 304 return GetExtensionViews(windowId, "TAB"); |
| 295 } | 305 } |
| 296 | 306 |
| 297 apiFunctions["devtools.getTabEvents"].handleRequest = function(tabId) { | 307 apiFunctions["devtools.getTabEvents"].handleRequest = function(tabId) { |
| 298 var tabIdProxy = {}; | 308 var tabIdProxy = {}; |
| 299 forEach(["onPageEvent", "onTabUrlChange", "onTabClose"], | 309 var functions = ["onPageEvent", "onTabUrlChange", "onTabClose"]; |
| 300 function(name) { | 310 functions.forEach(function(name) { |
| 301 // Event disambiguation is handled by name munging. See | 311 // Event disambiguation is handled by name munging. See |
| 302 // chrome/browser/extensions/extension_devtools_events.h for the C++ | 312 // chrome/browser/extensions/extension_devtools_events.h for the C++ |
| 303 // equivalent of this logic. | 313 // equivalent of this logic. |
| 304 tabIdProxy[name] = new chrome.Event("devtools." + tabId + "." + name); | 314 tabIdProxy[name] = new chrome.Event("devtools." + tabId + "." + name); |
| 305 }); | 315 }); |
| 306 return tabIdProxy; | 316 return tabIdProxy; |
| 307 } | 317 } |
| 308 | 318 |
| 309 setupPageActionEvents(extensionId); | 319 setupPageActionEvents(extensionId); |
| 310 setupToolstripEvents(GetRenderViewId()); | 320 setupToolstripEvents(GetRenderViewId()); |
| 311 }); | 321 }); |
| 312 })(); | 322 })(); |
| OLD | NEW |