12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/api/bluetooth/bluetooth_extension_function.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/ref_counted.h"
8c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "content/public/browser/browser_thread.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "device/bluetooth/bluetooth_adapter.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "device/bluetooth/bluetooth_adapter_factory.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/api/bluetooth/bluetooth_api.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/api/bluetooth/bluetooth_event_router.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochusing content::BrowserThread;
15c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const char kPlatformNotSupported[] =
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    "This operation is not supported on your platform";
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochextensions::BluetoothEventRouter* GetEventRouter(
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::BrowserContext* context) {
23c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
24c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  return extensions::BluetoothAPI::Get(context)->event_router();
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool IsBluetoothSupported(content::BrowserContext* context) {
28c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return GetEventRouter(context)->IsBluetoothSupported();
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void GetAdapter(const device::BluetoothAdapterFactory::AdapterCallback callback,
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                content::BrowserContext* context) {
34c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GetEventRouter(context)->GetAdapter(callback);
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace extensions {
411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace core_api {
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BluetoothExtensionFunction::BluetoothExtensionFunction() {
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BluetoothExtensionFunction::~BluetoothExtensionFunction() {
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
49010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)bool BluetoothExtensionFunction::RunAsync() {
50c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
51c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!IsBluetoothSupported(browser_context())) {
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SetError(kPlatformNotSupported);
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetAdapter(base::Bind(&BluetoothExtensionFunction::RunOnAdapterReady, this),
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)             browser_context());
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return true;
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void BluetoothExtensionFunction::RunOnAdapterReady(
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_refptr<device::BluetoothAdapter> adapter) {
64c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DoWork(adapter);
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace core_api
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace extensions
70