Avoid passing isTrusted in TaskConfigFactory.getTaskConfig()

The isTrusted logic should be abstracted at TaskTree.Node level
rather than at task level. This refactor helps supporting the
upcoming feature which enables referencing tasks from other files.

Change-Id: Icb662a4f3a577df7acd77f42867a6273383f17e4
diff --git a/src/main/java/com/googlesource/gerrit/plugins/task/TaskAttributeFactory.java b/src/main/java/com/googlesource/gerrit/plugins/task/TaskAttributeFactory.java
index 48109ec..b3f269f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/task/TaskAttributeFactory.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/task/TaskAttributeFactory.java
@@ -133,7 +133,7 @@
 
         boolean applicable = matchCache.match(task.applicable);
         if (!task.isVisible) {
-          if (!task.isTrusted || (!applicable && !options.onlyApplicable)) {
+          if (!node.isTrusted() || (!applicable && !options.onlyApplicable)) {
             return Optional.of(unknown());
           }
         }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/task/TaskConfig.java b/src/main/java/com/googlesource/gerrit/plugins/task/TaskConfig.java
index 47283a3..08dcf5c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/task/TaskConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/task/TaskConfig.java
@@ -70,12 +70,12 @@
     public List<String> subTasksFiles;
 
     public boolean isVisible;
-    public boolean isTrusted;
+    public boolean isMasqueraded;
 
-    public TaskBase(SubSectionKey s, boolean isVisible, boolean isTrusted) {
+    public TaskBase(SubSectionKey s, boolean isVisible, boolean isMasqueraded) {
       super(s);
       this.isVisible = isVisible;
-      this.isTrusted = isTrusted;
+      this.isMasqueraded = isMasqueraded;
       applicable = getString(s, KEY_APPLICABLE, null);
       duplicateKey = getString(s, KEY_DUPLICATE_KEY, null);
       exported = getProperties(s, KEY_EXPORT_PREFIX);
@@ -105,8 +105,8 @@
   public class Task extends TaskBase {
     public final TaskKey key;
 
-    public Task(SubSectionKey s, boolean isVisible, boolean isTrusted) {
-      super(s, isVisible, isTrusted);
+    public Task(SubSectionKey s, boolean isVisible, boolean isMasqueraded) {
+      super(s, isVisible, isMasqueraded);
       key = TaskKey.create(s);
     }
 
@@ -150,8 +150,8 @@
   public class TasksFactory extends TaskBase {
     public String namesFactory;
 
-    public TasksFactory(SubSectionKey s, boolean isVisible, boolean isTrusted) {
-      super(s, isVisible, isTrusted);
+    public TasksFactory(SubSectionKey s, boolean isVisible, boolean isMasqueraded) {
+      super(s, isVisible, isMasqueraded);
       namesFactory = getString(s, KEY_NAMES_FACTORY, null);
     }
   }
@@ -217,25 +217,25 @@
 
   protected final FileKey file;
   public boolean isVisible;
-  public boolean isTrusted;
+  public boolean isMasqueraded;
 
-  public TaskConfig(FileKey file, boolean isVisible, boolean isTrusted) {
-    this(file.branch(), file, isVisible, isTrusted);
+  public TaskConfig(FileKey file, boolean isVisible, boolean isMasqueraded) {
+    this(file.branch(), file, isVisible, isMasqueraded);
   }
 
   public TaskConfig(
-      Branch.NameKey masqueraded, FileKey file, boolean isVisible, boolean isTrusted) {
+      Branch.NameKey masqueraded, FileKey file, boolean isVisible, boolean isMasqueraded) {
     super(masqueraded, file.file());
     this.file = file;
     this.isVisible = isVisible;
-    this.isTrusted = isTrusted;
+    this.isMasqueraded = isMasqueraded;
   }
 
   protected List<Task> getTasks(String type) {
     List<Task> tasks = new ArrayList<>();
     // No need to get a task with no name (what would we call it?)
     for (String task : cfg.getSubsections(type)) {
-      tasks.add(new Task(subSectionKey(type, task), isVisible, isTrusted));
+      tasks.add(new Task(subSectionKey(type, task), isVisible, isMasqueraded));
     }
     return tasks;
   }
@@ -253,11 +253,11 @@
     SubSectionKey subSection = subSectionKey(SECTION_TASK, name);
     return getNames(subSection).isEmpty()
         ? Optional.empty()
-        : Optional.of(new Task(subSection, isVisible, isTrusted));
+        : Optional.of(new Task(subSection, isVisible, isMasqueraded));
   }
 
   public TasksFactory getTasksFactory(String name) {
-    return new TasksFactory(subSectionKey(SECTION_TASKS_FACTORY, name), isVisible, isTrusted);
+    return new TasksFactory(subSectionKey(SECTION_TASKS_FACTORY, name), isVisible, isMasqueraded);
   }
 
   public NamesFactory getNamesFactory(String name) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/task/TaskConfigFactory.java b/src/main/java/com/googlesource/gerrit/plugins/task/TaskConfigFactory.java
index bbf424b..feedd6a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/task/TaskConfigFactory.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/task/TaskConfigFactory.java
@@ -59,7 +59,7 @@
   }
 
   public TaskConfig getRootConfig() throws ConfigInvalidException, IOException {
-    return getTaskConfig(FileKey.create(getRootBranch(), DEFAULT), true);
+    return getTaskConfig(FileKey.create(getRootBranch(), DEFAULT));
   }
 
   public void masquerade(PatchSetArgument psa) {
@@ -70,23 +70,23 @@
     return new Branch.NameKey(allProjects, "refs/meta/config");
   }
 
-  public TaskConfig getTaskConfig(FileKey file, boolean isTrusted)
-      throws ConfigInvalidException, IOException {
+  public TaskConfig getTaskConfig(FileKey file) throws ConfigInvalidException, IOException {
     Branch.NameKey branch = file.branch();
     PatchSetArgument psa = psaMasquerades.get(branch);
     boolean visible = true; // invisible psas are filtered out by commandline
+    boolean isMasqueraded = false;
     if (psa == null) {
       visible = canRead(branch);
     } else {
-      isTrusted = false;
+      isMasqueraded = true;
       branch = new Branch.NameKey(psa.change.getProject(), psa.patchSet.getId().toRefName());
     }
 
     Project.NameKey project = file.branch().getParentKey();
     TaskConfig cfg =
-        psa == null
-            ? new TaskConfig(file, visible, isTrusted)
-            : new TaskConfig(branch, file, visible, isTrusted);
+        isMasqueraded
+            ? new TaskConfig(branch, file, visible, isMasqueraded)
+            : new TaskConfig(file, visible, isMasqueraded);
     try (Repository git = gitMgr.openRepository(project)) {
       cfg.load(project, git);
     } catch (IOException e) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/task/TaskTree.java b/src/main/java/com/googlesource/gerrit/plugins/task/TaskTree.java
index 456a279..3657974 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/task/TaskTree.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/task/TaskTree.java
@@ -185,6 +185,10 @@
     protected Properties getProperties() {
       return Properties.EMPTY_PARENT;
     }
+
+    protected boolean isTrusted() {
+      return true;
+    }
   }
 
   public class Node extends NodeList {
@@ -351,7 +355,7 @@
 
     protected List<Task> getPreloadedTasks(FileKey file)
         throws ConfigInvalidException, IOException {
-      return preloader.getTasks(taskFactory.getTaskConfig(file, task.isTrusted));
+      return preloader.getTasks(taskFactory.getTaskConfig(file));
     }
 
     @Override
@@ -362,6 +366,11 @@
     public boolean isChange() {
       return false;
     }
+
+    @Override
+    protected boolean isTrusted() {
+      return parent.isTrusted() && !task.isMasqueraded;
+    }
   }
 
   protected String resolveTaskFileName(String file) throws ConfigInvalidException {