Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(334)

Side by Side Diff: chrome/browser/extensions/api/bluetooth/bluetooth_api.cc

Issue 11360200: Decouple bluetooth_event_router from extension_system. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Removed MockBluetoothSocket Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" 5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h"
6 6
7 #if defined(OS_CHROMEOS) 7 #if defined(OS_CHROMEOS)
8 #include <errno.h> 8 #include <errno.h>
9 #endif 9 #endif
10 10
11 #include <string> 11 #include <string>
12 12
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_factory.h"
14 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" 15 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h"
15 #include "chrome/browser/extensions/bluetooth_event_router.h" 16 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h"
16 #include "chrome/browser/extensions/event_names.h" 17 #include "chrome/browser/extensions/event_names.h"
17 #include "chrome/browser/extensions/event_router.h" 18 #include "chrome/browser/extensions/event_router.h"
18 #include "chrome/browser/extensions/extension_service.h"
19 #include "chrome/browser/extensions/extension_system.h" 19 #include "chrome/browser/extensions/extension_system.h"
20 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/common/extensions/api/bluetooth.h" 21 #include "chrome/common/extensions/api/bluetooth.h"
22 #include "content/public/browser/browser_thread.h" 22 #include "content/public/browser/browser_thread.h"
23 #include "device/bluetooth/bluetooth_adapter.h" 23 #include "device/bluetooth/bluetooth_adapter.h"
24 #include "device/bluetooth/bluetooth_device.h" 24 #include "device/bluetooth/bluetooth_device.h"
25 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" 25 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h"
26 #include "device/bluetooth/bluetooth_service_record.h" 26 #include "device/bluetooth/bluetooth_service_record.h"
27 #include "device/bluetooth/bluetooth_socket.h" 27 #include "device/bluetooth/bluetooth_socket.h"
28 #include "device/bluetooth/bluetooth_utils.h" 28 #include "device/bluetooth/bluetooth_utils.h"
29 29
30 #if defined(OS_CHROMEOS) 30 #if defined(OS_CHROMEOS)
31 #include "base/safe_strerror_posix.h" 31 #include "base/safe_strerror_posix.h"
32 #endif 32 #endif
33 33
34 using device::BluetoothAdapter; 34 using device::BluetoothAdapter;
35 using device::BluetoothDevice; 35 using device::BluetoothDevice;
36 using device::BluetoothServiceRecord; 36 using device::BluetoothServiceRecord;
37 using device::BluetoothSocket; 37 using device::BluetoothSocket;
38 38
39 namespace { 39 namespace {
40 40
41 extensions::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { 41 extensions::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) {
42 return profile->GetExtensionService()->bluetooth_event_router(); 42 return extensions::BluetoothAPI::Get(profile)->bluetooth_event_router();
43 } 43 }
44 44
45 const BluetoothAdapter* GetAdapter(Profile* profile) { 45 const BluetoothAdapter* GetAdapter(Profile* profile) {
46 return GetEventRouter(profile)->adapter(); 46 return GetEventRouter(profile)->adapter();
47 } 47 }
48 48
49 BluetoothAdapter* GetMutableAdapter(Profile* profile) { 49 BluetoothAdapter* GetMutableAdapter(Profile* profile) {
50 BluetoothAdapter* adapter = GetEventRouter(profile)->GetMutableAdapter(); 50 return GetEventRouter(profile)->GetMutableAdapter();
51 return adapter;
52 } 51 }
53 52
54 bool IsBluetoothSupported(Profile* profile) { 53 bool IsBluetoothSupported(Profile* profile) {
55 return GetAdapter(profile) != NULL; 54 return GetAdapter(profile) != NULL;
56 } 55 }
57 56
58 } // namespace 57 } // namespace
59 58
60 namespace { 59 namespace {
61 60
(...skipping 16 matching lines...) Expand all
78 namespace Connect = extensions::api::bluetooth::Connect; 77 namespace Connect = extensions::api::bluetooth::Connect;
79 namespace Disconnect = extensions::api::bluetooth::Disconnect; 78 namespace Disconnect = extensions::api::bluetooth::Disconnect;
80 namespace GetDevices = extensions::api::bluetooth::GetDevices; 79 namespace GetDevices = extensions::api::bluetooth::GetDevices;
81 namespace GetServices = extensions::api::bluetooth::GetServices; 80 namespace GetServices = extensions::api::bluetooth::GetServices;
82 namespace Read = extensions::api::bluetooth::Read; 81 namespace Read = extensions::api::bluetooth::Read;
83 namespace SetOutOfBandPairingData = 82 namespace SetOutOfBandPairingData =
84 extensions::api::bluetooth::SetOutOfBandPairingData; 83 extensions::api::bluetooth::SetOutOfBandPairingData;
85 namespace Write = extensions::api::bluetooth::Write; 84 namespace Write = extensions::api::bluetooth::Write;
86 85
87 namespace extensions { 86 namespace extensions {
87
88 // static
89 BluetoothAPI* BluetoothAPI::Get(Profile* profile) {
90 return BluetoothAPIFactory::GetForProfile(profile);
91 }
92
93 BluetoothAPI::BluetoothAPI(Profile* profile) : profile_(profile) {
94 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
95 this, extensions::event_names::kBluetoothOnAvailabilityChanged);
96 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
97 this, extensions::event_names::kBluetoothOnPowerChanged);
98 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
99 this, extensions::event_names::kBluetoothOnDiscoveringChanged);
100 }
101
102 BluetoothAPI::~BluetoothAPI() {
103 }
104
105 ExtensionBluetoothEventRouter* BluetoothAPI::bluetooth_event_router() {
106 if (!bluetooth_event_router_)
107 bluetooth_event_router_.reset(new ExtensionBluetoothEventRouter(profile_));
108
109 return bluetooth_event_router_.get();
110 }
111
112 void BluetoothAPI::Shutdown() {
113 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
114 }
115
116 void BluetoothAPI::OnListenerAdded(const std::string& event_name) {
117 bluetooth_event_router()->OnListenerAdded();
118 }
119
120 void BluetoothAPI::OnListenerRemoved(const std::string& event_name) {
121 bluetooth_event_router()->OnListenerRemoved();
122 }
123
88 namespace api { 124 namespace api {
89 125
90 bool BluetoothIsAvailableFunction::RunImpl() { 126 bool BluetoothIsAvailableFunction::RunImpl() {
91 if (!IsBluetoothSupported(profile())) { 127 if (!IsBluetoothSupported(profile())) {
92 SetError(kPlatformNotSupported); 128 SetError(kPlatformNotSupported);
93 return false; 129 return false;
94 } 130 }
95 131
96 SetResult(Value::CreateBooleanValue(GetAdapter(profile())->IsPresent())); 132 SetResult(Value::CreateBooleanValue(GetAdapter(profile())->IsPresent()));
97 return true; 133 return true;
(...skipping 506 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 if (GetEventRouter(profile())->IsResponsibleForDiscovery()) { 640 if (GetEventRouter(profile())->IsResponsibleForDiscovery()) {
605 GetMutableAdapter(profile())->SetDiscovering(false, 641 GetMutableAdapter(profile())->SetDiscovering(false,
606 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), 642 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this),
607 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); 643 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this));
608 } 644 }
609 return true; 645 return true;
610 } 646 }
611 647
612 } // namespace api 648 } // namespace api
613 } // namespace extensions 649 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698