- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / renderer / extensions / chrome_v8_context.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/renderer/extensions/chrome_v8_context.h"
6
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/string_split.h"
10 #include "base/values.h"
11 #include "chrome/common/extensions/api/extension_api.h"
12 #include "chrome/common/extensions/extension.h"
13 #include "chrome/common/extensions/extension_set.h"
14 #include "chrome/common/extensions/features/base_feature_provider.h"
15 #include "chrome/renderer/extensions/chrome_v8_extension.h"
16 #include "chrome/renderer/extensions/module_system.h"
17 #include "chrome/renderer/extensions/user_script_slave.h"
18 #include "content/public/renderer/render_view.h"
19 #include "content/public/renderer/v8_value_converter.h"
20 #include "third_party/WebKit/public/web/WebFrame.h"
21 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h"
22 #include "third_party/WebKit/public/web/WebView.h"
23 #include "v8/include/v8.h"
24
25 using content::V8ValueConverter;
26
27 namespace extensions {
28
29 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context,
30                                  WebKit::WebFrame* web_frame,
31                                  const Extension* extension,
32                                  Feature::Context context_type)
33     : v8_context_(v8_context),
34       web_frame_(web_frame),
35       extension_(extension),
36       context_type_(context_type),
37       safe_builtins_(this),
38       isolate_(v8_context->GetIsolate()) {
39   VLOG(1) << "Created context:\n"
40           << "  extension id: " << GetExtensionID() << "\n"
41           << "  frame:        " << web_frame_ << "\n"
42           << "  context type: " << GetContextTypeDescription();
43 }
44
45 ChromeV8Context::~ChromeV8Context() {
46   VLOG(1) << "Destroyed context for extension\n"
47           << "  extension id: " << GetExtensionID();
48   Invalidate();
49 }
50
51 void ChromeV8Context::Invalidate() {
52   if (!is_valid())
53     return;
54   if (module_system_)
55     module_system_->Invalidate();
56   web_frame_ = NULL;
57   v8_context_.reset();
58 }
59
60 std::string ChromeV8Context::GetExtensionID() const {
61   return extension_.get() ? extension_->id() : std::string();
62 }
63
64 content::RenderView* ChromeV8Context::GetRenderView() const {
65   if (web_frame_ && web_frame_->view())
66     return content::RenderView::FromWebView(web_frame_->view());
67   else
68     return NULL;
69 }
70
71 GURL ChromeV8Context::GetURL() const {
72   return web_frame_ ?
73       UserScriptSlave::GetDataSourceURLForFrame(web_frame_) : GURL();
74 }
75
76 v8::Local<v8::Value> ChromeV8Context::CallFunction(
77     v8::Handle<v8::Function> function,
78     int argc,
79     v8::Handle<v8::Value> argv[]) const {
80   v8::HandleScope handle_scope(isolate());
81   v8::Context::Scope scope(v8_context());
82
83   WebKit::WebScopedMicrotaskSuppression suppression;
84   if (!is_valid())
85     return handle_scope.Close(v8::Undefined());
86
87   v8::Handle<v8::Object> global = v8_context()->Global();
88   if (!web_frame_)
89     return handle_scope.Close(function->Call(global, argc, argv));
90   return handle_scope.Close(
91       web_frame_->callFunctionEvenIfScriptDisabled(function,
92                                                    global,
93                                                    argc,
94                                                    argv));
95 }
96
97 bool ChromeV8Context::IsAnyFeatureAvailableToContext(
98     const std::string& api_name) {
99   return ExtensionAPI::GetSharedInstance()->IsAnyFeatureAvailableToContext(
100       api_name,
101       extension_.get(),
102       context_type_,
103       UserScriptSlave::GetDataSourceURLForFrame(web_frame_));
104 }
105
106 Feature::Availability ChromeV8Context::GetAvailability(
107     const std::string& api_name) {
108   return ExtensionAPI::GetSharedInstance()->IsAvailable(api_name,
109                                                         extension_.get(),
110                                                         context_type_,
111                                                         GetURL());
112 }
113
114 void ChromeV8Context::DispatchOnUnloadEvent() {
115   module_system_->CallModuleMethod("unload_event", "dispatch");
116 }
117
118 std::string ChromeV8Context::GetContextTypeDescription() {
119   switch (context_type_) {
120     case Feature::UNSPECIFIED_CONTEXT:         return "UNSPECIFIED";
121     case Feature::BLESSED_EXTENSION_CONTEXT:   return "BLESSED_EXTENSION";
122     case Feature::UNBLESSED_EXTENSION_CONTEXT: return "UNBLESSED_EXTENSION";
123     case Feature::CONTENT_SCRIPT_CONTEXT:      return "CONTENT_SCRIPT";
124     case Feature::WEB_PAGE_CONTEXT:            return "WEB_PAGE";
125   }
126   NOTREACHED();
127   return std::string();
128 }
129
130 ChromeV8Context* ChromeV8Context::GetContext() {
131   return this;
132 }
133
134 void ChromeV8Context::OnResponseReceived(const std::string& name,
135                                          int request_id,
136                                          bool success,
137                                          const base::ListValue& response,
138                                          const std::string& error) {
139   v8::HandleScope handle_scope(isolate());
140
141   scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
142   v8::Handle<v8::Value> argv[] = {
143     v8::Integer::New(request_id),
144     v8::String::New(name.c_str()),
145     v8::Boolean::New(success),
146     converter->ToV8Value(&response, v8_context_.NewHandle(isolate())),
147     v8::String::New(error.c_str())
148   };
149
150   v8::Handle<v8::Value> retval = module_system_->CallModuleMethod(
151       "sendRequest", "handleResponse", arraysize(argv), argv);
152
153   // In debug, the js will validate the callback parameters and return a
154   // string if a validation error has occured.
155   if (DCHECK_IS_ON()) {
156     if (!retval.IsEmpty() && !retval->IsUndefined()) {
157       std::string error = *v8::String::AsciiValue(retval);
158       DCHECK(false) << error;
159     }
160   }
161 }
162
163 }  // namespace extensions