| 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 var chrome; | 10 var chrome; |
| 11 (function() { | 11 (function() { |
| 12 native function GetNextRequestId(); | |
| 13 native function RegisterExtension(); | |
| 14 native function UnregisterExtension(); | |
| 15 native function GetViews(); | 12 native function GetViews(); |
| 16 native function GetWindow(); | 13 native function GetWindow(); |
| 17 native function GetCurrentWindow(); | 14 native function GetCurrentWindow(); |
| 18 native function GetLastFocusedWindow(); | 15 native function GetLastFocusedWindow(); |
| 19 native function CreateWindow(); | 16 native function CreateWindow(); |
| 20 native function UpdateWindow(); | 17 native function UpdateWindow(); |
| 21 native function RemoveWindow(); | 18 native function RemoveWindow(); |
| 22 native function GetAllWindows(); | 19 native function GetAllWindows(); |
| 23 native function GetTab(); | 20 native function GetTab(); |
| 24 native function GetSelectedTab(); | 21 native function GetSelectedTab(); |
| 25 native function GetAllTabsInWindow(); | 22 native function GetAllTabsInWindow(); |
| 26 native function CreateTab(); | 23 native function CreateTab(); |
| 27 native function UpdateTab(); | 24 native function UpdateTab(); |
| 28 native function MoveTab(); | 25 native function MoveTab(); |
| 29 native function RemoveTab(); | 26 native function RemoveTab(); |
| 30 native function EnablePageAction(); | 27 native function EnablePageAction(); |
| 31 native function DisablePageAction(); | 28 native function DisablePageAction(); |
| 32 native function GetBookmarks(); | 29 native function GetBookmarks(); |
| 33 native function GetBookmarkChildren(); | 30 native function GetBookmarkChildren(); |
| 34 native function GetBookmarkTree(); | 31 native function GetBookmarkTree(); |
| 35 native function SearchBookmarks(); | 32 native function SearchBookmarks(); |
| 36 native function RemoveBookmark(); | 33 native function RemoveBookmark(); |
| 37 native function CreateBookmark(); | 34 native function CreateBookmark(); |
| 38 native function MoveBookmark(); | 35 native function MoveBookmark(); |
| 39 native function SetBookmarkTitle(); | 36 native function SetBookmarkTitle(); |
| 37 native function GetChromeHidden(); |
| 40 | 38 |
| 41 if (!chrome) | 39 if (!chrome) |
| 42 chrome = {}; | 40 chrome = {}; |
| 43 | 41 |
| 42 var chromeHidden = GetChromeHidden(); |
| 43 |
| 44 // Validate arguments. | 44 // Validate arguments. |
| 45 function validate(args, schemas) { | 45 function validate(args, schemas) { |
| 46 if (args.length > schemas.length) | 46 if (args.length > schemas.length) |
| 47 throw new Error("Too many arguments."); | 47 throw new Error("Too many arguments."); |
| 48 | 48 |
| 49 for (var i = 0; i < schemas.length; i++) { | 49 for (var i = 0; i < schemas.length; i++) { |
| 50 if (i in args && args[i] !== null && args[i] !== undefined) { | 50 if (i in args && args[i] !== null && args[i] !== undefined) { |
| 51 var validator = new chrome.JSONSchemaValidator(); | 51 var validator = new chrome.JSONSchemaValidator(); |
| 52 validator.validate(args[i], schemas[i]); | 52 validator.validate(args[i], schemas[i]); |
| 53 if (validator.errors.length == 0) | 53 if (validator.errors.length == 0) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 65 message = message.substring(0, message.length - 2); | 65 message = message.substring(0, message.length - 2); |
| 66 message += "."; | 66 message += "."; |
| 67 | 67 |
| 68 throw new Error(message); | 68 throw new Error(message); |
| 69 } else if (!schemas[i].optional) { | 69 } else if (!schemas[i].optional) { |
| 70 throw new Error("Parameter " + i + " is required."); | 70 throw new Error("Parameter " + i + " is required."); |
| 71 } | 71 } |
| 72 } | 72 } |
| 73 } | 73 } |
| 74 | 74 |
| 75 // Callback handling. | 75 var sendRequest = chromeHidden.sendRequest; |
| 76 // TODO(aa): This function should not be publicly exposed. Pass it into V8 | |
| 77 // instead and hold one per-context. See the way event_bindings.js works. | |
| 78 var callbacks = []; | |
| 79 chrome.handleResponse_ = function(requestId, name, success, response, error) { | |
| 80 try { | |
| 81 if (!success) { | |
| 82 if (!error) | |
| 83 error = "Unknown error." | |
| 84 console.error("Error during " + name + ": " + error); | |
| 85 return; | |
| 86 } | |
| 87 | |
| 88 if (callbacks[requestId]) { | |
| 89 if (response) { | |
| 90 callbacks[requestId](JSON.parse(response)); | |
| 91 } else { | |
| 92 callbacks[requestId](); | |
| 93 } | |
| 94 } | |
| 95 } finally { | |
| 96 delete callbacks[requestId]; | |
| 97 } | |
| 98 }; | |
| 99 | |
| 100 // Send an API request and optionally register a callback. | |
| 101 function sendRequest(request, args, callback) { | |
| 102 // JSON.stringify doesn't support a root object which is undefined. | |
| 103 if (args === undefined) | |
| 104 args = null; | |
| 105 var sargs = JSON.stringify(args); | |
| 106 var requestId = GetNextRequestId(); | |
| 107 var hasCallback = false; | |
| 108 if (callback) { | |
| 109 hasCallback = true; | |
| 110 callbacks[requestId] = callback; | |
| 111 } | |
| 112 request(sargs, requestId, hasCallback); | |
| 113 } | |
| 114 | 76 |
| 115 //---------------------------------------------------------------------------- | 77 //---------------------------------------------------------------------------- |
| 116 | 78 |
| 117 // Windows. | 79 // Windows. |
| 118 chrome.windows = {}; | 80 chrome.windows = {}; |
| 119 | 81 |
| 120 chrome.windows.get = function(windowId, callback) { | 82 chrome.windows.get = function(windowId, callback) { |
| 121 validate(arguments, arguments.callee.params); | 83 validate(arguments, arguments.callee.params); |
| 122 sendRequest(GetWindow, windowId, callback); | 84 sendRequest(GetWindow, windowId, callback); |
| 123 }; | 85 }; |
| (...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 chrome.bookmarks.onChildrenReordered = | 483 chrome.bookmarks.onChildrenReordered = |
| 522 new chrome.Event("bookmark-children-reordered"); | 484 new chrome.Event("bookmark-children-reordered"); |
| 523 | 485 |
| 524 | 486 |
| 525 //---------------------------------------------------------------------------- | 487 //---------------------------------------------------------------------------- |
| 526 | 488 |
| 527 // Self. | 489 // Self. |
| 528 chrome.self = chrome.self || {}; | 490 chrome.self = chrome.self || {}; |
| 529 chrome.self.onConnect = new chrome.Event("channel-connect"); | 491 chrome.self.onConnect = new chrome.Event("channel-connect"); |
| 530 | 492 |
| 531 // Register | |
| 532 chrome.self.register_ = function() { | |
| 533 var extensionId = RegisterExtension(); | |
| 534 window.addEventListener('unload', function() { | |
| 535 UnregisterExtension(extensionId); }, false); | |
| 536 delete chrome.self.register_; | |
| 537 } | |
| 538 | |
| 539 chrome.self.getViews = function() { | 493 chrome.self.getViews = function() { |
| 540 return GetViews(); | 494 return GetViews(); |
| 541 } | 495 } |
| 542 })(); | 496 })(); |
| 543 | |
| OLD | NEW |