Chromium Code Reviews| 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 // This script contains privileged chrome extension related javascript APIs. | 5 // This script contains privileged chrome extension related javascript APIs. |
| 6 // It is loaded by pages whose URL has the chrome-extension protocol. | 6 // It is loaded by pages whose URL has the chrome-extension protocol. |
| 7 | 7 |
| 8 var chrome = chrome || {}; | 8 var chrome = chrome || {}; |
| 9 (function() { | 9 (function() { |
| 10 native function GetExtensionAPIDefinition(); | 10 native function GetExtensionAPIDefinition(); |
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 340 return tabIdProxy; | 340 return tabIdProxy; |
| 341 } | 341 } |
| 342 | 342 |
| 343 apiFunctions["i18n.getMessage"].handleRequest = | 343 apiFunctions["i18n.getMessage"].handleRequest = |
| 344 function(message_name, placeholders) { | 344 function(message_name, placeholders) { |
| 345 return GetL10nMessage(message_name, placeholders); | 345 return GetL10nMessage(message_name, placeholders); |
| 346 } | 346 } |
| 347 | 347 |
| 348 var canvas; | 348 var canvas; |
| 349 function setIconCommon(details, name, parameters) { | 349 function setIconCommon(details, name, parameters) { |
| 350 var EXTENSION_ACTION_SIZE = 19; | |
|
Finnur
2009/10/28 04:49:01
nit: EXTENSION_ACTION_IMAGE_SIZE (extension action
| |
| 351 | |
| 350 if ("iconIndex" in details) { | 352 if ("iconIndex" in details) { |
| 351 sendRequest(name, [details], parameters); | 353 sendRequest(name, [details], parameters); |
| 352 } else if ("imageData" in details) { | 354 } else if ("imageData" in details) { |
| 353 // Verify that this at least looks like an ImageData element. | 355 // Verify that this at least looks like an ImageData element. |
| 354 // Unfortunately, we cannot use instanceof because the ImageData | 356 // Unfortunately, we cannot use instanceof because the ImageData |
| 355 // constructor is not public. | 357 // constructor is not public. |
| 356 // | 358 // |
| 357 // We do this manually instead of using JSONSchema to avoid having these | 359 // We do this manually instead of using JSONSchema to avoid having these |
| 358 // properties show up in the doc. | 360 // properties show up in the doc. |
| 359 if (!("width" in details.imageData) || | 361 if (!("width" in details.imageData) || |
| 360 !("height" in details.imageData) || | 362 !("height" in details.imageData) || |
| 361 !("data" in details.imageData)) { | 363 !("data" in details.imageData)) { |
| 362 throw new Error( | 364 throw new Error( |
| 363 "The imageData property must contain an ImageData object."); | 365 "The imageData property must contain an ImageData object."); |
| 364 } | 366 } |
| 367 | |
| 368 if (details.imageData.width > EXTENSION_ACTION_SIZE || | |
| 369 details.imageData.height > EXTENSION_ACTION_SIZE) { | |
| 370 throw new Error( | |
| 371 "The imageData property must contain an ImageData object that " + | |
| 372 "is no larger than 19 pixels square."); | |
| 373 } | |
| 374 | |
| 365 sendCustomRequest(SetExtensionActionIcon, name, [details], parameters); | 375 sendCustomRequest(SetExtensionActionIcon, name, [details], parameters); |
| 366 } else if ("path" in details) { | 376 } else if ("path" in details) { |
| 367 var img = new Image(); | 377 var img = new Image(); |
| 368 img.onerror = function() { | 378 img.onerror = function() { |
| 369 console.error("Could not load browser action icon '" + details.path + | 379 console.error("Could not load browser action icon '" + details.path + |
| 370 "'."); | 380 "'."); |
| 371 } | 381 } |
| 372 img.onload = function() { | 382 img.onload = function() { |
| 373 var canvas = document.createElement("canvas"); | 383 var canvas = document.createElement("canvas"); |
| 374 canvas.width = img.width > 19 ? 19 : img.width; | 384 canvas.width = img.width > EXTENSION_ACTION_SIZE ? |
| 375 canvas.height = img.height > 19 ? 19 : img.height; | 385 EXTENSION_ACTION_SIZE : img.width; |
| 386 canvas.height = img.height > EXTENSION_ACTION_SIZE ? | |
| 387 EXTENSION_ACTION_SIZE : img.height; | |
| 376 | 388 |
| 377 var canvas_context = canvas.getContext('2d'); | 389 var canvas_context = canvas.getContext('2d'); |
| 378 canvas_context.clearRect(0, 0, canvas.width, canvas.height); | 390 canvas_context.clearRect(0, 0, canvas.width, canvas.height); |
| 379 canvas_context.drawImage(img, 0, 0, canvas.width, canvas.height); | 391 canvas_context.drawImage(img, 0, 0, canvas.width, canvas.height); |
| 380 delete details.path; | 392 delete details.path; |
| 381 details.imageData = canvas_context.getImageData(0, 0, canvas.width, | 393 details.imageData = canvas_context.getImageData(0, 0, canvas.width, |
| 382 canvas.height); | 394 canvas.height); |
| 383 sendCustomRequest(SetExtensionActionIcon, name, [details], parameters) ; | 395 sendCustomRequest(SetExtensionActionIcon, name, [details], parameters) ; |
| 384 } | 396 } |
| 385 img.src = details.path; | 397 img.src = details.path; |
| 386 } else { | 398 } else { |
| 387 throw new Error( | 399 throw new Error( |
| 388 "Either the path or imageData property must be specified."); | 400 "Either the path or imageData property must be specified."); |
| 389 } | 401 } |
| 390 } | 402 } |
| 391 | 403 |
| 392 apiFunctions["browserAction.setIcon"].handleRequest = function(details) { | 404 apiFunctions["browserAction.setIcon"].handleRequest = function(details) { |
| 393 setIconCommon(details, this.name, this.definition.parameters); | 405 setIconCommon(details, this.name, this.definition.parameters); |
| 394 }; | 406 }; |
| 395 | 407 |
| 396 apiFunctions["pageAction.setIcon"].handleRequest = function(details) { | 408 apiFunctions["pageAction.setIcon"].handleRequest = function(details) { |
| 397 setIconCommon(details, this.name, this.definition.parameters); | 409 setIconCommon(details, this.name, this.definition.parameters); |
| 398 }; | 410 }; |
| 399 | 411 |
| 400 setupBrowserActionEvent(extensionId); | 412 setupBrowserActionEvent(extensionId); |
| 401 setupPageActionEvents(extensionId); | 413 setupPageActionEvents(extensionId); |
| 402 setupToolstripEvents(GetRenderViewId()); | 414 setupToolstripEvents(GetRenderViewId()); |
| 403 }); | 415 }); |
| 404 })(); | 416 })(); |
| OLD | NEW |