summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/api_function.cc
blob: bfc27911865d0b8518f96bd1e0ccc8df72e093dd (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
// 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.

#include "chrome/browser/extensions/api/api_function.h"

#include "base/bind.h"
#include "chrome/browser/extensions/api/api_resource_controller.h"
#include "chrome/browser/extensions/api/api_resource_event_notifier.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"

using content::BrowserThread;

namespace extensions {

bool AsyncIOAPIFunction::RunImpl() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  extension_service_ = profile()->GetExtensionService();

  if (!Prepare()) {
    return false;
  }
  bool rv = BrowserThread::PostTask(
      BrowserThread::IO, FROM_HERE,
      base::Bind(&AsyncIOAPIFunction::WorkOnIOThread, this));
  DCHECK(rv);
  return true;
}

void AsyncIOAPIFunction::WorkOnIOThread() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  Work();
  bool rv = BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(&AsyncIOAPIFunction::RespondOnUIThread, this));
  DCHECK(rv);
}

void AsyncIOAPIFunction::RespondOnUIThread() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  SendResponse(Respond());
}

int AsyncIOAPIFunction::ExtractSrcId(size_t argument_position) {
  scoped_ptr<DictionaryValue> options(new DictionaryValue());
  if (args_->GetSize() > argument_position) {
    DictionaryValue* temp_options = NULL;
    if (args_->GetDictionary(argument_position, &temp_options))
      options.reset(temp_options->DeepCopy());
  }

  // If we tacked on a srcId to the options object, pull it out here to provide
  // to the Socket.
  int src_id = -1;
  if (options->HasKey(kSrcIdKey)) {
    EXTENSION_FUNCTION_VALIDATE(options->GetInteger(kSrcIdKey, &src_id));
  }

  return src_id;
}

APIResourceEventNotifier* AsyncIOAPIFunction::CreateEventNotifier(int src_id) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  return new APIResourceEventNotifier(
      profile()->GetExtensionEventRouter(), profile(), extension_id(),
      src_id, source_url());
}

APIResourceController* AsyncIOAPIFunction::controller() {
  // ExtensionService's APIResourceController is set exactly once, long before
  // this code is reached, so it's safe to access it on either the IO or UI
  // thread.
  return extension_service_->api_resource_controller();
}

}  // namespace extensions