summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_managed_mode_api.cc
blob: 44ac94019ff750b7177826a8cf3d3127d74f75b7 (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
// Copyright (c) 2012 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.

// Implementation of the Chrome Extensions Managed Mode API.

#include "chrome/browser/extensions/extension_managed_mode_api.h"

#include <string>

#include "base/bind.h"
#include "base/json/json_writer.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/extension_event_router.h"
#include "chrome/browser/extensions/extension_preference_api_constants.h"
#include "chrome/browser/managed_mode.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_details.h"

namespace {

// Event that is fired when we enter or leave managed mode.
const char kChangeEventName[] = "experimental.managedMode.onChange";

// Key to report whether the attempt to enter managed mode succeeded.
const char kEnterSuccessKey[] = "success";

}  // namespace

namespace keys = extension_preference_api_constants;

ExtensionManagedModeEventRouter::ExtensionManagedModeEventRouter(
    Profile* profile) : profile_(profile) {
}

void ExtensionManagedModeEventRouter::Init() {
  registrar_.Init(g_browser_process->local_state());
  registrar_.Add(prefs::kInManagedMode, this);
}

ExtensionManagedModeEventRouter::~ExtensionManagedModeEventRouter() {
}

void ExtensionManagedModeEventRouter::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  DCHECK_EQ(chrome::NOTIFICATION_PREF_CHANGED, type);
  const std::string& pref_name =
      *content::Details<std::string>(details).ptr();
  DCHECK_EQ(std::string(prefs::kInManagedMode), pref_name);

  ListValue args;
  DictionaryValue* dict = new DictionaryValue();
  args.Append(dict);
  dict->SetBoolean(extension_preference_api_constants::kValue,
      g_browser_process->local_state()->GetBoolean(prefs::kInManagedMode));
  std::string json_args;
  base::JSONWriter::Write(&args, &json_args);
  ExtensionEventRouter* event_router = profile_->GetExtensionEventRouter();
  event_router->DispatchEventToRenderers(kChangeEventName, json_args,
                                         NULL, GURL());
}

GetManagedModeFunction::~GetManagedModeFunction() { }

bool GetManagedModeFunction::RunImpl() {
  bool in_managed_mode = ManagedMode::IsInManagedMode();

  scoped_ptr<DictionaryValue> result(new DictionaryValue);
  result->SetBoolean(keys::kValue, in_managed_mode);
  result_.reset(result.release());
  return true;
}

EnterManagedModeFunction::~EnterManagedModeFunction() { }

bool EnterManagedModeFunction::RunImpl() {
  ManagedMode::EnterManagedMode(
      profile(),
      base::Bind(&EnterManagedModeFunction::SendResult, this));
  return true;
}

void EnterManagedModeFunction::SendResult(bool success) {
  scoped_ptr<DictionaryValue> result(new DictionaryValue);
  result->SetBoolean(kEnterSuccessKey, success);
  result_.reset(result.release());
  SendResponse(true);
}