Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: safari/ext/background.js

Issue 5564089086509056: Issue 1801 - Use URL objects to process URLs in the background page (Closed)
Patch Set: Rebased and addressed comments Created Feb. 11, 2015, 5:06 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « qunit/tests/url.js ('k') | safari/ext/content.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 (function() 18 (function()
19 { 19 {
20 /* Pages */ 20 /* Pages */
21 21
22 var pages = Object.create(null); 22 var pages = Object.create(null);
23 var pageCounter = 0; 23 var pageCounter = 0;
24 24
25 var Page = function(id, tab, url) 25 var Page = function(id, tab, url)
26 { 26 {
27 this._id = id; 27 this._id = id;
28 this._tab = tab; 28 this._tab = tab;
29 this._frames = [{url: url, parent: null}]; 29 this._frames = [{url: new URL(url), parent: null}];
30 30
31 if (tab.page) 31 if (tab.page)
32 this._messageProxy = new ext._MessageProxy(tab.page); 32 this._messageProxy = new ext._MessageProxy(tab.page);
33 else 33 else
34 // while the new tab page is shown on Safari 7, the 'page' property 34 // while the new tab page is shown on Safari 7, the 'page' property
35 // of the tab is undefined, and we can't send messages to that page 35 // of the tab is undefined, and we can't send messages to that page
36 this._messageProxy = { 36 this._messageProxy = {
37 handleRequest: function() {}, 37 handleRequest: function() {},
38 handleResponse: function() {}, 38 handleResponse: function() {},
39 sendMessage: function() {} 39 sendMessage: function() {}
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 // loaded in the same tab. If there is more than one match, 568 // loaded in the same tab. If there is more than one match,
569 // the most recent loaded page and frame is preferred. 569 // the most recent loaded page and frame is preferred.
570 for (var curPageId in tab._pages) 570 for (var curPageId in tab._pages)
571 { 571 {
572 var curPage = pages[curPageId]; 572 var curPage = pages[curPageId];
573 573
574 for (var i = 0; i < curPage._frames.length; i++) 574 for (var i = 0; i < curPage._frames.length; i++)
575 { 575 {
576 var curFrame = curPage._frames[i]; 576 var curFrame = curPage._frames[i];
577 577
578 if (curFrame.url == message.referrer) 578 if (curFrame.url.href == message.referrer)
579 { 579 {
580 pageId = curPageId; 580 pageId = curPageId;
581 page = curPage; 581 page = curPage;
582 parentFrame = curFrame; 582 parentFrame = curFrame;
583 } 583 }
584 584
585 if (i == 0) 585 if (i == 0)
586 { 586 {
587 lastPageId = curPageId; 587 lastPageId = curPageId;
588 lastPage = curPage; 588 lastPage = curPage;
589 lastPageTopLevelFrame = curFrame; 589 lastPageTopLevelFrame = curFrame;
590 } 590 }
591 } 591 }
592 } 592 }
593 593
594 // if we can't find the parent frame and its page, fall back to 594 // if we can't find the parent frame and its page, fall back to
595 // the page most recently loaded in the tab and its top level fram e 595 // the page most recently loaded in the tab and its top level fram e
596 if (!page) 596 if (!page)
597 { 597 {
598 pageId = lastPageId; 598 pageId = lastPageId;
599 page = lastPage; 599 page = lastPage;
600 parentFrame = lastPageTopLevelFrame; 600 parentFrame = lastPageTopLevelFrame;
601 } 601 }
602 602
603 frameId = page._frames.length; 603 frameId = page._frames.length;
604 page._frames.push({url: message.url, parent: parentFrame}); 604 page._frames.push({url: new URL(message.url), parent: parentFrame} );
605 } 605 }
606 606
607 event.message = {pageId: pageId, frameId: frameId}; 607 event.message = {pageId: pageId, frameId: frameId};
608 break; 608 break;
609 case "webRequest": 609 case "webRequest":
610 var page = pages[event.message.pageId]; 610 var page = pages[event.message.pageId];
611 var frame = page._frames[event.message.frameId];
612
611 var results = ext.webRequest.onBeforeRequest._dispatch( 613 var results = ext.webRequest.onBeforeRequest._dispatch(
612 event.message.url, 614 new URL(event.message.url, frame.url),
613 event.message.type, 615 event.message.type, page, frame
614 page,
615 page._frames[event.message.frameId]
616 ); 616 );
617 617
618 event.message = (results.indexOf(false) == -1); 618 event.message = (results.indexOf(false) == -1);
619 break; 619 break;
620 case "proxy": 620 case "proxy":
621 event.message = backgroundPageProxy.handleMessage(event.message); 621 event.message = backgroundPageProxy.handleMessage(event.message);
622 break; 622 break;
623 case "request": 623 case "request":
624 var page = pages[event.message.pageId]; 624 var page = pages[event.message.pageId];
625 var sender = {page: page, frame: page._frames[event.message.frameId] }; 625 var sender = {page: page, frame: page._frames[event.message.frameId] };
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
663 663
664 ext.showOptions = function(callback) 664 ext.showOptions = function(callback)
665 { 665 {
666 var optionsUrl = safari.extension.baseURI + "options.html"; 666 var optionsUrl = safari.extension.baseURI + "options.html";
667 667
668 for (var id in pages) 668 for (var id in pages)
669 { 669 {
670 var page = pages[id]; 670 var page = pages[id];
671 var tab = page._tab; 671 var tab = page._tab;
672 672
673 if (page.url == optionsUrl && tab.browserWindow == safari.application.acti veBrowserWindow) 673 if (page.url.href == optionsUrl && tab.browserWindow == safari.application .activeBrowserWindow)
674 { 674 {
675 tab.activate(); 675 tab.activate();
676 if (callback) 676 if (callback)
677 callback(page); 677 callback(page);
678 return; 678 return;
679 } 679 }
680 } 680 }
681 681
682 ext.pages.open(optionsUrl, callback); 682 ext.pages.open(optionsUrl, callback);
683 }; 683 };
684 })(); 684 })();
OLDNEW
« no previous file with comments | « qunit/tests/url.js ('k') | safari/ext/content.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld