summaryrefslogtreecommitdiffstats
path: root/extensions/common/permissions/media_galleries_permission.cc
blob: 6a432686a56e90806c34387f17130091c9b21d3a (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/common/permissions/media_galleries_permission.h"

#include <stddef.h>

#include <set>
#include <string>

#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "extensions/common/permissions/permissions_info.h"
#include "grit/extensions_strings.h"
#include "ui/base/l10n/l10n_util.h"

namespace extensions {

namespace {

// copyTo permission requires delete permission as a prerequisite.
// delete permission requires read permission as a prerequisite.
bool IsValidPermissionSet(bool has_read, bool has_copy_to, bool has_delete,
                          std::string* error) {
  if (has_copy_to) {
    if (has_read && has_delete)
      return true;
    if (error)
      *error = "copyTo permission requires read and delete permissions";
    return false;
  }
  if (has_delete) {
    if (has_read)
      return true;
    if (error)
      *error = "delete permission requires read permission";
    return false;
  }
  return true;
}

// Adds the permissions from the |data_set| to |ids|.
void AddPermissionsToLists(
    const std::set<MediaGalleriesPermissionData>& data_set,
    PermissionIDSet* ids) {
  // TODO(sashab): Once GetMessages() is deprecated, move this logic back into
  // GetPermissions().
  bool has_all_auto_detected = false;
  bool has_read = false;
  bool has_copy_to = false;
  bool has_delete = false;

  for (std::set<MediaGalleriesPermissionData>::const_iterator it =
           data_set.begin();
       it != data_set.end(); ++it) {
    if (it->permission() ==
        MediaGalleriesPermission::kAllAutoDetectedPermission)
      has_all_auto_detected = true;
    else if (it->permission() == MediaGalleriesPermission::kReadPermission)
      has_read = true;
    else if (it->permission() == MediaGalleriesPermission::kCopyToPermission)
      has_copy_to = true;
    else if (it->permission() == MediaGalleriesPermission::kDeletePermission)
      has_delete = true;
  }

  if (!IsValidPermissionSet(has_read, has_copy_to, has_delete, NULL)) {
    NOTREACHED();
    return;
  }

  // If |has_all_auto_detected| is false, then Chrome will prompt the user at
  // runtime when the extension call the getMediaGalleries API.
  if (!has_all_auto_detected)
    return;
  // No access permission case.
  if (!has_read)
    return;

  // Separate PermissionMessage IDs for read, copyTo, and delete. Otherwise an
  // extension can silently gain new access capabilities.
  ids->insert(APIPermission::kMediaGalleriesAllGalleriesRead);

  // For copyTo and delete, the proper combined permission message will be
  // derived in ChromePermissionMessageProvider::GetWarningMessages(), such
  // that the user get 1 entry for all media galleries access permissions,
  // rather than several separate entries.
  if (has_copy_to)
    ids->insert(APIPermission::kMediaGalleriesAllGalleriesCopyTo);
  if (has_delete)
    ids->insert(APIPermission::kMediaGalleriesAllGalleriesDelete);
}

}  // namespace

const char MediaGalleriesPermission::kAllAutoDetectedPermission[] =
    "allAutoDetected";
const char MediaGalleriesPermission::kScanPermission[] = "scan";
const char MediaGalleriesPermission::kReadPermission[] = "read";
const char MediaGalleriesPermission::kCopyToPermission[] = "copyTo";
const char MediaGalleriesPermission::kDeletePermission[] = "delete";

MediaGalleriesPermission::MediaGalleriesPermission(
    const APIPermissionInfo* info)
  : SetDisjunctionPermission<MediaGalleriesPermissionData,
                             MediaGalleriesPermission>(info) {
}

MediaGalleriesPermission::~MediaGalleriesPermission() {
}

bool MediaGalleriesPermission::FromValue(
    const base::Value* value,
    std::string* error,
    std::vector<std::string>* unhandled_permissions) {
  size_t unhandled_permissions_count = 0;
  if (unhandled_permissions)
    unhandled_permissions_count = unhandled_permissions->size();
  bool parsed_ok =
      SetDisjunctionPermission<MediaGalleriesPermissionData,
                               MediaGalleriesPermission>::FromValue(
                                   value, error, unhandled_permissions);
  if (unhandled_permissions) {
    for (size_t i = unhandled_permissions_count;
         i < unhandled_permissions->size();
         i++) {
      (*unhandled_permissions)[i] =
          "{\"mediaGalleries\": [" + (*unhandled_permissions)[i] + "]}";
    }
  }
  if (!parsed_ok)
    return false;

  bool has_read = false;
  bool has_copy_to = false;
  bool has_delete = false;
  for (std::set<MediaGalleriesPermissionData>::const_iterator it =
      data_set_.begin(); it != data_set_.end(); ++it) {
    if (it->permission() == kAllAutoDetectedPermission ||
        it->permission() == kScanPermission) {
      continue;
    }
    if (it->permission() == kReadPermission) {
      has_read = true;
      continue;
    }
    if (it->permission() == kCopyToPermission) {
      has_copy_to = true;
      continue;
    }
    if (it->permission() == kDeletePermission) {
      has_delete = true;
      continue;
    }

    // No other permissions, so reaching this means
    // MediaGalleriesPermissionData is probably out of sync in some way.
    // Fail so developers notice this.
    NOTREACHED();
    return false;
  }

  return IsValidPermissionSet(has_read, has_copy_to, has_delete, error);
}

PermissionIDSet MediaGalleriesPermission::GetPermissions() const {
  PermissionIDSet result;
  AddPermissionsToLists(data_set_, &result);
  return result;
}

}  // namespace extensions