Upstream version 11.39.244.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / xwalk_content_browser_client.cc
index 363b412..c5743e2 100644 (file)
@@ -9,22 +9,42 @@
 
 #include "base/command_line.h"
 #include "base/path_service.h"
-#include "base/platform_file.h"
+#include "base/files/file.h"
+#include "content/public/browser/browser_child_process_host.h"
 #include "content/public/browser/browser_main_parts.h"
+#include "content/public/browser/browser_ppapi_host.h"
+#include "content/public/browser/child_process_data.h"
+#include "content/public/browser/render_frame_host.h"
 #include "content/public/browser/render_process_host.h"
 #include "content/public/browser/resource_context.h"
+#include "content/public/browser/resource_dispatcher_host.h"
+#include "content/public/browser/storage_partition.h"
 #include "content/public/browser/web_contents.h"
 #include "content/public/common/main_function_params.h"
 #include "content/public/common/show_desktop_notification_params.h"
+#include "net/ssl/ssl_info.h"
 #include "net/url_request/url_request_context_getter.h"
+#include "ppapi/host/ppapi_host.h"
 #include "xwalk/extensions/common/xwalk_extension_switches.h"
-#include "xwalk/runtime/browser/xwalk_browser_main_parts.h"
+#include "xwalk/application/common/constants.h"
+#include "xwalk/runtime/browser/devtools/xwalk_devtools_delegate.h"
 #include "xwalk/runtime/browser/geolocation/xwalk_access_token_store.h"
 #include "xwalk/runtime/browser/media/media_capture_devices_dispatcher.h"
+#include "xwalk/runtime/browser/renderer_host/pepper/xwalk_browser_pepper_host_factory.h"
 #include "xwalk/runtime/browser/runtime_context.h"
 #include "xwalk/runtime/browser/runtime_quota_permission_context.h"
 #include "xwalk/runtime/browser/speech/speech_recognition_manager_delegate.h"
+#include "xwalk/runtime/browser/xwalk_browser_main_parts.h"
+#include "xwalk/runtime/browser/xwalk_render_message_filter.h"
 #include "xwalk/runtime/browser/xwalk_runner.h"
+#include "xwalk/runtime/common/xwalk_paths.h"
+
+#if !defined(DISABLE_NACL)
+#include "components/nacl/browser/nacl_browser.h"
+#include "components/nacl/browser/nacl_host_message_filter.h"
+#include "components/nacl/browser/nacl_process_host.h"
+#include "components/nacl/common/nacl_process_type.h"
+#endif
 
 #if defined(OS_ANDROID)
 #include "base/android/path_utils.h"
 #include "xwalk/runtime/browser/android/xwalk_cookie_access_policy.h"
 #include "xwalk/runtime/browser/android/xwalk_contents_client_bridge.h"
 #include "xwalk/runtime/browser/android/xwalk_web_contents_view_delegate.h"
-#include "xwalk/runtime/browser/runtime_resource_dispatcher_host_delegate_android.h"
 #include "xwalk/runtime/browser/xwalk_browser_main_parts_android.h"
 #include "xwalk/runtime/common/android/xwalk_globals_android.h"
+#else
+#include "xwalk/application/browser/application_system.h"
+#include "xwalk/application/browser/application_service.h"
+#include "xwalk/application/browser/application.h"
 #endif
 
 #if defined(OS_MACOSX)
 #endif
 
 #if defined(OS_TIZEN)
+#include "xwalk/application/common/application_manifest_constants.h"
+#include "xwalk/application/common/manifest_handlers/navigation_handler.h"
+#include "xwalk/runtime/browser/runtime_platform_util.h"
+#include "xwalk/runtime/browser/tizen/xwalk_web_contents_view_delegate.h"
 #include "xwalk/runtime/browser/xwalk_browser_main_parts_tizen.h"
 #endif
 
@@ -63,7 +90,8 @@ XWalkContentBrowserClient* XWalkContentBrowserClient::Get() {
 XWalkContentBrowserClient::XWalkContentBrowserClient(XWalkRunner* xwalk_runner)
     : xwalk_runner_(xwalk_runner),
       url_request_context_getter_(NULL),
-      main_parts_(NULL) {
+      main_parts_(NULL),
+      runtime_context_(NULL) {
   DCHECK(!g_browser_client);
   g_browser_client = this;
 }
@@ -90,9 +118,11 @@ content::BrowserMainParts* XWalkContentBrowserClient::CreateBrowserMainParts(
 
 net::URLRequestContextGetter* XWalkContentBrowserClient::CreateRequestContext(
     content::BrowserContext* browser_context,
-    content::ProtocolHandlerMap* protocol_handlers) {
-  url_request_context_getter_ = static_cast<RuntimeContext*>(browser_context)->
-      CreateRequestContext(protocol_handlers);
+    content::ProtocolHandlerMap* protocol_handlers,
+    content::URLRequestInterceptorScopedVector request_interceptors) {
+  runtime_context_ = static_cast<RuntimeContext*>(browser_context);
+  url_request_context_getter_ = runtime_context_->
+      CreateRequestContext(protocol_handlers, request_interceptors.Pass());
   return url_request_context_getter_;
 }
 
@@ -101,10 +131,12 @@ XWalkContentBrowserClient::CreateRequestContextForStoragePartition(
     content::BrowserContext* browser_context,
     const base::FilePath& partition_path,
     bool in_memory,
-    content::ProtocolHandlerMap* protocol_handlers) {
+    content::ProtocolHandlerMap* protocol_handlers,
+    content::URLRequestInterceptorScopedVector request_interceptors) {
   return static_cast<RuntimeContext*>(browser_context)->
       CreateRequestContextForStoragePartition(
-          partition_path, in_memory, protocol_handlers);
+          partition_path, in_memory, protocol_handlers,
+          request_interceptors.Pass());
 }
 
 // This allow us to append extra command line switches to the child
@@ -137,6 +169,9 @@ XWalkContentBrowserClient::GetWebContentsViewDelegate(
     content::WebContents* web_contents) {
 #if defined(OS_ANDROID)
   return new XWalkWebContentsViewDelegate(web_contents);
+#elif defined(OS_TIZEN)
+  return new XWalkWebContentsViewDelegate(
+      web_contents, xwalk_runner_->app_system()->application_service());
 #else
   return NULL;
 #endif
@@ -144,7 +179,20 @@ XWalkContentBrowserClient::GetWebContentsViewDelegate(
 
 void XWalkContentBrowserClient::RenderProcessWillLaunch(
     content::RenderProcessHost* host) {
+#if !defined(DISABLE_NACL)
+  int id = host->GetID();
+  net::URLRequestContextGetter* context =
+      host->GetStoragePartition()->GetURLRequestContext();
+
+  host->AddFilter(new nacl::NaClHostMessageFilter(
+      id,
+      // TODO(Halton): IsOffTheRecord?
+      false,
+      host->GetBrowserContext()->GetPath(),
+      context));
+#endif
   xwalk_runner_->OnRenderProcessWillLaunch(host);
+  host->AddFilter(new XWalkRenderMessageFilter);
 }
 
 content::MediaObserver* XWalkContentBrowserClient::GetMediaObserver() {
@@ -157,7 +205,7 @@ bool XWalkContentBrowserClient::AllowGetCookie(
     const net::CookieList& cookie_list,
     content::ResourceContext* context,
     int render_process_id,
-    int render_view_id) {
+    int render_frame_id) {
 #if defined(OS_ANDROID)
   return XWalkCookieAccessPolicy::GetInstance()->AllowGetCookie(
       url,
@@ -165,7 +213,7 @@ bool XWalkContentBrowserClient::AllowGetCookie(
       cookie_list,
       context,
       render_process_id,
-      render_view_id);
+      render_frame_id);
 #else
   return true;
 #endif
@@ -177,7 +225,7 @@ bool XWalkContentBrowserClient::AllowSetCookie(
     const std::string& cookie_line,
     content::ResourceContext* context,
     int render_process_id,
-    int render_view_id,
+    int render_frame_id,
     net::CookieOptions* options) {
 #if defined(OS_ANDROID)
   return XWalkCookieAccessPolicy::GetInstance()->AllowSetCookie(
@@ -186,88 +234,192 @@ bool XWalkContentBrowserClient::AllowSetCookie(
       cookie_line,
       context,
       render_process_id,
-      render_view_id,
+      render_frame_id,
       options);
 #else
   return true;
 #endif
 }
 
+void XWalkContentBrowserClient::AllowCertificateError(
+    int render_process_id,
+    int render_frame_id,
+    int cert_error,
+    const net::SSLInfo& ssl_info,
+    const GURL& request_url,
+    content::ResourceType resource_type,
+    bool overridable,
+    bool strict_enforcement,
+    bool expired_previous_decision,
+    const base::Callback<void(bool)>& callback, // NOLINT
+    content::CertificateRequestResultType* result) {
+  // Currently only Android handles it.
+  // TODO(yongsheng): applies it for other platforms?
+#if defined(OS_ANDROID)
+  XWalkContentsClientBridgeBase* client =
+      XWalkContentsClientBridgeBase::FromRenderFrameID(render_process_id,
+          render_frame_id);
+  bool cancel_request = true;
+  if (client)
+    client->AllowCertificateError(cert_error,
+                                  ssl_info.cert.get(),
+                                  request_url,
+                                  callback,
+                                  &cancel_request);
+  if (cancel_request)
+    *result = content::CERTIFICATE_REQUEST_RESULT_TYPE_DENY;
+#endif
+}
+
 void XWalkContentBrowserClient::RequestDesktopNotificationPermission(
     const GURL& source_origin,
-    int callback_context,
-    int render_process_id,
-    int render_view_id) {
+    content::RenderFrameHost* render_frame_host,
+    const base::Callback<void(blink::WebNotificationPermission)>& callback) {
 }
 
-blink::WebNotificationPresenter::Permission
+blink::WebNotificationPermission
 XWalkContentBrowserClient::CheckDesktopNotificationPermission(
     const GURL& source_url,
     content::ResourceContext* context,
     int render_process_id) {
 #if defined(OS_ANDROID)
-  return blink::WebNotificationPresenter::PermissionAllowed;
+  return blink::WebNotificationPermissionAllowed;
 #else
-  return blink::WebNotificationPresenter::PermissionNotAllowed;
+  return blink::WebNotificationPermissionDenied;
 #endif
 }
 
 void XWalkContentBrowserClient::ShowDesktopNotification(
     const content::ShowDesktopNotificationHostMsgParams& params,
-    int render_process_id,
-    int render_view_id,
-    bool worker) {
+    content::RenderFrameHost* render_frame_host,
+    scoped_ptr<content::DesktopNotificationDelegate> delegate,
+    base::Closure* cancel_callback) {
 #if defined(OS_ANDROID)
   XWalkContentsClientBridgeBase* bridge =
-      XWalkContentsClientBridgeBase::FromID(render_process_id, render_view_id);
-  bridge->ShowNotification(params, worker, render_process_id, render_view_id);
+      XWalkContentsClientBridgeBase::FromRenderFrameHost(render_frame_host);
+  bridge->ShowNotification(params, render_frame_host,
+      delegate.Pass(), cancel_callback);
 #endif
 }
 
-void XWalkContentBrowserClient::CancelDesktopNotification(
-    int render_process_id,
-    int render_view_id,
-    int notification_id) {
-#if defined(OS_ANDROID)
-  XWalkContentsClientBridgeBase* bridge =
-      XWalkContentsClientBridgeBase::FromID(render_process_id, render_view_id);
-  bridge->CancelNotification(
-      notification_id, render_process_id, render_view_id);
+void XWalkContentBrowserClient::RequestGeolocationPermission(
+    content::WebContents* web_contents,
+    int bridge_id,
+    const GURL& requesting_frame,
+    bool user_gesture,
+    base::Callback<void(bool)> result_callback,
+    base::Closure* cancel_callback) {
+#if defined(OS_ANDROID) || defined(OS_TIZEN)
+  if (!geolocation_permission_context_.get()) {
+    geolocation_permission_context_ =
+      new RuntimeGeolocationPermissionContext();
+  }
+  geolocation_permission_context_->RequestGeolocationPermission(
+    web_contents,
+    requesting_frame,
+    result_callback,
+    cancel_callback);
+#else
+  result_callback.Run(false);
 #endif
 }
 
-#if defined(OS_ANDROID)
-void XWalkContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
-    const CommandLine& command_line,
-    int child_process_id,
-    std::vector<content::FileDescriptorInfo>* mappings) {
-  int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ;
-  base::FilePath pak_file;
-  bool r = PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_file);
-  CHECK(r);
-  pak_file = pak_file.Append(FILE_PATH_LITERAL("paks"));
-  pak_file = pak_file.Append(FILE_PATH_LITERAL(kXWalkPakFilePath));
-
-  base::PlatformFile f =
-      base::CreatePlatformFile(pak_file, flags, NULL, NULL);
-  if (f == base::kInvalidPlatformFileValue) {
-    NOTREACHED() << "Failed to open file when creating renderer process: "
-                 << "xwalk.pak";
+void XWalkContentBrowserClient::DidCreatePpapiPlugin(
+    content::BrowserPpapiHost* browser_host) {
+#if defined(ENABLE_PLUGINS)
+  browser_host->GetPpapiHost()->AddHostFactoryFilter(
+      scoped_ptr<ppapi::host::HostFactory>(
+          new XWalkBrowserPepperHostFactory(browser_host)));
+#endif
+}
+
+content::BrowserPpapiHost*
+    XWalkContentBrowserClient::GetExternalBrowserPpapiHost(
+        int plugin_process_id) {
+#if !defined(DISABLE_NACL)
+  content::BrowserChildProcessHostIterator iter(PROCESS_TYPE_NACL_LOADER);
+  while (!iter.Done()) {
+    nacl::NaClProcessHost* host = static_cast<nacl::NaClProcessHost*>(
+        iter.GetDelegate());
+    if (host->process() &&
+        host->process()->GetData().id == plugin_process_id) {
+      // Found the plugin.
+      return host->browser_ppapi_host();
+    }
+    ++iter;
   }
-  mappings->push_back(
-      content::FileDescriptorInfo(kXWalkPakDescriptor,
-                                  base::FileDescriptor(f, true)));
+#endif
+  return NULL;
 }
 
 void XWalkContentBrowserClient::ResourceDispatcherHostCreated() {
-  RuntimeResourceDispatcherHostDelegateAndroid::
-  ResourceDispatcherHostCreated();
+  resource_dispatcher_host_delegate_ =
+      (RuntimeResourceDispatcherHostDelegate::Create()).Pass();
+  content::ResourceDispatcherHost::Get()->SetDelegate(
+      resource_dispatcher_host_delegate_.get());
 }
-#endif
 
 content::SpeechRecognitionManagerDelegate*
     XWalkContentBrowserClient::GetSpeechRecognitionManagerDelegate() {
   return new xwalk::XWalkSpeechRecognitionManagerDelegate();
 }
 
+#if !defined(OS_ANDROID)
+bool XWalkContentBrowserClient::CanCreateWindow(const GURL& opener_url,
+                             const GURL& opener_top_level_frame_url,
+                             const GURL& source_origin,
+                             WindowContainerType container_type,
+                             const GURL& target_url,
+                             const content::Referrer& referrer,
+                             WindowOpenDisposition disposition,
+                             const blink::WebWindowFeatures& features,
+                             bool user_gesture,
+                             bool opener_suppressed,
+                             content::ResourceContext* context,
+                             int render_process_id,
+                             int opener_id,
+                             bool* no_javascript_access) {
+  *no_javascript_access = false;
+  application::Application* app = xwalk_runner_->app_system()->
+      application_service()->GetApplicationByRenderHostID(render_process_id);
+  if (!app)
+    // If it's not a request from an application, always enable this action.
+    return true;
+
+  if (app->CanRequestURL(target_url)) {
+    LOG(INFO) << "[ALLOW] CreateWindow: " << target_url.spec();
+    return true;
+  }
+
+  LOG(INFO) << "[BlOCK] CreateWindow: " << target_url.spec();
+#if defined(OS_TIZEN)
+  platform_util::OpenExternal(target_url);
+#endif
+  return false;
+}
+#endif
+
+
+void XWalkContentBrowserClient::GetStoragePartitionConfigForSite(
+    content::BrowserContext* browser_context,
+    const GURL& site,
+    bool can_be_default,
+    std::string* partition_domain,
+    std::string* partition_name,
+    bool* in_memory) {
+  *in_memory = false;
+  partition_domain->clear();
+  partition_name->clear();
+
+#if !defined(OS_ANDROID)
+  if (site.SchemeIs(application::kApplicationScheme))
+    *partition_domain = site.host();
+#endif
+}
+
+content::DevToolsManagerDelegate*
+  XWalkContentBrowserClient::GetDevToolsManagerDelegate() {
+  return new XWalkDevToolsDelegate(runtime_context_);
+}
+
 }  // namespace xwalk