summaryrefslogtreecommitdiffstats
path: root/extensions/common/manifest_handlers/incognito_info.cc
blob: b7dc8b28138c3aa6b91fd47bb213d209a2575a6a (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
// Copyright (c) 2013 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/manifest_handlers/incognito_info.h"

#include "base/memory/scoped_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_constants.h"

namespace extensions {

namespace keys = manifest_keys;

IncognitoInfo::IncognitoInfo(Mode mode) : mode(mode) {}

IncognitoInfo::~IncognitoInfo() {
}

// static
bool IncognitoInfo::IsSplitMode(const Extension* extension) {
  IncognitoInfo* info = static_cast<IncognitoInfo*>(
      extension->GetManifestData(keys::kIncognito));
  return info ? info->mode == Mode::SPLIT : false;
}

// static
bool IncognitoInfo::IsIncognitoAllowed(const Extension* extension) {
  IncognitoInfo* info =
      static_cast<IncognitoInfo*>(extension->GetManifestData(keys::kIncognito));
  return info ? info->mode != Mode::NOT_ALLOWED : true;
}

IncognitoHandler::IncognitoHandler() {
}

IncognitoHandler::~IncognitoHandler() {
}

bool IncognitoHandler::Parse(Extension* extension, base::string16* error) {
  // Extensions and Chrome apps default to spanning mode.
  // Hosted and legacy packaged apps default to split mode.
  IncognitoInfo::Mode mode =
      extension->is_hosted_app() || extension->is_legacy_packaged_app()
          ? IncognitoInfo::Mode::SPLIT
          : IncognitoInfo::Mode::SPANNING;
  if (!extension->manifest()->HasKey(keys::kIncognito)) {
    extension->SetManifestData(keys::kIncognito, new IncognitoInfo(mode));
    return true;
  }

  std::string incognito_string;
  if (!extension->manifest()->GetString(keys::kIncognito, &incognito_string)) {
    *error = base::ASCIIToUTF16(manifest_errors::kInvalidIncognitoBehavior);
    return false;
  }

  if (incognito_string == manifest_values::kIncognitoSplit) {
    mode = IncognitoInfo::Mode::SPLIT;
  } else if (incognito_string == manifest_values::kIncognitoSpanning) {
    mode = IncognitoInfo::Mode::SPANNING;
  } else if (incognito_string == manifest_values::kIncognitoNotAllowed) {
    mode = IncognitoInfo::Mode::NOT_ALLOWED;
  } else {
    *error = base::ASCIIToUTF16(manifest_errors::kInvalidIncognitoBehavior);
    return false;
  }

  extension->SetManifestData(keys::kIncognito, new IncognitoInfo(mode));
  return true;
}

bool IncognitoHandler::AlwaysParseForType(Manifest::Type type) const {
  return true;
}

const std::vector<std::string> IncognitoHandler::Keys() const {
  return SingleKey(keys::kIncognito);
}

}  // namespace extensions