1 From 1fe3191d93a63d2279747388e237907439287698 Mon Sep 17 00:00:00 2001
2 From: Mikhail Pozdnyakov <mikhail.pozdnyakov@intel.com>
3 Date: Wed, 10 Jul 2013 17:23:24 +0300
4 Subject: [PATCH 10/33] Introducing C API for EFL Web View.
6 Wrap xwalk initialization
7 Introduce EinaSharedString
8 Introduce xwalk_view_url api
11 base/strings/efl/eina_shared_string.cc | 53 ++++++++++++++++++++++++++++++
12 base/strings/efl/eina_shared_string.h | 52 +++++++++++++++++++++++++++++
13 efl_webview/efl_webview.gyp | 4 +++
14 efl_webview/examples/main.cc | 39 +++++++++++-----------
15 efl_webview/lib/webview.cc | 41 +++++++++++++++++++++--
16 efl_webview/lib/webview.h | 24 +++++++++-----
17 efl_webview/public/xwalk_main.cc | 10 ++++++
18 efl_webview/public/xwalk_main.h | 19 +++++++++++
19 efl_webview/public/xwalk_view.cc | 60 ++++++++++++++++++++++++++++++++++
20 efl_webview/public/xwalk_view.h | 29 ++++++++++++++++
21 11 files changed, 303 insertions(+), 30 deletions(-)
22 create mode 100644 base/strings/efl/eina_shared_string.cc
23 create mode 100644 base/strings/efl/eina_shared_string.h
24 create mode 100644 efl_webview/public/xwalk_main.cc
25 create mode 100644 efl_webview/public/xwalk_main.h
26 create mode 100644 efl_webview/public/xwalk_view.cc
27 create mode 100644 efl_webview/public/xwalk_view.h
29 diff --git a/base/base.gyp b/base/base.gyp
30 index b9e7e36..6b80e40 100644
34 '../build/linux/system.gyp:efl',
37 + 'strings/efl/eina_shared_string.cc',
38 + 'strings/efl/eina_shared_string.h',
39 'message_pump_efl.cc',
42 diff --git a/base/strings/efl/eina_shared_string.cc b/base/strings/efl/eina_shared_string.cc
44 index 0000000..48b4862
46 +++ b/base/strings/efl/eina_shared_string.cc
48 +// Copyright (c) 2013 Intel Corporation. All rights reserved.
49 +// Use of this source code is governed by a BSD-style license that can be
50 +// found in the LICENSE file.
52 +#include "base/strings/efl/eina_shared_string.h"
54 +EinaSharedString::EinaSharedString(const EinaSharedString& other)
55 + : string_(eina_stringshare_ref(other.string_)) {
58 +EinaSharedString::EinaSharedString(const char* str)
59 + : string_(eina_stringshare_add(str)) {
62 +EinaSharedString::EinaSharedString(const std::string& str)
63 + : string_(eina_stringshare_add(str.c_str())) {
66 +EinaSharedString::~EinaSharedString() {
68 + eina_stringshare_del(string_);
71 +EinaSharedString& EinaSharedString::operator=(const EinaSharedString& other) {
72 + if (this != &other) {
74 + eina_stringshare_del(string_);
75 + string_ = eina_stringshare_ref(other.string_);
80 +EinaSharedString& EinaSharedString::operator=(const char* str) {
81 + eina_stringshare_replace(&string_, str);
85 +bool EinaSharedString::operator==(const char* str) const {
86 + return (!str || !string_) ? (str == string_) : !strcmp(string_, str);
89 +EinaSharedString EinaSharedString::Adopt(Eina_Stringshare* string) {
90 + EinaSharedString sharedString;
91 + sharedString.string_ = static_cast<const char*>(string);
92 + return sharedString;
95 +Eina_Stringshare* EinaSharedString::LeakString() {
96 + Eina_Stringshare* sharedString = string_;
99 + return sharedString;
101 diff --git a/base/strings/efl/eina_shared_string.h b/base/strings/efl/eina_shared_string.h
103 index 0000000..6fc0aee
105 +++ b/base/strings/efl/eina_shared_string.h
107 +// Copyright (c) 2013 Intel Corporation. All rights reserved.
108 +// Use of this source code is governed by a BSD-style license that can be
109 +// found in the LICENSE file.
111 +#ifndef EINA_SHARED_STRING_H_
112 +#define EINA_SHARED_STRING_H_
114 +#include "base/base_export.h"
122 +class BASE_EXPORT EinaSharedString {
124 + EinaSharedString() : string_(0) { }
125 + EinaSharedString(const EinaSharedString& other);
126 + EinaSharedString(const char* str);
127 + explicit EinaSharedString(const std::string&);
129 + ~EinaSharedString();
131 + Eina_Stringshare* LeakString();
133 + EinaSharedString& operator=(const EinaSharedString& other);
134 + EinaSharedString& operator=(const char* str);
136 + bool operator==(const EinaSharedString& other) const { return string_ == other.string_; }
137 + bool operator!=(const EinaSharedString& other) const { return !(*this == other); }
139 + bool operator==(const char* str) const;
140 + bool operator!=(const char* str) const { return !(*this == str); }
142 + operator const char* () const { return string_; }
144 + bool IsNull() const { return !string_; }
146 + size_t Length() const { return string_ ? static_cast<size_t>(eina_stringshare_strlen(string_)) : 0; }
148 + static EinaSharedString Adopt(Eina_Stringshare*);
151 + const char* string_;
156 +using base::EinaSharedString;
158 +#endif // EINA_SHARED_STRING_H_
159 diff --git a/efl_webview/efl_webview.gyp b/efl_webview/efl_webview.gyp
160 index f49badd..c71eb7b 100644
161 --- a/efl_webview/efl_webview.gyp
162 +++ b/efl_webview/efl_webview.gyp
164 'lib/web_runtime_context.h',
167 + 'public/xwalk_view.cc',
168 + 'public/xwalk_view.h',
169 + 'public/xwalk_main.cc',
170 + 'public/xwalk_main.h',
174 diff --git a/efl_webview/examples/main.cc b/efl_webview/examples/main.cc
175 index a41c986..db3a6dc 100644
176 --- a/efl_webview/examples/main.cc
177 +++ b/efl_webview/examples/main.cc
180 #include "efl_webview/lib/process_main.h"
181 #include "efl_webview/lib/webview.h"
182 +#include "efl_webview/public/xwalk_main.h"
183 +#include "efl_webview/public/xwalk_view.h"
185 static const char APP_NAME[] = "EFL WebView Example";
187 @@ -17,26 +19,23 @@ static int window_height = 600;
189 on_back_button_clicked(void *user_data, Evas_Object *back_button, void *event_info)
191 - xwalk::WebView* webview = static_cast<xwalk::WebView*>(user_data);
193 + xwalk_view_back((Evas_Object*)user_data);
197 on_forward_button_clicked(void *user_data, Evas_Object *forward_button, void *event_info)
199 - xwalk::WebView* webview = static_cast<xwalk::WebView*>(user_data);
200 - webview->Forward();
201 + xwalk_view_forward((Evas_Object*)user_data);
205 on_reload_button_clicked(void *user_data,
206 Evas_Object *forward_button, void *event_info)
208 - xwalk::WebView* webview = static_cast<xwalk::WebView*>(user_data);
210 + xwalk_view_reload((Evas_Object*)user_data);
213 -static void window_create()
214 +static void window_create(int argc)
217 Evas_Object* elm_window = elm_win_util_standard_add("efl-webview-window", APP_NAME);
218 @@ -84,36 +83,38 @@ static void window_create()
219 evas_object_show(reload_button);
222 - xwalk::WebView* webview_object = xwalk::WebView::Create(elm_window);
223 - Evas_Object* webview = webview_object->EvasObject();
224 - evas_object_size_hint_weight_set(webview, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
225 - evas_object_size_hint_align_set(webview, EVAS_HINT_FILL, EVAS_HINT_FILL);
226 - elm_box_pack_end(vertical_layout, webview);
227 - elm_object_focus_set(webview, EINA_TRUE);
228 - evas_object_show(webview);
229 + Evas_Object* web_view = xwalk_view_add(elm_window);
230 + evas_object_size_hint_weight_set(web_view, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
231 + evas_object_size_hint_align_set(web_view, EVAS_HINT_FILL, EVAS_HINT_FILL);
232 + elm_box_pack_end(vertical_layout, web_view);
233 + elm_object_focus_set(web_view, EINA_TRUE);
234 + evas_object_show(web_view);
236 evas_object_smart_callback_add(back_button, "clicked",
237 - on_back_button_clicked, webview_object);
238 + on_back_button_clicked, web_view);
239 evas_object_smart_callback_add(forward_button, "clicked",
240 - on_forward_button_clicked, webview_object);
241 + on_forward_button_clicked, web_view);
242 evas_object_smart_callback_add(reload_button, "clicked",
243 - on_reload_button_clicked, webview_object);
244 + on_reload_button_clicked, web_view);
246 evas_object_resize(elm_window, window_width, window_height);
247 evas_object_show(elm_window);
250 + xwalk_view_url_set(web_view, "http://google.com");
253 int main(int argc, char *argv[])
255 // FIXME: Handle chrome command line and url.
256 // It is needed only in development stage.
257 - xwalk::WebView::CommandLineInit(argc, argv);
258 + xwalk_init(argc, argv);
260 elm_init(argc, argv);
262 elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
265 + window_create(argc);
269 diff --git a/efl_webview/lib/webview.cc b/efl_webview/lib/webview.cc
270 index 061d30c..61ecb96 100644
271 --- a/efl_webview/lib/webview.cc
272 +++ b/efl_webview/lib/webview.cc
274 #include "efl_webview/lib/webview.h"
276 #include <Elementary.h>
280 #include "base/command_line.h"
281 #include "base/file_util.h"
282 #include "base/files/file_path.h"
284 #include "efl_webview/lib/web_runtime_context.h"
285 #include "net/base/net_util.h"
289 +typedef std::map<const Evas_Object*, xwalk::WebView*> EvasWebViewMap;
291 +inline EvasWebViewMap& EvasObjectToWebViewMap() {
292 +// FIXME: Temporary solution until web view has its own smart class.
293 + static EvasWebViewMap map;
301 struct WebView::Private {
302 @@ -58,6 +73,10 @@ WebView::WebView(Evas_Object* root_window)
304 private_->root_window = root_window;
305 private_->view_box = elm_box_add(private_->root_window);
306 + // FIXME: In future this has to be a separate Smart Class representing web view.
307 + // 'this' should be set as smart_data->priv
308 + EvasObjectToWebViewMap()[private_->view_box] = this;
310 elm_object_focus_allow_set(private_->view_box, EINA_TRUE);
312 private_->context = WebRuntimeContext::current();
313 @@ -78,14 +97,24 @@ WebView::WebView(Evas_Object* root_window)
316 WebView::~WebView() {
317 +// FIXME : And by the way who will invoke it?
318 + EvasObjectToWebViewMap().erase(private_->view_box);
319 evas_object_del(private_->view_box);
322 -void WebView::Forward() {
323 +bool WebView::CanGoBack() const {
324 + return private_->webContentsDelegate->WebContents()->GetController().CanGoBack();
327 +bool WebView::CanGoForward() const {
328 + return private_->webContentsDelegate->WebContents()->GetController().CanGoForward();
331 +void WebView::GoForward() {
332 private_->webContentsDelegate->WebContents()->GetController().GoForward();
335 -void WebView::Back() {
336 +void WebView::GoBack() {
337 private_->webContentsDelegate->WebContents()->GetController().GoBack();
340 @@ -94,6 +123,7 @@ void WebView::Reload() {
343 void WebView::LoadURL(const GURL& url) {
344 + url_ = EinaSharedString(url.spec());
345 content::NavigationController::LoadURLParams params(url);
346 params.transition_type = content::PageTransitionFromInt(
347 content::PAGE_TRANSITION_TYPED |
348 @@ -107,4 +137,11 @@ Evas_Object* WebView::EvasObject() {
349 return private_->view_box;
352 +WebView* ToWebView(const Evas_Object* evas_object) {
353 + EvasWebViewMap::iterator found = EvasObjectToWebViewMap().find(evas_object);
354 + if (found != EvasObjectToWebViewMap().end())
355 + return found->second;
360 diff --git a/efl_webview/lib/webview.h b/efl_webview/lib/webview.h
361 index ca1757e..ae4f98f 100644
362 --- a/efl_webview/lib/webview.h
363 +++ b/efl_webview/lib/webview.h
365 // Use of this source code is governed by a BSD-style license that can be
366 // found in the LICENSE file.
368 -#ifndef EFL_WEBVIEW_LIBWINDOW_H_
369 -#define EFL_WEBVIEW_LIBWINDOW_H_
370 +#ifndef EFL_WEBVIEW_LIB_WEBVIEW_H_
371 +#define EFL_WEBVIEW_LIB_WEBVIEW_H_
375 #include "base/basictypes.h"
376 #include "base/memory/scoped_ptr.h"
377 +#include "base/strings/efl/eina_shared_string.h"
378 #include "googleurl/src/gurl.h"
381 @@ -22,22 +23,27 @@ class EAPI WebView {
383 EAPI Evas_Object* EvasObject();
385 - EAPI void Forward();
387 - EAPI void Reload();
388 - EAPI void LoadURL(const GURL&);
389 + bool CanGoBack() const;
390 + bool CanGoForward() const;
394 + void LoadURL(const GURL&);
396 + const char* url() const { return url_; }
399 - explicit WebView(Evas_Object*);
400 + explicit WebView(Evas_Object* root_window);
403 scoped_ptr<Private> private_;
404 + EinaSharedString url_;
406 DISALLOW_COPY_AND_ASSIGN(WebView);
409 -WebView* ToWebView(Evas_Object*);
410 +WebView* ToWebView(const Evas_Object*);
414 -#endif // EFL_WEBVIEW_LIBWINDOW_H_
415 +#endif // EFL_WEBVIEW_LIB_WEBVIEW_H_
416 diff --git a/efl_webview/public/xwalk_main.cc b/efl_webview/public/xwalk_main.cc
418 index 0000000..218c55d
420 +++ b/efl_webview/public/xwalk_main.cc
422 +// Copyright (c) 2013 Intel Corporation. All rights reserved.
423 +// Use of this source code is governed by a BSD-style license that can be
424 +// found in the LICENSE file.
426 +#include "efl_webview/public/xwalk_main.h"
427 +#include "efl_webview/lib/webview.h"
429 +void xwalk_init(int argc, char *argv[]) {
430 + xwalk::WebView::CommandLineInit(argc, argv);
432 diff --git a/efl_webview/public/xwalk_main.h b/efl_webview/public/xwalk_main.h
434 index 0000000..b6c2f83
436 +++ b/efl_webview/public/xwalk_main.h
438 +// Copyright (c) 2013 Intel Corporation. All rights reserved.
439 +// Use of this source code is governed by a BSD-style license that can be
440 +// found in the LICENSE file.
442 +#ifndef EFL_WEBVIEW_PUBLIC_XWALK_MAIN_H_
443 +#define EFL_WEBVIEW_PUBLIC_XWALK_MAIN_H_
451 +EAPI void xwalk_init(int argc, char *argv[]);
456 +#endif // EFL_WEBVIEW_PUBLIC_XWALK_MAIN_H_
457 diff --git a/efl_webview/public/xwalk_view.cc b/efl_webview/public/xwalk_view.cc
459 index 0000000..b000143
461 +++ b/efl_webview/public/xwalk_view.cc
463 +// Copyright (c) 2013 Intel Corporation. All rights reserved.
464 +// Use of this source code is governed by a BSD-style license that can be
465 +// found in the LICENSE file.
467 +#include "efl_webview/public/xwalk_view.h"
468 +#include "efl_webview/lib/webview.h"
470 +using namespace xwalk;
472 +#define XWALK_VIEW_GET_IMPL_OR_RETURN(xwalk_view, impl, ...) \
473 + WebView* impl = ToWebView(xwalk_view); \
476 + EINA_LOG_CRIT("no private data for object %p", xwalk_view); \
477 + return __VA_ARGS__; \
482 +Evas_Object* xwalk_view_add(Evas_Object* root_window) {
483 + return WebView::Create(root_window)->EvasObject();
486 +Eina_Bool xwalk_view_url_set(Evas_Object* evas_object, const char* url) {
487 + XWALK_VIEW_GET_IMPL_OR_RETURN(evas_object, impl, false);
488 + EINA_SAFETY_ON_NULL_RETURN_VAL(url, false);
490 + impl->LoadURL(GURL(url));
494 +const char* xwalk_view_url_get(const Evas_Object* evas_object) {
495 + XWALK_VIEW_GET_IMPL_OR_RETURN(evas_object, impl, "");
496 + return impl->url();
499 +Eina_Bool xwalk_view_reload(Evas_Object* evas_object) {
500 + XWALK_VIEW_GET_IMPL_OR_RETURN(evas_object, impl, false);
506 +Eina_Bool xwalk_view_back(Evas_Object* evas_object) {
507 + XWALK_VIEW_GET_IMPL_OR_RETURN(evas_object, impl, false);
508 + if (impl->CanGoBack()) {
515 +Eina_Bool xwalk_view_forward(Evas_Object* evas_object) {
516 + XWALK_VIEW_GET_IMPL_OR_RETURN(evas_object, impl, false);
517 + if (impl->CanGoForward()) {
523 diff --git a/efl_webview/public/xwalk_view.h b/efl_webview/public/xwalk_view.h
525 index 0000000..a4c85d58
527 +++ b/efl_webview/public/xwalk_view.h
529 +// Copyright (c) 2013 Intel Corporation. All rights reserved.
530 +// Use of this source code is governed by a BSD-style license that can be
531 +// found in the LICENSE file.
533 +#ifndef EFL_WEBVIEW_PUBLIC_XWALK_VIEW_H_
534 +#define EFL_WEBVIEW_PUBLIC_XWALK_VIEW_H_
542 +EAPI Evas_Object *xwalk_view_add(Evas_Object *root_window);
544 +EAPI Eina_Bool xwalk_view_url_set(Evas_Object *obj, const char *url);
546 +EAPI const char *xwalk_view_url_get(const Evas_Object *obj);
548 +EAPI Eina_Bool xwalk_view_reload(Evas_Object *obj);
550 +EAPI Eina_Bool xwalk_view_back(Evas_Object *obj);
552 +EAPI Eina_Bool xwalk_view_forward(Evas_Object *obj);
557 +#endif // EFL_WEBVIEW_PUBLIC_XWALK_VIEW_H_