Bromite-AdBlockUpdaterService.patch 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  1. From: csagan5 <32685696+csagan5@users.noreply.github.com>
  2. Date: Sat, 14 Sep 2019 10:20:08 +0200
  3. Subject: Bromite AdBlockUpdaterService
  4. Add option to configure the ad blocker filters URL
  5. Disable look-alike, metrics, ablation and navigation throttles
  6. Do not use experiments to enable/disable presets
  7. Always enable ad filtering
  8. Download filters by checking Last-Modified header first
  9. Fix RestoreForeignSessionTab by recreating the tab (issue #681)
  10. ---
  11. chrome/android/chrome_java_resources.gni | 2 +
  12. chrome/android/chrome_java_sources.gni | 2 +
  13. .../java/res/layout/adblock_editor.xml | 67 +++++
  14. chrome/android/java/res/values/styles.xml | 18 ++
  15. chrome/android/java/res/values/values.xml | 2 +
  16. .../java/res/xml/adblock_preferences.xml | 25 ++
  17. .../android/java/res/xml/main_preferences.xml | 5 +
  18. .../browser/settings/AdBlockEditor.java | 92 ++++++
  19. .../browser/settings/AdBlockPreferences.java | 61 ++++
  20. .../chrome/browser/tabmodel/TabModelImpl.java | 2 +-
  21. chrome/app/generated_resources.grd | 10 +
  22. chrome/browser/after_startup_task_utils.cc | 5 +
  23. chrome/browser/browser_process.h | 6 +
  24. chrome/browser/browser_process_impl.cc | 20 ++
  25. chrome/browser/browser_process_impl.h | 2 +
  26. chrome/browser/chrome_browser_main.cc | 2 +
  27. .../browser/chrome_content_browser_client.cc | 15 -
  28. .../flags/android/cached_feature_flags.cc | 11 +
  29. .../browser/flags/CachedFeatureFlags.java | 10 +
  30. .../net/system_network_context_manager.cc | 4 +
  31. .../sessions/session_restore_android.cc | 4 +-
  32. .../strings/android_chrome_strings.grd | 14 +
  33. chrome/common/pref_names.cc | 3 +
  34. chrome/common/pref_names.h | 1 +
  35. components/component_updater/BUILD.gn | 7 +
  36. .../adblock_updater_service.cc | 272 ++++++++++++++++++
  37. .../adblock_updater_service.h | 99 +++++++
  38. .../download_filters_task.cc | 224 +++++++++++++++
  39. .../component_updater/download_filters_task.h | 129 +++++++++
  40. ...ent_subresource_filter_throttle_manager.cc | 11 +
  41. .../content/browser/ruleset_service.cc | 33 ++-
  42. .../content/browser/ruleset_service.h | 7 +-
  43. .../content/browser/ruleset_version.h | 4 +
  44. .../browser/verified_ruleset_dealer.cc | 4 +
  45. .../browser/subresource_filter_features.cc | 113 +-------
  46. .../core/common/indexed_ruleset.cc | 5 +-
  47. .../navigation_throttle_runner.cc | 5 -
  48. 37 files changed, 1157 insertions(+), 139 deletions(-)
  49. create mode 100644 chrome/android/java/res/layout/adblock_editor.xml
  50. create mode 100644 chrome/android/java/res/xml/adblock_preferences.xml
  51. create mode 100644 chrome/android/java/src/org/chromium/chrome/browser/settings/AdBlockEditor.java
  52. create mode 100644 chrome/android/java/src/org/chromium/chrome/browser/settings/AdBlockPreferences.java
  53. create mode 100644 components/component_updater/adblock_updater_service.cc
  54. create mode 100644 components/component_updater/adblock_updater_service.h
  55. create mode 100644 components/component_updater/download_filters_task.cc
  56. create mode 100644 components/component_updater/download_filters_task.h
  57. diff --git a/chrome/android/chrome_java_resources.gni b/chrome/android/chrome_java_resources.gni
  58. --- a/chrome/android/chrome_java_resources.gni
  59. +++ b/chrome/android/chrome_java_resources.gni
  60. @@ -557,6 +557,7 @@ chrome_java_resources = [
  61. "java/res/layout/account_chooser_dialog_title.xml",
  62. "java/res/layout/account_divider_preference.xml",
  63. "java/res/layout/account_management_account_row.xml",
  64. + "java/res/layout/adblock_editor.xml",
  65. "java/res/layout/auto_sign_in_first_run_dialog.xml",
  66. "java/res/layout/autofill_billing_address_dropdown.xml",
  67. "java/res/layout/autofill_card_unmask_prompt.xml",
  68. @@ -774,6 +775,7 @@ chrome_java_resources = [
  69. "java/res/xml/about_chrome_preferences.xml",
  70. "java/res/xml/accessibility_preferences.xml",
  71. "java/res/xml/account_management_preferences.xml",
  72. + "java/res/xml/adblock_preferences.xml",
  73. "java/res/xml/autofill_assistant_preferences.xml",
  74. "java/res/xml/bookmark_widget_info.xml",
  75. "java/res/xml/clear_browsing_data_preferences_tab.xml",
  76. diff --git a/chrome/android/chrome_java_sources.gni b/chrome/android/chrome_java_sources.gni
  77. --- a/chrome/android/chrome_java_sources.gni
  78. +++ b/chrome/android/chrome_java_sources.gni
  79. @@ -986,6 +986,8 @@ chrome_java_sources = [
  80. "java/src/org/chromium/chrome/browser/permissions/PermissionSettingsBridge.java",
  81. "java/src/org/chromium/chrome/browser/permissions/PermissionUpdateRequester.java",
  82. "java/src/org/chromium/chrome/browser/photo_picker/DecoderServiceImpl.java",
  83. + "java/src/org/chromium/chrome/browser/settings/AdBlockEditor.java",
  84. + "java/src/org/chromium/chrome/browser/settings/AdBlockPreferences.java",
  85. "java/src/org/chromium/chrome/browser/policy/PolicyAuditor.java",
  86. "java/src/org/chromium/chrome/browser/prerender/ChromePrerenderServiceImpl.java",
  87. "java/src/org/chromium/chrome/browser/previews/HttpsImageCompressionUtils.java",
  88. diff --git a/chrome/android/java/res/layout/adblock_editor.xml b/chrome/android/java/res/layout/adblock_editor.xml
  89. new file mode 100644
  90. --- /dev/null
  91. +++ b/chrome/android/java/res/layout/adblock_editor.xml
  92. @@ -0,0 +1,67 @@
  93. +<?xml version="1.0" encoding="utf-8"?>
  94. +<!-- Copyright 2015 The Chromium Authors. All rights reserved.
  95. + Use of this source code is governed by a BSD-style license that can be
  96. + found in the LICENSE file. -->
  97. +
  98. +<FrameLayout
  99. + xmlns:android="http://schemas.android.com/apk/res/android"
  100. + xmlns:app="http://schemas.android.com/apk/res-auto"
  101. + xmlns:tools="http://schemas.android.com/tools"
  102. + android:layout_width="match_parent"
  103. + android:layout_height="match_parent" >
  104. +
  105. + <ScrollView
  106. + android:layout_width="match_parent"
  107. + android:layout_height="match_parent"
  108. + android:id="@+id/scroll_view"
  109. + android:fillViewport="true" >
  110. +
  111. + <LinearLayout
  112. + android:layout_width="match_parent"
  113. + android:layout_height="wrap_content"
  114. + android:orientation="vertical"
  115. + android:focusableInTouchMode="true" >
  116. +
  117. + <com.google.android.material.textfield.TextInputLayout
  118. + android:id="@+id/adblock_url"
  119. + android:paddingTop="16dp"
  120. + android:paddingStart="@dimen/pref_autofill_content_spacing"
  121. + android:paddingEnd="@dimen/pref_autofill_content_spacing"
  122. + android:layout_width="match_parent"
  123. + android:layout_height="wrap_content" >
  124. + <!-- TODO(crbug.com/900912): Fix and remove lint ignore -->
  125. + <EditText
  126. + tools:ignore="Autofill"
  127. + android:id="@+id/adblock_url_edit"
  128. + android:layout_width="match_parent"
  129. + android:layout_height="wrap_content"
  130. + android:inputType="textUri"
  131. + android:singleLine="true"
  132. + android:hint="@string/options_adblock_edit_label" />
  133. + </com.google.android.material.textfield.TextInputLayout>
  134. +
  135. + <Space style="@style/ButtonBarTopSpacer" />
  136. + <View style="@style/ButtonBarTopDivider" />
  137. +
  138. + <LinearLayout style="@style/ButtonBar" >
  139. + <org.chromium.ui.widget.ButtonCompat
  140. + android:id="@+id/adblock_reset"
  141. + style="@style/ButtonBarButton"
  142. + android:text="@string/reset" />
  143. +
  144. + <org.chromium.ui.widget.ButtonCompat
  145. + android:id="@+id/adblock_cancel"
  146. + style="@style/ButtonBarButton"
  147. + android:text="@string/cancel" />
  148. +
  149. + <org.chromium.ui.widget.ButtonCompat
  150. + android:id="@+id/adblock_save"
  151. + style="@style/ButtonBarButton"
  152. + android:text="@string/save" />
  153. + </LinearLayout>
  154. + </LinearLayout>
  155. + </ScrollView>
  156. +
  157. + <include layout="@layout/settings_action_bar_shadow"/>
  158. +
  159. +</FrameLayout>
  160. diff --git a/chrome/android/java/res/values/styles.xml b/chrome/android/java/res/values/styles.xml
  161. --- a/chrome/android/java/res/values/styles.xml
  162. +++ b/chrome/android/java/res/values/styles.xml
  163. @@ -198,6 +198,24 @@
  164. </item>
  165. </style>
  166. + <style name="ButtonBarTopSpacer" parent="ButtonBarTopSpacerLight">
  167. + <item name="android:minHeight">5dp</item>
  168. + </style>
  169. + <style name="ButtonBar" parent="@android:style/Holo.Light.ButtonBar.AlertDialog">
  170. + <item name="android:layout_width">match_parent</item>
  171. + <item name="android:layout_height">wrap_content</item>
  172. + <item name="android:divider">?android:attr/dividerVertical</item>
  173. + <item name="android:dividerPadding">0dp</item>
  174. + <item name="android:showDividers">middle</item>
  175. + </style>
  176. + <style name="ButtonBarButton" parent="@style/TextButton">
  177. + <item name="android:layout_width">0dp</item>
  178. + <item name="android:layout_height">match_parent</item>
  179. + <item name="android:layout_weight">1</item>
  180. + <item name="android:paddingStart">4dp</item>
  181. + <item name="android:paddingEnd">4dp</item>
  182. + </style>
  183. +
  184. <!-- Button bar styles -->
  185. <style name="ButtonBarTopSpacerLight">
  186. <item name="android:layout_width">0dp</item>
  187. diff --git a/chrome/android/java/res/values/values.xml b/chrome/android/java/res/values/values.xml
  188. --- a/chrome/android/java/res/values/values.xml
  189. +++ b/chrome/android/java/res/values/values.xml
  190. @@ -25,6 +25,8 @@
  191. <!-- Compositor Tab Title Text -->
  192. <bool name="compositor_tab_title_fake_bold_text">true</bool>
  193. + <string name="adblock_help_url">https://www.bromite.org/custom-filters</string>
  194. +
  195. <string name="proxy_title">Proxy configuration</string>
  196. <string name="proxy_url">chrome://proxy</string>
  197. diff --git a/chrome/android/java/res/xml/adblock_preferences.xml b/chrome/android/java/res/xml/adblock_preferences.xml
  198. new file mode 100644
  199. --- /dev/null
  200. +++ b/chrome/android/java/res/xml/adblock_preferences.xml
  201. @@ -0,0 +1,25 @@
  202. +<?xml version="1.0" encoding="utf-8"?>
  203. +<!-- Copyright 2015 The Chromium Authors. All rights reserved.
  204. + Use of this source code is governed by a BSD-style license that can be
  205. + found in the LICENSE file. -->
  206. +
  207. +<PreferenceScreen
  208. + xmlns:android="http://schemas.android.com/apk/res/android"
  209. + xmlns:app="http://schemas.android.com/apk/res-auto">
  210. +
  211. + <org.chromium.components.browser_ui.settings.ChromeSwitchPreference
  212. + android:key="adblock_switch"
  213. + android:summaryOn="@string/text_on"
  214. + android:summaryOff="@string/text_off" />
  215. +
  216. + <org.chromium.chrome.browser.about_settings.HyperlinkPreference
  217. + android:key="adblock_help"
  218. + android:title="@string/adblock_help"
  219. + app:url="@string/adblock_help_url" />
  220. +
  221. + <Preference
  222. + android:key="adblock_edit"
  223. + android:title="@string/options_adblock_edit_label"
  224. + android:fragment="org.chromium.chrome.browser.settings.AdBlockEditor" />
  225. +
  226. +</PreferenceScreen>
  227. diff --git a/chrome/android/java/res/xml/main_preferences.xml b/chrome/android/java/res/xml/main_preferences.xml
  228. --- a/chrome/android/java/res/xml/main_preferences.xml
  229. +++ b/chrome/android/java/res/xml/main_preferences.xml
  230. @@ -42,6 +42,11 @@
  231. android:key="privacy"
  232. android:order="10"
  233. android:title="@string/prefs_privacy_security"/>
  234. + <Preference
  235. + android:fragment="org.chromium.chrome.browser.settings.AdBlockPreferences"
  236. + android:key="adblock"
  237. + android:order="13"
  238. + android:title="@string/prefs_adblock"/>
  239. <Preference
  240. android:key="notifications"
  241. android:order="12"
  242. diff --git a/chrome/android/java/src/org/chromium/chrome/browser/settings/AdBlockEditor.java b/chrome/android/java/src/org/chromium/chrome/browser/settings/AdBlockEditor.java
  243. new file mode 100644
  244. --- /dev/null
  245. +++ b/chrome/android/java/src/org/chromium/chrome/browser/settings/AdBlockEditor.java
  246. @@ -0,0 +1,92 @@
  247. +// Copyright 2015 The Chromium Authors. All rights reserved.
  248. +// Use of this source code is governed by a BSD-style license that can be
  249. +// found in the LICENSE file.
  250. +
  251. +package org.chromium.chrome.browser.settings;
  252. +
  253. +import android.os.Bundle;
  254. +import androidx.fragment.app.Fragment;
  255. +import android.text.Editable;
  256. +import android.text.TextWatcher;
  257. +import android.view.LayoutInflater;
  258. +import android.view.View;
  259. +import android.view.ViewGroup;
  260. +import android.widget.Button;
  261. +import android.widget.EditText;
  262. +
  263. +import org.chromium.components.browser_ui.settings.SettingsUtils;
  264. +import org.chromium.chrome.browser.flags.CachedFeatureFlags;
  265. +import org.chromium.chrome.R;
  266. +import org.chromium.components.url_formatter.UrlFormatter;
  267. +
  268. +/**
  269. + * Provides the Java-UI for editing AdBlock preferences.
  270. + */
  271. +public class AdBlockEditor extends Fragment implements TextWatcher {
  272. + private EditText mAdBlockFiltersUrlEdit;
  273. + private Button mSaveButton;
  274. + private Button mResetButton;
  275. +
  276. + @Override
  277. + public View onCreateView(LayoutInflater inflater, ViewGroup container,
  278. + Bundle savedInstanceState) {
  279. + super.onCreate(savedInstanceState);
  280. + getActivity().setTitle(R.string.options_adblock_edit_title);
  281. +
  282. + View v = inflater.inflate(R.layout.adblock_editor, container, false);
  283. + View scrollView = v.findViewById(R.id.scroll_view);
  284. + scrollView.getViewTreeObserver().addOnScrollChangedListener(
  285. + SettingsUtils.getShowShadowOnScrollListener(v, v.findViewById(R.id.shadow)));
  286. + mAdBlockFiltersUrlEdit = (EditText) v.findViewById(R.id.adblock_url_edit);
  287. + mAdBlockFiltersUrlEdit.setText(CachedFeatureFlags.getAdBlockFiltersURL());
  288. + mAdBlockFiltersUrlEdit.addTextChangedListener(this);
  289. + mAdBlockFiltersUrlEdit.requestFocus();
  290. +
  291. + initializeSaveCancelResetButtons(v);
  292. + return v;
  293. + }
  294. +
  295. + @Override
  296. + public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  297. + }
  298. +
  299. + @Override
  300. + public void onTextChanged(CharSequence s, int start, int before, int count) {
  301. + mSaveButton.setEnabled(s.length() != 0);
  302. + mResetButton.setEnabled(true);
  303. + }
  304. +
  305. + @Override
  306. + public void afterTextChanged(Editable s) {
  307. + }
  308. +
  309. + private void initializeSaveCancelResetButtons(View v) {
  310. + mResetButton = (Button) v.findViewById(R.id.adblock_reset);
  311. + mResetButton.setOnClickListener(new View.OnClickListener() {
  312. + @Override
  313. + public void onClick(View v) {
  314. + mAdBlockFiltersUrlEdit.setText(CachedFeatureFlags.getAdBlockFiltersURL());
  315. + getActivity().finish();
  316. + }
  317. + });
  318. +
  319. + mSaveButton = (Button) v.findViewById(R.id.adblock_save);
  320. + mSaveButton.setEnabled(false);
  321. + mSaveButton.setOnClickListener(new View.OnClickListener() {
  322. + @Override
  323. + public void onClick(View v) {
  324. + CachedFeatureFlags.setAdBlockFiltersURL(
  325. + UrlFormatter.fixupUrl(mAdBlockFiltersUrlEdit.getText().toString()).getSpec());
  326. + getActivity().finish();
  327. + }
  328. + });
  329. +
  330. + Button button = (Button) v.findViewById(R.id.adblock_cancel);
  331. + button.setOnClickListener(new View.OnClickListener() {
  332. + @Override
  333. + public void onClick(View v) {
  334. + getActivity().finish();
  335. + }
  336. + });
  337. + }
  338. +}
  339. diff --git a/chrome/android/java/src/org/chromium/chrome/browser/settings/AdBlockPreferences.java b/chrome/android/java/src/org/chromium/chrome/browser/settings/AdBlockPreferences.java
  340. new file mode 100644
  341. --- /dev/null
  342. +++ b/chrome/android/java/src/org/chromium/chrome/browser/settings/AdBlockPreferences.java
  343. @@ -0,0 +1,61 @@
  344. +// Copyright 2015 The Chromium Authors. All rights reserved.
  345. +// Use of this source code is governed by a BSD-style license that can be
  346. +// found in the LICENSE file.
  347. +
  348. +package org.chromium.chrome.browser.settings;
  349. +
  350. +import android.os.Bundle;
  351. +import androidx.preference.Preference;
  352. +import androidx.preference.PreferenceFragmentCompat;
  353. +import org.chromium.components.browser_ui.settings.ChromeSwitchPreference;
  354. +
  355. +import org.chromium.content_public.browser.BrowserContextHandle;
  356. +import org.chromium.components.browser_ui.site_settings.SiteSettingsPreferenceFragment;
  357. +import org.chromium.components.browser_ui.site_settings.WebsitePreferenceBridge;
  358. +import org.chromium.components.content_settings.ContentSettingsType;
  359. +import org.chromium.components.browser_ui.settings.SettingsUtils;
  360. +import org.chromium.chrome.browser.flags.CachedFeatureFlags;
  361. +import androidx.annotation.VisibleForTesting;
  362. +import org.chromium.chrome.R;
  363. +
  364. +/**
  365. + * Fragment that allows the user to configure AdBlock related preferences.
  366. + */
  367. +public class AdBlockPreferences extends SiteSettingsPreferenceFragment {
  368. + @VisibleForTesting
  369. + public static final String PREF_ADBLOCK_SWITCH = "adblock_switch";
  370. + private static final String PREF_ADBLOCK_EDIT = "adblock_edit";
  371. +
  372. + private Preference mAdBlockEdit;
  373. +
  374. + @Override
  375. + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
  376. + getActivity().setTitle(R.string.options_adblock_title);
  377. + SettingsUtils.addPreferencesFromResource(this, R.xml.adblock_preferences);
  378. +
  379. + BrowserContextHandle browserContextHandle =
  380. + getSiteSettingsDelegate().getBrowserContextHandle();
  381. +
  382. + ChromeSwitchPreference mAdBlockSwitch =
  383. + (ChromeSwitchPreference) findPreference(PREF_ADBLOCK_SWITCH);
  384. + boolean isAdBlockEnabled = !WebsitePreferenceBridge.isCategoryEnabled(browserContextHandle, ContentSettingsType.ADS);
  385. + mAdBlockSwitch.setChecked(isAdBlockEnabled);
  386. + mAdBlockSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
  387. + WebsitePreferenceBridge.setCategoryEnabled(browserContextHandle, ContentSettingsType.ADS, !(boolean) newValue);
  388. + return true;
  389. + });
  390. +
  391. + mAdBlockEdit = findPreference(PREF_ADBLOCK_EDIT);
  392. + updateCurrentAdBlockUrl();
  393. + }
  394. +
  395. + private void updateCurrentAdBlockUrl() {
  396. + mAdBlockEdit.setSummary(CachedFeatureFlags.getAdBlockFiltersURL());
  397. + }
  398. +
  399. + @Override
  400. + public void onResume() {
  401. + super.onResume();
  402. + updateCurrentAdBlockUrl();
  403. + }
  404. +}
  405. diff --git a/chrome/android/java/src/org/chromium/chrome/browser/tabmodel/TabModelImpl.java b/chrome/android/java/src/org/chromium/chrome/browser/tabmodel/TabModelImpl.java
  406. --- a/chrome/android/java/src/org/chromium/chrome/browser/tabmodel/TabModelImpl.java
  407. +++ b/chrome/android/java/src/org/chromium/chrome/browser/tabmodel/TabModelImpl.java
  408. @@ -801,7 +801,7 @@ public class TabModelImpl extends TabModelJniBridge {
  409. Tab parent, Profile profile, WebContents webContents) {
  410. return getTabCreator(profile.isOffTheRecord())
  411. .createTabWithWebContents(
  412. - parent, webContents, TabLaunchType.FROM_LONGPRESS_BACKGROUND);
  413. + parent, webContents, TabLaunchType.FROM_LINK);
  414. }
  415. @Override
  416. diff --git a/chrome/app/generated_resources.grd b/chrome/app/generated_resources.grd
  417. --- a/chrome/app/generated_resources.grd
  418. +++ b/chrome/app/generated_resources.grd
  419. @@ -11012,6 +11012,16 @@ Please help our engineers fix this problem. Tell us what happened right before y
  420. Never show this again.
  421. </message>
  422. + <!-- Ad Blocking preferences -->
  423. + <if expr="is_android">
  424. + <message name="IDS_OPTIONS_ADBLOCK_TITLE" desc="The title of the Ad Blocking option on Android" formatter_data="android_java">
  425. + Ad Blocking
  426. + </message>
  427. + <message name="IDS_OPTIONS_ADBLOCK_SUMMARY" desc="The title of the Ad Blocking summary on Android" formatter_data="android_java">
  428. + Configure Ad Blocking and filters URL
  429. + </message>
  430. + </if>
  431. +
  432. <!-- Ad Blocking UI strings. -->
  433. <message name="IDS_BLOCKED_ADS_PROMPT_TOOLTIP" desc="Explanation that Chrome blocked ads on this site. To be shown as a tooltip on the Ads blocked desktop bubble">
  434. Ads blocked on this site
  435. diff --git a/chrome/browser/after_startup_task_utils.cc b/chrome/browser/after_startup_task_utils.cc
  436. --- a/chrome/browser/after_startup_task_utils.cc
  437. +++ b/chrome/browser/after_startup_task_utils.cc
  438. @@ -26,6 +26,8 @@
  439. #include "ui/views/linux_ui/linux_ui.h"
  440. #endif
  441. +#include "chrome/browser/browser_process.h"
  442. +
  443. using content::BrowserThread;
  444. namespace {
  445. @@ -121,6 +123,9 @@ void SetBrowserStartupIsComplete() {
  446. g_after_startup_tasks.Get().clear();
  447. g_after_startup_tasks.Get().shrink_to_fit();
  448. + // initialize AdBlock engine scheduled updates
  449. + g_browser_process->adblock_updater()->Start();
  450. +
  451. // TODO(crbug.com/1052397): Revisit the macro expression once build flag switch
  452. // of lacros-chrome is complete.
  453. #if defined(OS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
  454. diff --git a/chrome/browser/browser_process.h b/chrome/browser/browser_process.h
  455. --- a/chrome/browser/browser_process.h
  456. +++ b/chrome/browser/browser_process.h
  457. @@ -22,6 +22,7 @@
  458. #include "build/chromeos_buildflags.h"
  459. #include "chrome/common/buildflags.h"
  460. #include "media/media_buildflags.h"
  461. +#include "components/component_updater/adblock_updater_service.h"
  462. class BackgroundModeManager;
  463. class BrowserProcessPlatformPart;
  464. @@ -71,6 +72,10 @@ namespace component_updater {
  465. class ComponentUpdateService;
  466. }
  467. +namespace adblock_updater {
  468. +class AdBlockUpdaterService;
  469. +}
  470. +
  471. namespace extensions {
  472. class EventRouterForwarder;
  473. }
  474. @@ -242,6 +247,7 @@ class BrowserProcess {
  475. #endif
  476. virtual component_updater::ComponentUpdateService* component_updater() = 0;
  477. + virtual adblock_updater::AdBlockUpdaterService* adblock_updater() = 0;
  478. virtual MediaFileSystemRegistry* media_file_system_registry() = 0;
  479. diff --git a/chrome/browser/browser_process_impl.cc b/chrome/browser/browser_process_impl.cc
  480. --- a/chrome/browser/browser_process_impl.cc
  481. +++ b/chrome/browser/browser_process_impl.cc
  482. @@ -1075,6 +1075,26 @@ BrowserProcessImpl::component_updater() {
  483. return component_updater_.get();
  484. }
  485. +adblock_updater::AdBlockUpdaterService*
  486. +BrowserProcessImpl::adblock_updater() {
  487. + if (adblock_updater_)
  488. + return adblock_updater_.get();
  489. +
  490. + if (!BrowserThread::CurrentlyOn(BrowserThread::UI))
  491. + return nullptr;
  492. +
  493. + std::unique_ptr<component_updater::UpdateScheduler> scheduler =
  494. + std::make_unique<component_updater::TimerUpdateScheduler>();
  495. +
  496. + adblock_updater_ = std::make_unique<adblock_updater::AdBlockUpdaterService>(
  497. + g_browser_process->system_network_context_manager()->GetSharedURLLoaderFactory(),
  498. + std::move(scheduler),
  499. + g_browser_process->subresource_filter_ruleset_service(),
  500. + local_state()->GetString(prefs::kAdBlockFiltersURL));
  501. +
  502. + return adblock_updater_.get();
  503. +}
  504. +
  505. void BrowserProcessImpl::OnKeepAliveStateChanged(bool is_keeping_alive) {
  506. if (is_keeping_alive)
  507. Pin();
  508. diff --git a/chrome/browser/browser_process_impl.h b/chrome/browser/browser_process_impl.h
  509. --- a/chrome/browser/browser_process_impl.h
  510. +++ b/chrome/browser/browser_process_impl.h
  511. @@ -204,6 +204,7 @@ class BrowserProcessImpl : public BrowserProcess,
  512. #endif
  513. component_updater::ComponentUpdateService* component_updater() override;
  514. + adblock_updater::AdBlockUpdaterService* adblock_updater() override;
  515. MediaFileSystemRegistry* media_file_system_registry() override;
  516. WebRtcLogUploader* webrtc_log_uploader() override;
  517. network_time::NetworkTimeTracker* network_time_tracker() override;
  518. @@ -390,6 +391,7 @@ class BrowserProcessImpl : public BrowserProcess,
  519. // to concerns over integrity of data shared between profiles,
  520. // but some users of component updater only install per-user.
  521. std::unique_ptr<component_updater::ComponentUpdateService> component_updater_;
  522. + std::unique_ptr<adblock_updater::AdBlockUpdaterService> adblock_updater_;
  523. #if !defined(OS_ANDROID) && !BUILDFLAG(IS_CHROMEOS_ASH)
  524. // Used to create a singleton instance of SodaInstallerImpl, which can be
  525. diff --git a/chrome/browser/chrome_browser_main.cc b/chrome/browser/chrome_browser_main.cc
  526. --- a/chrome/browser/chrome_browser_main.cc
  527. +++ b/chrome/browser/chrome_browser_main.cc
  528. @@ -1653,6 +1653,8 @@ int ChromeBrowserMainParts::PreMainMessageLoopRunImpl() {
  529. speech::SodaInstaller::GetInstance()->Init(profile_->GetPrefs(),
  530. browser_process_->local_state());
  531. #endif // !defined(OS_ANDROID) && !BUILDFLAG(IS_CHROMEOS_ASH)
  532. + // force AdBlock updater initialisation
  533. + g_browser_process->adblock_updater();
  534. }
  535. #if BUILDFLAG(IS_CHROMEOS_ASH)
  536. diff --git a/chrome/browser/chrome_content_browser_client.cc b/chrome/browser/chrome_content_browser_client.cc
  537. --- a/chrome/browser/chrome_content_browser_client.cc
  538. +++ b/chrome/browser/chrome_content_browser_client.cc
  539. @@ -62,7 +62,6 @@
  540. #include "chrome/browser/hid/chrome_hid_delegate.h"
  541. #include "chrome/browser/interstitials/enterprise_util.h"
  542. #include "chrome/browser/lifetime/browser_shutdown.h"
  543. -#include "chrome/browser/lookalikes/lookalike_url_navigation_throttle.h"
  544. #include "chrome/browser/media/audio_service_util.h"
  545. #include "chrome/browser/media/router/media_router_feature.h"
  546. #include "chrome/browser/media/webrtc/audio_debug_recordings_handler.h"
  547. @@ -4091,16 +4090,6 @@ ChromeContentBrowserClient::CreateThrottlesForNavigation(
  548. content::NavigationHandle* handle) {
  549. std::vector<std::unique_ptr<content::NavigationThrottle>> throttles;
  550. - // MetricsNavigationThrottle requires that it runs before NavigationThrottles
  551. - // that may delay or cancel navigations, so only NavigationThrottles that
  552. - // don't delay or cancel navigations (e.g. throttles that are only observing
  553. - // callbacks without affecting navigation behavior) should be added before
  554. - // MetricsNavigationThrottle.
  555. - if (handle->IsInMainFrame()) {
  556. - throttles.push_back(
  557. - page_load_metrics::MetricsNavigationThrottle::Create(handle));
  558. - }
  559. -
  560. #if BUILDFLAG(IS_CHROMEOS_ASH)
  561. MaybeAddThrottle(
  562. ash::WebTimeLimitNavigationThrottle::MaybeCreateThrottleFor(handle),
  563. @@ -4198,10 +4187,6 @@ ChromeContentBrowserClient::CreateThrottlesForNavigation(
  564. &throttles);
  565. #endif
  566. - MaybeAddThrottle(
  567. - LookalikeUrlNavigationThrottle::MaybeCreateNavigationThrottle(handle),
  568. - &throttles);
  569. -
  570. MaybeAddThrottle(PDFIFrameNavigationThrottle::MaybeCreateThrottleFor(handle),
  571. &throttles);
  572. #if BUILDFLAG(ENABLE_PDF)
  573. diff --git a/chrome/browser/flags/android/cached_feature_flags.cc b/chrome/browser/flags/android/cached_feature_flags.cc
  574. --- a/chrome/browser/flags/android/cached_feature_flags.cc
  575. +++ b/chrome/browser/flags/android/cached_feature_flags.cc
  576. @@ -8,6 +8,9 @@
  577. #include "base/android/jni_string.h"
  578. #include "base/feature_list.h"
  579. +#include "chrome/browser/browser_process.h"
  580. +#include "chrome/common/pref_names.h"
  581. +#include "components/prefs/pref_service.h"
  582. #include "content/public/common/content_features.h"
  583. #include "content/public/common/network_service_util.h"
  584. @@ -41,3 +44,11 @@ static jboolean JNI_CachedFeatureFlags_IsNetworkServiceWarmUpEnabled(
  585. return content::IsOutOfProcessNetworkService() &&
  586. base::FeatureList::IsEnabled(features::kWarmUpNetworkProcess);
  587. }
  588. +
  589. +static ScopedJavaLocalRef<jstring> JNI_CachedFeatureFlags_GetAdBlockFiltersURL(JNIEnv* env) {
  590. + return base::android::ConvertUTF8ToJavaString(env, g_browser_process->local_state()->GetString(prefs::kAdBlockFiltersURL));
  591. +}
  592. +
  593. +static void JNI_CachedFeatureFlags_SetAdBlockFiltersURL(JNIEnv* env, const JavaParamRef<jstring>& url) {
  594. + g_browser_process->local_state()->SetString(prefs::kAdBlockFiltersURL, base::android::ConvertJavaStringToUTF8(env, url));
  595. +}
  596. diff --git a/chrome/browser/flags/android/java/src/org/chromium/chrome/browser/flags/CachedFeatureFlags.java b/chrome/browser/flags/android/java/src/org/chromium/chrome/browser/flags/CachedFeatureFlags.java
  597. --- a/chrome/browser/flags/android/java/src/org/chromium/chrome/browser/flags/CachedFeatureFlags.java
  598. +++ b/chrome/browser/flags/android/java/src/org/chromium/chrome/browser/flags/CachedFeatureFlags.java
  599. @@ -267,6 +267,14 @@ public class CachedFeatureFlags {
  600. ChromeFeatureList.isEnabled(ChromeFeatureList.BACKGROUND_THREAD_POOL));
  601. }
  602. + public static void setAdBlockFiltersURL(String url) {
  603. + CachedFeatureFlagsJni.get().setAdBlockFiltersURL(url);
  604. + }
  605. +
  606. + public static String getAdBlockFiltersURL() {
  607. + return CachedFeatureFlagsJni.get().getAdBlockFiltersURL();
  608. + }
  609. +
  610. /**
  611. * Caches flags that must take effect on startup but are set via native code.
  612. */
  613. @@ -472,5 +480,7 @@ public class CachedFeatureFlags {
  614. @NativeMethods
  615. interface Natives {
  616. boolean isNetworkServiceWarmUpEnabled();
  617. + void setAdBlockFiltersURL(String url);
  618. + String getAdBlockFiltersURL();
  619. }
  620. }
  621. diff --git a/chrome/browser/net/system_network_context_manager.cc b/chrome/browser/net/system_network_context_manager.cc
  622. --- a/chrome/browser/net/system_network_context_manager.cc
  623. +++ b/chrome/browser/net/system_network_context_manager.cc
  624. @@ -351,6 +351,8 @@ SystemNetworkContextManager::SystemNetworkContextManager(
  625. SSLConfigServiceManager::CreateDefaultManager(local_state_)),
  626. proxy_config_monitor_(local_state_),
  627. stub_resolver_config_reader_(local_state_) {
  628. + local_state_->SetDefaultPrefValue(prefs::kAdBlockFiltersURL,
  629. + base::Value("https://www.bromite.org/filters/filters.dat"));
  630. #if !defined(OS_ANDROID)
  631. // QuicAllowed was not part of Android policy.
  632. const base::Value* value =
  633. @@ -421,6 +423,8 @@ SystemNetworkContextManager::~SystemNetworkContextManager() {
  634. void SystemNetworkContextManager::RegisterPrefs(PrefRegistrySimple* registry) {
  635. StubResolverConfigReader::RegisterPrefs(registry);
  636. + registry->RegisterStringPref(prefs::kAdBlockFiltersURL, std::string());
  637. +
  638. // Static auth params
  639. registry->RegisterStringPref(prefs::kAuthSchemes,
  640. "basic,digest,ntlm,negotiate");
  641. diff --git a/chrome/browser/sessions/session_restore_android.cc b/chrome/browser/sessions/session_restore_android.cc
  642. --- a/chrome/browser/sessions/session_restore_android.cc
  643. +++ b/chrome/browser/sessions/session_restore_android.cc
  644. @@ -42,7 +42,9 @@ content::WebContents* SessionRestore::RestoreForeignSessionTab(
  645. TabAndroid* current_tab = TabAndroid::FromWebContents(web_contents);
  646. DCHECK(current_tab);
  647. if (disposition == WindowOpenDisposition::CURRENT_TAB) {
  648. - current_tab->SwapWebContents(std::move(new_web_contents), false, false);
  649. + int active_tab_index = tab_model->GetActiveIndex();
  650. + tab_model->CreateTab(current_tab, new_web_contents.release());
  651. + tab_model->CloseTabAt(active_tab_index);
  652. } else {
  653. DCHECK(disposition == WindowOpenDisposition::NEW_FOREGROUND_TAB ||
  654. disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB);
  655. diff --git a/chrome/browser/ui/android/strings/android_chrome_strings.grd b/chrome/browser/ui/android/strings/android_chrome_strings.grd
  656. --- a/chrome/browser/ui/android/strings/android_chrome_strings.grd
  657. +++ b/chrome/browser/ui/android/strings/android_chrome_strings.grd
  658. @@ -214,6 +214,20 @@ CHAR_LIMIT guidelines:
  659. Enter VR
  660. </message>
  661. + <!-- AdBlock settings -->
  662. + <message name="IDS_PREFS_ADBLOCK" desc="Title of the AdBlock Settings screen. [CHAR-LIMIT=32]">
  663. + AdBlock settings
  664. + </message>
  665. + <message name="IDS_OPTIONS_ADBLOCK_EDIT_TITLE" desc="The title of the screen that allows users to change the URL that is used to fetch the ad blocker filters.">
  666. + Edit filters URL
  667. + </message>
  668. + <message name="IDS_OPTIONS_ADBLOCK_EDIT_LABEL" desc="The label for the edit text field that allows the user to change the URL that is used to fetch the ad blocker filters.">
  669. + Filters URL
  670. + </message>
  671. + <message name="IDS_ADBLOCK_HELP" desc="The title of the hyperlink that allows users to visit the web page with instructions for custom ad blocker filters.">
  672. + Visit help page
  673. + </message>
  674. +
  675. <!-- Notification channels -->
  676. <message name="IDS_NOTIFICATION_CATEGORY_GROUP_GENERAL" desc='Subheading for "General" section of a list of notification categories. [CHAR_LIMIT=32]'>
  677. General
  678. diff --git a/chrome/common/pref_names.cc b/chrome/common/pref_names.cc
  679. --- a/chrome/common/pref_names.cc
  680. +++ b/chrome/common/pref_names.cc
  681. @@ -2221,6 +2221,9 @@ const char kAudioCaptureAllowed[] = "hardware.audio_capture_enabled";
  682. // capture devices without prompt.
  683. const char kAudioCaptureAllowedUrls[] = "hardware.audio_capture_allowed_urls";
  684. +// Holds the URL to an indexed subresource filters file.
  685. +const char kAdBlockFiltersURL[] = "adblock.filters_url";
  686. +
  687. // A pref holding the value of the policy used to explicitly allow or deny
  688. // access to video capture devices. When enabled or not set, the user is
  689. // prompted for device access. When disabled, access to video capture devices
  690. diff --git a/chrome/common/pref_names.h b/chrome/common/pref_names.h
  691. --- a/chrome/common/pref_names.h
  692. +++ b/chrome/common/pref_names.h
  693. @@ -31,6 +31,7 @@ extern const char kDownloadRestrictions[];
  694. extern const char kForceEphemeralProfiles[];
  695. extern const char kHomePageIsNewTabPage[];
  696. extern const char kHomePage[];
  697. +extern const char kAdBlockFiltersURL[];
  698. extern const char kHttpsOnlyModeEnabled[];
  699. extern const char kImportantSitesDialogHistory[];
  700. extern const char kProfileCreationTime[];
  701. diff --git a/components/component_updater/BUILD.gn b/components/component_updater/BUILD.gn
  702. --- a/components/component_updater/BUILD.gn
  703. +++ b/components/component_updater/BUILD.gn
  704. @@ -10,6 +10,12 @@ static_library("component_updater") {
  705. "component_updater_command_line_config_policy.h",
  706. "component_updater_paths.cc",
  707. "component_updater_paths.h",
  708. +
  709. + "adblock_updater_service.cc",
  710. + "adblock_updater_service.h",
  711. + "download_filters_task.cc",
  712. + "download_filters_task.h",
  713. +
  714. "component_updater_service.cc",
  715. "component_updater_service.h",
  716. "component_updater_service_internal.h",
  717. @@ -36,6 +42,7 @@ static_library("component_updater") {
  718. "//components/crx_file",
  719. "//components/update_client",
  720. "//components/version_info",
  721. + "//services/network/public/mojom",
  722. "//third_party/boringssl:boringssl",
  723. "//ui/base",
  724. "//url",
  725. diff --git a/components/component_updater/adblock_updater_service.cc b/components/component_updater/adblock_updater_service.cc
  726. new file mode 100644
  727. --- /dev/null
  728. +++ b/components/component_updater/adblock_updater_service.cc
  729. @@ -0,0 +1,272 @@
  730. +/*
  731. + This file is part of Bromite.
  732. +
  733. + Bromite is free software: you can redistribute it and/or modify
  734. + it under the terms of the GNU General Public License as published by
  735. + the Free Software Foundation, either version 3 of the License, or
  736. + (at your option) any later version.
  737. +
  738. + Bromite is distributed in the hope that it will be useful,
  739. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  740. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  741. + GNU General Public License for more details.
  742. +
  743. + You should have received a copy of the GNU General Public License
  744. + along with Bromite. If not, see <https://www.gnu.org/licenses/>.
  745. +*/
  746. +
  747. +#include "components/component_updater/adblock_updater_service.h"
  748. +
  749. +#include <algorithm>
  750. +#include <map>
  751. +#include <string>
  752. +#include <utility>
  753. +#include <vector>
  754. +
  755. +#include "base/bind.h"
  756. +#include "base/callback.h"
  757. +#include "base/files/file_path.h"
  758. +#include "base/files/file_util.h"
  759. +#include "base/logging.h"
  760. +#include "base/macros.h"
  761. +#include "base/threading/thread_checker.h"
  762. +#include "base/threading/thread_task_runner_handle.h"
  763. +#include "base/time/time.h"
  764. +#include "base/timer/timer.h"
  765. +#include "url/gurl.h"
  766. +#include "base/strings/safe_sprintf.h"
  767. +#include "base/strings/string_number_conversions.h"
  768. +#include "base/strings/string_split.h"
  769. +namespace adblock_updater {
  770. +
  771. +// all constants express seconds
  772. +// these could be made configurable
  773. +const int initial_check_delay = 5,
  774. + next_check_delay = 60*60*24*7, // 1 week
  775. + on_demand_check_delay = 60; // minimum 1 minute between each on-demand check
  776. +
  777. +AdBlockUpdaterService::AdBlockUpdaterService(scoped_refptr<network::SharedURLLoaderFactory> shared_url_network_factory, std::unique_ptr<component_updater::UpdateScheduler> scheduler,
  778. + subresource_filter::RulesetService* ruleset_service, std::string filters_url)
  779. + : ruleset_service_(ruleset_service), shared_url_network_factory_(shared_url_network_factory), scheduler_(std::move(scheduler)) {
  780. + DCHECK(ruleset_service);
  781. +
  782. + filters_url_ = filters_url;
  783. +}
  784. +
  785. +AdBlockUpdaterService::~AdBlockUpdaterService() {
  786. + DCHECK(thread_checker_.CalledOnValidThread());
  787. +}
  788. +
  789. +void AdBlockUpdaterService::AddObserver(Observer* observer) {
  790. + DCHECK(thread_checker_.CalledOnValidThread());
  791. + observer_list_.AddObserver(observer);
  792. +}
  793. +
  794. +void AdBlockUpdaterService::RemoveObserver(Observer* observer) {
  795. + DCHECK(thread_checker_.CalledOnValidThread());
  796. + observer_list_.RemoveObserver(observer);
  797. +}
  798. +
  799. +void AdBlockUpdaterService::NotifyObservers(Event event) {
  800. + DCHECK(thread_checker_.CalledOnValidThread());
  801. + for (auto& observer : observer_list_)
  802. + observer.OnEvent(event);
  803. +}
  804. +
  805. +void AdBlockUpdaterService::Start() {
  806. + DCHECK(thread_checker_.CalledOnValidThread());
  807. +
  808. + // avoid multiple scheduling
  809. + if (scheduled_)
  810. + return;
  811. + scheduled_ = true;
  812. +
  813. + LOG(INFO) << "AdBlockUpdaterService: starting up. "
  814. + << "First update attempt will take place in "
  815. + << initial_check_delay << " seconds. "
  816. + << "Next update attempt will take place in "
  817. + << next_check_delay << " seconds. ";
  818. +
  819. + scheduler_->Schedule(
  820. + base::TimeDelta::FromSeconds(initial_check_delay),
  821. + base::TimeDelta::FromSeconds(next_check_delay),
  822. + base::BindRepeating(&AdBlockUpdaterService::OnDemandScheduledUpdate,
  823. + base::Unretained(this)), base::DoNothing());
  824. +}
  825. +
  826. +void AdBlockUpdaterService::OnDemandScheduledUpdate(component_updater::UpdateScheduler::OnFinishedCallback on_finished) {
  827. + //TODO: call on_finished
  828. + OnDemandUpdateAsNeeded(false, Callback());
  829. +}
  830. +
  831. +bool AdBlockUpdaterService::OnDemandUpdate(Callback on_finished) {
  832. + return OnDemandUpdateAsNeeded(true, std::move(on_finished));
  833. +}
  834. +
  835. +bool AdBlockUpdaterService::OnDemandUpdateAsNeeded(bool is_foreground, Callback on_finished) {
  836. + DCHECK(thread_checker_.CalledOnValidThread());
  837. +
  838. + // Check if the request is too early
  839. + if (!last_update_.is_null()) {
  840. + base::TimeDelta delta =
  841. + base::TimeTicks::Now() - last_update_;
  842. + if (delta < base::TimeDelta::FromSeconds(on_demand_check_delay)) {
  843. + LOG(INFO) << "AdBlockUpdaterService: update not necessary.";
  844. + return false;
  845. + }
  846. + }
  847. +
  848. + if (is_updating_) {
  849. + base::ThreadTaskRunnerHandle::Get()->PostTask(
  850. + FROM_HERE, base::BindOnce(std::move(on_finished),
  851. + Error::UPDATE_IN_PROGRESS));
  852. + return false;
  853. + }
  854. + is_updating_ = true;
  855. +
  856. + base::ThreadTaskRunnerHandle::Get()->PostTask(
  857. + FROM_HERE, base::BindOnce(&AdBlockUpdaterService::NotifyObservers, base::Unretained(this), Event::ADBLOCK_CHECKING_FOR_UPDATES));
  858. +
  859. + base::Time::Exploded e = {0};
  860. + base::Time min_last_modified = base::Time();
  861. + auto version = ruleset_service_->GetMostRecentlyIndexedVersion();
  862. + LOG(INFO) << "AdBlockUpdaterService: MostRecentIndexedVersion = " << version.content_version;
  863. + std::vector<std::string> tokens =
  864. + base::SplitString(version.content_version, ".", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
  865. + int i = 0;
  866. + bool failed = false;
  867. + for (const std::string& token : tokens) {
  868. + // parse as number
  869. + int n = 0;
  870. + if (!base::StringToInt(token, &n)) {
  871. + failed = true;
  872. + break;
  873. + }
  874. +
  875. + switch (i++) {
  876. + case 0:
  877. + e.year = 2019 + n;
  878. + break;
  879. + case 1:
  880. + e.month = n + 1;
  881. + break;
  882. + case 2:
  883. + e.day_of_month = n + 1;
  884. + break;
  885. + case 3:
  886. + e.second = n % 60;
  887. + n -= e.second;
  888. + n /= 60;
  889. + e.minute = n % 60;
  890. + e.hour = n / 60;
  891. + break;
  892. + default:
  893. + failed = true;
  894. + break;
  895. + }
  896. + }
  897. +
  898. + if (failed) {
  899. + LOG(WARNING) << "AdBlockUpdaterService: failed to parse most recent version as x.y.z.w dot-separated integers";
  900. + } else {
  901. + if (!base::Time::FromUTCExploded(e, &min_last_modified))
  902. + LOG(WARNING) << "AdBlockUpdaterService: failed to convert version to time.";
  903. + }
  904. +
  905. + // avoid making a new request if version-based time is recent enough
  906. + if (!failed) {
  907. + base::TimeDelta delta =
  908. + base::Time::Now() - min_last_modified;
  909. + if (delta < base::TimeDelta::FromSeconds(on_demand_check_delay)) {
  910. + LOG(INFO) << "AdBlockUpdaterService: update check not yet needed.";
  911. + is_updating_ = false;
  912. + return false;
  913. + }
  914. + }
  915. +
  916. + last_update_ = base::TimeTicks::Now();
  917. + auto task = base::MakeRefCounted<DownloadFiltersTask>(
  918. + shared_url_network_factory_,
  919. + is_foreground, filters_url_,
  920. + min_last_modified,
  921. + base::BindOnce(&AdBlockUpdaterService::OnUpdateComplete, base::Unretained(this),
  922. + std::move(on_finished)));
  923. +
  924. + // run task now; task is responsible for downloading the filters (if Last-Modified header is more recent)
  925. + // and then clearing the 'is_updating' status
  926. + base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
  927. + base::BindOnce(&DownloadFiltersTask::Run, base::Unretained(task.get())));
  928. + tasks_.insert(task);
  929. +
  930. + base::ThreadTaskRunnerHandle::Get()->PostTask(
  931. + FROM_HERE, base::BindOnce(&AdBlockUpdaterService::NotifyObservers, base::Unretained(this), Event::ADBLOCK_UPDATE_DOWNLOADING));
  932. +
  933. + return true;
  934. +}
  935. +
  936. +void AdBlockUpdaterService::OnUpdateComplete(Callback on_finished,
  937. + scoped_refptr<DownloadFiltersTask> task,
  938. + Error error) {
  939. + DCHECK(thread_checker_.CalledOnValidThread());
  940. +
  941. + auto file_path = task->file_path();
  942. + if (error == Error::NONE) {
  943. + base::ThreadTaskRunnerHandle::Get()->PostTask(
  944. + FROM_HERE, base::BindOnce(&AdBlockUpdaterService::NotifyObservers, base::Unretained(this), Event::ADBLOCK_UPDATE_READY));
  945. +
  946. + subresource_filter::UnindexedRulesetInfo ruleset_info;
  947. + ruleset_info.ruleset_path = file_path;
  948. + ruleset_info.delete_ruleset_path = true;
  949. + ruleset_info.content_version = "0.0.0.0";
  950. + DCHECK(!ruleset_info.ruleset_path.empty());
  951. +
  952. + // convert Last-Modified timestamp fetched by the DownloadFiltersTask to a semver version
  953. + auto t = task->last_modified();
  954. + bool ignore_version = t.is_null();
  955. + if (!ignore_version) {
  956. + base::Time::Exploded e;
  957. + t.UTCExplode(&e);
  958. +
  959. + // convert time to version
  960. + const int major = e.year - 2019,
  961. + minor = e.month - 1,
  962. + patch = e.day_of_month - 1,
  963. + revision = (e.hour*60+e.minute)*60 + e.second;
  964. + if (major < 0)
  965. + LOG(WARNING) << "AdBlockUpdaterService: too old Last-Modified header, ignoring version check.";
  966. + else {
  967. + char version_buffer[32];
  968. + base::strings::SafeSNPrintf(version_buffer, sizeof(version_buffer), "%d.%d.%d.%d",
  969. + major, minor, patch, revision);
  970. +
  971. + ruleset_info.content_version = version_buffer;
  972. +
  973. + LOG(INFO) << "AdBlockUpdaterService: indexing filters with version " << ruleset_info.content_version;
  974. + }
  975. + } else
  976. + LOG(WARNING) << "AdBlockUpdaterService: invalid Last-Modified header, ignoring version check.";
  977. + ruleset_service_->IndexAndStoreAndPublishRulesetIfNeeded(ruleset_info, ignore_version);
  978. +
  979. + base::ThreadTaskRunnerHandle::Get()->PostTask(
  980. + FROM_HERE, base::BindOnce(&AdBlockUpdaterService::NotifyObservers, base::Unretained(this), Event::ADBLOCK_UPDATED));
  981. + } else if (error == Error::UPDATE_NOT_NEEDED) {
  982. + base::ThreadTaskRunnerHandle::Get()->PostTask(
  983. + FROM_HERE, base::BindOnce(&AdBlockUpdaterService::NotifyObservers, base::Unretained(this), Event::ADBLOCK_NOT_UPDATED));
  984. + } else {
  985. + base::ThreadTaskRunnerHandle::Get()->PostTask(
  986. + FROM_HERE, base::BindOnce(&AdBlockUpdaterService::NotifyObservers, base::Unretained(this), Event::ADBLOCK_UPDATE_ERROR));
  987. + }
  988. +
  989. + //TODO: run these only when index-and-store is actually finished?
  990. + // would require exposing the callback in IndexAndStoreAndPublishRulesetIfNeeded
  991. + if (!on_finished.is_null()) {
  992. + base::ThreadTaskRunnerHandle::Get()->PostTask(
  993. + FROM_HERE, base::BindOnce(std::move(on_finished), error));
  994. + }
  995. +
  996. + // mark as not updating
  997. + is_updating_ = false;
  998. + tasks_.erase(task);
  999. +}
  1000. +
  1001. +} // namespace adblock_updater
  1002. diff --git a/components/component_updater/adblock_updater_service.h b/components/component_updater/adblock_updater_service.h
  1003. new file mode 100644
  1004. --- /dev/null
  1005. +++ b/components/component_updater/adblock_updater_service.h
  1006. @@ -0,0 +1,99 @@
  1007. +/*
  1008. + This file is part of Bromite.
  1009. +
  1010. + Bromite is free software: you can redistribute it and/or modify
  1011. + it under the terms of the GNU General Public License as published by
  1012. + the Free Software Foundation, either version 3 of the License, or
  1013. + (at your option) any later version.
  1014. +
  1015. + Bromite is distributed in the hope that it will be useful,
  1016. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  1017. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1018. + GNU General Public License for more details.
  1019. +
  1020. + You should have received a copy of the GNU General Public License
  1021. + along with Bromite. If not, see <https://www.gnu.org/licenses/>.
  1022. +*/
  1023. +
  1024. +#ifndef COMPONENTS_ADBLOCK_UPDATER_SERVICE_H
  1025. +#define COMPONENTS_ADBLOCK_UPDATER_SERVICE_H
  1026. +
  1027. +#include <stdint.h>
  1028. +
  1029. +#include <memory>
  1030. +#include <string>
  1031. +#include <vector>
  1032. +
  1033. +#include "base/callback_forward.h"
  1034. +#include "base/gtest_prod_util.h"
  1035. +#include "base/memory/ref_counted.h"
  1036. +#include "base/version.h"
  1037. +#include "build/build_config.h"
  1038. +#include "components/component_updater/update_scheduler.h"
  1039. +#include "url/gurl.h"
  1040. +#include "components/component_updater/download_filters_task.h"
  1041. +#include "components/subresource_filter/content/browser/ruleset_service.h"
  1042. +#include "services/network/public/cpp/shared_url_loader_factory.h"
  1043. +
  1044. +namespace adblock_updater {
  1045. +
  1046. +// Called when a non-blocking call in this module completes.
  1047. +using Callback = base::OnceCallback<void(Error error)>;
  1048. +
  1049. +class Observer {
  1050. + public:
  1051. + virtual ~Observer() {}
  1052. +
  1053. + // Called by the update service when a state change happens.
  1054. + virtual void OnEvent(Event event) = 0;
  1055. +};
  1056. +
  1057. +// The AdBlock update service is in charge of downloading and saving the
  1058. +// AdBlock filters.
  1059. +//
  1060. +// All methods are safe to call ONLY from the browser's main thread.
  1061. +class AdBlockUpdaterService {
  1062. + public:
  1063. + AdBlockUpdaterService(scoped_refptr<network::SharedURLLoaderFactory> shared_url_network_factory, std::unique_ptr<component_updater::UpdateScheduler> scheduler,
  1064. + subresource_filter::RulesetService* ruleset_service, std::string filters_url);
  1065. + ~AdBlockUpdaterService();
  1066. +
  1067. + // Adds an observer for this class. An observer should not be added more
  1068. + // than once. The caller retains the ownership of the observer object.
  1069. + void AddObserver(Observer* observer);
  1070. +
  1071. + // Removes an observer. It is safe for an observer to be removed while
  1072. + // the observers are being notified.
  1073. + void RemoveObserver(Observer* observer);
  1074. +
  1075. + // Will schedule automatic updates, run in background.
  1076. + void Start();
  1077. +
  1078. + // To be called for an user-triggered update.
  1079. + // Will not result in an actual update if the last update was too recently triggered.
  1080. + bool OnDemandUpdate(Callback on_finished);
  1081. +
  1082. + private:
  1083. + void NotifyObservers(Event event);
  1084. + void OnDemandScheduledUpdate(component_updater::UpdateScheduler::OnFinishedCallback on_finished);
  1085. + bool OnDemandUpdateAsNeeded(bool is_foreground, Callback on_finished);
  1086. + void OnUpdateComplete(Callback callback, scoped_refptr<DownloadFiltersTask> task, Error error);
  1087. +
  1088. + base::ObserverList<Observer>::Unchecked observer_list_;
  1089. + base::ThreadChecker thread_checker_;
  1090. + base::TimeTicks last_update_;
  1091. +
  1092. + subresource_filter::RulesetService* ruleset_service_;
  1093. + std::string filters_url_;
  1094. +
  1095. + scoped_refptr<network::SharedURLLoaderFactory> shared_url_network_factory_;
  1096. + std::unique_ptr<component_updater::UpdateScheduler> scheduler_;
  1097. +
  1098. + bool is_updating_ = false;
  1099. + bool scheduled_ = false;
  1100. + std::set<scoped_refptr<DownloadFiltersTask>> tasks_;
  1101. +};
  1102. +
  1103. +} // namespace adblock_updater
  1104. +
  1105. +#endif // COMPONENTS_ADBLOCK_UPDATER_SERVICE_H
  1106. diff --git a/components/component_updater/download_filters_task.cc b/components/component_updater/download_filters_task.cc
  1107. new file mode 100644
  1108. --- /dev/null
  1109. +++ b/components/component_updater/download_filters_task.cc
  1110. @@ -0,0 +1,224 @@
  1111. +/*
  1112. + This file is part of Bromite.
  1113. +
  1114. + Bromite is free software: you can redistribute it and/or modify
  1115. + it under the terms of the GNU General Public License as published by
  1116. + the Free Software Foundation, either version 3 of the License, or
  1117. + (at your option) any later version.
  1118. +
  1119. + Bromite is distributed in the hope that it will be useful,
  1120. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  1121. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1122. + GNU General Public License for more details.
  1123. +
  1124. + You should have received a copy of the GNU General Public License
  1125. + along with Bromite. If not, see <https://www.gnu.org/licenses/>.
  1126. +*/
  1127. +#include "components/component_updater/download_filters_task.h"
  1128. +
  1129. +#include <utility>
  1130. +
  1131. +#include "base/files/file_util.h"
  1132. +#include "base/bind.h"
  1133. +#include "base/location.h"
  1134. +#include "base/threading/thread_task_runner_handle.h"
  1135. +#include "base/logging.h"
  1136. +#include "net/base/load_flags.h"
  1137. +#include "url/gurl.h"
  1138. +#include "services/network/public/cpp/resource_request.h"
  1139. +#include "services/network/public/mojom/url_response_head.mojom.h"
  1140. +
  1141. +namespace adblock_updater {
  1142. +
  1143. +// maximum 10MB for the filters file
  1144. +const int kMaxBodySize = 1024 * 1024 * 10;
  1145. +
  1146. +const int kMaxRetriesOnNetworkChange = 3;
  1147. +
  1148. +const net::NetworkTrafficAnnotationTag traffic_annotation =
  1149. + net::DefineNetworkTrafficAnnotation("update_client", R"(
  1150. + semantics {
  1151. + sender: "Bromite AdBlock filters updater"
  1152. + description:
  1153. + "The AdBlock filters updater is responsible for updating the subresource filters."
  1154. + trigger: "Manual or automatic AdBlock filters updates."
  1155. + data:
  1156. + "Subresource filters rulesets, binary format"
  1157. + destination: WEBSITE
  1158. + }
  1159. + )");
  1160. +
  1161. +DownloadFiltersTask::DownloadFiltersTask(scoped_refptr<network::SharedURLLoaderFactory> shared_url_network_factory,
  1162. + bool is_foreground, const std::string& filters_url, base::Time min_last_modified,
  1163. + Callback callback)
  1164. + : shared_url_network_factory_(shared_url_network_factory),
  1165. + is_foreground_(is_foreground),
  1166. + complete_callback_(std::move(callback)) {
  1167. + DCHECK(!filters_url.empty());
  1168. + filters_url_ = GURL(filters_url);
  1169. + min_last_modified_ = min_last_modified;
  1170. +
  1171. + if (filters_url.empty()) {
  1172. + return;
  1173. + }
  1174. +
  1175. + createSimpleURLLoader(!min_last_modified_.is_null());
  1176. +}
  1177. +
  1178. +void DownloadFiltersTask::createSimpleURLLoader(bool headers_only) {
  1179. + // always reset response-related fields
  1180. + response_code_ = -1;
  1181. + final_url_ = GURL();
  1182. + download_start_time_ = base::TimeTicks();
  1183. +
  1184. + auto resource_request = std::make_unique<network::ResourceRequest>();
  1185. + resource_request->url = filters_url_;
  1186. + resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
  1187. + resource_request->load_flags = net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | net::LOAD_DO_NOT_SAVE_COOKIES;
  1188. + resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
  1189. + if (headers_only)
  1190. + // will chain two requests - first one is to check last modified header alone
  1191. + resource_request->method = "HEAD";
  1192. + else
  1193. + resource_request->method = "GET";
  1194. +
  1195. + simple_url_loader_ = network::SimpleURLLoader::Create(
  1196. + std::move(resource_request), traffic_annotation);
  1197. + simple_url_loader_->SetRetryOptions(
  1198. + kMaxRetriesOnNetworkChange,
  1199. + network::SimpleURLLoader::RetryMode::RETRY_ON_NETWORK_CHANGE);
  1200. + simple_url_loader_->SetAllowPartialResults(false);
  1201. + simple_url_loader_->SetOnResponseStartedCallback(base::BindOnce(
  1202. + &DownloadFiltersTask::OnResponseStarted, base::Unretained(this)));
  1203. +}
  1204. +
  1205. +DownloadFiltersTask::~DownloadFiltersTask() {
  1206. + DCHECK(thread_checker_.CalledOnValidThread());
  1207. +}
  1208. +
  1209. +void DownloadFiltersTask::Run() {
  1210. + DCHECK(thread_checker_.CalledOnValidThread());
  1211. +
  1212. + // will not be initialized if the URL was empty
  1213. + if (!simple_url_loader_) {
  1214. + TaskComplete(Error::INVALID_ARGUMENT);
  1215. + return;
  1216. + }
  1217. +
  1218. + download_start_time_ = base::TimeTicks::Now();
  1219. + if (min_last_modified_.is_null()) {
  1220. + internalDownload();
  1221. + } else {
  1222. + simple_url_loader_->DownloadHeadersOnly(
  1223. + shared_url_network_factory_.get(),
  1224. + base::BindOnce(&DownloadFiltersTask::OnHeadersDownloadComplete, base::Unretained(this))
  1225. + );
  1226. + }
  1227. +}
  1228. +
  1229. +void DownloadFiltersTask::internalDownload() {
  1230. + simple_url_loader_->DownloadToTempFile(
  1231. + shared_url_network_factory_.get(),
  1232. + base::BindOnce(&DownloadFiltersTask::OnDownloadComplete, base::Unretained(this)),
  1233. + kMaxBodySize);
  1234. +}
  1235. +
  1236. +void DownloadFiltersTask::OnHeadersDownloadComplete(scoped_refptr<net::HttpResponseHeaders> headers) {
  1237. + // something went wrong
  1238. + if (headers == nullptr) {
  1239. + OnDownloadComplete(base::FilePath());
  1240. + return;
  1241. + }
  1242. +
  1243. + // ignoring 'headers' as 'Last-Modified' has already been picked up by OnResponseStarted
  1244. + const base::TimeDelta dt =
  1245. + last_modified_ - min_last_modified_;
  1246. +
  1247. + if (dt.InSeconds() > 0) {
  1248. + // prepare for next simple URL loader and trigger download
  1249. + createSimpleURLLoader(false);
  1250. + internalDownload();
  1251. + return;
  1252. + }
  1253. +
  1254. + // the remote filters are not more recent than known ones
  1255. + TaskComplete(Error::UPDATE_NOT_NEEDED);
  1256. +}
  1257. +
  1258. +void DownloadFiltersTask::OnResponseStarted(
  1259. + const GURL& final_url,
  1260. + const network::mojom::URLResponseHead& response_head) {
  1261. +
  1262. + final_url_ = final_url;
  1263. + response_code_ = response_head.headers ? response_head.headers->response_code() : -1;
  1264. +
  1265. + if (!response_head.headers->GetLastModifiedValue(&last_modified_))
  1266. + LOG(WARNING) << "DownloadFiltersTask: fetching URL '" << final_url.spec() << "' with method " << (min_last_modified_.is_null() ? "GET" : "HEAD") << " (no Last-Modified header)";
  1267. + else
  1268. + LOG(INFO) << "DownloadFiltersTask: fetching URL '" << final_url.spec() << "' with method " << (min_last_modified_.is_null() ? "GET" : "HEAD");
  1269. +}
  1270. +
  1271. +void DownloadFiltersTask::OnDownloadComplete(base::FilePath file_path) {
  1272. + DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  1273. + int net_error = simple_url_loader_->NetError();
  1274. + int64_t content_size = simple_url_loader_->GetContentSize();
  1275. +
  1276. + const base::TimeTicks download_end_time(base::TimeTicks::Now());
  1277. + const base::TimeDelta download_time =
  1278. + download_end_time >= download_start_time_
  1279. + ? download_end_time - download_start_time_
  1280. + : base::TimeDelta();
  1281. +
  1282. + // Consider a 5xx response from the server as an indication to terminate
  1283. + // the request and avoid overloading the server in this case.
  1284. + // is not accepting requests for the moment.
  1285. + int error = -1;
  1286. + if (!file_path.empty() && response_code_ == 200) {
  1287. + DCHECK_EQ(0, net_error);
  1288. + error = 0;
  1289. + } else if (response_code_ != -1) {
  1290. + error = response_code_;
  1291. + } else {
  1292. + error = net_error;
  1293. + }
  1294. +
  1295. + LOG(INFO) << "DownloadFiltersTask: downloaded " << content_size << " bytes in "
  1296. + << download_time.InMilliseconds() << "ms from '" << final_url_.spec()
  1297. + << "' to '" << file_path << "' with net_error " << net_error << " and error " << error;
  1298. +
  1299. + if (error) {
  1300. + TaskComplete(Error::DOWNLOAD_ERROR);
  1301. + return;
  1302. + }
  1303. +
  1304. + file_path_ = file_path;
  1305. + TaskComplete(Error::NONE);
  1306. +}
  1307. +
  1308. +void DownloadFiltersTask::Cancel() {
  1309. + DCHECK(thread_checker_.CalledOnValidThread());
  1310. +
  1311. + LOG(INFO) << "DownloadFiltersTask: update cancelled";
  1312. +
  1313. + // deletion of the simple_url_loader_ will cause cancellation of its active request, if any
  1314. +
  1315. + TaskComplete(Error::UPDATE_CANCELED);
  1316. +}
  1317. +
  1318. +void DownloadFiltersTask::TaskComplete(Error error) {
  1319. + DCHECK(thread_checker_.CalledOnValidThread());
  1320. +
  1321. + base::ThreadTaskRunnerHandle::Get()->PostTask(
  1322. + FROM_HERE, base::BindOnce(std::move(complete_callback_),
  1323. + scoped_refptr<DownloadFiltersTask>(this), error));
  1324. +}
  1325. +
  1326. +base::Time DownloadFiltersTask::last_modified() {
  1327. + return last_modified_;
  1328. +}
  1329. +
  1330. +base::FilePath DownloadFiltersTask::file_path() {
  1331. + return file_path_;
  1332. +}
  1333. +
  1334. +} // namespace adblock_updater
  1335. diff --git a/components/component_updater/download_filters_task.h b/components/component_updater/download_filters_task.h
  1336. new file mode 100644
  1337. --- /dev/null
  1338. +++ b/components/component_updater/download_filters_task.h
  1339. @@ -0,0 +1,129 @@
  1340. +/*
  1341. + This file is part of Bromite.
  1342. +
  1343. + Bromite is free software: you can redistribute it and/or modify
  1344. + it under the terms of the GNU General Public License as published by
  1345. + the Free Software Foundation, either version 3 of the License, or
  1346. + (at your option) any later version.
  1347. +
  1348. + Bromite is distributed in the hope that it will be useful,
  1349. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  1350. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1351. + GNU General Public License for more details.
  1352. +
  1353. + You should have received a copy of the GNU General Public License
  1354. + along with Bromite. If not, see <https://www.gnu.org/licenses/>.
  1355. +*/
  1356. +
  1357. +#ifndef COMPONENTS_DOWNLOAD_FILTERS_TASK_H_
  1358. +#define COMPONENTS_DOWNLOAD_FILTERS_TASK_H_
  1359. +
  1360. +#include <string>
  1361. +#include <vector>
  1362. +
  1363. +#include "base/callback.h"
  1364. +#include "base/macros.h"
  1365. +#include "base/memory/ref_counted.h"
  1366. +#include "base/threading/thread_checker.h"
  1367. +#include "components/update_client/network.h"
  1368. +#include "url/gurl.h"
  1369. +#include "base/files/file_path.h"
  1370. +#include "services/network/public/cpp/shared_url_loader_factory.h"
  1371. +#include "services/network/public/cpp/simple_url_loader.h"
  1372. +
  1373. +namespace adblock_updater {
  1374. +
  1375. +// Errors generated as a result of calling Run() or by the service itself (UPDATE_IN_PROGRESS or UPDATE_CANCELED)
  1376. +enum class Error {
  1377. + NONE = 0,
  1378. + UPDATE_IN_PROGRESS = 1,
  1379. + UPDATE_CANCELED = 2,
  1380. + UPDATE_NOT_NEEDED = 3,
  1381. + DOWNLOAD_ERROR = 4,
  1382. + INVALID_ARGUMENT = 5,
  1383. + MAX_VALUE,
  1384. +};
  1385. +
  1386. +enum class Event {
  1387. + // Sent before the update client does an update check.
  1388. + ADBLOCK_CHECKING_FOR_UPDATES = 1,
  1389. +
  1390. + // Sent after the new filters have been downloaded but before the install
  1391. + // or the upgrade is attempted.
  1392. + ADBLOCK_UPDATE_READY,
  1393. +
  1394. + // Sent when filters are being downloaded.
  1395. + ADBLOCK_UPDATE_DOWNLOADING,
  1396. +
  1397. + // Sent when filters have been successfully updated.
  1398. + ADBLOCK_UPDATED,
  1399. +
  1400. + // Sent when filters have not been updated because there
  1401. + // was no new version available
  1402. + //TODO: implement this with the headers check
  1403. + ADBLOCK_NOT_UPDATED,
  1404. +
  1405. + // Sent when an error ocurred during an update for any reason, including
  1406. + // the update check itself failed, or the download of the update payload
  1407. + // failed, or applying the update failed.
  1408. + ADBLOCK_UPDATE_ERROR,
  1409. +};
  1410. +
  1411. +// Defines a specialized task for updating AdBlock filters.
  1412. +class DownloadFiltersTask : public base::RefCounted<DownloadFiltersTask> {
  1413. + public:
  1414. + using Callback =
  1415. + base::OnceCallback<void(scoped_refptr<DownloadFiltersTask> task, Error error)>;
  1416. +
  1417. + // |shared_url_network_factory| is injected here for the URL loader factory.
  1418. + // |is_foreground| is true when the update task is initiated by the user.
  1419. + // |filters_url| is the URL to load filters from.
  1420. + // |complete_callback| is called to return the execution flow back to creator of
  1421. + // this task when the task is done.
  1422. + DownloadFiltersTask(scoped_refptr<network::SharedURLLoaderFactory> shared_url_network_factory,
  1423. + bool is_foreground, const std::string& filters_url, base::Time min_last_modified,
  1424. + Callback complete_callback);
  1425. +
  1426. + void Run();
  1427. +
  1428. + void Cancel();
  1429. +
  1430. + base::FilePath file_path();
  1431. +
  1432. + base::Time last_modified();
  1433. +
  1434. + private:
  1435. + ~DownloadFiltersTask();
  1436. +
  1437. + void OnDownloadProgress(uint64_t current);
  1438. + void OnResponseStarted(const GURL& final_url,
  1439. + const network::mojom::URLResponseHead& response_head);
  1440. + void OnDownloadComplete(base::FilePath file_path);
  1441. + void OnHeadersDownloadComplete(scoped_refptr<net::HttpResponseHeaders> headers);
  1442. + void createSimpleURLLoader(bool);
  1443. + void internalDownload();
  1444. +
  1445. + // Called when the task has completed either because the task has run or
  1446. + // it has been canceled.
  1447. + void TaskComplete(Error error);
  1448. +
  1449. + base::ThreadChecker thread_checker_;
  1450. + scoped_refptr<network::SharedURLLoaderFactory> shared_url_network_factory_;
  1451. + const bool is_foreground_;
  1452. + Callback complete_callback_;
  1453. + std::unique_ptr<network::SimpleURLLoader> simple_url_loader_;
  1454. +
  1455. + // fields populated while downloading
  1456. + base::TimeTicks download_start_time_;
  1457. + GURL final_url_, filters_url_;
  1458. + int response_code_;
  1459. + base::Time last_modified_, min_last_modified_;
  1460. + base::FilePath file_path_;
  1461. +
  1462. + friend class base::RefCounted<DownloadFiltersTask>;
  1463. + DISALLOW_COPY_AND_ASSIGN(DownloadFiltersTask);
  1464. +};
  1465. +
  1466. +} // namespace update_client
  1467. +
  1468. +#endif // COMPONENTS_DOWNLOAD_FILTERS_TASK_H_
  1469. diff --git a/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager.cc b/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager.cc
  1470. --- a/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager.cc
  1471. +++ b/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager.cc
  1472. @@ -651,6 +651,17 @@ ContentSubresourceFilterThrottleManager::
  1473. throttle->NotifyPageActivationWithRuleset(EnsureRulesetHandle(),
  1474. ad_tagging_state);
  1475. }
  1476. +
  1477. + //TODO: could use same logic as in SubresourceFilterSafeBrowsingActivationThrottle::NotifyResult()
  1478. + {
  1479. + subresource_filter::ActivationDecision ignored_decision;
  1480. + mojom::ActivationState ad_filtering_state;
  1481. + ad_filtering_state.activation_level = profile_interaction_manager_->OnPageActivationComputed(
  1482. + navigation_handle, mojom::ActivationLevel::kEnabled, &ignored_decision);
  1483. + throttle->NotifyPageActivationWithRuleset(EnsureRulesetHandle(),
  1484. + ad_filtering_state);
  1485. + }
  1486. +
  1487. return throttle;
  1488. }
  1489. diff --git a/components/subresource_filter/content/browser/ruleset_service.cc b/components/subresource_filter/content/browser/ruleset_service.cc
  1490. --- a/components/subresource_filter/content/browser/ruleset_service.cc
  1491. +++ b/components/subresource_filter/content/browser/ruleset_service.cc
  1492. @@ -47,9 +47,7 @@ namespace {
  1493. void RecordIndexAndWriteRulesetResult(
  1494. RulesetService::IndexAndWriteRulesetResult result) {
  1495. - UMA_HISTOGRAM_ENUMERATION(
  1496. - "SubresourceFilter.WriteRuleset.Result", static_cast<int>(result),
  1497. - static_cast<int>(RulesetService::IndexAndWriteRulesetResult::MAX));
  1498. + VLOG(1) << "SubresourceFilter.WriteRuleset.Result: " << static_cast<int>(result);
  1499. }
  1500. // Implements operations on a `sentinel file`, which is used as a safeguard to
  1501. @@ -229,10 +227,13 @@ RulesetService::RulesetService(
  1502. RulesetService::~RulesetService() {}
  1503. void RulesetService::IndexAndStoreAndPublishRulesetIfNeeded(
  1504. - const UnindexedRulesetInfo& unindexed_ruleset_info) {
  1505. - if (unindexed_ruleset_info.content_version.empty())
  1506. + const UnindexedRulesetInfo& unindexed_ruleset_info, bool ignore_recent_version) {
  1507. + if (unindexed_ruleset_info.content_version.empty()) {
  1508. + LOG(INFO) << "RulesetService: ignoring update with empty version.";
  1509. return;
  1510. + }
  1511. + if (!ignore_recent_version) {
  1512. // Trying to store a ruleset with the same version for a second time would
  1513. // not only be futile, but would fail on Windows due to "File System
  1514. // Tunneling" as long as the previously stored copy of the rules is still
  1515. @@ -242,13 +243,16 @@ void RulesetService::IndexAndStoreAndPublishRulesetIfNeeded(
  1516. if (most_recently_indexed_version.IsCurrentFormatVersion() &&
  1517. most_recently_indexed_version.content_version ==
  1518. unindexed_ruleset_info.content_version) {
  1519. + LOG(INFO) << "RulesetService: ignoring update with equal or older version.";
  1520. return;
  1521. }
  1522. + }
  1523. // Before initialization, retain information about the most recently supplied
  1524. // unindexed ruleset, to be processed during initialization.
  1525. if (!is_initialized_) {
  1526. queued_unindexed_ruleset_info_ = unindexed_ruleset_info;
  1527. + LOG(INFO) << "RulesetService: ignoring update while not initialized.";
  1528. return;
  1529. }
  1530. @@ -267,6 +271,18 @@ IndexedRulesetVersion RulesetService::GetMostRecentlyIndexedVersion() const {
  1531. IndexedRulesetVersion RulesetService::IndexAndWriteRuleset(
  1532. const base::FilePath& indexed_ruleset_base_dir,
  1533. const UnindexedRulesetInfo& unindexed_ruleset_info) {
  1534. + IndexedRulesetVersion version = IndexAndWriteRulesetInternal(indexed_ruleset_base_dir, unindexed_ruleset_info);
  1535. + // cleanup temporary file when done
  1536. + if (unindexed_ruleset_info.delete_ruleset_path) {
  1537. + base::DeleteFile(unindexed_ruleset_info.ruleset_path);
  1538. + }
  1539. + return version;
  1540. +}
  1541. +
  1542. +// static
  1543. +IndexedRulesetVersion RulesetService::IndexAndWriteRulesetInternal(
  1544. + const base::FilePath& indexed_ruleset_base_dir,
  1545. + const UnindexedRulesetInfo& unindexed_ruleset_info) {
  1546. base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
  1547. base::BlockingType::MAY_BLOCK);
  1548. @@ -274,6 +290,7 @@ IndexedRulesetVersion RulesetService::IndexAndWriteRuleset(
  1549. unindexed_ruleset_info);
  1550. if (!unindexed_ruleset_stream_generator.ruleset_stream()) {
  1551. + LOG(WARNING) << "RulesetService: failed to open: " << unindexed_ruleset_info.ruleset_path;
  1552. RecordIndexAndWriteRulesetResult(
  1553. IndexAndWriteRulesetResult::FAILED_OPENING_UNINDEXED_RULESET);
  1554. return IndexedRulesetVersion();
  1555. @@ -287,6 +304,7 @@ IndexedRulesetVersion RulesetService::IndexAndWriteRuleset(
  1556. indexed_ruleset_base_dir, indexed_version);
  1557. if (!base::CreateDirectory(indexed_ruleset_version_dir)) {
  1558. + LOG(WARNING) << "RulesetService: failed to create version dir: " << indexed_ruleset_version_dir;
  1559. RecordIndexAndWriteRulesetResult(
  1560. IndexAndWriteRulesetResult::FAILED_CREATING_VERSION_DIR);
  1561. return IndexedRulesetVersion();
  1562. @@ -312,6 +330,7 @@ IndexedRulesetVersion RulesetService::IndexAndWriteRuleset(
  1563. RulesetIndexer indexer;
  1564. if (!(*g_index_ruleset_func)(&unindexed_ruleset_stream_generator, &indexer)) {
  1565. + LOG(WARNING) << "RulesetService: failed parsing.";
  1566. RecordIndexAndWriteRulesetResult(
  1567. IndexAndWriteRulesetResult::FAILED_PARSING_UNINDEXED_RULESET);
  1568. return IndexedRulesetVersion();
  1569. @@ -332,6 +351,8 @@ IndexedRulesetVersion RulesetService::IndexAndWriteRuleset(
  1570. if (result != IndexAndWriteRulesetResult::SUCCESS)
  1571. return IndexedRulesetVersion();
  1572. + LOG(INFO) << "RulesetService: successful parsing.";
  1573. +
  1574. DCHECK(indexed_version.IsValid());
  1575. return indexed_version;
  1576. }
  1577. @@ -456,6 +477,7 @@ void RulesetService::IndexAndStoreRuleset(
  1578. void RulesetService::OnWrittenRuleset(WriteRulesetCallback result_callback,
  1579. const IndexedRulesetVersion& version) {
  1580. DCHECK(!result_callback.is_null());
  1581. + LOG(INFO) << "RulesetService: valid version: " << version.IsValid();
  1582. if (!version.IsValid())
  1583. return;
  1584. version.SaveToPrefs(local_state_);
  1585. @@ -468,7 +490,6 @@ void RulesetService::OpenAndPublishRuleset(
  1586. IndexedRulesetLocator::GetRulesetDataFilePath(
  1587. IndexedRulesetLocator::GetSubdirectoryPathForVersion(
  1588. indexed_ruleset_base_dir_, version));
  1589. -
  1590. publisher_->TryOpenAndSetRulesetFile(
  1591. file_path, version.checksum,
  1592. base::BindOnce(&RulesetService::OnRulesetSet, AsWeakPtr()));
  1593. diff --git a/components/subresource_filter/content/browser/ruleset_service.h b/components/subresource_filter/content/browser/ruleset_service.h
  1594. --- a/components/subresource_filter/content/browser/ruleset_service.h
  1595. +++ b/components/subresource_filter/content/browser/ruleset_service.h
  1596. @@ -184,7 +184,7 @@ class RulesetService : public base::SupportsWeakPtr<RulesetService> {
  1597. //
  1598. // Virtual so that it can be mocked out in tests.
  1599. virtual void IndexAndStoreAndPublishRulesetIfNeeded(
  1600. - const UnindexedRulesetInfo& unindexed_ruleset_info);
  1601. + const UnindexedRulesetInfo& unindexed_ruleset_info, bool ignore_recent_version = false);
  1602. // Get the ruleset version associated with the current local_state_.
  1603. IndexedRulesetVersion GetMostRecentlyIndexedVersion() const;
  1604. @@ -217,6 +217,11 @@ class RulesetService : public base::SupportsWeakPtr<RulesetService> {
  1605. const base::FilePath& indexed_ruleset_base_dir,
  1606. const UnindexedRulesetInfo& unindexed_ruleset_info);
  1607. + // internal function used to wrap the temporary file deletion for unindexed rulesets
  1608. + static IndexedRulesetVersion IndexAndWriteRulesetInternal(
  1609. + const base::FilePath& indexed_ruleset_base_dir,
  1610. + const UnindexedRulesetInfo& unindexed_ruleset_info);
  1611. +
  1612. // Reads the rules via the |unindexed_ruleset_stream_generator|, and indexes
  1613. // them using |indexer|. Returns whether the entire ruleset could be parsed.
  1614. static bool IndexRuleset(
  1615. diff --git a/components/subresource_filter/content/browser/ruleset_version.h b/components/subresource_filter/content/browser/ruleset_version.h
  1616. --- a/components/subresource_filter/content/browser/ruleset_version.h
  1617. +++ b/components/subresource_filter/content/browser/ruleset_version.h
  1618. @@ -53,6 +53,10 @@ struct UnindexedRulesetInfo {
  1619. // can be indicated not only by setting |license_path| to empty, but also by
  1620. // setting it to any non existent path.
  1621. base::FilePath license_path;
  1622. +
  1623. + // Whether to delete or not the ruleset path once done indexing; useful for disposal
  1624. + // of temporary files.
  1625. + bool delete_ruleset_path;
  1626. };
  1627. // Encapsulates the combination of the binary format version of the indexed
  1628. diff --git a/components/subresource_filter/content/browser/verified_ruleset_dealer.cc b/components/subresource_filter/content/browser/verified_ruleset_dealer.cc
  1629. --- a/components/subresource_filter/content/browser/verified_ruleset_dealer.cc
  1630. +++ b/components/subresource_filter/content/browser/verified_ruleset_dealer.cc
  1631. @@ -11,6 +11,7 @@
  1632. #include "base/check.h"
  1633. #include "base/files/file.h"
  1634. #include "base/location.h"
  1635. +#include "base/logging.h"
  1636. #include "base/metrics/histogram_macros.h"
  1637. #include "base/notreached.h"
  1638. #include "base/task_runner_util.h"
  1639. @@ -39,6 +40,9 @@ RulesetFilePtr VerifiedRulesetDealer::OpenAndSetRulesetFile(
  1640. TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("loading"),
  1641. "VerifiedRulesetDealer::OpenAndSetRulesetFile", "file_valid",
  1642. file->IsValid());
  1643. +
  1644. + LOG(INFO) << "OpenAndSetRulesetFile: " << file_path << " is valid: " << file->IsValid();
  1645. +
  1646. if (file->IsValid()) {
  1647. SetRulesetFile(file->Duplicate());
  1648. expected_checksum_ = expected_checksum;
  1649. diff --git a/components/subresource_filter/core/browser/subresource_filter_features.cc b/components/subresource_filter/core/browser/subresource_filter_features.cc
  1650. --- a/components/subresource_filter/core/browser/subresource_filter_features.cc
  1651. +++ b/components/subresource_filter/core/browser/subresource_filter_features.cc
  1652. @@ -55,69 +55,7 @@ class CommaSeparatedStrings {
  1653. const std::vector<base::StringPiece> pieces_;
  1654. };
  1655. -std::string TakeVariationParamOrReturnEmpty(
  1656. - std::map<std::string, std::string>* params,
  1657. - const std::string& key) {
  1658. - auto it = params->find(key);
  1659. - if (it == params->end())
  1660. - return std::string();
  1661. - std::string value = std::move(it->second);
  1662. - params->erase(it);
  1663. - return value;
  1664. -}
  1665. -
  1666. -mojom::ActivationLevel ParseActivationLevel(
  1667. - const base::StringPiece activation_level) {
  1668. - if (base::LowerCaseEqualsASCII(activation_level, kActivationLevelEnabled))
  1669. - return mojom::ActivationLevel::kEnabled;
  1670. - else if (base::LowerCaseEqualsASCII(activation_level, kActivationLevelDryRun))
  1671. - return mojom::ActivationLevel::kDryRun;
  1672. - return mojom::ActivationLevel::kDisabled;
  1673. -}
  1674. -
  1675. -ActivationScope ParseActivationScope(const base::StringPiece activation_scope) {
  1676. - if (base::LowerCaseEqualsASCII(activation_scope, kActivationScopeAllSites))
  1677. - return ActivationScope::ALL_SITES;
  1678. - else if (base::LowerCaseEqualsASCII(activation_scope,
  1679. - kActivationScopeActivationList))
  1680. - return ActivationScope::ACTIVATION_LIST;
  1681. - return ActivationScope::NO_SITES;
  1682. -}
  1683. -
  1684. -ActivationList ParseActivationList(std::string activation_lists_string) {
  1685. - CommaSeparatedStrings activation_lists(std::move(activation_lists_string));
  1686. - if (activation_lists.CaseInsensitiveContains(
  1687. - kActivationListPhishingInterstitial)) {
  1688. - return ActivationList::PHISHING_INTERSTITIAL;
  1689. - } else if (activation_lists.CaseInsensitiveContains(
  1690. - kActivationListSocialEngineeringAdsInterstitial)) {
  1691. - return ActivationList::SOCIAL_ENG_ADS_INTERSTITIAL;
  1692. - } else if (activation_lists.CaseInsensitiveContains(
  1693. - kActivationListSubresourceFilter)) {
  1694. - return ActivationList::SUBRESOURCE_FILTER;
  1695. - } else if (activation_lists.CaseInsensitiveContains(
  1696. - kActivationListBetterAds)) {
  1697. - return ActivationList::BETTER_ADS;
  1698. - }
  1699. - return ActivationList::NONE;
  1700. -}
  1701. -
  1702. -// Will return a value between 0 and 1 inclusive.
  1703. -double ParsePerformanceMeasurementRate(const std::string& rate) {
  1704. - double value = 0.0;
  1705. - if (!base::StringToDouble(rate, &value) || value < 0)
  1706. - return 0.0;
  1707. - return value < 1 ? value : 1;
  1708. -}
  1709. -
  1710. -int ParseInt(const base::StringPiece value) {
  1711. - int result = 0;
  1712. - base::StringToInt(value, &result);
  1713. - return result;
  1714. -}
  1715. -
  1716. -std::vector<Configuration> FillEnabledPresetConfigurations(
  1717. - std::map<std::string, std::string>* params) {
  1718. +std::vector<Configuration> FillEnabledPresetConfigurations() {
  1719. // If ad tagging is enabled, turn on the dryrun automatically.
  1720. bool ad_tagging_enabled = base::FeatureList::IsEnabled(kAdTagging);
  1721. const struct {
  1722. @@ -125,23 +63,16 @@ std::vector<Configuration> FillEnabledPresetConfigurations(
  1723. bool enabled_by_default;
  1724. Configuration (*factory_method)();
  1725. } kAvailablePresetConfigurations[] = {
  1726. - {kPresetLiveRunOnPhishingSites, true,
  1727. + {kPresetLiveRunOnPhishingSites, false,
  1728. &Configuration::MakePresetForLiveRunOnPhishingSites},
  1729. {kPresetPerformanceTestingDryRunOnAllSites, ad_tagging_enabled,
  1730. &Configuration::MakePresetForPerformanceTestingDryRunOnAllSites},
  1731. {kPresetLiveRunForBetterAds, true,
  1732. &Configuration::MakePresetForLiveRunForBetterAds}};
  1733. - CommaSeparatedStrings enabled_presets(
  1734. - TakeVariationParamOrReturnEmpty(params, kEnablePresetsParameterName));
  1735. - CommaSeparatedStrings disabled_presets(
  1736. - TakeVariationParamOrReturnEmpty(params, kDisablePresetsParameterName));
  1737. -
  1738. std::vector<Configuration> enabled_configurations;
  1739. for (const auto& available_preset : kAvailablePresetConfigurations) {
  1740. - if ((enabled_presets.CaseInsensitiveContains(available_preset.name) ||
  1741. - available_preset.enabled_by_default) &&
  1742. - !disabled_presets.CaseInsensitiveContains(available_preset.name)) {
  1743. + if (available_preset.enabled_by_default) {
  1744. enabled_configurations.push_back(available_preset.factory_method());
  1745. }
  1746. }
  1747. @@ -149,46 +80,10 @@ std::vector<Configuration> FillEnabledPresetConfigurations(
  1748. return enabled_configurations;
  1749. }
  1750. -Configuration ParseExperimentalConfiguration(
  1751. - std::map<std::string, std::string>* params) {
  1752. - Configuration configuration;
  1753. -
  1754. - // ActivationConditions:
  1755. - configuration.activation_conditions.activation_scope = ParseActivationScope(
  1756. - TakeVariationParamOrReturnEmpty(params, kActivationScopeParameterName));
  1757. -
  1758. - configuration.activation_conditions.activation_list = ParseActivationList(
  1759. - TakeVariationParamOrReturnEmpty(params, kActivationListsParameterName));
  1760. -
  1761. - configuration.activation_conditions.priority =
  1762. - ParseInt(TakeVariationParamOrReturnEmpty(
  1763. - params, kActivationPriorityParameterName));
  1764. -
  1765. - // ActivationOptions:
  1766. - configuration.activation_options.activation_level = ParseActivationLevel(
  1767. - TakeVariationParamOrReturnEmpty(params, kActivationLevelParameterName));
  1768. -
  1769. - configuration.activation_options.performance_measurement_rate =
  1770. - ParsePerformanceMeasurementRate(TakeVariationParamOrReturnEmpty(
  1771. - params, kPerformanceMeasurementRateParameterName));
  1772. -
  1773. - // GeneralSettings:
  1774. - configuration.general_settings.ruleset_flavor =
  1775. - TakeVariationParamOrReturnEmpty(params, kRulesetFlavorParameterName);
  1776. -
  1777. - return configuration;
  1778. -}
  1779. -
  1780. std::vector<Configuration> ParseEnabledConfigurations() {
  1781. - std::map<std::string, std::string> params;
  1782. - base::GetFieldTrialParamsByFeature(kSafeBrowsingSubresourceFilter, &params);
  1783. -
  1784. std::vector<Configuration> configs;
  1785. if (base::FeatureList::IsEnabled(kSafeBrowsingSubresourceFilter))
  1786. - configs = FillEnabledPresetConfigurations(&params);
  1787. -
  1788. - Configuration experimental_config = ParseExperimentalConfiguration(&params);
  1789. - configs.push_back(std::move(experimental_config));
  1790. + configs = FillEnabledPresetConfigurations();
  1791. return configs;
  1792. }
  1793. diff --git a/components/subresource_filter/core/common/indexed_ruleset.cc b/components/subresource_filter/core/common/indexed_ruleset.cc
  1794. --- a/components/subresource_filter/core/common/indexed_ruleset.cc
  1795. +++ b/components/subresource_filter/core/common/indexed_ruleset.cc
  1796. @@ -5,6 +5,7 @@
  1797. #include "components/subresource_filter/core/common/indexed_ruleset.h"
  1798. #include "base/check.h"
  1799. +#include "base/logging.h"
  1800. #include "base/metrics/histogram_macros.h"
  1801. #include "base/trace_event/trace_event.h"
  1802. #include "components/subresource_filter/core/common/first_party_origin.h"
  1803. @@ -36,8 +37,10 @@ VerifyStatus GetVerifyStatus(const uint8_t* buffer,
  1804. // least once. The verifier detects a subset of the errors detected by the
  1805. // checksum, and is unneeded once expected_checksum is consistently nonzero.
  1806. flatbuffers::Verifier verifier(buffer, size);
  1807. + int local_checksum = LocalGetChecksum(buffer, size);
  1808. + LOG(INFO) << "GetVerifyStatus: expected checksum = 0x" << std::hex << expected_checksum << ", local checksum = 0x" << std::hex << local_checksum;
  1809. if (expected_checksum != 0 &&
  1810. - expected_checksum != LocalGetChecksum(buffer, size)) {
  1811. + expected_checksum != local_checksum) {
  1812. return flat::VerifyIndexedRulesetBuffer(verifier)
  1813. ? VerifyStatus::kChecksumFailVerifierPass
  1814. : VerifyStatus::kChecksumFailVerifierFail;
  1815. diff --git a/content/browser/renderer_host/navigation_throttle_runner.cc b/content/browser/renderer_host/navigation_throttle_runner.cc
  1816. --- a/content/browser/renderer_host/navigation_throttle_runner.cc
  1817. +++ b/content/browser/renderer_host/navigation_throttle_runner.cc
  1818. @@ -184,11 +184,6 @@ void NavigationThrottleRunner::RegisterNavigationThrottles() {
  1819. // than other throttles that might care about those navigations, e.g.
  1820. // throttles handling pages with 407 errors that require extra authentication.
  1821. AddThrottle(HttpErrorNavigationThrottle::MaybeCreateThrottleFor(*request));
  1822. -
  1823. - // Insert all testing NavigationThrottles last.
  1824. - throttles_.insert(throttles_.end(),
  1825. - std::make_move_iterator(testing_throttles.begin()),
  1826. - std::make_move_iterator(testing_throttles.end()));
  1827. }
  1828. NavigationThrottle* NavigationThrottleRunner::GetDeferringThrottle() const {
  1829. --
  1830. 2.20.1