Run Tizen Webapps in single process mode
[platform/framework/web/crosswalk-tizen.git] / atom / browser / net / js_asker.cc
1 // Copyright (c) 2015 GitHub, Inc.
2 // Use of this source code is governed by the MIT license that can be
3 // found in the LICENSE file.
4
5 #include "atom/browser/net/js_asker.h"
6
7 #include <vector>
8
9 #include "atom/common/native_mate_converters/callback.h"
10 #include "atom/common/native_mate_converters/v8_value_converter.h"
11 #include "tizen/common/env_variables.h"
12
13 namespace atom {
14
15 namespace internal {
16
17 namespace {
18
19 // The callback which is passed to |handler|.
20 void HandlerCallback(const BeforeStartCallback& before_start,
21                      const ResponseCallback& callback,
22                      mate::Arguments* args) {
23   // If there is no argument passed then we failed.
24   v8::Local<v8::Value> value;
25   if (!args->GetNext(&value)) {
26     content::BrowserThread::PostTask(
27         content::BrowserThread::IO, FROM_HERE,
28         base::Bind(callback, false, nullptr));
29     return;
30   }
31
32   // Give the job a chance to parse V8 value.
33   before_start.Run(args->isolate(), value);
34
35   // Pass whatever user passed to the actaul request job.
36   V8ValueConverter converter;
37   v8::Local<v8::Context> context = args->isolate()->GetCurrentContext();
38   std::unique_ptr<base::Value> options(converter.FromV8Value(value, context));
39   content::BrowserThread::PostTask(
40       content::BrowserThread::IO, FROM_HERE,
41       base::Bind(callback, true, base::Passed(&options)));
42 }
43
44 }  // namespace
45
46 void AskForOptions(v8::Isolate* isolate,
47                    const JavaScriptHandler& handler,
48                    std::unique_ptr<base::DictionaryValue> request_details,
49                    const BeforeStartCallback& before_start,
50                    const ResponseCallback& callback) {
51   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
52   if (!::tizen::is_single_process)
53     v8::Locker locker(isolate);
54   v8::HandleScope handle_scope(isolate);
55   v8::Local<v8::Context> context = isolate->GetCurrentContext();
56   v8::Context::Scope context_scope(context);
57   handler.Run(
58       *(request_details.get()),
59       mate::ConvertToV8(isolate,
60                         base::Bind(&HandlerCallback, before_start, callback)));
61 }
62
63 bool IsErrorOptions(base::Value* value, int* error) {
64   if (value->IsType(base::Value::TYPE_DICTIONARY)) {
65     base::DictionaryValue* dict = static_cast<base::DictionaryValue*>(value);
66     if (dict->GetInteger("error", error))
67       return true;
68   } else if (value->IsType(base::Value::TYPE_INTEGER)) {
69     if (value->GetAsInteger(error))
70       return true;
71   }
72   return false;
73 }
74
75 }  // namespace internal
76
77 }  // namespace atom