summaryrefslogtreecommitdiffstats
path: root/chrome/browser/remoting/firewall_traversal_tab_helper.cc
blob: b59c8ca33efd1abf8c8c429b7a5b0930c43f65d7 (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
// Copyright (c) 2011 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 "chrome/browser/remoting/firewall_traversal_tab_helper.h"

#include "base/stringprintf.h"
#include "base/json/json_writer.h"
#include "base/values.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/browser/renderer_host/render_view_host.h"
#include "content/common/notification_service.h"
#include "content/common/view_messages.h"
#include "ipc/ipc_message_macros.h"

FirewallTraversalTabHelper::FirewallTraversalTabHelper(
    TabContents* tab_contents)
    : TabContentsObserver(tab_contents) {
  // Register for notifications about all interested prefs change.
  Profile* profile =
      Profile::FromBrowserContext(tab_contents->browser_context());
  PrefService* prefs = profile->GetPrefs();
  pref_change_registrar_.Init(prefs);
  if (prefs) {
    pref_change_registrar_.Add(prefs::kRemoteAccessClientFirewallTraversal,
                               this);
    // kRemoteAccessHostFirewallTraversal intentionally not added because
    // the host plugin is going to track it itself.
  }
}

FirewallTraversalTabHelper::~FirewallTraversalTabHelper() {
  // We don't want any notifications while we're running our destructor.
  pref_change_registrar_.RemoveAll();
}

// static
void FirewallTraversalTabHelper::RegisterUserPrefs(PrefService* prefs) {
  prefs->RegisterBooleanPref(prefs::kRemoteAccessClientFirewallTraversal,
                             true,
                             PrefService::UNSYNCABLE_PREF);
  prefs->RegisterBooleanPref(prefs::kRemoteAccessHostFirewallTraversal,
                             true,
                             PrefService::UNSYNCABLE_PREF);
}

void FirewallTraversalTabHelper::Observe(int type,
                                const NotificationSource& source,
                                const NotificationDetails& details) {
  switch (type) {
    case chrome::NOTIFICATION_PREF_CHANGED: {
      std::string* pref_name_in = Details<std::string>(details).ptr();
      Profile* profile =
          Profile::FromBrowserContext(tab_contents()->browser_context());
      DCHECK(Source<PrefService>(source).ptr() == profile->GetPrefs());
      if (*pref_name_in == prefs::kRemoteAccessClientFirewallTraversal) {
        UpdateFirewallTraversalState();
      } else {
        NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
      }
      break;
    }
    default:
      NOTREACHED();
  }
}

void FirewallTraversalTabHelper::UpdateFirewallTraversalState() {
  const char* pref_name = prefs::kRemoteAccessClientFirewallTraversal;
  Profile* profile =
      Profile::FromBrowserContext(tab_contents()->browser_context());
  bool enabled = profile->GetPrefs()->GetBoolean(pref_name);

  DictionaryValue value;
  value.SetBoolean(pref_name, enabled);
  std::string policy;
  base::JSONWriter::Write(&value, false, &policy);
  Send(new ViewMsg_UpdateRemoteAccessClientFirewallTraversal(routing_id(),
                                                             policy));
}

bool FirewallTraversalTabHelper::OnMessageReceived(const IPC::Message& msg) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(FirewallTraversalTabHelper, msg)
    IPC_MESSAGE_HANDLER(ViewHostMsg_RequestRemoteAccessClientFirewallTraversal,
                        UpdateFirewallTraversalState)

    // Have the super handle all other messages.
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()

  return handled;
}