1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/common/extensions/extension.h"
#include <algorithm>
#include "app/l10n_util.h"
#include "base/base64.h"
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/i18n/rtl.h"
#include "base/logging.h"
#include "base/sha2.h"
#include "base/singleton.h"
#include "base/stl_util-inl.h"
#include "base/third_party/nss/blapi.h"
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "base/version.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/extensions/extension_action.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/extension_error_utils.h"
#include "chrome/common/extensions/extension_l10n_util.h"
#include "chrome/common/extensions/extension_resource.h"
#include "chrome/common/extensions/user_script.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/url_constants.h"
#include "googleurl/src/url_util.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "net/base/registry_controlled_domain.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "webkit/glue/image_decoder.h"
namespace keys = extension_manifest_keys;
namespace values = extension_manifest_values;
namespace errors = extension_manifest_errors;
namespace {
const int kPEMOutputColumns = 65;
// KEY MARKERS
const char kKeyBeginHeaderMarker[] = "-----BEGIN";
const char kKeyBeginFooterMarker[] = "-----END";
const char kKeyInfoEndMarker[] = "KEY-----";
const char kPublic[] = "PUBLIC";
const char kPrivate[] = "PRIVATE";
const int kRSAKeySize = 1024;
// Converts a normal hexadecimal string into the alphabet used by extensions.
// We use the characters 'a'-'p' instead of '0'-'f' to avoid ever having a
// completely numeric host, since some software interprets that as an IP
// address.
static void ConvertHexadecimalToIDAlphabet(std::string* id) {
for (size_t i = 0; i < id->size(); ++i) {
int val;
if (base::HexStringToInt(id->substr(i, 1), &val))
(*id)[i] = val + 'a';
else
(*id)[i] = 'a';
}
}
const int kValidWebExtentSchemes =
URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS;
// These keys are allowed by all crx files (apps, extensions, themes, etc).
static const char* kBaseCrxKeys[] = {
keys::kCurrentLocale,
keys::kDefaultLocale,
keys::kDescription,
keys::kIcons,
keys::kName,
keys::kPublicKey,
keys::kSignature,
keys::kVersion,
keys::kUpdateURL
};
bool IsBaseCrxKey(const std::string& key) {
for (size_t i = 0; i < arraysize(kBaseCrxKeys); ++i) {
if (key == kBaseCrxKeys[i])
return true;
}
return false;
}
// Names of API modules that do not require a permission.
const char kBrowserActionModuleName[] = "browserAction";
const char kBrowserActionsModuleName[] = "browserActions";
const char kDevToolsModuleName[] = "devtools";
const char kExtensionModuleName[] = "extension";
const char kI18NModuleName[] = "i18n";
const char kPageActionModuleName[] = "pageAction";
const char kPageActionsModuleName[] = "pageActions";
const char kTestModuleName[] = "test";
// Names of modules that can be used without listing it in the permissions
// section of the manifest.
const char* kNonPermissionModuleNames[] = {
kBrowserActionModuleName,
kBrowserActionsModuleName,
kDevToolsModuleName,
kExtensionModuleName,
kI18NModuleName,
kPageActionModuleName,
kPageActionsModuleName,
kTestModuleName
};
const size_t kNumNonPermissionModuleNames =
arraysize(kNonPermissionModuleNames);
// Names of functions (within modules requiring permissions) that can be used
// without asking for the module permission. In other words, functions you can
// use with no permissions specified.
const char* kNonPermissionFunctionNames[] = {
"tabs.create",
"tabs.update"
};
const size_t kNumNonPermissionFunctionNames =
arraysize(kNonPermissionFunctionNames);
// A singleton object containing global data needed by the extension objects.
class ExtensionConfig {
public:
static ExtensionConfig* GetSingleton() {
return Singleton<ExtensionConfig>::get();
}
int GetPermissionMessageId(const std::string& permission) {
return Extension::kPermissions[permission_map_[permission]].message_id;
}
Extension::ScriptingWhitelist* whitelist() { return &scripting_whitelist_; }
private:
friend struct DefaultSingletonTraits<ExtensionConfig>;
ExtensionConfig() {
for (size_t i = 0; i < Extension::kNumPermissions; ++i)
permission_map_[Extension::kPermissions[i].name] = i;
};
~ExtensionConfig() { }
std::map<const std::string, size_t> permission_map_;
// A whitelist of extensions that can script anywhere. Do not add to this
// list (except in tests) without consulting the Extensions team first.
// Note: Component extensions have this right implicitly and do not need to be
// added to this list.
Extension::ScriptingWhitelist scripting_whitelist_;
};
// Aliased to kTabPermission for purposes of API checks, but not allowed
// in the permissions field of the manifest.
static const char kWindowPermission[] = "windows";
} // namespace
const FilePath::CharType Extension::kManifestFilename[] =
FILE_PATH_LITERAL("manifest.json");
const FilePath::CharType Extension::kLocaleFolder[] =
FILE_PATH_LITERAL("_locales");
const FilePath::CharType Extension::kMessagesFilename[] =
FILE_PATH_LITERAL("messages.json");
#if defined(OS_WIN)
const char Extension::kExtensionRegistryPath[] =
"Software\\Google\\Chrome\\Extensions";
#endif
// first 16 bytes of SHA256 hashed public key.
const size_t Extension::kIdSize = 16;
const char Extension::kMimeType[] = "application/x-chrome-extension";
const int Extension::kIconSizes[] = {
EXTENSION_ICON_LARGE,
EXTENSION_ICON_MEDIUM,
EXTENSION_ICON_SMALL,
EXTENSION_ICON_SMALLISH,
EXTENSION_ICON_BITTY
};
const int Extension::kPageActionIconMaxSize = 19;
const int Extension::kBrowserActionIconMaxSize = 19;
// Explicit permissions -- permission declaration required.
const char Extension::kBackgroundPermission[] = "background";
const char Extension::kContextMenusPermission[] = "contextMenus";
const char Extension::kBookmarkPermission[] = "bookmarks";
const char Extension::kCookiePermission[] = "cookies";
const char Extension::kExperimentalPermission[] = "experimental";
const char Extension::kGeolocationPermission[] = "geolocation";
const char Extension::kHistoryPermission[] = "history";
const char Extension::kIdlePermission[] = "idle";
const char Extension::kManagementPermission[] = "management";
const char Extension::kNotificationPermission[] = "notifications";
const char Extension::kProxyPermission[] = "proxy";
const char Extension::kTabPermission[] = "tabs";
const char Extension::kUnlimitedStoragePermission[] = "unlimitedStorage";
const char Extension::kWebstorePrivatePermission[] = "webstorePrivate";
// In general, all permissions should have an install message.
// See ExtensionsTest.PermissionMessages for an explanation of each
// exception.
const Extension::Permission Extension::kPermissions[] = {
{ kBackgroundPermission, 0 },
{ kBookmarkPermission, IDS_EXTENSION_PROMPT2_WARNING_BOOKMARKS },
{ kContextMenusPermission, 0 },
{ kCookiePermission, 0 },
{ kExperimentalPermission, 0 },
{ kGeolocationPermission, IDS_EXTENSION_PROMPT2_WARNING_GEOLOCATION },
{ kIdlePermission, 0 },
{ kHistoryPermission, IDS_EXTENSION_PROMPT2_WARNING_BROWSING_HISTORY },
{ kManagementPermission, IDS_EXTENSION_PROMPT2_WARNING_MANAGEMENT },
{ kNotificationPermission, 0 },
{ kProxyPermission, 0 },
{ kTabPermission, IDS_EXTENSION_PROMPT2_WARNING_BROWSING_HISTORY },
{ kUnlimitedStoragePermission, 0 },
{ kWebstorePrivatePermission, 0 },
};
const size_t Extension::kNumPermissions =
arraysize(Extension::kPermissions);
const char* const Extension::kHostedAppPermissionNames[] = {
Extension::kBackgroundPermission,
Extension::kGeolocationPermission,
Extension::kNotificationPermission,
Extension::kUnlimitedStoragePermission,
Extension::kWebstorePrivatePermission,
};
const size_t Extension::kNumHostedAppPermissions =
arraysize(Extension::kHostedAppPermissionNames);
// We purposefully don't put this into kPermissionNames.
const char Extension::kOldUnlimitedStoragePermission[] = "unlimited_storage";
//
// Extension::StaticData
//
Extension::StaticData::StaticData()
: incognito_split_mode(false),
location(INVALID),
converted_from_user_script(false),
is_theme(false),
is_app(false),
launch_container(extension_misc::LAUNCH_TAB),
launch_width(0),
launch_height(0) {
}
Extension::StaticData::~StaticData() {
}
//
// Extension::RuntimeData
//
Extension::RuntimeData::RuntimeData()
: background_page_ready(false),
being_upgraded(false) {
}
Extension::RuntimeData::~RuntimeData() {
}
//
// Extension
//
// static
int Extension::GetPermissionMessageId(const std::string& permission) {
return ExtensionConfig::GetSingleton()->GetPermissionMessageId(permission);
}
std::vector<string16> Extension::GetPermissionMessages() {
std::vector<string16> messages;
if (!plugins().empty()) {
messages.push_back(
l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT2_WARNING_FULL_ACCESS));
return messages;
}
string16 host_msg = GetHostPermissionMessage();
if (!host_msg.empty())
messages.push_back(host_msg);
std::set<string16> simple_msgs = GetSimplePermissionMessages();
messages.insert(messages.end(), simple_msgs.begin(), simple_msgs.end());
return messages;
}
std::set<string16> Extension::GetSimplePermissionMessages() {
std::set<string16> messages;
std::set<std::string>::const_iterator i;
for (i = api_permissions().begin(); i != api_permissions().end(); ++i) {
int message_id = GetPermissionMessageId(*i);
if (message_id)
messages.insert(l10n_util::GetStringUTF16(message_id));
}
return messages;
}
std::vector<std::string> Extension::GetDistinctHosts() {
return GetDistinctHosts(GetEffectiveHostPermissions().patterns());
}
// static
std::vector<std::string> Extension::GetDistinctHosts(
const URLPatternList& host_patterns) {
// Vector because we later want to access these by index.
std::vector<std::string> distinct_hosts;
std::set<std::string> rcd_set;
for (size_t i = 0; i < host_patterns.size(); ++i) {
std::string candidate = host_patterns[i].host();
size_t registry = net::RegistryControlledDomainService::GetRegistryLength(
candidate, false);
if (registry && registry != std::string::npos) {
std::string no_rcd(candidate, 0, candidate.size() - registry);
if (rcd_set.count(no_rcd))
continue;
rcd_set.insert(no_rcd);
}
if (std::find(distinct_hosts.begin(), distinct_hosts.end(), candidate) ==
distinct_hosts.end()) {
distinct_hosts.push_back(candidate);
}
}
return distinct_hosts;
}
string16 Extension::GetHostPermissionMessage() {
if (HasEffectiveAccessToAllHosts())
return l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT2_WARNING_ALL_HOSTS);
std::vector<std::string> hosts = GetDistinctHosts();
if (hosts.size() == 1) {
return l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT2_WARNING_1_HOST,
UTF8ToUTF16(hosts[0]));
} else if (hosts.size() == 2) {
return l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT2_WARNING_2_HOSTS,
UTF8ToUTF16(hosts[0]),
UTF8ToUTF16(hosts[1]));
} else if (hosts.size() == 3) {
return l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT2_WARNING_3_HOSTS,
UTF8ToUTF16(hosts[0]),
UTF8ToUTF16(hosts[1]),
UTF8ToUTF16(hosts[2]));
} else if (hosts.size() >= 4) {
return l10n_util::GetStringFUTF16(
IDS_EXTENSION_PROMPT2_WARNING_4_OR_MORE_HOSTS,
UTF8ToUTF16(hosts[0]),
UTF8ToUTF16(hosts[1]),
base::IntToString16(hosts.size() - 2));
}
return string16();
}
FilePath Extension::MaybeNormalizePath(const FilePath& path) {
#if defined(OS_WIN)
// Normalize any drive letter to upper-case. We do this for consistency with
// net_utils::FilePathToFileURL(), which does the same thing, to make string
// comparisons simpler.
std::wstring path_str = path.value();
if (path_str.size() >= 2 && path_str[0] >= L'a' && path_str[0] <= L'z' &&
path_str[1] == ':')
path_str[0] += ('A' - 'a');
return FilePath(path_str);
#else
return path;
#endif
}
// static
bool Extension::IsHostedAppPermission(const std::string& str) {
for (size_t i = 0; i < Extension::kNumHostedAppPermissions; ++i) {
if (str == Extension::kHostedAppPermissionNames[i]) {
return true;
}
}
return false;
}
Extension::~Extension() {
}
const std::string Extension::VersionString() const {
return version()->GetString();
}
// static
bool Extension::IsExtension(const FilePath& file_name) {
return file_name.MatchesExtension(chrome::kExtensionFileExtension);
}
// static
bool Extension::IdIsValid(const std::string& id) {
// Verify that the id is legal.
if (id.size() != (kIdSize * 2))
return false;
// We only support lowercase IDs, because IDs can be used as URL components
// (where GURL will lowercase it).
std::string temp = StringToLowerASCII(id);
for (size_t i = 0; i < temp.size(); i++)
if (temp[i] < 'a' || temp[i] > 'p')
return false;
return true;
}
// static
std::string Extension::GenerateIdForPath(const FilePath& path) {
FilePath new_path = Extension::MaybeNormalizePath(path);
std::string id;
if (!GenerateId(WideToUTF8(new_path.ToWStringHack()),&id))
return "";
return id;
}
Extension::HistogramType Extension::GetHistogramType() {
if (is_theme())
return TYPE_THEME;
if (converted_from_user_script())
return TYPE_USER_SCRIPT;
if (is_hosted_app())
return TYPE_HOSTED_APP;
if (is_packaged_app())
return TYPE_PACKAGED_APP;
return TYPE_EXTENSION;
}
// static
GURL Extension::GetResourceURL(const GURL& extension_url,
const std::string& relative_path) {
DCHECK(extension_url.SchemeIs(chrome::kExtensionScheme));
DCHECK_EQ("/", extension_url.path());
GURL ret_val = GURL(extension_url.spec() + relative_path);
DCHECK(StartsWithASCII(ret_val.spec(), extension_url.spec(), false));
return ret_val;
}
bool Extension::GenerateId(const std::string& input, std::string* output) {
CHECK(output);
if (input.empty())
return false;
uint8 hash[Extension::kIdSize];
base::SHA256HashString(input, hash, sizeof(hash));
*output = StringToLowerASCII(base::HexEncode(hash, sizeof(hash)));
ConvertHexadecimalToIDAlphabet(output);
return true;
}
// Helper method that loads a UserScript object from a dictionary in the
// content_script list of the manifest.
bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script,
int definition_index, std::string* error,
UserScript* result) {
// run_at
if (content_script->HasKey(keys::kRunAt)) {
std::string run_location;
if (!content_script->GetString(keys::kRunAt, &run_location)) {
*error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidRunAt,
base::IntToString(definition_index));
return false;
}
if (run_location == values::kRunAtDocumentStart) {
result->set_run_location(UserScript::DOCUMENT_START);
} else if (run_location == values::kRunAtDocumentEnd) {
result->set_run_location(UserScript::DOCUMENT_END);
} else if (run_location == values::kRunAtDocumentIdle) {
result->set_run_location(UserScript::DOCUMENT_IDLE);
} else {
*error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidRunAt,
base::IntToString(definition_index));
return false;
}
}
// all frames
if (content_script->HasKey(keys::kAllFrames)) {
bool all_frames = false;
if (!content_script->GetBoolean(keys::kAllFrames, &all_frames)) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidAllFrames, base::IntToString(definition_index));
return false;
}
result->set_match_all_frames(all_frames);
}
// matches
ListValue* matches = NULL;
if (!content_script->GetList(keys::kMatches, &matches)) {
*error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidMatches,
base::IntToString(definition_index));
return false;
}
if (matches->GetSize() == 0) {
*error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidMatchCount,
base::IntToString(definition_index));
return false;
}
for (size_t j = 0; j < matches->GetSize(); ++j) {
std::string match_str;
if (!matches->GetString(j, &match_str)) {
*error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidMatch,
base::IntToString(definition_index), base::IntToString(j));
return false;
}
URLPattern pattern;
if (CanExecuteScriptEverywhere())
pattern = URLPattern(URLPattern::SCHEME_ALL);
else
pattern = URLPattern(UserScript::kValidUserScriptSchemes);
if (!pattern.Parse(match_str)) {
*error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidMatch,
base::IntToString(definition_index), base::IntToString(j));
return false;
}
result->add_url_pattern(pattern);
}
// include/exclude globs (mostly for Greasemonkey compatibility)
if (!LoadGlobsHelper(content_script, definition_index, keys::kIncludeGlobs,
error, &UserScript::add_glob, result)) {
return false;
}
if (!LoadGlobsHelper(content_script, definition_index, keys::kExcludeGlobs,
error, &UserScript::add_exclude_glob, result)) {
return false;
}
// js and css keys
ListValue* js = NULL;
if (content_script->HasKey(keys::kJs) &&
!content_script->GetList(keys::kJs, &js)) {
*error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidJsList,
base::IntToString(definition_index));
return false;
}
ListValue* css = NULL;
if (content_script->HasKey(keys::kCss) &&
!content_script->GetList(keys::kCss, &css)) {
*error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidCssList,
base::IntToString(definition_index));
return false;
}
// The manifest needs to have at least one js or css user script definition.
if (((js ? js->GetSize() : 0) + (css ? css->GetSize() : 0)) == 0) {
*error = ExtensionErrorUtils::FormatErrorMessage(errors::kMissingFile,
base::IntToString(definition_index));
return false;
}
if (js) {
for (size_t script_index = 0; script_index < js->GetSize();
++script_index) {
Value* value;
std::string relative;
if (!js->Get(script_index, &value) || !value->GetAsString(&relative)) {
*error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidJs,
base::IntToString(definition_index),
base::IntToString(script_index));
return false;
}
GURL url = GetResourceURL(relative);
ExtensionResource resource = GetResource(relative);
result->js_scripts().push_back(UserScript::File(
resource.extension_root(), resource.relative_path(), url));
}
}
if (css) {
for (size_t script_index = 0; script_index < css->GetSize();
++script_index) {
Value* value;
std::string relative;
if (!css->Get(script_index, &value) || !value->GetAsString(&relative)) {
*error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidCss,
base::IntToString(definition_index),
base::IntToString(script_index));
return false;
}
GURL url = GetResourceURL(relative);
ExtensionResource resource = GetResource(relative);
result->css_scripts().push_back(UserScript::File(
resource.extension_root(), resource.relative_path(), url));
}
}
return true;
}
bool Extension::LoadGlobsHelper(
const DictionaryValue* content_script,
int content_script_index,
const char* globs_property_name,
std::string* error,
void(UserScript::*add_method)(const std::string& glob),
UserScript *instance) {
if (!content_script->HasKey(globs_property_name))
return true; // they are optional
ListValue* list = NULL;
if (!content_script->GetList(globs_property_name, &list)) {
*error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidGlobList,
base::IntToString(content_script_index),
globs_property_name);
return false;
}
for (size_t i = 0; i < list->GetSize(); ++i) {
std::string glob;
if (!list->GetString(i, &glob)) {
*error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidGlob,
base::IntToString(content_script_index),
globs_property_name,
base::IntToString(i));
return false;
}
(instance->*add_method)(glob);
}
return true;
}
ExtensionAction* Extension::LoadExtensionActionHelper(
const DictionaryValue* extension_action, std::string* error) {
scoped_ptr<ExtensionAction> result(new ExtensionAction());
result->set_extension_id(id());
// Page actions are hidden by default, and browser actions ignore
// visibility.
result->SetIsVisible(ExtensionAction::kDefaultTabId, false);
// TODO(EXTENSIONS_DEPRECATED): icons list is obsolete.
ListValue* icons = NULL;
if (extension_action->HasKey(keys::kPageActionIcons) &&
extension_action->GetList(keys::kPageActionIcons, &icons)) {
for (ListValue::const_iterator iter = icons->begin();
iter != icons->end(); ++iter) {
std::string path;
if (!(*iter)->GetAsString(&path) || path.empty()) {
*error = errors::kInvalidPageActionIconPath;
return NULL;
}
result->icon_paths()->push_back(path);
}
}
// TODO(EXTENSIONS_DEPRECATED): Read the page action |id| (optional).
std::string id;
if (extension_action->HasKey(keys::kPageActionId)) {
if (!extension_action->GetString(keys::kPageActionId, &id)) {
*error = errors::kInvalidPageActionId;
return NULL;
}
result->set_id(id);
}
std::string default_icon;
// Read the page action |default_icon| (optional).
if (extension_action->HasKey(keys::kPageActionDefaultIcon)) {
if (!extension_action->GetString(keys::kPageActionDefaultIcon,
&default_icon) ||
default_icon.empty()) {
*error = errors::kInvalidPageActionIconPath;
return NULL;
}
result->set_default_icon_path(default_icon);
}
// Read the page action title from |default_title| if present, |name| if not
// (both optional).
std::string title;
if (extension_action->HasKey(keys::kPageActionDefaultTitle)) {
if (!extension_action->GetString(keys::kPageActionDefaultTitle, &title)) {
*error = errors::kInvalidPageActionDefaultTitle;
return NULL;
}
} else if (extension_action->HasKey(keys::kName)) {
if (!extension_action->GetString(keys::kName, &title)) {
*error = errors::kInvalidPageActionName;
return NULL;
}
}
result->SetTitle(ExtensionAction::kDefaultTabId, title);
// Read the action's |popup| (optional).
const char* popup_key = NULL;
if (extension_action->HasKey(keys::kPageActionDefaultPopup))
popup_key = keys::kPageActionDefaultPopup;
// For backward compatibility, alias old key "popup" to new
// key "default_popup".
if (extension_action->HasKey(keys::kPageActionPopup)) {
if (popup_key) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidPageActionOldAndNewKeys,
keys::kPageActionDefaultPopup,
keys::kPageActionPopup);
return NULL;
}
popup_key = keys::kPageActionPopup;
}
if (popup_key) {
DictionaryValue* popup = NULL;
std::string url_str;
if (extension_action->GetString(popup_key, &url_str)) {
// On success, |url_str| is set. Nothing else to do.
} else if (extension_action->GetDictionary(popup_key, &popup)) {
// TODO(EXTENSIONS_DEPRECATED): popup is now a string only.
// Support the old dictionary format for backward compatibility.
if (!popup->GetString(keys::kPageActionPopupPath, &url_str)) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidPageActionPopupPath, "<missing>");
return NULL;
}
} else {
*error = errors::kInvalidPageActionPopup;
return NULL;
}
if (!url_str.empty()) {
// An empty string is treated as having no popup.
GURL url = GetResourceURL(url_str);
if (!url.is_valid()) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidPageActionPopupPath, url_str);
return NULL;
}
result->SetPopupUrl(ExtensionAction::kDefaultTabId, url);
} else {
DCHECK(!result->HasPopup(ExtensionAction::kDefaultTabId))
<< "Shouldn't be posible for the popup to be set.";
}
}
return result.release();
}
bool Extension::ContainsNonThemeKeys(const DictionaryValue& source) {
for (DictionaryValue::key_iterator key = source.begin_keys();
key != source.end_keys(); ++key) {
if (!IsBaseCrxKey(*key) && *key != keys::kTheme)
return true;
}
return false;
}
bool Extension::LoadIsApp(const DictionaryValue* manifest,
std::string* error) {
if (manifest->HasKey(keys::kApp))
mutable_static_data_->is_app = true;
return true;
}
bool Extension::LoadExtent(const DictionaryValue* manifest,
const char* key,
ExtensionExtent* extent,
const char* list_error,
const char* value_error,
std::string* error) {
Value* temp = NULL;
if (!manifest->Get(key, &temp))
return true;
if (temp->GetType() != Value::TYPE_LIST) {
*error = list_error;
return false;
}
ListValue* pattern_list = static_cast<ListValue*>(temp);
for (size_t i = 0; i < pattern_list->GetSize(); ++i) {
std::string pattern_string;
if (!pattern_list->GetString(i, &pattern_string)) {
*error = ExtensionErrorUtils::FormatErrorMessage(value_error,
base::UintToString(i));
return false;
}
URLPattern pattern(kValidWebExtentSchemes);
if (!pattern.Parse(pattern_string)) {
*error = ExtensionErrorUtils::FormatErrorMessage(value_error,
base::UintToString(i));
return false;
}
// Do not allow authors to claim "<all_urls>". That would make no sense.
if (pattern.match_all_urls()) {
*error = ExtensionErrorUtils::FormatErrorMessage(value_error,
base::UintToString(i));
return false;
}
// We do not allow authors to put wildcards in their paths. Instead, we
// imply one at the end.
if (pattern.path().find('*') != std::string::npos) {
*error = ExtensionErrorUtils::FormatErrorMessage(value_error,
base::UintToString(i));
return false;
}
pattern.set_path(pattern.path() + '*');
extent->AddPattern(pattern);
}
return true;
}
bool Extension::LoadLaunchURL(const DictionaryValue* manifest,
std::string* error) {
Value* temp = NULL;
// launch URL can be either local (to chrome-extension:// root) or an absolute
// web URL.
if (manifest->Get(keys::kLaunchLocalPath, &temp)) {
if (manifest->Get(keys::kLaunchWebURL, NULL)) {
*error = errors::kLaunchPathAndURLAreExclusive;
return false;
}
std::string launch_path;
if (!temp->GetAsString(&launch_path)) {
*error = errors::kInvalidLaunchLocalPath;
return false;
}
// Ensure the launch path is a valid relative URL.
GURL resolved = url().Resolve(launch_path);
if (!resolved.is_valid() || resolved.GetOrigin() != url()) {
*error = errors::kInvalidLaunchLocalPath;
return false;
}
mutable_static_data_->launch_local_path = launch_path;
} else if (manifest->Get(keys::kLaunchWebURL, &temp)) {
std::string launch_url;
if (!temp->GetAsString(&launch_url)) {
*error = errors::kInvalidLaunchWebURL;
return false;
}
// Ensure the launch URL is a valid absolute URL.
if (!GURL(launch_url).is_valid()) {
*error = errors::kInvalidLaunchWebURL;
return false;
}
mutable_static_data_->launch_web_url = launch_url;
} else if (is_app()) {
*error = errors::kLaunchURLRequired;
return false;
}
// If there is no extent, we default the extent based on the launch URL.
if (web_extent().is_empty() && !launch_web_url().empty()) {
GURL launch_url(launch_web_url());
URLPattern pattern(kValidWebExtentSchemes);
if (!pattern.SetScheme("*")) {
*error = errors::kInvalidLaunchWebURL;
return false;
}
pattern.set_host(launch_url.host());
pattern.set_path("/*");
mutable_static_data_->extent.AddPattern(pattern);
}
// In order for the --apps-gallery-url switch to work with the gallery
// process isolation, we must insert any provided value into the component
// app's launch url and web extent.
if (id() == extension_misc::kWebStoreAppId) {
GURL gallery_url(CommandLine::ForCurrentProcess()->
GetSwitchValueASCII(switches::kAppsGalleryURL));
if (gallery_url.is_valid()) {
mutable_static_data_->launch_web_url = gallery_url.spec();
URLPattern pattern(URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS);
pattern.Parse(gallery_url.spec());
pattern.set_path(pattern.path() + '*');
mutable_static_data_->extent.AddPattern(pattern);
}
}
return true;
}
bool Extension::LoadLaunchContainer(const DictionaryValue* manifest,
std::string* error) {
Value* temp = NULL;
if (!manifest->Get(keys::kLaunchContainer, &temp))
return true;
std::string launch_container_string;
if (!temp->GetAsString(&launch_container_string)) {
*error = errors::kInvalidLaunchContainer;
return false;
}
if (launch_container_string == values::kLaunchContainerPanel) {
mutable_static_data_->launch_container = extension_misc::LAUNCH_PANEL;
} else if (launch_container_string == values::kLaunchContainerTab) {
mutable_static_data_->launch_container = extension_misc::LAUNCH_TAB;
} else {
*error = errors::kInvalidLaunchContainer;
return false;
}
// Validate the container width if present.
if (manifest->Get(keys::kLaunchWidth, &temp)) {
if (launch_container() != extension_misc::LAUNCH_PANEL &&
launch_container() != extension_misc::LAUNCH_WINDOW) {
*error = errors::kInvalidLaunchWidthContainer;
return false;
}
if (!temp->GetAsInteger(&mutable_static_data_->launch_width) ||
mutable_static_data_->launch_width < 0) {
mutable_static_data_->launch_width = 0;
*error = errors::kInvalidLaunchWidth;
return false;
}
}
// Validate container height if present.
if (manifest->Get(keys::kLaunchHeight, &temp)) {
if (launch_container() != extension_misc::LAUNCH_PANEL &&
launch_container() != extension_misc::LAUNCH_WINDOW) {
*error = errors::kInvalidLaunchHeightContainer;
return false;
}
if (!temp->GetAsInteger(&mutable_static_data_->launch_height) ||
mutable_static_data_->launch_height < 0) {
mutable_static_data_->launch_height = 0;
*error = errors::kInvalidLaunchHeight;
return false;
}
}
return true;
}
bool Extension::EnsureNotHybridApp(const DictionaryValue* manifest,
std::string* error) {
if (web_extent().is_empty())
return true;
for (DictionaryValue::key_iterator key = manifest->begin_keys();
key != manifest->end_keys(); ++key) {
if (!IsBaseCrxKey(*key) &&
*key != keys::kApp &&
*key != keys::kPermissions &&
*key != keys::kOptionsPage) {
*error = errors::kHostedAppsCannotIncludeExtensionFeatures;
return false;
}
}
return true;
}
Extension::Extension(const FilePath& path)
: mutable_static_data_(new StaticData),
runtime_data_(new RuntimeData) {
DCHECK(path.IsAbsolute());
static_data_ = mutable_static_data_;
mutable_static_data_->location = INVALID;
mutable_static_data_->path = MaybeNormalizePath(path);
}
ExtensionResource Extension::GetResource(const std::string& relative_path) {
#if defined(OS_POSIX)
FilePath relative_file_path(relative_path);
#elif defined(OS_WIN)
FilePath relative_file_path(UTF8ToWide(relative_path));
#endif
return ExtensionResource(id(), path(), relative_file_path);
}
ExtensionResource Extension::GetResource(const FilePath& relative_file_path) {
return ExtensionResource(id(), path(), relative_file_path);
}
// TODO(rafaelw): Move ParsePEMKeyBytes, ProducePEM & FormatPEMForOutput to a
// util class in base:
// http://code.google.com/p/chromium/issues/detail?id=13572
bool Extension::ParsePEMKeyBytes(const std::string& input,
std::string* output) {
DCHECK(output);
if (!output)
return false;
if (input.length() == 0)
return false;
std::string working = input;
if (StartsWithASCII(working, kKeyBeginHeaderMarker, true)) {
working = CollapseWhitespaceASCII(working, true);
size_t header_pos = working.find(kKeyInfoEndMarker,
sizeof(kKeyBeginHeaderMarker) - 1);
if (header_pos == std::string::npos)
return false;
size_t start_pos = header_pos + sizeof(kKeyInfoEndMarker) - 1;
size_t end_pos = working.rfind(kKeyBeginFooterMarker);
if (end_pos == std::string::npos)
return false;
if (start_pos >= end_pos)
return false;
working = working.substr(start_pos, end_pos - start_pos);
if (working.length() == 0)
return false;
}
return base::Base64Decode(working, output);
}
bool Extension::ProducePEM(const std::string& input,
std::string* output) {
CHECK(output);
if (input.length() == 0)
return false;
return base::Base64Encode(input, output);
}
bool Extension::FormatPEMForFileOutput(const std::string input,
std::string* output,
bool is_public) {
CHECK(output);
if (input.length() == 0)
return false;
*output = "";
output->append(kKeyBeginHeaderMarker);
output->append(" ");
output->append(is_public ? kPublic : kPrivate);
output->append(" ");
output->append(kKeyInfoEndMarker);
output->append("\n");
for (size_t i = 0; i < input.length(); ) {
int slice = std::min<int>(input.length() - i, kPEMOutputColumns);
output->append(input.substr(i, slice));
output->append("\n");
i += slice;
}
output->append(kKeyBeginFooterMarker);
output->append(" ");
output->append(is_public ? kPublic : kPrivate);
output->append(" ");
output->append(kKeyInfoEndMarker);
output->append("\n");
return true;
}
// static
// TODO(aa): A problem with this code is that we silently allow upgrades to
// extensions that require less permissions than the current version, but then
// we don't silently allow them to go back. In order to fix this, we would need
// to remember the max set of permissions we ever granted a single extension.
bool Extension::IsPrivilegeIncrease(Extension* old_extension,
Extension* new_extension) {
// If the old extension had native code access, we don't need to go any
// further. Things can't get any worse.
if (old_extension->plugins().size() > 0)
return false;
// Otherwise, if the new extension has a plugin, it's a privilege increase.
if (new_extension->plugins().size() > 0)
return true;
// If we are increasing the set of hosts we have access to (not
// counting scheme differences), it's a privilege increase.
if (!old_extension->HasEffectiveAccessToAllHosts()) {
if (new_extension->HasEffectiveAccessToAllHosts())
return true;
// TODO(erikkay) This will trip when you add a new distinct hostname,
// but we should unique based on RCD as well. crbug.com/57042
std::vector<std::string> old_hosts = old_extension->GetDistinctHosts();
std::vector<std::string> new_hosts = new_extension->GetDistinctHosts();
std::set<std::string> old_hosts_set(old_hosts.begin(), old_hosts.end());
std::set<std::string> new_hosts_set(new_hosts.begin(), new_hosts.end());
std::set<std::string> new_only;
std::set_difference(new_hosts_set.begin(), new_hosts_set.end(),
old_hosts_set.begin(), old_hosts_set.end(),
std::inserter(new_only, new_only.end()));
if (new_only.size())
return true;
}
std::set<string16> old_messages =
old_extension->GetSimplePermissionMessages();
std::set<string16> new_messages =
new_extension->GetSimplePermissionMessages();
std::set<string16> new_only;
std::set_difference(new_messages.begin(), new_messages.end(),
old_messages.begin(), old_messages.end(),
std::inserter(new_only, new_only.end()));
// If there are any new permission messages, then it's an increase.
if (!new_only.empty())
return true;
return false;
}
// static
void Extension::DecodeIcon(Extension* extension,
Icons icon_size,
scoped_ptr<SkBitmap>* result) {
FilePath icon_path = extension->GetIconResource(
icon_size, ExtensionIconSet::MATCH_EXACTLY).GetFilePath();
DecodeIconFromPath(icon_path, icon_size, result);
}
// static
void Extension::DecodeIconFromPath(const FilePath& icon_path,
Icons icon_size,
scoped_ptr<SkBitmap>* result) {
ExtensionResource::CheckFileAccessFromFileThread();
if (icon_path.empty())
return;
std::string file_contents;
if (!file_util::ReadFileToString(icon_path, &file_contents)) {
LOG(ERROR) << "Could not read icon file: "
<< WideToUTF8(icon_path.ToWStringHack());
return;
}
// Decode the image using WebKit's image decoder.
const unsigned char* data =
reinterpret_cast<const unsigned char*>(file_contents.data());
webkit_glue::ImageDecoder decoder;
scoped_ptr<SkBitmap> decoded(new SkBitmap());
*decoded = decoder.Decode(data, file_contents.length());
if (decoded->empty()) {
LOG(ERROR) << "Could not decode icon file: "
<< WideToUTF8(icon_path.ToWStringHack());
return;
}
if (decoded->width() != icon_size || decoded->height() != icon_size) {
LOG(ERROR) << "Icon file has unexpected size: "
<< base::IntToString(decoded->width()) << "x"
<< base::IntToString(decoded->height());
return;
}
result->swap(decoded);
}
GURL Extension::GetBaseURLFromExtensionId(const std::string& extension_id) {
return GURL(std::string(chrome::kExtensionScheme) +
chrome::kStandardSchemeSeparator + extension_id + "/");
}
bool Extension::InitFromValue(const DictionaryValue& source, bool require_key,
std::string* error) {
// Unit tests reuse Extension objects, so we need to reset mutable_static_data
// when we re-initialize.
mutable_static_data_ = const_cast<StaticData*>(static_data_.get());
if (source.HasKey(keys::kPublicKey)) {
std::string public_key_bytes;
if (!source.GetString(keys::kPublicKey,
&mutable_static_data_->public_key) ||
!ParsePEMKeyBytes(mutable_static_data_->public_key,
&public_key_bytes) ||
!GenerateId(public_key_bytes, &mutable_static_data_->id)) {
*error = errors::kInvalidKey;
return false;
}
} else if (require_key) {
*error = errors::kInvalidKey;
return false;
} else {
// If there is a path, we generate the ID from it. This is useful for
// development mode, because it keeps the ID stable across restarts and
// reloading the extension.
mutable_static_data_->id = Extension::GenerateIdForPath(path());
if (mutable_static_data_->id.empty()) {
NOTREACHED() << "Could not create ID from path.";
return false;
}
}
// Make a copy of the manifest so we can store it in prefs.
mutable_static_data_->manifest_value.reset(
static_cast<DictionaryValue*>(source.DeepCopy()));
// Initialize the URL.
mutable_static_data_->extension_url =
Extension::GetBaseURLFromExtensionId(id());
// Initialize version.
std::string version_str;
if (!source.GetString(keys::kVersion, &version_str)) {
*error = errors::kInvalidVersion;
return false;
}
mutable_static_data_->version.reset(
Version::GetVersionFromString(version_str));
if (!mutable_static_data_->version.get() ||
mutable_static_data_->version->components().size() > 4) {
*error = errors::kInvalidVersion;
return false;
}
// Initialize name.
string16 localized_name;
if (!source.GetString(keys::kName, &localized_name)) {
*error = errors::kInvalidName;
return false;
}
base::i18n::AdjustStringForLocaleDirection(localized_name, &localized_name);
mutable_static_data_->name = UTF16ToUTF8(localized_name);
// Initialize description (if present).
if (source.HasKey(keys::kDescription)) {
if (!source.GetString(keys::kDescription,
&mutable_static_data_->description)) {
*error = errors::kInvalidDescription;
return false;
}
}
// Initialize update url (if present).
if (source.HasKey(keys::kUpdateURL)) {
std::string tmp;
if (!source.GetString(keys::kUpdateURL, &tmp)) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidUpdateURL, "");
return false;
}
mutable_static_data_->update_url = GURL(tmp);
if (!mutable_static_data_->update_url.is_valid() ||
mutable_static_data_->update_url.has_ref()) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidUpdateURL, tmp);
return false;
}
}
// Validate minimum Chrome version (if present). We don't need to store this,
// since the extension is not valid if it is incorrect.
if (source.HasKey(keys::kMinimumChromeVersion)) {
std::string minimum_version_string;
if (!source.GetString(keys::kMinimumChromeVersion,
&minimum_version_string)) {
*error = errors::kInvalidMinimumChromeVersion;
return false;
}
scoped_ptr<Version> minimum_version(
Version::GetVersionFromString(minimum_version_string));
if (!minimum_version.get()) {
*error = errors::kInvalidMinimumChromeVersion;
return false;
}
chrome::VersionInfo current_version_info;
if (!current_version_info.is_valid()) {
NOTREACHED();
return false;
}
scoped_ptr<Version> current_version(
Version::GetVersionFromString(current_version_info.Version()));
if (!current_version.get()) {
DCHECK(false);
return false;
}
if (current_version->CompareTo(*minimum_version) < 0) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kChromeVersionTooLow,
l10n_util::GetStringUTF8(IDS_PRODUCT_NAME),
minimum_version_string);
return false;
}
}
// Initialize converted_from_user_script (if present)
source.GetBoolean(keys::kConvertedFromUserScript,
&mutable_static_data_->converted_from_user_script);
// Initialize icons (if present).
if (source.HasKey(keys::kIcons)) {
DictionaryValue* icons_value = NULL;
if (!source.GetDictionary(keys::kIcons, &icons_value)) {
*error = errors::kInvalidIcons;
return false;
}
for (size_t i = 0; i < arraysize(kIconSizes); ++i) {
std::string key = base::IntToString(kIconSizes[i]);
if (icons_value->HasKey(key)) {
std::string icon_path;
if (!icons_value->GetString(key, &icon_path)) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidIconPath, key);
return false;
}
if (icon_path.size() > 0 && icon_path[0] == '/')
icon_path = icon_path.substr(1);
if (icon_path.empty()) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidIconPath, key);
return false;
}
mutable_static_data_->icons.Add(kIconSizes[i], icon_path);
}
}
}
// Initialize themes (if present).
mutable_static_data_->is_theme = false;
if (source.HasKey(keys::kTheme)) {
// Themes cannot contain extension keys.
if (ContainsNonThemeKeys(source)) {
*error = errors::kThemesCannotContainExtensions;
return false;
}
DictionaryValue* theme_value;
if (!source.GetDictionary(keys::kTheme, &theme_value)) {
*error = errors::kInvalidTheme;
return false;
}
mutable_static_data_->is_theme = true;
DictionaryValue* images_value;
if (theme_value->GetDictionary(keys::kThemeImages, &images_value)) {
// Validate that the images are all strings
for (DictionaryValue::key_iterator iter = images_value->begin_keys();
iter != images_value->end_keys(); ++iter) {
std::string val;
if (!images_value->GetString(*iter, &val)) {
*error = errors::kInvalidThemeImages;
return false;
}
}
mutable_static_data_->theme_images.reset(
static_cast<DictionaryValue*>(images_value->DeepCopy()));
}
DictionaryValue* colors_value;
if (theme_value->GetDictionary(keys::kThemeColors, &colors_value)) {
// Validate that the colors are RGB or RGBA lists
for (DictionaryValue::key_iterator iter = colors_value->begin_keys();
iter != colors_value->end_keys(); ++iter) {
ListValue* color_list;
double alpha;
int alpha_int;
int color;
// The color must be a list
if (!colors_value->GetListWithoutPathExpansion(*iter, &color_list) ||
// And either 3 items (RGB) or 4 (RGBA)
((color_list->GetSize() != 3) &&
((color_list->GetSize() != 4) ||
// For RGBA, the fourth item must be a real or int alpha value
(!color_list->GetReal(3, &alpha) &&
!color_list->GetInteger(3, &alpha_int)))) ||
// For both RGB and RGBA, the first three items must be ints (R,G,B)
!color_list->GetInteger(0, &color) ||
!color_list->GetInteger(1, &color) ||
!color_list->GetInteger(2, &color)) {
*error = errors::kInvalidThemeColors;
return false;
}
}
mutable_static_data_->theme_colors.reset(
static_cast<DictionaryValue*>(colors_value->DeepCopy()));
}
DictionaryValue* tints_value;
if (theme_value->GetDictionary(keys::kThemeTints, &tints_value)) {
// Validate that the tints are all reals.
for (DictionaryValue::key_iterator iter = tints_value->begin_keys();
iter != tints_value->end_keys(); ++iter) {
ListValue* tint_list;
double v;
int vi;
if (!tints_value->GetListWithoutPathExpansion(*iter, &tint_list) ||
tint_list->GetSize() != 3 ||
!(tint_list->GetReal(0, &v) || tint_list->GetInteger(0, &vi)) ||
!(tint_list->GetReal(1, &v) || tint_list->GetInteger(1, &vi)) ||
!(tint_list->GetReal(2, &v) || tint_list->GetInteger(2, &vi))) {
*error = errors::kInvalidThemeTints;
return false;
}
}
mutable_static_data_->theme_tints.reset(
static_cast<DictionaryValue*>(tints_value->DeepCopy()));
}
DictionaryValue* display_properties_value;
if (theme_value->GetDictionary(keys::kThemeDisplayProperties,
&display_properties_value)) {
mutable_static_data_->theme_display_properties.reset(
static_cast<DictionaryValue*>(display_properties_value->DeepCopy()));
}
return true;
}
// Initialize plugins (optional).
if (source.HasKey(keys::kPlugins)) {
ListValue* list_value;
if (!source.GetList(keys::kPlugins, &list_value)) {
*error = errors::kInvalidPlugins;
return false;
}
#if defined(OS_CHROMEOS)
if (list_value->GetSize() > 0) {
*error = errors::kIllegalPlugins;
return false;
}
#endif
for (size_t i = 0; i < list_value->GetSize(); ++i) {
DictionaryValue* plugin_value;
std::string path_str;
bool is_public = false;
if (!list_value->GetDictionary(i, &plugin_value)) {
*error = errors::kInvalidPlugins;
return false;
}
// Get plugins[i].path.
if (!plugin_value->GetString(keys::kPluginsPath, &path_str)) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidPluginsPath, base::IntToString(i));
return false;
}
// Get plugins[i].content (optional).
if (plugin_value->HasKey(keys::kPluginsPublic)) {
if (!plugin_value->GetBoolean(keys::kPluginsPublic, &is_public)) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidPluginsPublic, base::IntToString(i));
return false;
}
}
mutable_static_data_->plugins.push_back(PluginInfo());
mutable_static_data_->plugins.back().path = path().AppendASCII(path_str);
mutable_static_data_->plugins.back().is_public = is_public;
}
}
// Initialize background url (optional).
if (source.HasKey(keys::kBackground)) {
std::string background_str;
if (!source.GetString(keys::kBackground, &background_str)) {
*error = errors::kInvalidBackground;
return false;
}
mutable_static_data_->background_url = GetResourceURL(background_str);
}
// Initialize toolstrips. This is deprecated for public use.
// NOTE(erikkay) Although deprecated, we intend to preserve this parsing
// code indefinitely. Please contact me or Joi for details as to why.
if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableExperimentalExtensionApis) &&
source.HasKey(keys::kToolstrips)) {
ListValue* list_value;
if (!source.GetList(keys::kToolstrips, &list_value)) {
*error = errors::kInvalidToolstrips;
return false;
}
for (size_t i = 0; i < list_value->GetSize(); ++i) {
GURL toolstrip;
DictionaryValue* toolstrip_value;
std::string toolstrip_path;
if (list_value->GetString(i, &toolstrip_path)) {
// Support a simple URL value for backwards compatibility.
toolstrip = GetResourceURL(toolstrip_path);
} else if (list_value->GetDictionary(i, &toolstrip_value)) {
if (!toolstrip_value->GetString(keys::kToolstripPath,
&toolstrip_path)) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidToolstrip, base::IntToString(i));
return false;
}
toolstrip = GetResourceURL(toolstrip_path);
} else {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidToolstrip, base::IntToString(i));
return false;
}
mutable_static_data_->toolstrips.push_back(toolstrip);
}
}
// Initialize content scripts (optional).
if (source.HasKey(keys::kContentScripts)) {
ListValue* list_value;
if (!source.GetList(keys::kContentScripts, &list_value)) {
*error = errors::kInvalidContentScriptsList;
return false;
}
for (size_t i = 0; i < list_value->GetSize(); ++i) {
DictionaryValue* content_script;
if (!list_value->GetDictionary(i, &content_script)) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidContentScript, base::IntToString(i));
return false;
}
UserScript script;
if (!LoadUserScriptHelper(content_script, i, error, &script))
return false; // Failed to parse script context definition.
script.set_extension_id(id());
if (mutable_static_data_->converted_from_user_script) {
script.set_emulate_greasemonkey(true);
script.set_match_all_frames(true); // Greasemonkey matches all frames.
}
mutable_static_data_->content_scripts.push_back(script);
}
}
// Initialize page action (optional).
DictionaryValue* page_action_value = NULL;
if (source.HasKey(keys::kPageActions)) {
ListValue* list_value;
if (!source.GetList(keys::kPageActions, &list_value)) {
*error = errors::kInvalidPageActionsList;
return false;
}
size_t list_value_length = list_value->GetSize();
if (list_value_length == 0u) {
// A list with zero items is allowed, and is equivalent to not having
// a page_actions key in the manifest. Don't set |page_action_value|.
} else if (list_value_length == 1u) {
if (!list_value->GetDictionary(0, &page_action_value)) {
*error = errors::kInvalidPageAction;
return false;
}
} else { // list_value_length > 1u.
*error = errors::kInvalidPageActionsListSize;
return false;
}
} else if (source.HasKey(keys::kPageAction)) {
if (!source.GetDictionary(keys::kPageAction, &page_action_value)) {
*error = errors::kInvalidPageAction;
return false;
}
}
// If page_action_value is not NULL, then there was a valid page action.
if (page_action_value) {
mutable_static_data_->page_action.reset(
LoadExtensionActionHelper(page_action_value, error));
if (!mutable_static_data_->page_action.get())
return false; // Failed to parse page action definition.
}
// Initialize browser action (optional).
if (source.HasKey(keys::kBrowserAction)) {
DictionaryValue* browser_action_value;
if (!source.GetDictionary(keys::kBrowserAction, &browser_action_value)) {
*error = errors::kInvalidBrowserAction;
return false;
}
mutable_static_data_->browser_action.reset(
LoadExtensionActionHelper(browser_action_value, error));
if (!mutable_static_data_->browser_action.get())
return false; // Failed to parse browser action definition.
}
// Load App settings.
if (!LoadIsApp(mutable_static_data_->manifest_value.get(), error) ||
!LoadExtent(mutable_static_data_->manifest_value.get(), keys::kWebURLs,
&mutable_static_data_->extent,
errors::kInvalidWebURLs, errors::kInvalidWebURL, error) ||
!EnsureNotHybridApp(mutable_static_data_->manifest_value.get(), error) ||
!LoadLaunchURL(mutable_static_data_->manifest_value.get(), error) ||
!LoadLaunchContainer(mutable_static_data_->manifest_value.get(), error)) {
return false;
}
// Initialize options page url (optional).
// Funtion LoadIsApp() set mutable_static_data_->is_app above.
if (source.HasKey(keys::kOptionsPage)) {
std::string options_str;
if (!source.GetString(keys::kOptionsPage, &options_str)) {
*error = errors::kInvalidOptionsPage;
return false;
}
if (is_hosted_app()) {
// hosted apps require an absolute URL.
GURL options_url(options_str);
if (!options_url.is_valid() ||
!(options_url.SchemeIs("http") || options_url.SchemeIs("https"))) {
*error = errors::kInvalidOptionsPageInHostedApp;
return false;
}
mutable_static_data_->options_url = options_url;
} else {
GURL absolute(options_str);
if (absolute.is_valid()) {
*error = errors::kInvalidOptionsPageExpectUrlInPackage;
return false;
}
mutable_static_data_->options_url = GetResourceURL(options_str);
if (!mutable_static_data_->options_url.is_valid()) {
*error = errors::kInvalidOptionsPage;
return false;
}
}
}
// Initialize the permissions (optional).
if (source.HasKey(keys::kPermissions)) {
ListValue* permissions = NULL;
if (!source.GetList(keys::kPermissions, &permissions)) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidPermissions, "");
return false;
}
for (size_t i = 0; i < permissions->GetSize(); ++i) {
std::string permission_str;
if (!permissions->GetString(i, &permission_str)) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidPermission, base::IntToString(i));
return false;
}
// Only COMPONENT extensions can use the webstorePrivate APIs.
// TODO(asargent) - We want a more general purpose mechanism for this,
// and better error messages. (http://crbug.com/54013)
if (permission_str == kWebstorePrivatePermission &&
mutable_static_data_->location != Extension::COMPONENT) {
continue;
}
// Remap the old unlimited storage permission name.
if (permission_str == kOldUnlimitedStoragePermission)
permission_str = kUnlimitedStoragePermission;
if (web_extent().is_empty() || location() == Extension::COMPONENT) {
// Check if it's a module permission. If so, enable that permission.
if (IsAPIPermission(permission_str)) {
mutable_static_data_->api_permissions.insert(permission_str);
continue;
}
} else {
// Hosted apps only get access to a subset of the valid permissions.
if (IsHostedAppPermission(permission_str)) {
mutable_static_data_->api_permissions.insert(permission_str);
continue;
}
}
// Otherwise, it's a host pattern permission.
URLPattern pattern = URLPattern(CanExecuteScriptEverywhere() ?
URLPattern::SCHEME_ALL :
(UserScript::kValidUserScriptSchemes |
URLPattern::SCHEME_CHROMEUI) & ~URLPattern::SCHEME_FILE);
if (!pattern.Parse(permission_str)) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidPermission, base::IntToString(i));
return false;
}
if (!CanSpecifyHostPermission(pattern)) {
*error = ExtensionErrorUtils::FormatErrorMessage(
errors::kInvalidPermissionScheme, base::IntToString(i));
return false;
}
// The path component is not used for host permissions, so we force it to
// match all paths.
pattern.set_path("/*");
mutable_static_data_->host_permissions.push_back(pattern);
}
}
if (source.HasKey(keys::kDefaultLocale)) {
if (!source.GetString(keys::kDefaultLocale,
&mutable_static_data_->default_locale) ||
mutable_static_data_->default_locale.empty()) {
*error = errors::kInvalidDefaultLocale;
return false;
}
}
// Chrome URL overrides (optional)
if (source.HasKey(keys::kChromeURLOverrides)) {
DictionaryValue* overrides;
if (!source.GetDictionary(keys::kChromeURLOverrides, &overrides)) {
*error = errors::kInvalidChromeURLOverrides;
return false;
}
// Validate that the overrides are all strings
for (DictionaryValue::key_iterator iter = overrides->begin_keys();
iter != overrides->end_keys(); ++iter) {
std::string page = *iter;
std::string val;
// Restrict override pages to a list of supported URLs.
if ((page != chrome::kChromeUINewTabHost &&
#if defined(TOUCH_UI)
page != chrome::kChromeUIKeyboardHost &&
#endif
page != chrome::kChromeUIBookmarksHost &&
page != chrome::kChromeUIHistoryHost) ||
!overrides->GetStringWithoutPathExpansion(*iter, &val)) {
*error = errors::kInvalidChromeURLOverrides;
return false;
}
// Replace the entry with a fully qualified chrome-extension:// URL.
mutable_static_data_->chrome_url_overrides[page] = GetResourceURL(val);
}
// An extension may override at most one page.
if (overrides->size() > 1) {
*error = errors::kMultipleOverrides;
return false;
}
}
if (source.HasKey(keys::kOmniboxKeyword)) {
if (!source.GetString(keys::kOmniboxKeyword,
&mutable_static_data_->omnibox_keyword) ||
mutable_static_data_->omnibox_keyword.empty()) {
*error = errors::kInvalidOmniboxKeyword;
return false;
}
if (!HasApiPermission(Extension::kExperimentalPermission)) {
*error = errors::kOmniboxExperimental;
return false;
}
}
// Initialize devtools page url (optional).
if (source.HasKey(keys::kDevToolsPage)) {
std::string devtools_str;
if (!source.GetString(keys::kDevToolsPage, &devtools_str)) {
*error = errors::kInvalidDevToolsPage;
return false;
}
if (!HasApiPermission(Extension::kExperimentalPermission)) {
*error = errors::kDevToolsExperimental;
return false;
}
mutable_static_data_->devtools_url = GetResourceURL(devtools_str);
}
// Initialize incognito behavior. Apps default to split mode, extensions
// default to spanning.
mutable_static_data_->incognito_split_mode = is_app();
if (source.HasKey(keys::kIncognito)) {
std::string value;
if (!source.GetString(keys::kIncognito, &value)) {
*error = errors::kInvalidIncognitoBehavior;
return false;
}
if (value == values::kIncognitoSpanning) {
mutable_static_data_->incognito_split_mode = false;
} else if (value == values::kIncognitoSplit) {
mutable_static_data_->incognito_split_mode = true;
} else {
*error = errors::kInvalidIncognitoBehavior;
return false;
}
}
if (HasMultipleUISurfaces()) {
*error = errors::kOneUISurfaceOnly;
return false;
}
InitEffectiveHostPermissions();
// Although |source| is passed in as a const, it's still possible to modify
// it. This is dangerous since the utility process re-uses |source| after
// it calls InitFromValue, passing it up to the browser process which calls
// InitFromValue again. As a result, we need to make sure that nobody
// accidentally modifies it.
DCHECK(source.Equals(mutable_static_data_->manifest_value.get()));
// Ensure we can't modify our static data anymore.
mutable_static_data_ = NULL;
return true;
}
// static
std::string Extension::ChromeStoreLaunchURL() {
std::string gallery_prefix = extension_urls::kGalleryBrowsePrefix;
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAppsGalleryURL))
gallery_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kAppsGalleryURL);
if (EndsWith(gallery_prefix, "/", true))
gallery_prefix = gallery_prefix.substr(0, gallery_prefix.length() - 1);
return gallery_prefix;
}
GURL Extension::GalleryUrl() const {
if (!update_url().DomainIs("google.com"))
return GURL();
// TODO(erikkay): This may not be entirely correct with the webstore.
// I think it will have a mixture of /extensions/detail and /webstore/detail
// URLs. Perhaps they'll handle this nicely with redirects?
GURL url(ChromeStoreLaunchURL() + std::string("/detail/") + id());
return url;
}
std::set<FilePath> Extension::GetBrowserImages() {
std::set<FilePath> image_paths;
// TODO(viettrungluu): These |FilePath::FromWStringHack(UTF8ToWide())|
// indicate that we're doing something wrong.
// Extension icons.
for (ExtensionIconSet::IconMap::const_iterator iter = icons().map().begin();
iter != icons().map().end(); ++iter) {
image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(iter->second)));
}
// Theme images.
DictionaryValue* theme_images = GetThemeImages();
if (theme_images) {
for (DictionaryValue::key_iterator it = theme_images->begin_keys();
it != theme_images->end_keys(); ++it) {
std::string val;
if (theme_images->GetStringWithoutPathExpansion(*it, &val))
image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(val)));
}
}
// Page action icons.
if (page_action()) {
std::vector<std::string>* icon_paths = page_action()->icon_paths();
for (std::vector<std::string>::iterator iter = icon_paths->begin();
iter != icon_paths->end(); ++iter) {
image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(*iter)));
}
}
// Browser action icons.
if (browser_action()) {
std::vector<std::string>* icon_paths = browser_action()->icon_paths();
for (std::vector<std::string>::iterator iter = icon_paths->begin();
iter != icon_paths->end(); ++iter) {
image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(*iter)));
}
}
return image_paths;
}
GURL Extension::GetFullLaunchURL() const {
if (!launch_local_path().empty())
return url().Resolve(launch_local_path());
else
return GURL(launch_web_url());
}
bool Extension::GetBackgroundPageReady() {
return (GetRuntimeData()->background_page_ready ||
background_url().is_empty());
}
void Extension::SetBackgroundPageReady() {
DCHECK(!background_url().is_empty());
GetRuntimeData()->background_page_ready = true;
NotificationService::current()->Notify(
NotificationType::EXTENSION_BACKGROUND_PAGE_READY,
Source<Extension>(this),
NotificationService::NoDetails());
}
static std::string SizeToString(const gfx::Size& max_size) {
return base::IntToString(max_size.width()) + "x" +
base::IntToString(max_size.height());
}
// static
void Extension::SetScriptingWhitelist(
const std::vector<std::string>& whitelist) {
ScriptingWhitelist* current_whitelist =
ExtensionConfig::GetSingleton()->whitelist();
current_whitelist->clear();
for (ScriptingWhitelist::const_iterator it = whitelist.begin();
it != whitelist.end(); ++it) {
current_whitelist->push_back(*it);
}
}
void Extension::SetCachedImage(const ExtensionResource& source,
const SkBitmap& image,
const gfx::Size& original_size) {
DCHECK(source.extension_root() == path()); // The resource must come from
// this extension.
const FilePath& path = source.relative_path();
gfx::Size actual_size(image.width(), image.height());
if (actual_size == original_size) {
GetRuntimeData()->image_cache_[
RuntimeData::ImageCacheKey(path, std::string())] = image;
} else {
GetRuntimeData()->image_cache_[
RuntimeData::ImageCacheKey(path, SizeToString(actual_size))] = image;
}
}
bool Extension::HasCachedImage(const ExtensionResource& source,
const gfx::Size& max_size) {
DCHECK(source.extension_root() == path()); // The resource must come from
// this extension.
return GetCachedImageImpl(source, max_size) != NULL;
}
SkBitmap Extension::GetCachedImage(const ExtensionResource& source,
const gfx::Size& max_size) {
DCHECK(source.extension_root() == path()); // The resource must come from
// this extension.
SkBitmap* image = GetCachedImageImpl(source, max_size);
return image ? *image : SkBitmap();
}
SkBitmap* Extension::GetCachedImageImpl(const ExtensionResource& source,
const gfx::Size& max_size) {
const FilePath& path = source.relative_path();
// Look for exact size match.
RuntimeData::ImageCache::iterator i = GetRuntimeData()->image_cache_.find(
RuntimeData::ImageCacheKey(path, SizeToString(max_size)));
if (i != GetRuntimeData()->image_cache_.end())
return &(i->second);
// If we have the original size version cached, return that if it's small
// enough.
i = GetRuntimeData()->image_cache_.find(
RuntimeData::ImageCacheKey(path, std::string()));
if (i != GetRuntimeData()->image_cache_.end()) {
SkBitmap& image = i->second;
if (image.width() <= max_size.width() &&
image.height() <= max_size.height())
return &(i->second);
}
return NULL;
}
ExtensionResource Extension::GetIconResource(
int size, ExtensionIconSet::MatchType match_type) {
std::string path = icons().Get(size, match_type);
if (path.empty())
return ExtensionResource();
return GetResource(path);
}
GURL Extension::GetIconURL(int size, ExtensionIconSet::MatchType match_type) {
std::string path = icons().Get(size, match_type);
if (path.empty())
return GURL();
else
return GetResourceURL(path);
}
bool Extension::CanSpecifyHostPermission(const URLPattern pattern) const {
if (!pattern.match_all_urls() &&
pattern.MatchesScheme(chrome::kChromeUIScheme)) {
// Only allow access to chrome://favicon to regular extensions. Component
// extensions can have access to all of chrome://*.
return (pattern.host() == chrome::kChromeUIFavIconHost ||
CanExecuteScriptEverywhere());
}
// Otherwise, the valid schemes were handled by URLPattern.
return true;
}
// static.
bool Extension::HasApiPermission(
const std::set<std::string>& api_permissions,
const std::string& function_name) {
std::string permission_name = function_name;
for (size_t i = 0; i < kNumNonPermissionFunctionNames; ++i) {
if (permission_name == kNonPermissionFunctionNames[i])
return true;
}
// See if this is a function or event name first and strip out the package.
// Functions will be of the form package.function
// Events will be of the form package/id or package.optional.stuff
size_t separator = function_name.find_first_of("./");
if (separator != std::string::npos)
permission_name = function_name.substr(0, separator);
// windows and tabs are the same permission.
if (permission_name == kWindowPermission)
permission_name = Extension::kTabPermission;
if (api_permissions.count(permission_name))
return true;
for (size_t i = 0; i < kNumNonPermissionModuleNames; ++i) {
if (permission_name == kNonPermissionModuleNames[i]) {
return true;
}
}
return false;
}
bool Extension::HasHostPermission(const GURL& url) const {
for (URLPatternList::const_iterator host = host_permissions().begin();
host != host_permissions().end(); ++host) {
if (host->MatchesUrl(url))
return true;
}
return false;
}
void Extension::InitEffectiveHostPermissions() {
for (URLPatternList::const_iterator host = host_permissions().begin();
host != host_permissions().end(); ++host)
mutable_static_data_->effective_host_permissions.AddPattern(*host);
for (UserScriptList::const_iterator content_script =
content_scripts().begin();
content_script != content_scripts().end(); ++content_script) {
UserScript::PatternList::const_iterator pattern =
content_script->url_patterns().begin();
for (; pattern != content_script->url_patterns().end(); ++pattern)
mutable_static_data_->effective_host_permissions.AddPattern(*pattern);
}
}
bool Extension::HasMultipleUISurfaces() const {
int num_surfaces = 0;
if (page_action())
++num_surfaces;
if (browser_action())
++num_surfaces;
if (is_app())
++num_surfaces;
return num_surfaces > 1;
}
// static
bool Extension::CanExecuteScriptOnPage(
const GURL& page_url, bool can_execute_script_everywhere,
const std::vector<URLPattern>* host_permissions,
UserScript* script,
std::string* error) {
DCHECK(!(host_permissions && script)) << "Shouldn't specify both";
// The gallery is special-cased as a restricted URL for scripting to prevent
// access to special JS bindings we expose to the gallery (and avoid things
// like extensions removing the "report abuse" link).
// TODO(erikkay): This seems like the wrong test. Shouldn't we we testing
// against the store app extent?
if ((page_url.host() == GURL(Extension::ChromeStoreLaunchURL()).host()) &&
!can_execute_script_everywhere &&
!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kAllowScriptingGallery)) {
if (error)
*error = errors::kCannotScriptGallery;
return false;
}
if (host_permissions) {
for (size_t i = 0; i < host_permissions->size(); ++i) {
if ((*host_permissions)[i].MatchesUrl(page_url))
return true;
}
}
if (script) {
if (script->MatchesUrl(page_url))
return true;
}
if (error) {
*error = ExtensionErrorUtils::FormatErrorMessage(errors::kCannotAccessPage,
page_url.spec());
}
return false;
}
bool Extension::HasEffectiveAccessToAllHosts() const {
// Some APIs effectively grant access to every site. New ones should be
// added here. (I'm looking at you, network API)
if (HasApiPermission(kProxyPermission))
return true;
for (URLPatternList::const_iterator host = host_permissions().begin();
host != host_permissions().end(); ++host) {
if (host->match_subdomains() && host->host().empty())
return true;
}
for (UserScriptList::const_iterator content_script =
content_scripts().begin();
content_script != content_scripts().end(); ++content_script) {
UserScript::PatternList::const_iterator pattern =
content_script->url_patterns().begin();
for (; pattern != content_script->url_patterns().end(); ++pattern) {
if (pattern->match_subdomains() && pattern->host().empty())
return true;
}
}
return false;
}
bool Extension::IsAPIPermission(const std::string& str) {
for (size_t i = 0; i < Extension::kNumPermissions; ++i) {
if (str == Extension::kPermissions[i].name) {
// Only allow the experimental API permission if the command line
// flag is present, or if the extension is a component of Chrome.
if (str == Extension::kExperimentalPermission) {
if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableExperimentalExtensionApis)) {
return true;
} else if (location() == Extension::COMPONENT) {
return true;
} else {
return false;
}
} else {
return true;
}
}
}
return false;
}
bool Extension::CanExecuteScriptEverywhere() const {
if (location() == Extension::COMPONENT)
return true;
ScriptingWhitelist* whitelist =
ExtensionConfig::GetSingleton()->whitelist();
for (ScriptingWhitelist::const_iterator it = whitelist->begin();
it != whitelist->end(); ++it) {
if (id() == *it) {
return true;
}
}
return false;
}
Extension::RuntimeData* Extension::GetRuntimeData() const {
// TODO(mpcomplete): it would be nice if I could verify we were on the UI
// thread, but we're in common and don't have access to BrowserThread.
return const_cast<Extension::RuntimeData*>(runtime_data_.get());
}
ExtensionInfo::ExtensionInfo(const DictionaryValue* manifest,
const std::string& id,
const FilePath& path,
Extension::Location location)
: extension_id(id),
extension_path(path),
extension_location(location) {
if (manifest)
extension_manifest.reset(
static_cast<DictionaryValue*>(manifest->DeepCopy()));
}
ExtensionInfo::~ExtensionInfo() {}
UninstalledExtensionInfo::UninstalledExtensionInfo(
const Extension& extension)
: extension_id(extension.id()),
extension_api_permissions(extension.api_permissions()),
is_theme(extension.is_theme()),
is_app(extension.is_app()),
converted_from_user_script(extension.converted_from_user_script()),
update_url(extension.update_url()) {}
UninstalledExtensionInfo::~UninstalledExtensionInfo() {}
|