summaryrefslogtreecommitdiffstats
path: root/devtools/client/application/src/reducers/manifest-state.js
blob: 1f7fbf0bb61c395e4c0095f23d68a99a5e7a1821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const {
  MANIFEST_CATEGORIES,
  MANIFEST_ISSUE_LEVELS,
  MANIFEST_MEMBER_VALUE_TYPES,
  FETCH_MANIFEST_FAILURE,
  FETCH_MANIFEST_START,
  FETCH_MANIFEST_SUCCESS,
  RESET_MANIFEST,
} = require("resource://devtools/client/application/src/constants.js");

function _processRawManifestIcons(rawIcons) {
  // NOTE: about `rawIcons` array we are getting from platform:
  // - Icons that do not comform to the spec are filtered out
  // - We will always get a `src`
  // - We will always get `purpose` with a value (default is `["any"]`)
  // - `sizes` may be undefined
  // - `type` may be undefined
  return rawIcons.map(icon => {
    return {
      key: {
        sizes: Array.isArray(icon.sizes) ? icon.sizes.join(" ") : icon.sizes,
        contentType: icon.type,
      },
      value: {
        src: icon.src,
        purpose: icon.purpose.join(" "),
      },
      type: MANIFEST_MEMBER_VALUE_TYPES.ICON,
    };
  });
}

function _processRawManifestMembers(rawManifest) {
  function getCategoryForMember(key) {
    switch (key) {
      case "name":
      case "short_name":
        return MANIFEST_CATEGORIES.IDENTITY;
      default:
        return MANIFEST_CATEGORIES.PRESENTATION;
    }
  }

  function getValueTypeForMember(key) {
    switch (key) {
      case "start_url":
      case "scope":
        return MANIFEST_MEMBER_VALUE_TYPES.URL;
      case "theme_color":
      case "background_color":
        return MANIFEST_MEMBER_VALUE_TYPES.COLOR;
      default:
        return MANIFEST_MEMBER_VALUE_TYPES.STRING;
    }
  }

  const res = {
    [MANIFEST_CATEGORIES.IDENTITY]: [],
    [MANIFEST_CATEGORIES.PRESENTATION]: [],
  };

  // filter out extra metadata members (those with moz_ prefix) and icons
  const rawMembers = Object.entries(rawManifest).filter(
    ([key]) => !key.startsWith("moz_") && !(key === "icons")
  );

  for (const [key, value] of rawMembers) {
    const category = getCategoryForMember(key);
    const type = getValueTypeForMember(key);
    res[category].push({ key, value, type });
  }

  return res;
}

function _processRawManifestIssues(issues) {
  return issues.map(x => {
    return {
      level: x.warn
        ? MANIFEST_ISSUE_LEVELS.WARNING
        : MANIFEST_ISSUE_LEVELS.ERROR,
      message: x.warn || x.error,
      type: x.type || null,
    };
  });
}

function _processRawManifest(rawManifest) {
  const res = {
    url: rawManifest.moz_manifest_url,
  };

  // group manifest members by category
  Object.assign(res, _processRawManifestMembers(rawManifest));
  // process icons
  res.icons = _processRawManifestIcons(rawManifest.icons || []);
  // process error messages
  res.validation = _processRawManifestIssues(rawManifest.moz_validation || []);

  return res;
}

function ManifestState() {
  return {
    errorMessage: "",
    isLoading: false,
    manifest: undefined,
  };
}

function manifestReducer(state = ManifestState(), action) {
  switch (action.type) {
    case FETCH_MANIFEST_START:
      return Object.assign({}, state, {
        isLoading: true,
        mustLoadManifest: false,
      });

    case FETCH_MANIFEST_FAILURE:
      const { error } = action;
      // If we add a redux middleware to log errors, we should move the
      // console.error below there.
      console.error(error);
      return Object.assign({}, state, {
        errorMessage: error,
        isLoading: false,
        manifest: null,
      });

    case FETCH_MANIFEST_SUCCESS:
      // NOTE: we don't get an error when the page does not have a manifest,
      // but a `null` value there.
      const { manifest } = action;
      return Object.assign({}, state, {
        errorMessage: "",
        isLoading: false,
        manifest: manifest ? _processRawManifest(manifest) : null,
      });

    case RESET_MANIFEST:
      const defaultState = ManifestState();
      return defaultState;

    default:
      return state;
  }
}

module.exports = {
  ManifestState,
  manifestReducer,
};