Merge pull request #9423 from electron/brightray-import

Import brightray source and remove submodule
This commit is contained in:
Kevin Sawicki 2017-05-11 08:35:41 -07:00 committed by GitHub
commit ae71dfe3d1
168 changed files with 13547 additions and 82 deletions

9
.gitignore vendored
View file

@ -5,7 +5,7 @@
/dist/
/external_binaries/
/out/
/vendor/brightray/vendor/download/
/vendor/download/
/vendor/debian_wheezy_amd64-sysroot/
/vendor/debian_wheezy_arm-sysroot/
/vendor/debian_wheezy_i386-sysroot/
@ -27,3 +27,10 @@ node_modules/
*.vcxproj.filters
*.sln
*.log
/brightray/brightray.opensdf
/brightray/brightray.sdf
/brightray/brightray.sln
/brightray/brightray.vcxproj*
/brightray/brightray.suo
/brightray/brightray.v12.suo
/brightray/brightray.xcodeproj/

9
.gitmodules vendored
View file

@ -1,6 +1,3 @@
[submodule "vendor/brightray"]
path = vendor/brightray
url = https://github.com/electron/brightray.git
[submodule "vendor/node"]
path = vendor/node
url = https://github.com/electron/node.git
@ -25,3 +22,9 @@
[submodule "vendor/pdf_viewer"]
path = vendor/pdf_viewer
url = https://github.com/electron/pdf-viewer.git
[submodule "vendor/gyp"]
path = vendor/gyp
url = https://github.com/electron/gyp
[submodule "vendor/libchromiumcontent"]
path = vendor/libchromiumcontent
url = https://github.com/electron/libchromiumcontent

1
brightray/CPPLINT.cfg Normal file
View file

@ -0,0 +1 @@
filter=-build/header_guard,-build/include_what_you_use,-legal/copyright,-runtime/references

19
brightray/LICENSE Normal file
View file

@ -0,0 +1,19 @@
Copyright (c) 2013-2014 Adam Roben <adam@roben.org>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -0,0 +1,27 @@
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

397
brightray/brightray.gyp Normal file
View file

@ -0,0 +1,397 @@
{
'variables': {
# The libraries brightray will be compiled to.
'linux_system_libraries': 'gtk+-2.0 dbus-1 x11 x11-xcb xcb xi xcursor xdamage xrandr xcomposite xext xfixes xrender xtst xscrnsaver gconf-2.0 gmodule-2.0 nss'
},
'includes': [
'filenames.gypi',
],
'targets': [
{
'target_name': 'brightray',
'type': 'static_library',
'include_dirs': [
'.',
'<(libchromiumcontent_src_dir)',
'<(libchromiumcontent_src_dir)/skia/config',
'<(libchromiumcontent_src_dir)/third_party/boringssl/src/include',
'<(libchromiumcontent_src_dir)/third_party/skia/include/core',
'<(libchromiumcontent_src_dir)/third_party/mojo/src',
'<(libchromiumcontent_src_dir)/third_party/WebKit',
'<(libchromiumcontent_dir)/gen',
],
'direct_dependent_settings': {
'include_dirs': [
'.',
'../vendor',
'<(libchromiumcontent_src_dir)',
'<(libchromiumcontent_src_dir)/gpu',
'<(libchromiumcontent_src_dir)/skia/config',
'<(libchromiumcontent_src_dir)/third_party/boringssl/src/include',
'<(libchromiumcontent_src_dir)/third_party/skia/include/core',
'<(libchromiumcontent_src_dir)/third_party/skia/include/config',
'<(libchromiumcontent_src_dir)/third_party/icu/source/common',
'<(libchromiumcontent_src_dir)/third_party/mojo/src',
'<(libchromiumcontent_src_dir)/third_party/khronos',
'<(libchromiumcontent_src_dir)/third_party/WebKit',
'<(libchromiumcontent_dir)/gen',
'<(libchromiumcontent_dir)/gen/third_party/WebKit',
],
},
'sources': [ '<@(brightray_sources)' ],
'conditions': [
# Link with libraries of libchromiumcontent.
['OS=="linux" and libchromiumcontent_component==0', {
# On Linux we have to use "--whole-archive" to force executable
# to include all symbols, otherwise we will have plenty of
# unresolved symbols errors.
'direct_dependent_settings': {
'ldflags': [
'-Wl,--whole-archive',
'<@(libchromiumcontent_libraries)',
'-Wl,--no-whole-archive',
],
}
}, { # (Release build on Linux)
'link_settings': {
'libraries': [ '<@(libchromiumcontent_libraries)' ]
},
}], # (Normal builds)
# Linux specific link settings.
['OS=="linux"', {
'link_settings': {
'ldflags': [
'<!@(<(pkg-config) --libs-only-L --libs-only-other <(linux_system_libraries))',
],
'libraries': [
'-lpthread',
'<!@(<(pkg-config) --libs-only-l <(linux_system_libraries))',
],
},
'cflags': [
'<!@(<(pkg-config) --cflags <(linux_system_libraries))',
# Needed by using libgtkui:
'-Wno-deprecated-register',
'-Wno-sentinel',
],
'cflags_cc': [
'-Wno-reserved-user-defined-literal',
],
'direct_dependent_settings': {
'cflags': [
'<!@(<(pkg-config) --cflags <(linux_system_libraries))',
'-Wno-deprecated-register',
'-Wno-sentinel',
],
},
'conditions': [
['libchromiumcontent_component', {
'link_settings': {
'libraries': [
# Following libraries are always linked statically.
'<(libchromiumcontent_dir)/libgtkui.a',
'<(libchromiumcontent_dir)/libhttp_server.a',
'<(libchromiumcontent_dir)/libdesktop_capture.a',
'<(libchromiumcontent_dir)/libdom_keycode_converter.a',
'<(libchromiumcontent_dir)/libsystem_wrappers.a',
'<(libchromiumcontent_dir)/librtc_base.a',
'<(libchromiumcontent_dir)/librtc_base_approved.a',
'<(libchromiumcontent_dir)/libwebrtc_common.a',
'<(libchromiumcontent_dir)/libyuv.a',
'<(libchromiumcontent_dir)/librenderer.a',
'<(libchromiumcontent_dir)/libsecurity_state.a',
# Friends of libpdf.a:
# On Linux we have to use "--whole-archive" to include
# all symbols, otherwise there will be plenty of
# unresolved symbols errors.
'-Wl,--whole-archive',
'<(libchromiumcontent_dir)/libpdf.a',
'<(libchromiumcontent_dir)/libppapi_cpp_objects.a',
'<(libchromiumcontent_dir)/libppapi_internal_module.a',
'<(libchromiumcontent_dir)/libjpeg.a',
'<(libchromiumcontent_dir)/libpdfium.a',
'<(libchromiumcontent_dir)/libfdrm.a',
'<(libchromiumcontent_dir)/libformfiller.a',
'<(libchromiumcontent_dir)/libfpdfapi.a',
'<(libchromiumcontent_dir)/libfpdfdoc.a',
'<(libchromiumcontent_dir)/libfpdftext.a',
'<(libchromiumcontent_dir)/libfxcodec.a',
'<(libchromiumcontent_dir)/libfxedit.a',
'<(libchromiumcontent_dir)/libfxge.a',
'<(libchromiumcontent_dir)/libfxjs.a',
'<(libchromiumcontent_dir)/libjavascript.a',
'<(libchromiumcontent_dir)/libpdfwindow.a',
'<(libchromiumcontent_dir)/libfx_agg.a',
'<(libchromiumcontent_dir)/libfx_lcms2.a',
'<(libchromiumcontent_dir)/libfx_libopenjpeg.a',
'<(libchromiumcontent_dir)/libfx_zlib.a',
'-Wl,--no-whole-archive',
],
},
}, {
'link_settings': {
'libraries': [
# Link with ffmpeg.
'<(libchromiumcontent_dir)/libffmpeg.so',
# Following libraries are required by libchromiumcontent:
'-lasound',
'-lcap',
'-lcups',
'-lrt',
'-ldl',
'-lresolv',
'-lfontconfig',
'-lfreetype',
'-lexpat',
],
},
}],
['target_arch=="arm"', {
'link_settings': {
'libraries!': [
'<(libchromiumcontent_dir)/libdesktop_capture_differ_sse2.a',
],
},
}],
],
}], # OS=="linux"
['OS=="mac"', {
'link_settings': {
'libraries': [
'$(SDKROOT)/System/Library/Frameworks/AppKit.framework',
# Required by webrtc:
'$(SDKROOT)/System/Library/Frameworks/OpenGL.framework',
'$(SDKROOT)/System/Library/Frameworks/IOKit.framework',
# Required by media:
'$(SDKROOT)/System/Library/Frameworks/VideoToolbox.framework',
],
},
'conditions': [
['libchromiumcontent_component', {
'link_settings': {
'libraries': [
# Following libraries are always linked statically.
'<(libchromiumcontent_dir)/libhttp_server.a',
'<(libchromiumcontent_dir)/libdesktop_capture.a',
'<(libchromiumcontent_dir)/libdom_keycode_converter.a',
'<(libchromiumcontent_dir)/librtc_base.a',
'<(libchromiumcontent_dir)/librtc_base_approved.a',
'<(libchromiumcontent_dir)/libsystem_wrappers.a',
'<(libchromiumcontent_dir)/libwebrtc_common.a',
'<(libchromiumcontent_dir)/libyuv.a',
'<(libchromiumcontent_dir)/librenderer.a',
'<(libchromiumcontent_dir)/libsecurity_state.a',
# Friends of libpdf.a:
'<(libchromiumcontent_dir)/libpdf.a',
'<(libchromiumcontent_dir)/libppapi_cpp_objects.a',
'<(libchromiumcontent_dir)/libppapi_internal_module.a',
'<(libchromiumcontent_dir)/libjpeg.a',
'<(libchromiumcontent_dir)/libpdfium.a',
'<(libchromiumcontent_dir)/libfdrm.a',
'<(libchromiumcontent_dir)/libformfiller.a',
'<(libchromiumcontent_dir)/libfpdfapi.a',
'<(libchromiumcontent_dir)/libfpdfdoc.a',
'<(libchromiumcontent_dir)/libfpdftext.a',
'<(libchromiumcontent_dir)/libfxcodec.a',
'<(libchromiumcontent_dir)/libfxcrt.a',
'<(libchromiumcontent_dir)/libfxedit.a',
'<(libchromiumcontent_dir)/libfxge.a',
'<(libchromiumcontent_dir)/libfxjs.a',
'<(libchromiumcontent_dir)/libjavascript.a',
'<(libchromiumcontent_dir)/libpdfwindow.a',
'<(libchromiumcontent_dir)/libfx_agg.a',
'<(libchromiumcontent_dir)/libfx_freetype.a',
'<(libchromiumcontent_dir)/libfx_lcms2.a',
'<(libchromiumcontent_dir)/libfx_libopenjpeg.a',
'<(libchromiumcontent_dir)/libfx_zlib.a',
],
},
}, {
'link_settings': {
'libraries': [
# Link with ffmpeg.
'<(libchromiumcontent_dir)/libffmpeg.dylib',
# Link with system frameworks.
# ui_base.gypi:
'$(SDKROOT)/System/Library/Frameworks/Accelerate.framework',
# net.gypi:
'$(SDKROOT)/System/Library/Frameworks/Foundation.framework',
'$(SDKROOT)/System/Library/Frameworks/Security.framework',
'$(SDKROOT)/System/Library/Frameworks/SystemConfiguration.framework',
'-lresolv',
# media.gyp:
'$(SDKROOT)/System/Library/Frameworks/AudioToolbox.framework',
'$(SDKROOT)/System/Library/Frameworks/AudioUnit.framework',
'$(SDKROOT)/System/Library/Frameworks/AVFoundation.framework',
'$(SDKROOT)/System/Library/Frameworks/CoreAudio.framework',
'$(SDKROOT)/System/Library/Frameworks/CoreMedia.framework',
'$(SDKROOT)/System/Library/Frameworks/CoreMIDI.framework',
'$(SDKROOT)/System/Library/Frameworks/CoreVideo.framework',
# surface.gyp:
'$(SDKROOT)/System/Library/Frameworks/IOSurface.framework',
# content_common.gypi:
'$(SDKROOT)/System/Library/Frameworks/QuartzCore.framework',
# base.gyp:
'$(SDKROOT)/System/Library/Frameworks/ApplicationServices.framework',
'$(SDKROOT)/System/Library/Frameworks/Carbon.framework',
'$(SDKROOT)/System/Library/Frameworks/CoreFoundation.framework',
# device/gamepad/BUILD.gn:
'$(SDKROOT)/System/Library/Frameworks/GameController.framework',
# content_browser.gypi:
'-lbsm',
# content_common.gypi:
'-lsandbox',
# bluetooth.gyp:
'$(SDKROOT)/System/Library/Frameworks/IOBluetooth.framework',
# components/wifi/BUILD.gn:
'$(SDKROOT)/System/Library/Frameworks/CoreWLAN.framework',
],
},
}],
]
}], # OS=="mac"
['OS=="win"', {
'conditions': [
['libchromiumcontent_component', {
'link_settings': {
'libraries': [
# Needed by desktop_capture.lib:
'-ld3d11.lib',
'-ldxgi.lib',
# Following libs are always linked statically.
'<(libchromiumcontent_dir)/base_static.lib',
'<(libchromiumcontent_dir)/sandbox.lib',
'<(libchromiumcontent_dir)/sandbox_helper_win.lib',
'<(libchromiumcontent_dir)/http_server.lib',
'<(libchromiumcontent_dir)/desktop_capture.lib',
'<(libchromiumcontent_dir)/dom_keycode_converter.lib',
'<(libchromiumcontent_dir)/rtc_base.lib',
'<(libchromiumcontent_dir)/rtc_base_approved.lib',
'<(libchromiumcontent_dir)/system_wrappers.lib',
'<(libchromiumcontent_dir)/webrtc_common.lib',
'<(libchromiumcontent_dir)/libyuv.lib',
'<(libchromiumcontent_dir)/renderer.lib',
'<(libchromiumcontent_dir)/security_state.lib',
# Friends of pdf.lib:
'<(libchromiumcontent_dir)/pdf.lib',
'<(libchromiumcontent_dir)/ppapi_cpp_objects.lib',
'<(libchromiumcontent_dir)/ppapi_internal_module.lib',
'<(libchromiumcontent_dir)/libjpeg.lib',
'<(libchromiumcontent_dir)/pdfium.lib',
'<(libchromiumcontent_dir)/fdrm.lib',
'<(libchromiumcontent_dir)/formfiller.lib',
'<(libchromiumcontent_dir)/fpdfapi.lib',
'<(libchromiumcontent_dir)/fpdfdoc.lib',
'<(libchromiumcontent_dir)/fpdftext.lib',
'<(libchromiumcontent_dir)/fpdftext.lib',
'<(libchromiumcontent_dir)/fxcodec.lib',
'<(libchromiumcontent_dir)/fxcrt.lib',
'<(libchromiumcontent_dir)/fxedit.lib',
'<(libchromiumcontent_dir)/fxge.lib',
'<(libchromiumcontent_dir)/fxjs.lib',
'<(libchromiumcontent_dir)/javascript.lib',
'<(libchromiumcontent_dir)/pdfwindow.lib',
'<(libchromiumcontent_dir)/fx_agg.lib',
'<(libchromiumcontent_dir)/fx_freetype.lib',
'<(libchromiumcontent_dir)/fx_lcms2.lib',
'<(libchromiumcontent_dir)/fx_libopenjpeg.lib',
'<(libchromiumcontent_dir)/fx_zlib.lib',
],
},
}, {
# Link with system libraries.
'link_settings': {
'libraries': [
# Link with ffmpeg.
'<(libchromiumcontent_dir)/ffmpeg.dll',
# content_browser.gypi:
'-lsensorsapi.lib',
'-lportabledeviceguids.lib',
# content_common.gypi:
'-ld3d9.lib',
'-ld3d11.lib',
'-ldxgi.lib',
'-ldxva2.lib',
'-lstrmiids.lib',
'-lmf.lib',
'-lmfplat.lib',
'-lmfuuid.lib',
# media.gyp:
'-ldxguid.lib',
'-lmfreadwrite.lib',
'-lmfuuid.lib',
],
'msvs_settings': {
'VCLinkerTool': {
'AdditionalDependencies': [
'advapi32.lib',
'dbghelp.lib',
'delayimp.lib',
'dwmapi.lib',
'gdi32.lib',
'netapi32.lib',
'oleacc.lib',
'user32.lib',
'usp10.lib',
'version.lib',
'winspool.lib',
'wtsapi32.lib',
# bluetooth.gyp:
'Bthprops.lib',
'BluetoothApis.lib',
# base.gyp:
'cfgmgr32.lib',
'powrprof.lib',
'setupapi.lib',
# net_common.gypi:
'crypt32.lib',
'dhcpcsvc.lib',
'ncrypt.lib',
'rpcrt4.lib',
'secur32.lib',
'urlmon.lib',
'winhttp.lib',
# ui/gfx/BUILD.gn:
'dwrite.lib',
# skia/BUILD.gn:
'fontsub.lib',
],
'DelayLoadDLLs': [
'wtsapi32.dll',
# content_common.gypi:
'd3d9.dll',
'd3d11.dll',
'dxva2.dll',
# media.gyp:
'mf.dll',
'mfplat.dll',
'mfreadwrite.dll',
# bluetooth.gyp:
'BluetoothApis.dll',
'Bthprops.cpl',
'setupapi.dll',
# base.gyp:
'cfgmgr32.dll',
'powrprof.dll',
'setupapi.dll',
# net_common.gypi:
'crypt32.dll',
'dhcpcsvc.dll',
'rpcrt4.dll',
'secur32.dll',
'urlmon.dll',
'winhttp.dll',
# windows runtime
'API-MS-WIN-CORE-WINRT-L1-1-0.DLL',
'API-MS-WIN-CORE-WINRT-STRING-L1-1-0.DLL',
],
},
},
},
}], # libchromiumcontent_component
],
}], # OS=="win"
],
},
],
}

361
brightray/brightray.gypi Normal file
View file

@ -0,0 +1,361 @@
{
'includes': [
'../vendor/download/libchromiumcontent/filenames.gypi',
],
'variables': {
'libchromiumcontent_component%': 1,
'pkg-config%': 'pkg-config',
'conditions': [
# The "libchromiumcontent_component" is defined when calling "gyp".
['libchromiumcontent_component', {
'libchromiumcontent_dir%': '<(libchromiumcontent_shared_libraries_dir)',
'libchromiumcontent_libraries%': '<(libchromiumcontent_shared_libraries)',
'libchromiumcontent_v8_libraries%': '<(libchromiumcontent_shared_v8_libraries)',
}, {
'libchromiumcontent_dir%': '<(libchromiumcontent_static_libraries_dir)',
'libchromiumcontent_libraries%': '<(libchromiumcontent_static_libraries)',
'libchromiumcontent_v8_libraries%': '<(libchromiumcontent_static_v8_libraries)',
}],
],
},
'target_defaults': {
'includes': [
# Rules for excluding e.g. foo_win.cc from the build on non-Windows.
'filename_rules.gypi',
],
# Putting this in "configurations" will make overrides not working.
'xcode_settings': {
'ALWAYS_SEARCH_USER_PATHS': 'NO',
'ARCHS': ['x86_64'],
'COMBINE_HIDPI_IMAGES': 'YES',
'GCC_ENABLE_CPP_EXCEPTIONS': 'NO',
'GCC_ENABLE_CPP_RTTI': 'NO',
'GCC_TREAT_WARNINGS_AS_ERRORS': 'YES',
'CLANG_CXX_LANGUAGE_STANDARD': 'c++11',
'MACOSX_DEPLOYMENT_TARGET': '10.9',
'RUN_CLANG_STATIC_ANALYZER': 'YES',
'USE_HEADER_MAP': 'NO',
},
'msvs_configuration_attributes': {
'OutputDirectory': '<(DEPTH)\\build\\$(ConfigurationName)',
'IntermediateDirectory': '$(OutDir)\\obj\\$(ProjectName)',
'CharacterSet': '1',
},
'msvs_system_include_dirs': [
'$(VSInstallDir)/VC/atlmfc/include',
],
'msvs_settings': {
'VCCLCompilerTool': {
'AdditionalOptions': ['/MP'],
'MinimalRebuild': 'false',
'BufferSecurityCheck': 'true',
'EnableFunctionLevelLinking': 'true',
'RuntimeTypeInfo': 'false',
'WarningLevel': '4',
'WarnAsError': 'true',
'DebugInformationFormat': '3',
# Programs that use the Standard C++ library must be compiled with
# C++
# exception handling enabled.
# http://support.microsoft.com/kb/154419
'ExceptionHandling': 1,
},
'VCLinkerTool': {
'GenerateDebugInformation': 'true',
'MapFileName': '$(OutDir)\\$(TargetName).map',
'ImportLibrary': '$(OutDir)\\lib\\$(TargetName).lib',
'LargeAddressAware': '2',
'AdditionalOptions': [
# ATL 8.0 included in WDK 7.1 makes the linker to generate
# following
# warnings:
# - warning LNK4254: section 'ATL' (50000040) merged into
# '.rdata' (40000040) with different attributes
# - warning LNK4078: multiple 'ATL' sections found with
# different attributes
'/ignore:4254',
'/ignore:4078',
# views_chromiumcontent.lib generates this warning because it's
# symobls are defined as dllexport but used as static library:
# - warning LNK4217: locally defined symbol imported in function
# - warning LNK4049: locally defined symbol imported
'/ignore:4217',
'/ignore:4049',
],
},
},
'configurations': {
# The "Debug" and "Release" configurations are not actually used.
'Debug': {},
'Release': {},
'Common_Base': {
'abstract': 1,
'defines': [
# We are using Release version libchromiumcontent:
'NDEBUG',
# Needed by gin:
'V8_USE_EXTERNAL_STARTUP_DATA',
# From skia_for_chromium_defines.gypi:
'SK_SUPPORT_LEGACY_GETTOPDEVICE',
'SK_SUPPORT_LEGACY_BITMAP_CONFIG',
'SK_SUPPORT_LEGACY_DEVICE_VIRTUAL_ISOPAQUE',
'SK_SUPPORT_LEGACY_N32_NAME',
'SK_SUPPORT_LEGACY_SETCONFIG',
'SK_IGNORE_ETC1_SUPPORT',
'SK_IGNORE_GPU_DITHER',
# NACL is not enabled:
'DISABLE_NACL',
],
'conditions': [
['OS!="mac"', {
'defines': [
'TOOLKIT_VIEWS',
'USE_AURA',
],
}],
['OS in ["mac", "win"]', {
'defines': [
'USE_OPENSSL',
],
}, {
'defines': [
'USE_X11',
# "use_nss_certs" is set to 1 in libchromiumcontent.
'USE_NSS_CERTS',
'USE_NSS', # deprecated after Chrome 45.
],
}],
['OS=="linux"', {
'defines': [
'_LARGEFILE_SOURCE',
'_LARGEFILE64_SOURCE',
'_FILE_OFFSET_BITS=64',
],
'cflags_cc': [
'-D__STRICT_ANSI__',
'-fno-rtti',
],
}], # OS=="linux"
['OS=="mac"', {
'defines': [
# The usage of "webrtc/modules/desktop_capture/desktop_capture_options.h"
# is required to see this macro.
'WEBRTC_MAC',
],
}], # OS=="mac"
['OS=="win"', {
'include_dirs': [
'<(libchromiumcontent_src_dir)/third_party/wtl/include',
],
'defines': [
'_WIN32_WINNT=0x0602',
'WINVER=0x0602',
'WIN32',
'_WINDOWS',
'NOMINMAX',
'PSAPI_VERSION=1',
'_CRT_RAND_S',
'CERT_CHAIN_PARA_HAS_EXTRA_FIELDS',
'WIN32_LEAN_AND_MEAN',
'_ATL_NO_OPENGL',
'_SECURE_ATL',
# The usage of "webrtc/modules/desktop_capture/desktop_capture_options.h"
# is required to see this macro.
'WEBRTC_WIN',
],
'conditions': [
['target_arch=="x64"', {
'msvs_configuration_platform': 'x64',
'msvs_settings': {
'VCLinkerTool': {
'MinimumRequiredVersion': '5.02', # Server 2003.
'TargetMachine': '17', # x86 - 64
# Doesn't exist x64 SDK. Should use oleaut32 in any case.
'IgnoreDefaultLibraryNames': [ 'olepro32.lib' ],
},
'VCLibrarianTool': {
'TargetMachine': '17', # x64
},
},
}],
],
}], # OS=="win"
],
}, # Common_Base
'Debug_Base': {
'abstract': 1,
'defines': [
# Use this instead of "NDEBUG" to determine whether we are in
# Debug build, because "NDEBUG" is already used by Chromium.
'DEBUG',
# Require when using libchromiumcontent.
'COMPONENT_BUILD',
'GURL_DLL',
'SKIA_DLL',
'USING_V8_SHARED',
'WEBKIT_DLL',
],
'msvs_settings': {
'VCCLCompilerTool': {
'RuntimeLibrary': '2', # /MD (nondebug DLL)
'Optimization': '0', # 0 = /Od
# See http://msdn.microsoft.com/en-us/library/8wtf2dfz(VS.71).aspx
'BasicRuntimeChecks': '3', # 3 = all checks enabled, 0 = off
},
},
}, # Debug_Base
'Release_Base': {
'abstract': 1,
'msvs_settings': {
'VCCLCompilerTool': {
'RuntimeLibrary': '0', # /MT (nondebug static)
# 1, optimizeMinSpace, Minimize Size (/O1)
'Optimization': '1',
# 2, favorSize - Favor small code (/Os)
'FavorSizeOrSpeed': '2',
# See http://msdn.microsoft.com/en-us/library/47238hez(VS.71).aspx
'InlineFunctionExpansion': '2', # 2 = max
# See http://msdn.microsoft.com/en-us/library/2kxx5t2c(v=vs.80).aspx
'OmitFramePointers': 'false',
# The above is not sufficient (http://crbug.com/106711): it
# simply eliminates an explicit "/Oy", but both /O2 and /Ox
# perform FPO regardless, so we must explicitly disable.
# We still want the false setting above to avoid having
# "/Oy /Oy-" and warnings about overriding.
'AdditionalOptions': ['/Oy-'],
},
'VCLinkerTool': {
# Turn off incremental linking to save binary size.
'LinkIncremental': '1', # /INCREMENTAL:NO
},
},
'conditions': [
['OS=="linux"', {
'cflags': [
'-O2',
# Generate symbols, will be stripped later.
'-g',
# Don't emit the GCC version ident directives, they just end up
# in the .comment section taking up binary size.
'-fno-ident',
# Put data and code in their own sections, so that unused symbols
# can be removed at link time with --gc-sections.
'-fdata-sections',
'-ffunction-sections',
],
'ldflags': [
# Specifically tell the linker to perform optimizations.
# See http://lwn.net/Articles/192624/ .
'-Wl,-O1',
'-Wl,--as-needed',
'-Wl,--gc-sections',
],
}], # OS=="linux"
],
}, # Release_Base
'conditions': [
['libchromiumcontent_component', {
'D': {
'inherit_from': ['Common_Base', 'Debug_Base'],
}, # D (Debug)
}, {
'R': {
'inherit_from': ['Common_Base', 'Release_Base'],
}, # R (Release)
}], # libchromiumcontent_component
['OS=="win"', {
'conditions': [
# gyp always assumes "_x64" targets on Windows.
['libchromiumcontent_component', {
'D_x64': {
'inherit_from': ['Common_Base', 'Debug_Base'],
}, # D_x64
}, {
'R_x64': {
'inherit_from': ['Common_Base', 'Release_Base'],
}, # R_x64
}], # libchromiumcontent_component
],
}], # OS=="win"
],
}, # configurations
'target_conditions': [
# Putting this under "configurations" doesn't work.
['libchromiumcontent_component', {
'xcode_settings': {
'GCC_OPTIMIZATION_LEVEL': '0',
},
}, { # "Debug_Base"
'xcode_settings': {
'DEAD_CODE_STRIPPING': 'YES', # -Wl,-dead_strip
'GCC_OPTIMIZATION_LEVEL': '2',
'OTHER_CFLAGS': [
'-fno-inline',
'-fno-omit-frame-pointer',
'-fno-builtin',
'-fno-optimize-sibling-calls',
],
},
}], # "Release_Base"
['OS=="mac" and libchromiumcontent_component==0 and _type in ["executable", "shared_library"]', {
'xcode_settings': {
# Generates symbols and strip the binary.
'DEBUG_INFORMATION_FORMAT': 'dwarf-with-dsym',
'DEPLOYMENT_POSTPROCESSING': 'YES',
'STRIP_INSTALLED_PRODUCT': 'YES',
'STRIPFLAGS': '-x',
},
}], # OS=="mac" and libchromiumcontent_component==0 and _type in ["executable", "shared_library"]
['OS=="linux" and target_arch=="ia32" and _toolset=="target"', {
'ldflags': [
# Workaround for linker OOM.
'-Wl,--no-keep-memory',
],
}],
], # target_conditions
# Ignored compiler warnings of Chromium.
'conditions': [
['OS=="mac"', {
'xcode_settings': {
'WARNING_CFLAGS': [
'-Wall',
'-Wextra',
'-Wno-unused-parameter',
'-Wno-missing-field-initializers',
'-Wno-deprecated-declarations',
'-Wno-undefined-var-template', # https://crbug.com/604888
'-Wno-unneeded-internal-declaration',
'-Wno-inconsistent-missing-override',
],
},
}],
['OS=="linux"', {
'cflags': [
'-Wno-inconsistent-missing-override',
'-Wno-undefined-var-template', # https://crbug.com/604888
],
}],
['OS=="win"', {
'msvs_disabled_warnings': [
4100, # unreferenced formal parameter
4121, # alignment of a member was sensitive to packing
4127, # conditional expression is constant
4189, # local variable is initialized but not referenced
4244, # 'initializing' : conversion from 'double' to 'size_t', possible loss of data
4245, # 'initializing' : conversion from 'int' to 'const net::QuicVersionTag', signed/unsigned mismatch
4251, # class 'std::xx' needs to have dll-interface.
4310, # cast truncates constant value
4355, # 'this' : used in base member initializer list
4480, # nonstandard extension used: specifying underlying type for enum
4481, # nonstandard extension used: override specifier 'override'
4510, # default constructor could not be generated
4512, # assignment operator could not be generated
4610, # user defined constructor required
4702, # unreachable code
4819, # The file contains a character that cannot be represented in the current code page
],
}],
], # conditions
}, # target_defaults
}

View file

@ -0,0 +1,45 @@
// Copyright 2015 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.
#ifndef BROWSER_BRIGHTRAY_PATHS_H_
#define BROWSER_BRIGHTRAY_PATHS_H_
#include "base/compiler_specific.h"
#if defined(OS_WIN)
#include "base/base_paths_win.h"
#elif defined(OS_MACOSX)
#include "base/base_paths_mac.h"
#endif
#if defined(OS_POSIX)
#include "base/base_paths_posix.h"
#endif
namespace brightray {
enum {
PATH_START = 11000,
DIR_USER_DATA = PATH_START, // Directory where user data can be written.
DIR_USER_CACHE, // Directory where user cache can be written.
#if defined(OS_LINUX)
DIR_APP_DATA, // Application Data directory under the user profile.
#else
DIR_APP_DATA = base::DIR_APP_DATA,
#endif
#if defined(OS_POSIX)
DIR_CACHE = base::DIR_CACHE, // Directory where to put cache data.
#else
DIR_CACHE = base::DIR_APP_DATA,
#endif
PATH_END
};
} // namespace brightray
#endif // BROWSER_BRIGHTRAY_PATHS_H_

View file

@ -0,0 +1,91 @@
// 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-CHROMIUM file.
#include "browser/browser_client.h"
#include "base/path_service.h"
#include "browser/browser_context.h"
#include "browser/browser_main_parts.h"
#include "browser/devtools_manager_delegate.h"
#include "browser/media/media_capture_devices_dispatcher.h"
#include "browser/notification_presenter.h"
#include "browser/platform_notification_service.h"
#include "content/public/common/url_constants.h"
namespace brightray {
namespace {
BrowserClient* g_browser_client;
} // namespace
BrowserClient* BrowserClient::Get() {
return g_browser_client;
}
BrowserClient::BrowserClient()
: browser_main_parts_(nullptr) {
DCHECK(!g_browser_client);
g_browser_client = this;
}
BrowserClient::~BrowserClient() {
}
NotificationPresenter* BrowserClient::GetNotificationPresenter() {
if (!notification_presenter_) {
// Create a new presenter if on OS X, Linux, or Windows 8+
notification_presenter_.reset(NotificationPresenter::Create());
}
return notification_presenter_.get();
}
BrowserMainParts* BrowserClient::OverrideCreateBrowserMainParts(
const content::MainFunctionParams&) {
return new BrowserMainParts;
}
content::BrowserMainParts* BrowserClient::CreateBrowserMainParts(
const content::MainFunctionParams& parameters) {
DCHECK(!browser_main_parts_);
browser_main_parts_ = OverrideCreateBrowserMainParts(parameters);
return browser_main_parts_;
}
content::MediaObserver* BrowserClient::GetMediaObserver() {
return MediaCaptureDevicesDispatcher::GetInstance();
}
content::PlatformNotificationService*
BrowserClient::GetPlatformNotificationService() {
if (!notification_service_)
notification_service_.reset(new PlatformNotificationService(this));
return notification_service_.get();
}
void BrowserClient::GetAdditionalAllowedSchemesForFileSystem(
std::vector<std::string>* additional_schemes) {
additional_schemes->push_back(content::kChromeDevToolsScheme);
additional_schemes->push_back(content::kChromeUIScheme);
}
net::NetLog* BrowserClient::GetNetLog() {
return &net_log_;
}
base::FilePath BrowserClient::GetDefaultDownloadDirectory() {
// ~/Downloads
base::FilePath path;
if (PathService::Get(base::DIR_HOME, &path))
path = path.Append(FILE_PATH_LITERAL("Downloads"));
return path;
}
content::DevToolsManagerDelegate* BrowserClient::GetDevToolsManagerDelegate() {
return new DevToolsManagerDelegate;
}
} // namespace brightray

View file

@ -0,0 +1,69 @@
// 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-CHROMIUM file.
#ifndef BRIGHTRAY_BROWSER_BROWSER_CLIENT_H_
#define BRIGHTRAY_BROWSER_BROWSER_CLIENT_H_
#include "browser/net_log.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/content_browser_client.h"
namespace brightray {
class BrowserContext;
class BrowserMainParts;
class NotificationPresenter;
class PlatformNotificationService;
class BrowserClient : public content::ContentBrowserClient {
public:
static BrowserClient* Get();
BrowserClient();
~BrowserClient();
BrowserMainParts* browser_main_parts() { return browser_main_parts_; }
NotificationPresenter* GetNotificationPresenter();
// Subclasses should override this to enable or disable WebNotification.
virtual void WebNotificationAllowed(
int render_process_id,
const base::Callback<void(bool, bool)>& callback) {
callback.Run(false, true);
}
// Subclasses that override this (e.g., to provide their own protocol
// handlers) should call this implementation after doing their own work.
content::BrowserMainParts* CreateBrowserMainParts(
const content::MainFunctionParams&) override;
content::MediaObserver* GetMediaObserver() override;
content::PlatformNotificationService* GetPlatformNotificationService()
override;
void GetAdditionalAllowedSchemesForFileSystem(
std::vector<std::string>* additional_schemes) override;
net::NetLog* GetNetLog() override;
base::FilePath GetDefaultDownloadDirectory() override;
content::DevToolsManagerDelegate* GetDevToolsManagerDelegate() override;
protected:
// Subclasses should override this to provide their own BrowserMainParts
// implementation. The lifetime of the returned instance is managed by the
// caller.
virtual BrowserMainParts* OverrideCreateBrowserMainParts(
const content::MainFunctionParams&);
private:
BrowserMainParts* browser_main_parts_;
NetLog net_log_;
std::unique_ptr<PlatformNotificationService> notification_service_;
std::unique_ptr<NotificationPresenter> notification_presenter_;
DISALLOW_COPY_AND_ASSIGN(BrowserClient);
};
} // namespace brightray
#endif

View file

@ -0,0 +1,240 @@
// 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-CHROMIUM file.
#include "browser/browser_context.h"
#include "browser/media/media_device_id_salt.h"
#include "browser/brightray_paths.h"
#include "browser/browser_client.h"
#include "browser/inspectable_web_contents_impl.h"
#include "browser/network_delegate.h"
#include "browser/permission_manager.h"
#include "browser/special_storage_policy.h"
#include "browser/zoom_level_delegate.h"
#include "common/application_info.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "components/prefs/json_pref_store.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/pref_service_factory.h"
#include "base/strings/string_util.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_context.h"
#include "content/public/browser/storage_partition.h"
#include "net/base/escape.h"
using content::BrowserThread;
namespace brightray {
namespace {
// Convert string to lower case and escape it.
std::string MakePartitionName(const std::string& input) {
return net::EscapePath(base::ToLowerASCII(input));
}
} // namespace
class BrowserContext::ResourceContext : public content::ResourceContext {
public:
ResourceContext() : getter_(nullptr) {}
void set_url_request_context_getter(URLRequestContextGetter* getter) {
getter_ = getter;
}
private:
net::HostResolver* GetHostResolver() override {
return getter_->host_resolver();
}
net::URLRequestContext* GetRequestContext() override {
return getter_->GetURLRequestContext();
}
std::string GetMediaDeviceIDSalt() override {
auto media_device_id_salt_ = getter_->GetMediaDeviceIDSalt();
if (media_device_id_salt_)
return media_device_id_salt_->GetSalt();
return content::ResourceContext::GetMediaDeviceIDSalt();
}
URLRequestContextGetter* getter_;
};
// static
BrowserContext::BrowserContextMap BrowserContext::browser_context_map_;
// static
scoped_refptr<BrowserContext> BrowserContext::Get(
const std::string& partition, bool in_memory) {
PartitionKey key(partition, in_memory);
if (browser_context_map_[key].get())
return make_scoped_refptr(browser_context_map_[key].get());
return nullptr;
}
BrowserContext::BrowserContext(const std::string& partition, bool in_memory)
: in_memory_(in_memory),
resource_context_(new ResourceContext),
storage_policy_(new SpecialStoragePolicy),
weak_factory_(this) {
if (!PathService::Get(DIR_USER_DATA, &path_)) {
PathService::Get(DIR_APP_DATA, &path_);
path_ = path_.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName()));
PathService::Override(DIR_USER_DATA, path_);
}
if (!in_memory_ && !partition.empty())
path_ = path_.Append(FILE_PATH_LITERAL("Partitions"))
.Append(base::FilePath::FromUTF8Unsafe(
MakePartitionName(partition)));
content::BrowserContext::Initialize(this, path_);
browser_context_map_[PartitionKey(partition, in_memory)] = GetWeakPtr();
}
BrowserContext::~BrowserContext() {
NotifyWillBeDestroyed(this);
ShutdownStoragePartitions();
BrowserThread::DeleteSoon(BrowserThread::IO,
FROM_HERE,
resource_context_.release());
}
void BrowserContext::InitPrefs() {
auto prefs_path = GetPath().Append(FILE_PATH_LITERAL("Preferences"));
PrefServiceFactory prefs_factory;
prefs_factory.SetUserPrefsFile(prefs_path,
JsonPrefStore::GetTaskRunnerForFile(
prefs_path, BrowserThread::GetBlockingPool()).get());
auto registry = make_scoped_refptr(new PrefRegistrySimple);
RegisterInternalPrefs(registry.get());
RegisterPrefs(registry.get());
prefs_ = prefs_factory.Create(registry.get());
}
void BrowserContext::RegisterInternalPrefs(PrefRegistrySimple* registry) {
InspectableWebContentsImpl::RegisterPrefs(registry);
MediaDeviceIDSalt::RegisterPrefs(registry);
ZoomLevelDelegate::RegisterPrefs(registry);
}
URLRequestContextGetter* BrowserContext::GetRequestContext() {
return static_cast<URLRequestContextGetter*>(
GetDefaultStoragePartition(this)->GetURLRequestContext());
}
net::URLRequestContextGetter* BrowserContext::CreateRequestContext(
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector protocol_interceptors) {
DCHECK(!url_request_getter_.get());
url_request_getter_ = new URLRequestContextGetter(
this,
network_controller_handle(),
static_cast<NetLog*>(BrowserClient::Get()->GetNetLog()),
GetPath(),
in_memory_,
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE),
protocol_handlers,
std::move(protocol_interceptors));
resource_context_->set_url_request_context_getter(url_request_getter_.get());
return url_request_getter_.get();
}
net::NetworkDelegate* BrowserContext::CreateNetworkDelegate() {
return new NetworkDelegate;
}
MediaDeviceIDSalt* BrowserContext::GetMediaDeviceIDSalt() {
if (IsOffTheRecord())
return nullptr;
if (!media_device_id_salt_.get())
media_device_id_salt_.reset(new MediaDeviceIDSalt(prefs_.get()));
return media_device_id_salt_.get();
}
base::FilePath BrowserContext::GetPath() const {
return path_;
}
std::unique_ptr<content::ZoomLevelDelegate>
BrowserContext::CreateZoomLevelDelegate(const base::FilePath& partition_path) {
if (!IsOffTheRecord()) {
return base::MakeUnique<ZoomLevelDelegate>(prefs(), partition_path);
}
return std::unique_ptr<content::ZoomLevelDelegate>();
}
bool BrowserContext::IsOffTheRecord() const {
return in_memory_;
}
content::ResourceContext* BrowserContext::GetResourceContext() {
return resource_context_.get();
}
content::DownloadManagerDelegate* BrowserContext::GetDownloadManagerDelegate() {
return nullptr;
}
content::BrowserPluginGuestManager* BrowserContext::GetGuestManager() {
return nullptr;
}
storage::SpecialStoragePolicy* BrowserContext::GetSpecialStoragePolicy() {
return storage_policy_.get();
}
content::PushMessagingService* BrowserContext::GetPushMessagingService() {
return nullptr;
}
content::SSLHostStateDelegate* BrowserContext::GetSSLHostStateDelegate() {
return nullptr;
}
content::PermissionManager* BrowserContext::GetPermissionManager() {
if (!permission_manager_.get())
permission_manager_.reset(new PermissionManager);
return permission_manager_.get();
}
content::BackgroundSyncController*
BrowserContext::GetBackgroundSyncController() {
return nullptr;
}
net::URLRequestContextGetter*
BrowserContext::CreateRequestContextForStoragePartition(
const base::FilePath& partition_path,
bool in_memory,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) {
return nullptr;
}
net::URLRequestContextGetter*
BrowserContext::CreateMediaRequestContext() {
return url_request_getter_.get();
}
net::URLRequestContextGetter*
BrowserContext::CreateMediaRequestContextForStoragePartition(
const base::FilePath& partition_path,
bool in_memory) {
return nullptr;
}
} // namespace brightray

View file

@ -0,0 +1,142 @@
// 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-CHROMIUM file.
#ifndef BRIGHTRAY_BROWSER_BROWSER_CONTEXT_H_
#define BRIGHTRAY_BROWSER_BROWSER_CONTEXT_H_
#include <map>
#include "browser/net/devtools_network_controller_handle.h"
#include "browser/permission_manager.h"
#include "browser/url_request_context_getter.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "content/public/browser/browser_context.h"
class PrefRegistrySimple;
class PrefService;
namespace storage {
class SpecialStoragePolicy;
}
namespace brightray {
class MediaDeviceIDSalt;
class PermissionManager;
class BrowserContext : public base::RefCounted<BrowserContext>,
public content::BrowserContext,
public brightray::URLRequestContextGetter::Delegate {
public:
// Get the BrowserContext according to its |partition| and |in_memory|,
// empty pointer when be returned when there is no matching BrowserContext.
static scoped_refptr<BrowserContext> Get(
const std::string& partition, bool in_memory);
base::WeakPtr<BrowserContext> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
// Get the request context, if there is no one, create it.
URLRequestContextGetter* GetRequestContext();
// content::BrowserContext:
std::unique_ptr<content::ZoomLevelDelegate> CreateZoomLevelDelegate(
const base::FilePath& partition_path) override;
bool IsOffTheRecord() const override;
content::ResourceContext* GetResourceContext() override;
content::DownloadManagerDelegate* GetDownloadManagerDelegate() override;
content::BrowserPluginGuestManager* GetGuestManager() override;
storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override;
content::PushMessagingService* GetPushMessagingService() override;
content::SSLHostStateDelegate* GetSSLHostStateDelegate() override;
content::PermissionManager* GetPermissionManager() override;
content::BackgroundSyncController* GetBackgroundSyncController() override;
net::URLRequestContextGetter* CreateRequestContext(
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) override;
net::URLRequestContextGetter* CreateRequestContextForStoragePartition(
const base::FilePath& partition_path,
bool in_memory,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) override;
net::URLRequestContextGetter* CreateMediaRequestContext() override;
net::URLRequestContextGetter* CreateMediaRequestContextForStoragePartition(
const base::FilePath& partition_path,
bool in_memory) override;
URLRequestContextGetter* url_request_context_getter() const {
return url_request_getter_.get();
}
DevToolsNetworkControllerHandle* network_controller_handle() {
return &network_controller_handle_;
}
void InitPrefs();
PrefService* prefs() { return prefs_.get(); }
protected:
BrowserContext(const std::string& partition, bool in_memory);
~BrowserContext() override;
// Subclasses should override this to register custom preferences.
virtual void RegisterPrefs(PrefRegistrySimple* pref_registry) {}
// URLRequestContextGetter::Delegate:
net::NetworkDelegate* CreateNetworkDelegate() override;
MediaDeviceIDSalt* GetMediaDeviceIDSalt() override;
base::FilePath GetPath() const override;
private:
friend class base::RefCounted<BrowserContext>;
class ResourceContext;
void RegisterInternalPrefs(PrefRegistrySimple* pref_registry);
// partition_id => browser_context
struct PartitionKey {
std::string partition;
bool in_memory;
PartitionKey(const std::string& partition, bool in_memory)
: partition(partition), in_memory(in_memory) {}
bool operator<(const PartitionKey& other) const {
if (partition == other.partition)
return in_memory < other.in_memory;
return partition < other.partition;
}
bool operator==(const PartitionKey& other) const {
return (partition == other.partition) && (in_memory == other.in_memory);
}
};
using BrowserContextMap =
std::map<PartitionKey, base::WeakPtr<brightray::BrowserContext>>;
static BrowserContextMap browser_context_map_;
base::FilePath path_;
bool in_memory_;
DevToolsNetworkControllerHandle network_controller_handle_;
std::unique_ptr<ResourceContext> resource_context_;
scoped_refptr<URLRequestContextGetter> url_request_getter_;
scoped_refptr<storage::SpecialStoragePolicy> storage_policy_;
std::unique_ptr<PrefService> prefs_;
std::unique_ptr<PermissionManager> permission_manager_;
std::unique_ptr<MediaDeviceIDSalt> media_device_id_salt_;
base::WeakPtrFactory<BrowserContext> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(BrowserContext);
};
} // namespace brightray
#endif

View file

@ -0,0 +1,261 @@
// 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-CHROMIUM file.
#include "browser/browser_main_parts.h"
#include "browser/browser_context.h"
#include "browser/devtools_manager_delegate.h"
#include "browser/web_ui_controller_factory.h"
#include "common/main_delegate.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_switches.h"
#include "media/base/localized_strings.h"
#include "net/proxy/proxy_resolver_v8.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/material_design/material_design_controller.h"
#if defined(USE_AURA)
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/views/widget/desktop_aura/desktop_screen.h"
#include "ui/wm/core/wm_state.h"
#endif
#if defined(TOOLKIT_VIEWS)
#include "browser/views/views_delegate.h"
#endif
#if defined(USE_X11)
#include "base/environment.h"
#include "base/path_service.h"
#include "base/nix/xdg_util.h"
#include "base/threading/thread_task_runner_handle.h"
#include "browser/brightray_paths.h"
#include "chrome/browser/ui/libgtkui/gtk_ui.h"
#include "ui/base/x/x11_util.h"
#include "ui/base/x/x11_util_internal.h"
#include "ui/views/linux_ui/linux_ui.h"
#endif
#if defined(OS_WIN)
#include "ui/base/cursor/cursor_loader_win.h"
#include "ui/base/l10n/l10n_util_win.h"
#include "ui/gfx/platform_font_win.h"
#endif
#if defined(OS_LINUX)
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/dbus/dbus_bluez_manager_wrapper_linux.h"
#endif
namespace brightray {
namespace {
#if defined(OS_WIN)
// gfx::Font callbacks
void AdjustUIFont(LOGFONT* logfont) {
l10n_util::AdjustUIFont(logfont);
}
int GetMinimumFontSize() {
return 10;
}
#endif
#if defined(USE_X11)
// Indicates that we're currently responding to an IO error (by shutting down).
bool g_in_x11_io_error_handler = false;
// Number of seconds to wait for UI thread to get an IO error if we get it on
// the background thread.
const int kWaitForUIThreadSeconds = 10;
void OverrideLinuxAppDataPath() {
base::FilePath path;
if (PathService::Get(DIR_APP_DATA, &path))
return;
std::unique_ptr<base::Environment> env(base::Environment::Create());
path = base::nix::GetXDGDirectory(env.get(),
base::nix::kXdgConfigHomeEnvVar,
base::nix::kDotConfigDir);
PathService::Override(DIR_APP_DATA, path);
}
int BrowserX11ErrorHandler(Display* d, XErrorEvent* error) {
if (!g_in_x11_io_error_handler) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(&ui::LogErrorEventDescription, d, *error));
}
return 0;
}
// This function is used to help us diagnose crash dumps that happen
// during the shutdown process.
NOINLINE void WaitingForUIThreadToHandleIOError() {
// Ensure function isn't optimized away.
asm("");
sleep(kWaitForUIThreadSeconds);
}
int BrowserX11IOErrorHandler(Display* d) {
if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
// Wait for the UI thread (which has a different connection to the X server)
// to get the error. We can't call shutdown from this thread without
// tripping an error. Doing it through a function so that we'll be able
// to see it in any crash dumps.
WaitingForUIThreadToHandleIOError();
return 0;
}
// If there's an IO error it likely means the X server has gone away.
// If this CHECK fails, then that means SessionEnding() below triggered some
// code that tried to talk to the X server, resulting in yet another error.
CHECK(!g_in_x11_io_error_handler);
g_in_x11_io_error_handler = true;
LOG(ERROR) << "X IO error received (X server probably went away)";
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
return 0;
}
int X11EmptyErrorHandler(Display* d, XErrorEvent* error) {
return 0;
}
int X11EmptyIOErrorHandler(Display* d) {
return 0;
}
#endif
base::string16 MediaStringProvider(media::MessageId id) {
switch (id) {
case media::DEFAULT_AUDIO_DEVICE_NAME:
return base::ASCIIToUTF16("Default");
#if defined(OS_WIN)
case media::COMMUNICATIONS_AUDIO_DEVICE_NAME:
return base::ASCIIToUTF16("Communications");
#endif
default:
return base::string16();
}
}
} // namespace
BrowserMainParts::BrowserMainParts() {
}
BrowserMainParts::~BrowserMainParts() {
}
void BrowserMainParts::PreEarlyInitialization() {
std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
feature_list->InitializeFromCommandLine("", "");
base::FeatureList::SetInstance(std::move(feature_list));
#if defined(USE_X11)
views::LinuxUI::SetInstance(BuildGtkUi());
OverrideLinuxAppDataPath();
// Installs the X11 error handlers for the browser process used during
// startup. They simply print error messages and exit because
// we can't shutdown properly while creating and initializing services.
ui::SetX11ErrorHandlers(nullptr, nullptr);
#endif
}
void BrowserMainParts::ToolkitInitialized() {
ui::MaterialDesignController::Initialize();
#if defined(USE_AURA) && defined(USE_X11)
views::LinuxUI::instance()->Initialize();
#endif
#if defined(USE_AURA)
wm_state_.reset(new wm::WMState);
#endif
#if defined(TOOLKIT_VIEWS)
views_delegate_.reset(new ViewsDelegate);
#endif
#if defined(OS_WIN)
gfx::PlatformFontWin::adjust_font_callback = &AdjustUIFont;
gfx::PlatformFontWin::get_minimum_font_size_callback = &GetMinimumFontSize;
wchar_t module_name[MAX_PATH] = { 0 };
if (GetModuleFileName(NULL, module_name, MAX_PATH))
ui::CursorLoaderWin::SetCursorResourceModule(module_name);
#endif
}
void BrowserMainParts::PreMainMessageLoopStart() {
#if defined(OS_MACOSX)
l10n_util::OverrideLocaleWithCocoaLocale();
#endif
InitializeResourceBundle("");
#if defined(OS_MACOSX)
InitializeMainNib();
#endif
media::SetLocalizedStringProvider(MediaStringProvider);
}
void BrowserMainParts::PreMainMessageLoopRun() {
content::WebUIControllerFactory::RegisterFactory(
WebUIControllerFactory::GetInstance());
// --remote-debugging-port
auto command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kRemoteDebuggingPort))
DevToolsManagerDelegate::StartHttpHandler();
}
void BrowserMainParts::PostMainMessageLoopStart() {
#if defined(USE_X11)
// Installs the X11 error handlers for the browser process after the
// main message loop has started. This will allow us to exit cleanly
// if X exits before us.
ui::SetX11ErrorHandlers(BrowserX11ErrorHandler, BrowserX11IOErrorHandler);
#endif
#if defined(OS_LINUX)
bluez::DBusBluezManagerWrapperLinux::Initialize();
#endif
}
void BrowserMainParts::PostMainMessageLoopRun() {
#if defined(USE_X11)
// Unset the X11 error handlers. The X11 error handlers log the errors using a
// |PostTask()| on the message-loop. But since the message-loop is in the
// process of terminating, this can cause errors.
ui::SetX11ErrorHandlers(X11EmptyErrorHandler, X11EmptyIOErrorHandler);
#endif
}
int BrowserMainParts::PreCreateThreads() {
#if defined(USE_AURA)
display::Screen* screen = views::CreateDesktopScreen();
display::Screen::SetScreenInstance(screen);
#if defined(USE_X11)
views::LinuxUI::instance()->UpdateDeviceScaleFactor();
#endif
#endif
return 0;
}
void BrowserMainParts::PostDestroyThreads() {
#if defined(OS_LINUX)
device::BluetoothAdapterFactory::Shutdown();
bluez::DBusBluezManagerWrapperLinux::Shutdown();
#endif
}
} // namespace brightray

View file

@ -0,0 +1,60 @@
// 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-CHROMIUM file.
#ifndef BRIGHTRAY_BROWSER_BROWSER_MAIN_PARTS_H_
#define BRIGHTRAY_BROWSER_BROWSER_MAIN_PARTS_H_
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "content/public/browser/browser_main_parts.h"
#if defined(TOOLKIT_VIEWS)
namespace brightray {
class ViewsDelegate;
}
#endif
#if defined(USE_AURA)
namespace wm {
class WMState;
}
#endif
namespace brightray {
class BrowserMainParts : public content::BrowserMainParts {
public:
BrowserMainParts();
~BrowserMainParts();
protected:
// content::BrowserMainParts:
void PreEarlyInitialization() override;
void ToolkitInitialized() override;
void PreMainMessageLoopStart() override;
void PreMainMessageLoopRun() override;
void PostMainMessageLoopStart() override;
void PostMainMessageLoopRun() override;
int PreCreateThreads() override;
void PostDestroyThreads() override;
private:
#if defined(OS_MACOSX)
void InitializeMainNib();
#endif
#if defined(TOOLKIT_VIEWS)
std::unique_ptr<ViewsDelegate> views_delegate_;
#endif
#if defined(USE_AURA)
std::unique_ptr<wm::WMState> wm_state_;
#endif
DISALLOW_COPY_AND_ASSIGN(BrowserMainParts);
};
} // namespace brightray
#endif

View file

@ -0,0 +1,22 @@
#import "browser_main_parts.h"
#import "base/logging.h"
#import "base/mac/bundle_locations.h"
#import <AppKit/AppKit.h>
namespace brightray {
// Replicates NSApplicationMain, but doesn't start a run loop.
void BrowserMainParts::InitializeMainNib() {
auto infoDictionary = base::mac::OuterBundle().infoDictionary;
auto principalClass = NSClassFromString([infoDictionary objectForKey:@"NSPrincipalClass"]);
auto application = [principalClass sharedApplication];
NSString *mainNibName = [infoDictionary objectForKey:@"NSMainNibFile"];
auto mainNib = [[NSNib alloc] initWithNibNamed:mainNibName bundle:base::mac::FrameworkBundle()];
[mainNib instantiateWithOwner:application topLevelObjects:nil];
[mainNib release];
}
} // namespace brightray

View file

@ -0,0 +1,53 @@
// Copyright 2014 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 "browser/devtools_contents_resizing_strategy.h"
#include <algorithm>
DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy()
: hide_inspected_contents_(false) {
}
DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy(
const gfx::Rect& bounds)
: bounds_(bounds),
hide_inspected_contents_(bounds_.IsEmpty() && !bounds_.x() &&
!bounds_.y()) {
}
void DevToolsContentsResizingStrategy::CopyFrom(
const DevToolsContentsResizingStrategy& strategy) {
bounds_ = strategy.bounds();
hide_inspected_contents_ = strategy.hide_inspected_contents();
}
bool DevToolsContentsResizingStrategy::Equals(
const DevToolsContentsResizingStrategy& strategy) {
return bounds_ == strategy.bounds() &&
hide_inspected_contents_ == strategy.hide_inspected_contents();
}
void ApplyDevToolsContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy,
const gfx::Size& container_size,
gfx::Rect* new_devtools_bounds,
gfx::Rect* new_contents_bounds) {
new_devtools_bounds->SetRect(
0, 0, container_size.width(), container_size.height());
const gfx::Rect& bounds = strategy.bounds();
if (bounds.size().IsEmpty() && !strategy.hide_inspected_contents()) {
new_contents_bounds->SetRect(
0, 0, container_size.width(), container_size.height());
return;
}
int left = std::min(bounds.x(), container_size.width());
int top = std::min(bounds.y(), container_size.height());
int width = std::min(bounds.width(), container_size.width() - left);
int height = std::min(bounds.height(), container_size.height() - top);
new_contents_bounds->SetRect(left, top, width, height);
}

View file

@ -0,0 +1,48 @@
// Copyright 2014 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.
#ifndef BRIGHTRAY_BROWSER_DEVTOOLS_CONTENTS_RESIZING_STRATEGY_H_
#define BRIGHTRAY_BROWSER_DEVTOOLS_CONTENTS_RESIZING_STRATEGY_H_
#include "base/macros.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
// This class knows how to resize both DevTools and inspected WebContents
// inside a browser window hierarchy.
class DevToolsContentsResizingStrategy {
public:
DevToolsContentsResizingStrategy();
explicit DevToolsContentsResizingStrategy(
const gfx::Rect& bounds);
void CopyFrom(const DevToolsContentsResizingStrategy& strategy);
bool Equals(const DevToolsContentsResizingStrategy& strategy);
const gfx::Rect& bounds() const { return bounds_; }
bool hide_inspected_contents() const { return hide_inspected_contents_; }
private:
// Contents bounds. When non-empty, used instead of insets.
gfx::Rect bounds_;
// Determines whether inspected contents is visible.
bool hide_inspected_contents_;
DISALLOW_COPY_AND_ASSIGN(DevToolsContentsResizingStrategy);
};
// Applies contents resizing strategy, producing bounds for devtools and
// page contents views. Generally, page contents view is placed atop of devtools
// inside a common parent view, which size should be passed in |container_size|.
// When unknown, providing empty rect as previous devtools and contents bounds
// is allowed.
void ApplyDevToolsContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy,
const gfx::Size& container_size,
gfx::Rect* new_devtools_bounds,
gfx::Rect* new_contents_bounds);
#endif // BRIGHTRAY_BROWSER_DEVTOOLS_CONTENTS_RESIZING_STRATEGY_H_

View file

@ -0,0 +1,207 @@
// Copyright 2013 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-CHROMIUM file.
#include "browser/devtools_embedder_message_dispatcher.h"
#include "base/bind.h"
#include "base/values.h"
namespace brightray {
namespace {
using DispatchCallback = DevToolsEmbedderMessageDispatcher::DispatchCallback;
bool GetValue(const base::Value& value, std::string* result) {
return value.GetAsString(result);
}
bool GetValue(const base::Value& value, int* result) {
return value.GetAsInteger(result);
}
bool GetValue(const base::Value& value, bool* result) {
return value.GetAsBoolean(result);
}
bool GetValue(const base::Value& value, gfx::Rect* rect) {
const base::DictionaryValue* dict;
if (!value.GetAsDictionary(&dict))
return false;
int x = 0;
int y = 0;
int width = 0;
int height = 0;
if (!dict->GetInteger("x", &x) ||
!dict->GetInteger("y", &y) ||
!dict->GetInteger("width", &width) ||
!dict->GetInteger("height", &height))
return false;
rect->SetRect(x, y, width, height);
return true;
}
template <typename T>
struct StorageTraits {
using StorageType = T;
};
template <typename T>
struct StorageTraits<const T&> {
using StorageType = T;
};
template <typename... Ts>
struct ParamTuple {
bool Parse(const base::ListValue& list,
const base::ListValue::const_iterator& it) {
return it == list.end();
}
template <typename H, typename... As>
void Apply(const H& handler, As... args) {
handler.Run(args...);
}
};
template <typename T, typename... Ts>
struct ParamTuple<T, Ts...> {
bool Parse(const base::ListValue& list,
const base::ListValue::const_iterator& it) {
return it != list.end() && GetValue(**it, &head) &&
tail.Parse(list, it + 1);
}
template <typename H, typename... As>
void Apply(const H& handler, As... args) {
tail.template Apply<H, As..., T>(handler, args..., head);
}
typename StorageTraits<T>::StorageType head;
ParamTuple<Ts...> tail;
};
template<typename... As>
bool ParseAndHandle(const base::Callback<void(As...)>& handler,
const DispatchCallback& callback,
const base::ListValue& list) {
ParamTuple<As...> tuple;
if (!tuple.Parse(list, list.begin()))
return false;
tuple.Apply(handler);
return true;
}
template<typename... As>
bool ParseAndHandleWithCallback(
const base::Callback<void(const DispatchCallback&, As...)>& handler,
const DispatchCallback& callback,
const base::ListValue& list) {
ParamTuple<As...> tuple;
if (!tuple.Parse(list, list.begin()))
return false;
tuple.Apply(handler, callback);
return true;
}
} // namespace
/**
* Dispatcher for messages sent from the frontend running in an
* isolated renderer (chrome-devtools:// or chrome://inspect) to the embedder
* in the browser.
*
* The messages are sent via InspectorFrontendHost.sendMessageToEmbedder or
* chrome.send method accordingly.
*/
class DispatcherImpl : public DevToolsEmbedderMessageDispatcher {
public:
~DispatcherImpl() override {}
bool Dispatch(const DispatchCallback& callback,
const std::string& method,
const base::ListValue* params) override {
auto it = handlers_.find(method);
return it != handlers_.end() && it->second.Run(callback, *params);
}
template<typename... As>
void RegisterHandler(const std::string& method,
void (Delegate::*handler)(As...),
Delegate* delegate) {
handlers_[method] = base::Bind(&ParseAndHandle<As...>,
base::Bind(handler,
base::Unretained(delegate)));
}
template<typename... As>
void RegisterHandlerWithCallback(
const std::string& method,
void (Delegate::*handler)(const DispatchCallback&, As...),
Delegate* delegate) {
handlers_[method] = base::Bind(&ParseAndHandleWithCallback<As...>,
base::Bind(handler,
base::Unretained(delegate)));
}
private:
using Handler = base::Callback<bool(const DispatchCallback&,
const base::ListValue&)>;
using HandlerMap = std::map<std::string, Handler>;
HandlerMap handlers_;
};
// static
DevToolsEmbedderMessageDispatcher*
DevToolsEmbedderMessageDispatcher::CreateForDevToolsFrontend(
Delegate* delegate) {
auto* d = new DispatcherImpl();
d->RegisterHandler("bringToFront", &Delegate::ActivateWindow, delegate);
d->RegisterHandler("closeWindow", &Delegate::CloseWindow, delegate);
d->RegisterHandler("loadCompleted", &Delegate::LoadCompleted, delegate);
d->RegisterHandler("setInspectedPageBounds",
&Delegate::SetInspectedPageBounds, delegate);
d->RegisterHandler("inspectElementCompleted",
&Delegate::InspectElementCompleted, delegate);
d->RegisterHandler("inspectedURLChanged",
&Delegate::InspectedURLChanged, delegate);
d->RegisterHandlerWithCallback("setIsDocked",
&Delegate::SetIsDocked, delegate);
d->RegisterHandler("openInNewTab", &Delegate::OpenInNewTab, delegate);
d->RegisterHandler("save", &Delegate::SaveToFile, delegate);
d->RegisterHandler("append", &Delegate::AppendToFile, delegate);
d->RegisterHandler("requestFileSystems",
&Delegate::RequestFileSystems, delegate);
d->RegisterHandler("addFileSystem", &Delegate::AddFileSystem, delegate);
d->RegisterHandler("removeFileSystem", &Delegate::RemoveFileSystem, delegate);
d->RegisterHandler("upgradeDraggedFileSystemPermissions",
&Delegate::UpgradeDraggedFileSystemPermissions, delegate);
d->RegisterHandler("indexPath", &Delegate::IndexPath, delegate);
d->RegisterHandlerWithCallback("loadNetworkResource",
&Delegate::LoadNetworkResource, delegate);
d->RegisterHandler("stopIndexing", &Delegate::StopIndexing, delegate);
d->RegisterHandler("searchInPath", &Delegate::SearchInPath, delegate);
d->RegisterHandler("setWhitelistedShortcuts",
&Delegate::SetWhitelistedShortcuts, delegate);
d->RegisterHandler("zoomIn", &Delegate::ZoomIn, delegate);
d->RegisterHandler("zoomOut", &Delegate::ZoomOut, delegate);
d->RegisterHandler("resetZoom", &Delegate::ResetZoom, delegate);
d->RegisterHandler("setDevicesUpdatesEnabled",
&Delegate::SetDevicesUpdatesEnabled, delegate);
d->RegisterHandler("dispatchProtocolMessage",
&Delegate::DispatchProtocolMessageFromDevToolsFrontend,
delegate);
d->RegisterHandlerWithCallback("sendJsonRequest",
&Delegate::SendJsonRequest, delegate);
d->RegisterHandlerWithCallback("getPreferences",
&Delegate::GetPreferences, delegate);
d->RegisterHandler("setPreference", &Delegate::SetPreference, delegate);
d->RegisterHandler("removePreference", &Delegate::RemovePreference, delegate);
d->RegisterHandler("clearPreferences", &Delegate::ClearPreferences, delegate);
return d;
}
} // namespace brightray

View file

@ -0,0 +1,96 @@
// Copyright 2013 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-CHROMIUM file.
#ifndef BRIGHTRAY_BROWSER_DEVTOOLS_EMBEDDER_MESSAGE_DISPATCHER_H_
#define BRIGHTRAY_BROWSER_DEVTOOLS_EMBEDDER_MESSAGE_DISPATCHER_H_
#include <map>
#include <string>
#include "base/callback.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
namespace base {
class ListValue;
class Value;
}
namespace brightray {
/**
* Dispatcher for messages sent from the DevTools frontend running in an
* isolated renderer (on chrome-devtools://) to the embedder in the browser.
*
* The messages are sent via InspectorFrontendHost.sendMessageToEmbedder method.
*/
class DevToolsEmbedderMessageDispatcher {
public:
class Delegate {
public:
using DispatchCallback = base::Callback<void(const base::Value*)>;
virtual ~Delegate() {}
virtual void ActivateWindow() = 0;
virtual void CloseWindow() = 0;
virtual void LoadCompleted() = 0;
virtual void SetInspectedPageBounds(const gfx::Rect& rect) = 0;
virtual void InspectElementCompleted() = 0;
virtual void InspectedURLChanged(const std::string& url) = 0;
virtual void SetIsDocked(const DispatchCallback& callback,
bool is_docked) = 0;
virtual void OpenInNewTab(const std::string& url) = 0;
virtual void SaveToFile(const std::string& url,
const std::string& content,
bool save_as) = 0;
virtual void AppendToFile(const std::string& url,
const std::string& content) = 0;
virtual void RequestFileSystems() = 0;
virtual void AddFileSystem(const std::string& file_system_path) = 0;
virtual void RemoveFileSystem(const std::string& file_system_path) = 0;
virtual void UpgradeDraggedFileSystemPermissions(
const std::string& file_system_url) = 0;
virtual void IndexPath(int index_request_id,
const std::string& file_system_path) = 0;
virtual void StopIndexing(int index_request_id) = 0;
virtual void LoadNetworkResource(const DispatchCallback& callback,
const std::string& url,
const std::string& headers,
int stream_id) = 0;
virtual void SearchInPath(int search_request_id,
const std::string& file_system_path,
const std::string& query) = 0;
virtual void SetWhitelistedShortcuts(const std::string& message) = 0;
virtual void ZoomIn() = 0;
virtual void ZoomOut() = 0;
virtual void ResetZoom() = 0;
virtual void SetDevicesUpdatesEnabled(bool enabled) = 0;
virtual void DispatchProtocolMessageFromDevToolsFrontend(
const std::string& message) = 0;
virtual void SendJsonRequest(const DispatchCallback& callback,
const std::string& browser_id,
const std::string& url) = 0;
virtual void GetPreferences(const DispatchCallback& callback) = 0;
virtual void SetPreference(const std::string& name,
const std::string& value) = 0;
virtual void RemovePreference(const std::string& name) = 0;
virtual void ClearPreferences() = 0;
};
using DispatchCallback = Delegate::DispatchCallback;
virtual ~DevToolsEmbedderMessageDispatcher() {}
virtual bool Dispatch(const DispatchCallback& callback,
const std::string& method,
const base::ListValue* params) = 0;
static DevToolsEmbedderMessageDispatcher* CreateForDevToolsFrontend(
Delegate* delegate);
};
} // namespace brightray
#endif // BRIGHTRAY_BROWSER_DEVTOOLS_EMBEDDER_MESSAGE_DISPATCHER_H_

View file

@ -0,0 +1,495 @@
// Copyright 2013 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 "browser/devtools_file_system_indexer.h"
#include <stddef.h>
#include <iterator>
#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/files/file_util_proxy.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/browser_thread.h"
using base::Bind;
using base::Callback;
using base::FileEnumerator;
using base::FilePath;
using base::Time;
using base::TimeDelta;
using base::TimeTicks;
using content::BrowserThread;
using std::map;
using std::set;
using std::string;
using std::vector;
namespace brightray {
namespace {
typedef int32_t Trigram;
typedef char TrigramChar;
typedef uint16_t FileId;
const int kMinTimeoutBetweenWorkedNotification = 200;
// Trigram characters include all ASCII printable characters (32-126) except for
// the capital letters, because the index is case insensitive.
const size_t kTrigramCharacterCount = 126 - 'Z' - 1 + 'A' - ' ' + 1;
const size_t kTrigramCount =
kTrigramCharacterCount * kTrigramCharacterCount * kTrigramCharacterCount;
const int kMaxReadLength = 10 * 1024;
const TrigramChar kUndefinedTrigramChar = -1;
const TrigramChar kBinaryTrigramChar = -2;
const Trigram kUndefinedTrigram = -1;
template <typename Char>
bool IsAsciiUpper(Char c) {
return c >= 'A' && c <= 'Z';
}
class Index {
public:
Index();
Time LastModifiedTimeForFile(const FilePath& file_path);
void SetTrigramsForFile(const FilePath& file_path,
const vector<Trigram>& index,
const Time& time);
vector<FilePath> Search(string query);
void PrintStats();
void NormalizeVectors();
private:
~Index();
FileId GetFileId(const FilePath& file_path);
typedef map<FilePath, FileId> FileIdsMap;
FileIdsMap file_ids_;
FileId last_file_id_;
// The index in this vector is the trigram id.
vector<vector<FileId> > index_;
typedef map<FilePath, Time> IndexedFilesMap;
IndexedFilesMap index_times_;
vector<bool> is_normalized_;
DISALLOW_COPY_AND_ASSIGN(Index);
};
base::LazyInstance<Index>::Leaky g_trigram_index = LAZY_INSTANCE_INITIALIZER;
TrigramChar TrigramCharForChar(char c) {
static TrigramChar* trigram_chars = nullptr;
if (!trigram_chars) {
trigram_chars = new TrigramChar[256];
for (size_t i = 0; i < 256; ++i) {
if (i > 127) {
trigram_chars[i] = kUndefinedTrigramChar;
continue;
}
char ch = static_cast<char>(i);
if (ch == '\t')
ch = ' ';
if (IsAsciiUpper(ch))
ch = ch - 'A' + 'a';
bool is_binary_char = ch < 9 || (ch >= 14 && ch < 32) || ch == 127;
if (is_binary_char) {
trigram_chars[i] = kBinaryTrigramChar;
continue;
}
if (ch < ' ') {
trigram_chars[i] = kUndefinedTrigramChar;
continue;
}
if (ch >= 'Z')
ch = ch - 'Z' - 1 + 'A';
ch -= ' ';
char signed_trigram_count = static_cast<char>(kTrigramCharacterCount);
CHECK(ch >= 0 && ch < signed_trigram_count);
trigram_chars[i] = ch;
}
}
unsigned char uc = static_cast<unsigned char>(c);
return trigram_chars[uc];
}
Trigram TrigramAtIndex(const vector<TrigramChar>& trigram_chars, size_t index) {
static int kTrigramCharacterCountSquared =
kTrigramCharacterCount * kTrigramCharacterCount;
if (trigram_chars[index] == kUndefinedTrigramChar ||
trigram_chars[index + 1] == kUndefinedTrigramChar ||
trigram_chars[index + 2] == kUndefinedTrigramChar)
return kUndefinedTrigram;
Trigram trigram = kTrigramCharacterCountSquared * trigram_chars[index] +
kTrigramCharacterCount * trigram_chars[index + 1] +
trigram_chars[index + 2];
return trigram;
}
Index::Index() : last_file_id_(0) {
index_.resize(kTrigramCount);
is_normalized_.resize(kTrigramCount);
std::fill(is_normalized_.begin(), is_normalized_.end(), true);
}
Index::~Index() {}
Time Index::LastModifiedTimeForFile(const FilePath& file_path) {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
Time last_modified_time;
if (index_times_.find(file_path) != index_times_.end())
last_modified_time = index_times_[file_path];
return last_modified_time;
}
void Index::SetTrigramsForFile(const FilePath& file_path,
const vector<Trigram>& index,
const Time& time) {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
FileId file_id = GetFileId(file_path);
auto it = index.begin();
for (; it != index.end(); ++it) {
Trigram trigram = *it;
index_[trigram].push_back(file_id);
is_normalized_[trigram] = false;
}
index_times_[file_path] = time;
}
vector<FilePath> Index::Search(string query) {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
const char* data = query.c_str();
vector<TrigramChar> trigram_chars;
trigram_chars.reserve(query.size());
for (size_t i = 0; i < query.size(); ++i) {
TrigramChar trigram_char = TrigramCharForChar(data[i]);
if (trigram_char == kBinaryTrigramChar)
trigram_char = kUndefinedTrigramChar;
trigram_chars.push_back(trigram_char);
}
vector<Trigram> trigrams;
for (size_t i = 0; i + 2 < query.size(); ++i) {
Trigram trigram = TrigramAtIndex(trigram_chars, i);
if (trigram != kUndefinedTrigram)
trigrams.push_back(trigram);
}
set<FileId> file_ids;
bool first = true;
vector<Trigram>::const_iterator it = trigrams.begin();
for (; it != trigrams.end(); ++it) {
Trigram trigram = *it;
if (first) {
std::copy(index_[trigram].begin(),
index_[trigram].end(),
std::inserter(file_ids, file_ids.begin()));
first = false;
continue;
}
set<FileId> intersection = base::STLSetIntersection<set<FileId> >(
file_ids, index_[trigram]);
file_ids.swap(intersection);
}
vector<FilePath> result;
FileIdsMap::const_iterator ids_it = file_ids_.begin();
for (; ids_it != file_ids_.end(); ++ids_it) {
if (trigrams.empty() ||
file_ids.find(ids_it->second) != file_ids.end()) {
result.push_back(ids_it->first);
}
}
return result;
}
FileId Index::GetFileId(const FilePath& file_path) {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
string file_path_str = file_path.AsUTF8Unsafe();
if (file_ids_.find(file_path) != file_ids_.end())
return file_ids_[file_path];
file_ids_[file_path] = ++last_file_id_;
return last_file_id_;
}
void Index::NormalizeVectors() {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
for (size_t i = 0; i < kTrigramCount; ++i) {
if (!is_normalized_[i]) {
std::sort(index_[i].begin(), index_[i].end());
if (index_[i].capacity() > index_[i].size())
vector<FileId>(index_[i]).swap(index_[i]);
is_normalized_[i] = true;
}
}
}
void Index::PrintStats() {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
LOG(ERROR) << "Index stats:";
size_t size = 0;
size_t maxSize = 0;
size_t capacity = 0;
for (size_t i = 0; i < kTrigramCount; ++i) {
if (index_[i].size() > maxSize)
maxSize = index_[i].size();
size += index_[i].size();
capacity += index_[i].capacity();
}
LOG(ERROR) << " - total trigram count: " << size;
LOG(ERROR) << " - max file count per trigram: " << maxSize;
LOG(ERROR) << " - total vectors capacity " << capacity;
size_t total_index_size =
capacity * sizeof(FileId) + sizeof(vector<FileId>) * kTrigramCount;
LOG(ERROR) << " - estimated total index size " << total_index_size;
}
typedef Callback<void(bool, const vector<bool>&)> IndexerCallback;
} // namespace
DevToolsFileSystemIndexer::FileSystemIndexingJob::FileSystemIndexingJob(
const FilePath& file_system_path,
const TotalWorkCallback& total_work_callback,
const WorkedCallback& worked_callback,
const DoneCallback& done_callback)
: file_system_path_(file_system_path),
total_work_callback_(total_work_callback),
worked_callback_(worked_callback),
done_callback_(done_callback),
current_file_(
BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE).get()),
files_indexed_(0),
stopped_(false) {
current_trigrams_set_.resize(kTrigramCount);
current_trigrams_.reserve(kTrigramCount);
}
DevToolsFileSystemIndexer::FileSystemIndexingJob::~FileSystemIndexingJob() {}
void DevToolsFileSystemIndexer::FileSystemIndexingJob::Start() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
Bind(&FileSystemIndexingJob::CollectFilesToIndex, this));
}
void DevToolsFileSystemIndexer::FileSystemIndexingJob::Stop() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostTask(BrowserThread::FILE,
FROM_HERE,
Bind(&FileSystemIndexingJob::StopOnFileThread, this));
}
void DevToolsFileSystemIndexer::FileSystemIndexingJob::StopOnFileThread() {
stopped_ = true;
}
void DevToolsFileSystemIndexer::FileSystemIndexingJob::CollectFilesToIndex() {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
if (stopped_)
return;
if (!file_enumerator_) {
file_enumerator_.reset(
new FileEnumerator(file_system_path_, true, FileEnumerator::FILES));
}
FilePath file_path = file_enumerator_->Next();
if (file_path.empty()) {
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
Bind(total_work_callback_, file_path_times_.size()));
indexing_it_ = file_path_times_.begin();
IndexFiles();
return;
}
Time saved_last_modified_time =
g_trigram_index.Get().LastModifiedTimeForFile(file_path);
FileEnumerator::FileInfo file_info = file_enumerator_->GetInfo();
Time current_last_modified_time = file_info.GetLastModifiedTime();
if (current_last_modified_time > saved_last_modified_time) {
file_path_times_[file_path] = current_last_modified_time;
}
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
Bind(&FileSystemIndexingJob::CollectFilesToIndex, this));
}
void DevToolsFileSystemIndexer::FileSystemIndexingJob::IndexFiles() {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
if (stopped_)
return;
if (indexing_it_ == file_path_times_.end()) {
g_trigram_index.Get().NormalizeVectors();
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_callback_);
return;
}
FilePath file_path = indexing_it_->first;
current_file_.CreateOrOpen(
file_path,
base::File::FLAG_OPEN | base::File::FLAG_READ,
Bind(&FileSystemIndexingJob::StartFileIndexing, this));
}
void DevToolsFileSystemIndexer::FileSystemIndexingJob::StartFileIndexing(
base::File::Error error) {
if (!current_file_.IsValid()) {
FinishFileIndexing(false);
return;
}
current_file_offset_ = 0;
current_trigrams_.clear();
std::fill(current_trigrams_set_.begin(), current_trigrams_set_.end(), false);
ReadFromFile();
}
void DevToolsFileSystemIndexer::FileSystemIndexingJob::ReadFromFile() {
if (stopped_) {
CloseFile();
return;
}
current_file_.Read(current_file_offset_, kMaxReadLength,
Bind(&FileSystemIndexingJob::OnRead, this));
}
void DevToolsFileSystemIndexer::FileSystemIndexingJob::OnRead(
base::File::Error error,
const char* data,
int bytes_read) {
if (error != base::File::FILE_OK) {
FinishFileIndexing(false);
return;
}
if (!bytes_read || bytes_read < 3) {
FinishFileIndexing(true);
return;
}
size_t size = static_cast<size_t>(bytes_read);
vector<TrigramChar> trigram_chars;
trigram_chars.reserve(size);
for (size_t i = 0; i < size; ++i) {
TrigramChar trigram_char = TrigramCharForChar(data[i]);
if (trigram_char == kBinaryTrigramChar) {
current_trigrams_.clear();
FinishFileIndexing(true);
return;
}
trigram_chars.push_back(trigram_char);
}
for (size_t i = 0; i + 2 < size; ++i) {
Trigram trigram = TrigramAtIndex(trigram_chars, i);
if ((trigram != kUndefinedTrigram) && !current_trigrams_set_[trigram]) {
current_trigrams_set_[trigram] = true;
current_trigrams_.push_back(trigram);
}
}
current_file_offset_ += bytes_read - 2;
ReadFromFile();
}
void DevToolsFileSystemIndexer::FileSystemIndexingJob::FinishFileIndexing(
bool success) {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
CloseFile();
if (success) {
FilePath file_path = indexing_it_->first;
g_trigram_index.Get().SetTrigramsForFile(
file_path, current_trigrams_, file_path_times_[file_path]);
}
ReportWorked();
++indexing_it_;
IndexFiles();
}
void DevToolsFileSystemIndexer::FileSystemIndexingJob::CloseFile() {
if (current_file_.IsValid())
current_file_.Close(Bind(&FileSystemIndexingJob::CloseCallback, this));
}
void DevToolsFileSystemIndexer::FileSystemIndexingJob::CloseCallback(
base::File::Error error) {}
void DevToolsFileSystemIndexer::FileSystemIndexingJob::ReportWorked() {
TimeTicks current_time = TimeTicks::Now();
bool should_send_worked_nitification = true;
if (!last_worked_notification_time_.is_null()) {
TimeDelta delta = current_time - last_worked_notification_time_;
if (delta.InMilliseconds() < kMinTimeoutBetweenWorkedNotification)
should_send_worked_nitification = false;
}
++files_indexed_;
if (should_send_worked_nitification) {
last_worked_notification_time_ = current_time;
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE, Bind(worked_callback_, files_indexed_));
files_indexed_ = 0;
}
}
DevToolsFileSystemIndexer::DevToolsFileSystemIndexer() {
}
DevToolsFileSystemIndexer::~DevToolsFileSystemIndexer() {}
scoped_refptr<DevToolsFileSystemIndexer::FileSystemIndexingJob>
DevToolsFileSystemIndexer::IndexPath(
const string& file_system_path,
const TotalWorkCallback& total_work_callback,
const WorkedCallback& worked_callback,
const DoneCallback& done_callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
scoped_refptr<FileSystemIndexingJob> indexing_job =
new FileSystemIndexingJob(FilePath::FromUTF8Unsafe(file_system_path),
total_work_callback,
worked_callback,
done_callback);
indexing_job->Start();
return indexing_job;
}
void DevToolsFileSystemIndexer::SearchInPath(const string& file_system_path,
const string& query,
const SearchCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
Bind(&DevToolsFileSystemIndexer::SearchInPathOnFileThread,
this,
file_system_path,
query,
callback));
}
void DevToolsFileSystemIndexer::SearchInPathOnFileThread(
const string& file_system_path,
const string& query,
const SearchCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
vector<FilePath> file_paths = g_trigram_index.Get().Search(query);
vector<string> result;
FilePath path = FilePath::FromUTF8Unsafe(file_system_path);
vector<FilePath>::const_iterator it = file_paths.begin();
for (; it != file_paths.end(); ++it) {
if (path.IsParent(*it))
result.push_back(it->AsUTF8Unsafe());
}
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, Bind(callback, result));
}
} // namespace brightray

View file

@ -0,0 +1,115 @@
// Copyright 2013 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.
#ifndef BROWSER_DEVTOOLS_FILE_SYSTEM_INDEXER_H_
#define BROWSER_DEVTOOLS_FILE_SYSTEM_INDEXER_H_
#include <stdint.h>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/files/file_proxy.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
namespace base {
class FilePath;
class FileEnumerator;
class Time;
}
namespace content {
class WebContents;
}
namespace brightray {
class DevToolsFileSystemIndexer
: public base::RefCountedThreadSafe<DevToolsFileSystemIndexer> {
public:
typedef base::Callback<void(int)> TotalWorkCallback;
typedef base::Callback<void(int)> WorkedCallback;
typedef base::Callback<void()> DoneCallback;
typedef base::Callback<void(const std::vector<std::string>&)> SearchCallback;
class FileSystemIndexingJob : public base::RefCounted<FileSystemIndexingJob> {
public:
void Stop();
private:
friend class base::RefCounted<FileSystemIndexingJob>;
friend class DevToolsFileSystemIndexer;
FileSystemIndexingJob(const base::FilePath& file_system_path,
const TotalWorkCallback& total_work_callback,
const WorkedCallback& worked_callback,
const DoneCallback& done_callback);
virtual ~FileSystemIndexingJob();
void Start();
void StopOnFileThread();
void CollectFilesToIndex();
void IndexFiles();
void StartFileIndexing(base::File::Error error);
void ReadFromFile();
void OnRead(base::File::Error error,
const char* data,
int bytes_read);
void FinishFileIndexing(bool success);
void CloseFile();
void CloseCallback(base::File::Error error);
void ReportWorked();
base::FilePath file_system_path_;
TotalWorkCallback total_work_callback_;
WorkedCallback worked_callback_;
DoneCallback done_callback_;
std::unique_ptr<base::FileEnumerator> file_enumerator_;
typedef std::map<base::FilePath, base::Time> FilePathTimesMap;
FilePathTimesMap file_path_times_;
FilePathTimesMap::const_iterator indexing_it_;
base::FileProxy current_file_;
int64_t current_file_offset_;
typedef int32_t Trigram;
std::vector<Trigram> current_trigrams_;
// The index in this vector is the trigram id.
std::vector<bool> current_trigrams_set_;
base::TimeTicks last_worked_notification_time_;
int files_indexed_;
bool stopped_;
};
DevToolsFileSystemIndexer();
// Performs file system indexing for given |file_system_path| and sends
// progress callbacks.
scoped_refptr<FileSystemIndexingJob> IndexPath(
const std::string& file_system_path,
const TotalWorkCallback& total_work_callback,
const WorkedCallback& worked_callback,
const DoneCallback& done_callback);
// Performs trigram search for given |query| in |file_system_path|.
void SearchInPath(const std::string& file_system_path,
const std::string& query,
const SearchCallback& callback);
private:
friend class base::RefCountedThreadSafe<DevToolsFileSystemIndexer>;
virtual ~DevToolsFileSystemIndexer();
void SearchInPathOnFileThread(const std::string& file_system_path,
const std::string& query,
const SearchCallback& callback);
DISALLOW_COPY_AND_ASSIGN(DevToolsFileSystemIndexer);
};
} // namespace brightray
#endif // BROWSER_DEVTOOLS_FILE_SYSTEM_INDEXER_H_

View file

@ -0,0 +1,131 @@
// Copyright (c) 2014 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-CHROMIUM file.
#include "browser/devtools_manager_delegate.h"
#include <vector>
#include "browser/net/devtools_network_protocol_handler.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "common/content_client.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/devtools_frontend_host.h"
#include "content/public/browser/devtools_socket_factory.h"
#include "content/public/browser/favicon_status.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_constants.h"
#include "content/public/common/user_agent.h"
#include "content/shell/grit/shell_resources.h"
#include "net/base/net_errors.h"
#include "net/socket/tcp_server_socket.h"
#include "net/socket/stream_socket.h"
#include "ui/base/resource/resource_bundle.h"
namespace brightray {
namespace {
class TCPServerSocketFactory : public content::DevToolsSocketFactory {
public:
TCPServerSocketFactory(const std::string& address, int port)
: address_(address), port_(port) {
}
private:
// content::ServerSocketFactory.
std::unique_ptr<net::ServerSocket> CreateForHttpServer() override {
std::unique_ptr<net::ServerSocket> socket(
new net::TCPServerSocket(nullptr, net::NetLogSource()));
if (socket->ListenWithAddressAndPort(address_, port_, 10) != net::OK)
return std::unique_ptr<net::ServerSocket>();
return socket;
}
std::unique_ptr<net::ServerSocket> CreateForTethering(
std::string* name) override {
return std::unique_ptr<net::ServerSocket>();
}
std::string address_;
uint16_t port_;
DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
};
std::unique_ptr<content::DevToolsSocketFactory>
CreateSocketFactory() {
auto& command_line = *base::CommandLine::ForCurrentProcess();
// See if the user specified a port on the command line (useful for
// automation). If not, use an ephemeral port by specifying 0.
int port = 0;
if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
int temp_port;
std::string port_str =
command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
if (base::StringToInt(port_str, &temp_port) &&
temp_port > 0 && temp_port < 65535) {
port = temp_port;
} else {
DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
}
}
return std::unique_ptr<content::DevToolsSocketFactory>(
new TCPServerSocketFactory("127.0.0.1", port));
}
} // namespace
// DevToolsManagerDelegate ---------------------------------------------------
// static
void DevToolsManagerDelegate::StartHttpHandler() {
content::DevToolsAgentHost::StartRemoteDebuggingServer(
CreateSocketFactory(),
std::string(),
base::FilePath(),
base::FilePath(),
std::string(),
GetBrightrayUserAgent());
}
DevToolsManagerDelegate::DevToolsManagerDelegate()
: handler_(new DevToolsNetworkProtocolHandler) {
}
DevToolsManagerDelegate::~DevToolsManagerDelegate() {
}
void DevToolsManagerDelegate::Inspect(content::DevToolsAgentHost* agent_host) {
}
base::DictionaryValue* DevToolsManagerDelegate::HandleCommand(
content::DevToolsAgentHost* agent_host,
base::DictionaryValue* command) {
return handler_->HandleCommand(agent_host, command);
}
scoped_refptr<content::DevToolsAgentHost>
DevToolsManagerDelegate::CreateNewTarget(const GURL& url) {
return nullptr;
}
std::string DevToolsManagerDelegate::GetDiscoveryPageHTML() {
return ResourceBundle::GetSharedInstance().GetRawDataResource(
IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string();
}
std::string DevToolsManagerDelegate::GetFrontendResource(
const std::string& path) {
return content::DevToolsFrontendHost::GetFrontendResource(path).as_string();
}
} // namespace brightray

View file

@ -0,0 +1,42 @@
// Copyright (c) 2014 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-CHROMIUM file.
#ifndef BROWSER_DEVTOOLS_MANAGER_DELEGATE_H_
#define BROWSER_DEVTOOLS_MANAGER_DELEGATE_H_
#include "base/macros.h"
#include "base/compiler_specific.h"
#include "content/browser/devtools/devtools_http_handler.h"
#include "content/public/browser/devtools_manager_delegate.h"
namespace brightray {
class DevToolsNetworkProtocolHandler;
class DevToolsManagerDelegate : public content::DevToolsManagerDelegate {
public:
static void StartHttpHandler();
DevToolsManagerDelegate();
virtual ~DevToolsManagerDelegate();
// DevToolsManagerDelegate implementation.
void Inspect(content::DevToolsAgentHost* agent_host) override;
base::DictionaryValue* HandleCommand(
content::DevToolsAgentHost* agent_host,
base::DictionaryValue* command) override;
scoped_refptr<content::DevToolsAgentHost> CreateNewTarget(
const GURL& url) override;
std::string GetDiscoveryPageHTML() override;
std::string GetFrontendResource(const std::string& path) override;
private:
std::unique_ptr<DevToolsNetworkProtocolHandler> handler_;
DISALLOW_COPY_AND_ASSIGN(DevToolsManagerDelegate);
};
} // namespace brightray
#endif // BROWSER_DEVTOOLS_MANAGER_DELEGATE_H_

View file

@ -0,0 +1,127 @@
// 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-CHROMIUM file.
#include "browser/devtools_ui.h"
#include <string>
#include "base/memory/ref_counted_memory.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "content/public/browser/devtools_frontend_host.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
namespace brightray {
namespace {
const char kChromeUIDevToolsHost[] = "devtools";
const char kChromeUIDevToolsBundledPath[] = "bundled";
std::string PathWithoutParams(const std::string& path) {
return GURL(std::string("chrome-devtools://devtools/") + path)
.path().substr(1);
}
std::string GetMimeTypeForPath(const std::string& path) {
std::string filename = PathWithoutParams(path);
if (base::EndsWith(filename, ".html", base::CompareCase::INSENSITIVE_ASCII)) {
return "text/html";
} else if (base::EndsWith(filename, ".css",
base::CompareCase::INSENSITIVE_ASCII)) {
return "text/css";
} else if (base::EndsWith(filename, ".js",
base::CompareCase::INSENSITIVE_ASCII)) {
return "application/javascript";
} else if (base::EndsWith(filename, ".png",
base::CompareCase::INSENSITIVE_ASCII)) {
return "image/png";
} else if (base::EndsWith(filename, ".gif",
base::CompareCase::INSENSITIVE_ASCII)) {
return "image/gif";
} else if (base::EndsWith(filename, ".svg",
base::CompareCase::INSENSITIVE_ASCII)) {
return "image/svg+xml";
} else if (base::EndsWith(filename, ".manifest",
base::CompareCase::INSENSITIVE_ASCII)) {
return "text/cache-manifest";
}
return "text/html";
}
class BundledDataSource : public content::URLDataSource {
public:
BundledDataSource() {}
// content::URLDataSource implementation.
std::string GetSource() const override {
return kChromeUIDevToolsHost;
}
void StartDataRequest(
const std::string& path,
const content::ResourceRequestInfo::WebContentsGetter& wc_getter,
const GotDataCallback& callback) override {
// Serve request from local bundle.
std::string bundled_path_prefix(kChromeUIDevToolsBundledPath);
bundled_path_prefix += "/";
if (base::StartsWith(path, bundled_path_prefix,
base::CompareCase::INSENSITIVE_ASCII)) {
StartBundledDataRequest(path.substr(bundled_path_prefix.length()),
callback);
return;
}
// We do not handle remote and custom requests.
callback.Run(nullptr);
}
std::string GetMimeType(const std::string& path) const override {
return GetMimeTypeForPath(path);
}
bool ShouldAddContentSecurityPolicy() const override {
return false;
}
bool ShouldDenyXFrameOptions() const override {
return false;
}
bool ShouldServeMimeTypeAsContentTypeHeader() const override {
return true;
}
void StartBundledDataRequest(const std::string& path,
const GotDataCallback& callback) {
std::string filename = PathWithoutParams(path);
base::StringPiece resource =
content::DevToolsFrontendHost::GetFrontendResource(filename);
DLOG_IF(WARNING, resource.empty())
<< "Unable to find dev tool resource: " << filename
<< ". If you compiled with debug_devtools=1, try running with "
"--debug-devtools.";
scoped_refptr<base::RefCountedStaticMemory> bytes(
new base::RefCountedStaticMemory(resource.data(), resource.length()));
callback.Run(bytes.get());
}
private:
~BundledDataSource() override {}
DISALLOW_COPY_AND_ASSIGN(BundledDataSource);
};
} // namespace
DevToolsUI::DevToolsUI(content::BrowserContext* browser_context,
content::WebUI* web_ui)
: WebUIController(web_ui) {
web_ui->SetBindings(0);
content::URLDataSource::Add(browser_context, new BundledDataSource());
}
} // namespace brightray

View file

@ -0,0 +1,27 @@
// Copyright (c) 2011 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-CHROMIUM file.
#ifndef BRIGHTRAY_BROWSER_DEVTOOLS_UI_H_
#define BRIGHTRAY_BROWSER_DEVTOOLS_UI_H_
#include "base/compiler_specific.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_ui_controller.h"
namespace brightray {
class BrowserContext;
class DevToolsUI : public content::WebUIController {
public:
explicit DevToolsUI(content::BrowserContext* browser_context,
content::WebUI* web_ui);
private:
DISALLOW_COPY_AND_ASSIGN(DevToolsUI);
};
} // namespace brightray
#endif

View file

@ -0,0 +1,18 @@
#include "browser/inspectable_web_contents.h"
#include "browser/inspectable_web_contents_impl.h"
namespace brightray {
InspectableWebContents* InspectableWebContents::Create(
const content::WebContents::CreateParams& create_params) {
auto contents = content::WebContents::Create(create_params);
return Create(contents);
}
InspectableWebContents* InspectableWebContents::Create(
content::WebContents* web_contents) {
return new InspectableWebContentsImpl(web_contents);
}
} // namespace brightray

View file

@ -0,0 +1,53 @@
#ifndef BRIGHTRAY_INSPECTABLE_WEB_CONTENTS_H_
#define BRIGHTRAY_INSPECTABLE_WEB_CONTENTS_H_
#include "content/public/browser/web_contents.h"
namespace base {
class Value;
}
namespace content {
class DevToolsAgentHost;
}
namespace brightray {
class InspectableWebContentsDelegate;
class InspectableWebContentsView;
class InspectableWebContents {
public:
static InspectableWebContents* Create(
const content::WebContents::CreateParams&);
// The returned InspectableWebContents takes ownership of the passed-in
// WebContents.
static InspectableWebContents* Create(content::WebContents*);
virtual ~InspectableWebContents() {}
virtual InspectableWebContentsView* GetView() const = 0;
virtual content::WebContents* GetWebContents() const = 0;
virtual content::WebContents* GetDevToolsWebContents() const = 0;
// The delegate manages its own life.
virtual void SetDelegate(InspectableWebContentsDelegate* delegate) = 0;
virtual InspectableWebContentsDelegate* GetDelegate() const = 0;
virtual void SetDockState(const std::string& state) = 0;
virtual void ShowDevTools() = 0;
virtual void CloseDevTools() = 0;
virtual bool IsDevToolsViewShowing() = 0;
virtual void AttachTo(scoped_refptr<content::DevToolsAgentHost>) = 0;
virtual void Detach() = 0;
virtual void CallClientFunction(const std::string& function_name,
const base::Value* arg1 = nullptr,
const base::Value* arg2 = nullptr,
const base::Value* arg3 = nullptr) = 0;
virtual void InspectElement(int x, int y) = 0;
};
} // namespace brightray
#endif

View file

@ -0,0 +1,34 @@
#ifndef BRIGHTRAY_INSPECTABLE_WEB_CONTENTS_DELEGATE_H_
#define BRIGHTRAY_INSPECTABLE_WEB_CONTENTS_DELEGATE_H_
#include <string>
namespace brightray {
class InspectableWebContentsDelegate {
public:
virtual ~InspectableWebContentsDelegate() {}
// Requested by WebContents of devtools.
virtual void DevToolsReloadPage() {}
virtual void DevToolsSaveToFile(
const std::string& url, const std::string& content, bool save_as) {}
virtual void DevToolsAppendToFile(
const std::string& url, const std::string& content) {}
virtual void DevToolsRequestFileSystems() {}
virtual void DevToolsAddFileSystem(
const base::FilePath& file_system_path) {}
virtual void DevToolsRemoveFileSystem(
const base::FilePath& file_system_path) {}
virtual void DevToolsIndexPath(
int request_id, const std::string& file_system_path) {}
virtual void DevToolsStopIndexing(int request_id) {}
virtual void DevToolsSearchInPath(
int request_id,
const std::string& file_system_path,
const std::string& query) {}
};
} // namespace brightray
#endif // BRIGHTRAY_INSPECTABLE_WEB_CONTENTS_DELEGATE_H_

View file

@ -0,0 +1,782 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Copyright (c) 2013 Adam Roben <adam@roben.org>. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file.
#include "browser/inspectable_web_contents_impl.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/metrics/histogram.h"
#include "base/strings/pattern.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "browser/browser_client.h"
#include "browser/browser_context.h"
#include "browser/browser_main_parts.h"
#include "browser/inspectable_web_contents_delegate.h"
#include "browser/inspectable_web_contents_view.h"
#include "browser/inspectable_web_contents_view_delegate.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/prefs/pref_registry_simple.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/host_zoom_map.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/common/user_agent.h"
#include "ipc/ipc_channel.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_response_writer.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
namespace brightray {
namespace {
const double kPresetZoomFactors[] = { 0.25, 0.333, 0.5, 0.666, 0.75, 0.9, 1.0,
1.1, 1.25, 1.5, 1.75, 2.0, 2.5, 3.0, 4.0,
5.0 };
const char kChromeUIDevToolsURL[] =
"chrome-devtools://devtools/bundled/inspector.html?"
"remoteBase=%s&"
"can_dock=%s&"
"toolbarColor=rgba(223,223,223,1)&"
"textColor=rgba(0,0,0,1)&"
"experiments=true";
const char kChromeUIDevToolsRemoteFrontendBase[] =
"https://chrome-devtools-frontend.appspot.com/";
const char kChromeUIDevToolsRemoteFrontendPath[] = "serve_file";
const char kDevToolsBoundsPref[] = "brightray.devtools.bounds";
const char kDevToolsZoomPref[] = "brightray.devtools.zoom";
const char kDevToolsPreferences[] = "brightray.devtools.preferences";
const char kFrontendHostId[] = "id";
const char kFrontendHostMethod[] = "method";
const char kFrontendHostParams[] = "params";
const char kTitleFormat[] = "Developer Tools - %s";
const size_t kMaxMessageChunkSize = IPC::Channel::kMaximumMessageSize / 4;
void RectToDictionary(const gfx::Rect& bounds, base::DictionaryValue* dict) {
dict->SetInteger("x", bounds.x());
dict->SetInteger("y", bounds.y());
dict->SetInteger("width", bounds.width());
dict->SetInteger("height", bounds.height());
}
void DictionaryToRect(const base::DictionaryValue& dict, gfx::Rect* bounds) {
int x = 0, y = 0, width = 800, height = 600;
dict.GetInteger("x", &x);
dict.GetInteger("y", &y);
dict.GetInteger("width", &width);
dict.GetInteger("height", &height);
*bounds = gfx::Rect(x, y, width, height);
}
bool IsPointInRect(const gfx::Point& point, const gfx::Rect& rect) {
return point.x() > rect.x() && point.x() < (rect.width() + rect.x()) &&
point.y() > rect.y() && point.y() < (rect.height() + rect.y());
}
bool IsPointInScreen(const gfx::Point& point) {
for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) {
if (IsPointInRect(point, display.bounds()))
return true;
}
return false;
}
void SetZoomLevelForWebContents(content::WebContents* web_contents,
double level) {
content::HostZoomMap::SetZoomLevel(web_contents, level);
}
double GetNextZoomLevel(double level, bool out) {
double factor = content::ZoomLevelToZoomFactor(level);
size_t size = arraysize(kPresetZoomFactors);
for (size_t i = 0; i < size; ++i) {
if (!content::ZoomValuesEqual(kPresetZoomFactors[i], factor))
continue;
if (out && i > 0)
return content::ZoomFactorToZoomLevel(kPresetZoomFactors[i - 1]);
if (!out && i != size - 1)
return content::ZoomFactorToZoomLevel(kPresetZoomFactors[i + 1]);
}
return level;
}
GURL GetRemoteBaseURL() {
return GURL(base::StringPrintf(
"%s%s/%s/",
kChromeUIDevToolsRemoteFrontendBase,
kChromeUIDevToolsRemoteFrontendPath,
content::GetWebKitRevision().c_str()));
}
GURL GetDevToolsURL(bool can_dock) {
auto url_string =
base::StringPrintf(kChromeUIDevToolsURL,
GetRemoteBaseURL().spec().c_str(),
can_dock ? "true" : "");
return GURL(url_string);
}
// ResponseWriter -------------------------------------------------------------
class ResponseWriter : public net::URLFetcherResponseWriter {
public:
ResponseWriter(base::WeakPtr<InspectableWebContentsImpl> bindings,
int stream_id);
~ResponseWriter() override;
// URLFetcherResponseWriter overrides:
int Initialize(const net::CompletionCallback& callback) override;
int Write(net::IOBuffer* buffer,
int num_bytes,
const net::CompletionCallback& callback) override;
int Finish(int net_error, const net::CompletionCallback& callback) override;
private:
base::WeakPtr<InspectableWebContentsImpl> bindings_;
int stream_id_;
DISALLOW_COPY_AND_ASSIGN(ResponseWriter);
};
ResponseWriter::ResponseWriter(
base::WeakPtr<InspectableWebContentsImpl> bindings,
int stream_id)
: bindings_(bindings),
stream_id_(stream_id) {
}
ResponseWriter::~ResponseWriter() {
}
int ResponseWriter::Initialize(const net::CompletionCallback& callback) {
return net::OK;
}
int ResponseWriter::Write(net::IOBuffer* buffer,
int num_bytes,
const net::CompletionCallback& callback) {
auto* id = new base::Value(stream_id_);
base::StringValue* chunk =
new base::StringValue(std::string(buffer->data(), num_bytes));
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&InspectableWebContentsImpl::CallClientFunction,
bindings_, "DevToolsAPI.streamWrite",
base::Owned(id), base::Owned(chunk), nullptr));
return num_bytes;
}
int ResponseWriter::Finish(int net_error,
const net::CompletionCallback& callback) {
return net::OK;
}
} // namespace
// Implemented separately on each platform.
InspectableWebContentsView* CreateInspectableContentsView(
InspectableWebContentsImpl* inspectable_web_contents_impl);
void InspectableWebContentsImpl::RegisterPrefs(PrefRegistrySimple* registry) {
std::unique_ptr<base::DictionaryValue> bounds_dict(new base::DictionaryValue);
RectToDictionary(gfx::Rect(0, 0, 800, 600), bounds_dict.get());
registry->RegisterDictionaryPref(kDevToolsBoundsPref, bounds_dict.release());
registry->RegisterDoublePref(kDevToolsZoomPref, 0.);
registry->RegisterDictionaryPref(kDevToolsPreferences);
}
InspectableWebContentsImpl::InspectableWebContentsImpl(
content::WebContents* web_contents)
: frontend_loaded_(false),
can_dock_(true),
delegate_(nullptr),
web_contents_(web_contents),
weak_factory_(this) {
auto context =
static_cast<BrowserContext*>(web_contents_->GetBrowserContext());
pref_service_ = context->prefs();
auto bounds_dict = pref_service_->GetDictionary(kDevToolsBoundsPref);
if (bounds_dict) {
DictionaryToRect(*bounds_dict, &devtools_bounds_);
// Sometimes the devtools window is out of screen or has too small size.
if (devtools_bounds_.height() < 100 || devtools_bounds_.width() < 100) {
devtools_bounds_.set_height(600);
devtools_bounds_.set_width(800);
}
if (!IsPointInScreen(devtools_bounds_.origin())) {
gfx::Rect display;
if (web_contents->GetNativeView()) {
display = display::Screen::GetScreen()->
GetDisplayNearestWindow(web_contents->GetNativeView()).bounds();
} else {
display = display::Screen::GetScreen()->GetPrimaryDisplay().bounds();
}
devtools_bounds_.set_x(display.x() +
(display.width() - devtools_bounds_.width()) / 2);
devtools_bounds_.set_y(
display.y() + (display.height() - devtools_bounds_.height()) / 2);
}
}
view_.reset(CreateInspectableContentsView(this));
}
InspectableWebContentsImpl::~InspectableWebContentsImpl() {
// Unsubscribe from devtools and Clean up resources.
if (devtools_web_contents_) {
devtools_web_contents_->SetDelegate(nullptr);
// Calling this also unsubscribes the observer, so WebContentsDestroyed
// won't be called again.
WebContentsDestroyed();
}
// Let destructor destroy devtools_web_contents_.
}
InspectableWebContentsView* InspectableWebContentsImpl::GetView() const {
return view_.get();
}
content::WebContents* InspectableWebContentsImpl::GetWebContents() const {
return web_contents_.get();
}
content::WebContents* InspectableWebContentsImpl::GetDevToolsWebContents()
const {
return devtools_web_contents_.get();
}
void InspectableWebContentsImpl::InspectElement(int x, int y) {
if (agent_host_.get())
agent_host_->InspectElement(this, x, y);
}
void InspectableWebContentsImpl::SetDelegate(
InspectableWebContentsDelegate* delegate) {
delegate_ = delegate;
}
InspectableWebContentsDelegate* InspectableWebContentsImpl::GetDelegate()
const {
return delegate_;
}
void InspectableWebContentsImpl::SetDockState(const std::string& state) {
if (state == "detach") {
can_dock_ = false;
} else {
can_dock_ = true;
dock_state_ = state;
}
}
void InspectableWebContentsImpl::ShowDevTools() {
// Show devtools only after it has done loading, this is to make sure the
// SetIsDocked is called *BEFORE* ShowDevTools.
if (!devtools_web_contents_) {
embedder_message_dispatcher_.reset(
DevToolsEmbedderMessageDispatcher::CreateForDevToolsFrontend(this));
content::WebContents::CreateParams create_params(
web_contents_->GetBrowserContext());
devtools_web_contents_.reset(content::WebContents::Create(create_params));
Observe(devtools_web_contents_.get());
devtools_web_contents_->SetDelegate(this);
AttachTo(content::DevToolsAgentHost::GetOrCreateFor(web_contents_.get()));
devtools_web_contents_->GetController().LoadURL(
GetDevToolsURL(can_dock_),
content::Referrer(),
ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
std::string());
} else {
view_->ShowDevTools();
}
}
void InspectableWebContentsImpl::CloseDevTools() {
if (devtools_web_contents_) {
frontend_loaded_ = false;
view_->CloseDevTools();
devtools_web_contents_.reset();
web_contents_->Focus();
}
}
bool InspectableWebContentsImpl::IsDevToolsViewShowing() {
return devtools_web_contents_ && view_->IsDevToolsViewShowing();
}
void InspectableWebContentsImpl::AttachTo(
scoped_refptr<content::DevToolsAgentHost> host) {
if (agent_host_.get())
Detach();
agent_host_ = std::move(host);
// Terminate existing debugging connections and start debugging.
agent_host_->ForceAttachClient(this);
}
void InspectableWebContentsImpl::Detach() {
if (agent_host_.get())
agent_host_->DetachClient(this);
agent_host_ = nullptr;
}
void InspectableWebContentsImpl::CallClientFunction(
const std::string& function_name,
const base::Value* arg1,
const base::Value* arg2,
const base::Value* arg3) {
if (!devtools_web_contents_)
return;
std::string javascript = function_name + "(";
if (arg1) {
std::string json;
base::JSONWriter::Write(*arg1, &json);
javascript.append(json);
if (arg2) {
base::JSONWriter::Write(*arg2, &json);
javascript.append(", ").append(json);
if (arg3) {
base::JSONWriter::Write(*arg3, &json);
javascript.append(", ").append(json);
}
}
}
javascript.append(");");
devtools_web_contents_->GetMainFrame()->ExecuteJavaScript(
base::UTF8ToUTF16(javascript));
}
gfx::Rect InspectableWebContentsImpl::GetDevToolsBounds() const {
return devtools_bounds_;
}
void InspectableWebContentsImpl::SaveDevToolsBounds(const gfx::Rect& bounds) {
base::DictionaryValue bounds_dict;
RectToDictionary(bounds, &bounds_dict);
pref_service_->Set(kDevToolsBoundsPref, bounds_dict);
devtools_bounds_ = bounds;
}
double InspectableWebContentsImpl::GetDevToolsZoomLevel() const {
return pref_service_->GetDouble(kDevToolsZoomPref);
}
void InspectableWebContentsImpl::UpdateDevToolsZoomLevel(double level) {
pref_service_->SetDouble(kDevToolsZoomPref, level);
}
void InspectableWebContentsImpl::ActivateWindow() {
// Set the zoom level.
SetZoomLevelForWebContents(GetDevToolsWebContents(),
GetDevToolsZoomLevel());
}
void InspectableWebContentsImpl::CloseWindow() {
GetDevToolsWebContents()->DispatchBeforeUnload();
}
void InspectableWebContentsImpl::LoadCompleted() {
frontend_loaded_ = true;
view_->ShowDevTools();
// If the devtools can dock, "SetIsDocked" will be called by devtools itself.
if (!can_dock_) {
SetIsDocked(DispatchCallback(), false);
} else {
if (dock_state_.empty()) {
const base::DictionaryValue* prefs = pref_service_->GetDictionary(
kDevToolsPreferences);
std::string current_dock_state;
prefs->GetString("currentDockState", &current_dock_state);
base::RemoveChars(current_dock_state, "\"", &dock_state_);
}
base::string16 javascript = base::UTF8ToUTF16(
"Components.dockController.setDockSide(\"" + dock_state_ + "\");");
devtools_web_contents_->GetMainFrame()->ExecuteJavaScript(javascript);
}
if (view_->GetDelegate())
view_->GetDelegate()->DevToolsOpened();
}
void InspectableWebContentsImpl::SetInspectedPageBounds(const gfx::Rect& rect) {
DevToolsContentsResizingStrategy strategy(rect);
if (contents_resizing_strategy_.Equals(strategy))
return;
contents_resizing_strategy_.CopyFrom(strategy);
view_->SetContentsResizingStrategy(contents_resizing_strategy_);
}
void InspectableWebContentsImpl::InspectElementCompleted() {
}
void InspectableWebContentsImpl::InspectedURLChanged(const std::string& url) {
view_->SetTitle(base::UTF8ToUTF16(base::StringPrintf(kTitleFormat,
url.c_str())));
}
void InspectableWebContentsImpl::LoadNetworkResource(
const DispatchCallback& callback,
const std::string& url,
const std::string& headers,
int stream_id) {
GURL gurl(url);
if (!gurl.is_valid()) {
base::DictionaryValue response;
response.SetInteger("statusCode", 404);
callback.Run(&response);
return;
}
auto browser_context =
static_cast<BrowserContext*>(devtools_web_contents_->GetBrowserContext());
net::URLFetcher* fetcher =
(net::URLFetcher::Create(gurl, net::URLFetcher::GET, this)).release();
pending_requests_[fetcher] = callback;
fetcher->SetRequestContext(browser_context->url_request_context_getter());
fetcher->SetExtraRequestHeaders(headers);
fetcher->SaveResponseWithWriter(
std::unique_ptr<net::URLFetcherResponseWriter>(
new ResponseWriter(weak_factory_.GetWeakPtr(), stream_id)));
fetcher->Start();
}
void InspectableWebContentsImpl::SetIsDocked(const DispatchCallback& callback,
bool docked) {
view_->SetIsDocked(docked);
if (!callback.is_null())
callback.Run(nullptr);
}
void InspectableWebContentsImpl::OpenInNewTab(const std::string& url) {
}
void InspectableWebContentsImpl::SaveToFile(
const std::string& url, const std::string& content, bool save_as) {
if (delegate_)
delegate_->DevToolsSaveToFile(url, content, save_as);
}
void InspectableWebContentsImpl::AppendToFile(
const std::string& url, const std::string& content) {
if (delegate_)
delegate_->DevToolsAppendToFile(url, content);
}
void InspectableWebContentsImpl::RequestFileSystems() {
if (delegate_)
delegate_->DevToolsRequestFileSystems();
}
void InspectableWebContentsImpl::AddFileSystem(
const std::string& file_system_path) {
if (delegate_)
delegate_->DevToolsAddFileSystem(
base::FilePath::FromUTF8Unsafe(file_system_path));
}
void InspectableWebContentsImpl::RemoveFileSystem(
const std::string& file_system_path) {
if (delegate_)
delegate_->DevToolsRemoveFileSystem(
base::FilePath::FromUTF8Unsafe(file_system_path));
}
void InspectableWebContentsImpl::UpgradeDraggedFileSystemPermissions(
const std::string& file_system_url) {
}
void InspectableWebContentsImpl::IndexPath(
int request_id, const std::string& file_system_path) {
if (delegate_)
delegate_->DevToolsIndexPath(request_id, file_system_path);
}
void InspectableWebContentsImpl::StopIndexing(int request_id) {
if (delegate_)
delegate_->DevToolsStopIndexing(request_id);
}
void InspectableWebContentsImpl::SearchInPath(
int request_id,
const std::string& file_system_path,
const std::string& query) {
if (delegate_)
delegate_->DevToolsSearchInPath(request_id, file_system_path, query);
}
void InspectableWebContentsImpl::SetWhitelistedShortcuts(
const std::string& message) {
}
void InspectableWebContentsImpl::ZoomIn() {
double new_level = GetNextZoomLevel(GetDevToolsZoomLevel(), false);
SetZoomLevelForWebContents(GetDevToolsWebContents(), new_level);
UpdateDevToolsZoomLevel(new_level);
}
void InspectableWebContentsImpl::ZoomOut() {
double new_level = GetNextZoomLevel(GetDevToolsZoomLevel(), true);
SetZoomLevelForWebContents(GetDevToolsWebContents(), new_level);
UpdateDevToolsZoomLevel(new_level);
}
void InspectableWebContentsImpl::ResetZoom() {
SetZoomLevelForWebContents(GetDevToolsWebContents(), 0.);
UpdateDevToolsZoomLevel(0.);
}
void InspectableWebContentsImpl::SetDevicesUpdatesEnabled(bool enabled) {
}
void InspectableWebContentsImpl::DispatchProtocolMessageFromDevToolsFrontend(
const std::string& message) {
// If the devtools wants to reload the page, hijack the message and handle it
// to the delegate.
if (base::MatchPattern(message, "{\"id\":*,"
"\"method\":\"Page.reload\","
"\"params\":*}")) {
if (delegate_)
delegate_->DevToolsReloadPage();
return;
}
if (agent_host_.get())
agent_host_->DispatchProtocolMessage(this, message);
}
void InspectableWebContentsImpl::SendJsonRequest(
const DispatchCallback& callback,
const std::string& browser_id,
const std::string& url) {
callback.Run(nullptr);
}
void InspectableWebContentsImpl::GetPreferences(
const DispatchCallback& callback) {
const base::DictionaryValue* prefs = pref_service_->GetDictionary(
kDevToolsPreferences);
callback.Run(prefs);
}
void InspectableWebContentsImpl::SetPreference(const std::string& name,
const std::string& value) {
DictionaryPrefUpdate update(pref_service_, kDevToolsPreferences);
update.Get()->SetStringWithoutPathExpansion(name, value);
}
void InspectableWebContentsImpl::RemovePreference(const std::string& name) {
DictionaryPrefUpdate update(pref_service_, kDevToolsPreferences);
update.Get()->RemoveWithoutPathExpansion(name, nullptr);
}
void InspectableWebContentsImpl::ClearPreferences() {
DictionaryPrefUpdate update(pref_service_, kDevToolsPreferences);
update.Get()->Clear();
}
void InspectableWebContentsImpl::HandleMessageFromDevToolsFrontend(
const std::string& message) {
std::string method;
base::ListValue empty_params;
base::ListValue* params = &empty_params;
base::DictionaryValue* dict = nullptr;
std::unique_ptr<base::Value> parsed_message(base::JSONReader::Read(message));
if (!parsed_message ||
!parsed_message->GetAsDictionary(&dict) ||
!dict->GetString(kFrontendHostMethod, &method) ||
(dict->HasKey(kFrontendHostParams) &&
!dict->GetList(kFrontendHostParams, &params))) {
LOG(ERROR) << "Invalid message was sent to embedder: " << message;
return;
}
int id = 0;
dict->GetInteger(kFrontendHostId, &id);
embedder_message_dispatcher_->Dispatch(
base::Bind(&InspectableWebContentsImpl::SendMessageAck,
weak_factory_.GetWeakPtr(),
id),
method,
params);
}
void InspectableWebContentsImpl::DispatchProtocolMessage(
content::DevToolsAgentHost* agent_host, const std::string& message) {
if (!frontend_loaded_)
return;
if (message.length() < kMaxMessageChunkSize) {
base::string16 javascript = base::UTF8ToUTF16(
"DevToolsAPI.dispatchMessage(" + message + ");");
devtools_web_contents_->GetMainFrame()->ExecuteJavaScript(javascript);
return;
}
base::Value total_size(static_cast<int>(message.length()));
for (size_t pos = 0; pos < message.length(); pos += kMaxMessageChunkSize) {
base::StringValue message_value(message.substr(pos, kMaxMessageChunkSize));
CallClientFunction("DevToolsAPI.dispatchMessageChunk",
&message_value, pos ? nullptr : &total_size, nullptr);
}
}
void InspectableWebContentsImpl::AgentHostClosed(
content::DevToolsAgentHost* agent_host, bool replaced) {
}
void InspectableWebContentsImpl::RenderFrameHostChanged(
content::RenderFrameHost* old_host,
content::RenderFrameHost* new_host) {
if (new_host->GetParent())
return;
frontend_host_.reset(content::DevToolsFrontendHost::Create(
new_host,
base::Bind(&InspectableWebContentsImpl::HandleMessageFromDevToolsFrontend,
base::Unretained(this))));
}
void InspectableWebContentsImpl::WebContentsDestroyed() {
frontend_loaded_ = false;
Observe(nullptr);
Detach();
for (const auto& pair : pending_requests_)
delete pair.first;
if (view_ && view_->GetDelegate())
view_->GetDelegate()->DevToolsClosed();
}
bool InspectableWebContentsImpl::DidAddMessageToConsole(
content::WebContents* source,
int32_t level,
const base::string16& message,
int32_t line_no,
const base::string16& source_id) {
logging::LogMessage("CONSOLE", line_no, level).stream() << "\"" <<
message << "\", source: " << source_id << " (" << line_no << ")";
return true;
}
bool InspectableWebContentsImpl::ShouldCreateWebContents(
content::WebContents* web_contents,
content::SiteInstance* source_site_instance,
int32_t route_id,
int32_t main_frame_route_id,
int32_t main_frame_widget_route_id,
content::mojom::WindowContainerType window_container_type,
const GURL& opener_url,
const std::string& frame_name,
const GURL& target_url,
const std::string& partition_id,
content::SessionStorageNamespace* session_storage_namespace) {
return false;
}
void InspectableWebContentsImpl::HandleKeyboardEvent(
content::WebContents* source,
const content::NativeWebKeyboardEvent& event) {
auto delegate = web_contents_->GetDelegate();
if (delegate)
delegate->HandleKeyboardEvent(source, event);
}
void InspectableWebContentsImpl::CloseContents(content::WebContents* source) {
// This is where the devtools closes itself (by clicking the x button).
CloseDevTools();
}
content::ColorChooser* InspectableWebContentsImpl::OpenColorChooser(
content::WebContents* source,
SkColor color,
const std::vector<content::ColorSuggestion>& suggestions) {
auto delegate = web_contents_->GetDelegate();
if (delegate)
return delegate->OpenColorChooser(source, color, suggestions);
return nullptr;
}
void InspectableWebContentsImpl::RunFileChooser(
content::RenderFrameHost* render_frame_host,
const content::FileChooserParams& params) {
auto delegate = web_contents_->GetDelegate();
if (delegate)
delegate->RunFileChooser(render_frame_host, params);
}
void InspectableWebContentsImpl::EnumerateDirectory(
content::WebContents* source,
int request_id,
const base::FilePath& path) {
auto delegate = web_contents_->GetDelegate();
if (delegate)
delegate->EnumerateDirectory(source, request_id, path);
}
void InspectableWebContentsImpl::OnWebContentsFocused() {
#if defined(TOOLKIT_VIEWS)
if (view_->GetDelegate())
view_->GetDelegate()->DevToolsFocused();
#endif
}
void InspectableWebContentsImpl::DidStartNavigationToPendingEntry(
const GURL& url,
content::ReloadType reload_type) {
frontend_host_.reset(content::DevToolsFrontendHost::Create(
web_contents()->GetMainFrame(),
base::Bind(&InspectableWebContentsImpl::HandleMessageFromDevToolsFrontend,
base::Unretained(this))));
}
void InspectableWebContentsImpl::OnURLFetchComplete(
const net::URLFetcher* source) {
DCHECK(source);
auto it = pending_requests_.find(source);
DCHECK(it != pending_requests_.end());
base::DictionaryValue response;
auto* headers = new base::DictionaryValue();
net::HttpResponseHeaders* rh = source->GetResponseHeaders();
response.SetInteger("statusCode", rh ? rh->response_code() : 200);
response.Set("headers", headers);
size_t iterator = 0;
std::string name;
std::string value;
while (rh && rh->EnumerateHeaderLines(&iterator, &name, &value))
headers->SetString(name, value);
it->second.Run(&response);
pending_requests_.erase(it);
delete source;
}
void InspectableWebContentsImpl::SendMessageAck(int request_id,
const base::Value* arg) {
base::Value id_value(request_id);
CallClientFunction("DevToolsAPI.embedderMessageAck",
&id_value, arg, nullptr);
}
} // namespace brightray

View file

@ -0,0 +1,202 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Copyright (c) 2013 Adam Roben <adam@roben.org>. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file.
#ifndef BRIGHTRAY_BROWSER_INSPECTABLE_WEB_CONTENTS_IMPL_H_
#define BRIGHTRAY_BROWSER_INSPECTABLE_WEB_CONTENTS_IMPL_H_
#include "browser/inspectable_web_contents.h"
#include "browser/devtools_contents_resizing_strategy.h"
#include "browser/devtools_embedder_message_dispatcher.h"
#include "base/memory/weak_ptr.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/devtools_frontend_host.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "ui/gfx/geometry/rect.h"
class PrefService;
class PrefRegistrySimple;
namespace content {
class DevToolsAgentHost;
}
namespace brightray {
class InspectableWebContentsDelegate;
class InspectableWebContentsView;
class InspectableWebContentsImpl :
public InspectableWebContents,
public content::DevToolsAgentHostClient,
public content::WebContentsObserver,
public content::WebContentsDelegate,
public DevToolsEmbedderMessageDispatcher::Delegate,
public net::URLFetcherDelegate {
public:
static void RegisterPrefs(PrefRegistrySimple* pref_registry);
explicit InspectableWebContentsImpl(content::WebContents*);
virtual ~InspectableWebContentsImpl();
InspectableWebContentsView* GetView() const override;
content::WebContents* GetWebContents() const override;
content::WebContents* GetDevToolsWebContents() const override;
void SetDelegate(InspectableWebContentsDelegate* delegate) override;
InspectableWebContentsDelegate* GetDelegate() const override;
void SetDockState(const std::string& state) override;
void ShowDevTools() override;
void CloseDevTools() override;
bool IsDevToolsViewShowing() override;
void AttachTo(scoped_refptr<content::DevToolsAgentHost>) override;
void Detach() override;
void CallClientFunction(const std::string& function_name,
const base::Value* arg1,
const base::Value* arg2,
const base::Value* arg3) override;
void InspectElement(int x, int y) override;
// Return the last position and size of devtools window.
gfx::Rect GetDevToolsBounds() const;
void SaveDevToolsBounds(const gfx::Rect& bounds);
// Return the last set zoom level of devtools window.
double GetDevToolsZoomLevel() const;
void UpdateDevToolsZoomLevel(double level);
private:
// DevToolsEmbedderMessageDispacher::Delegate
void ActivateWindow() override;
void CloseWindow() override;
void LoadCompleted() override;
void SetInspectedPageBounds(const gfx::Rect& rect) override;
void InspectElementCompleted() override;
void InspectedURLChanged(const std::string& url) override;
void LoadNetworkResource(const DispatchCallback& callback,
const std::string& url,
const std::string& headers,
int stream_id) override;
void SetIsDocked(const DispatchCallback& callback, bool is_docked) override;
void OpenInNewTab(const std::string& url) override;
void SaveToFile(const std::string& url,
const std::string& content,
bool save_as) override;
void AppendToFile(const std::string& url,
const std::string& content) override;
void RequestFileSystems() override;
void AddFileSystem(const std::string& file_system_path) override;
void RemoveFileSystem(const std::string& file_system_path) override;
void UpgradeDraggedFileSystemPermissions(
const std::string& file_system_url) override;
void IndexPath(int index_request_id,
const std::string& file_system_path) override;
void StopIndexing(int index_request_id) override;
void SearchInPath(int search_request_id,
const std::string& file_system_path,
const std::string& query) override;
void SetWhitelistedShortcuts(const std::string& message) override;
void ZoomIn() override;
void ZoomOut() override;
void ResetZoom() override;
void SetDevicesUpdatesEnabled(bool enabled) override;
void DispatchProtocolMessageFromDevToolsFrontend(
const std::string& message) override;
void SendJsonRequest(const DispatchCallback& callback,
const std::string& browser_id,
const std::string& url) override;
void GetPreferences(const DispatchCallback& callback) override;
void SetPreference(const std::string& name,
const std::string& value) override;
void RemovePreference(const std::string& name) override;
void ClearPreferences() override;
// content::DevToolsFrontendHostDelegate:
void HandleMessageFromDevToolsFrontend(const std::string& message);
// content::DevToolsAgentHostClient:
void DispatchProtocolMessage(content::DevToolsAgentHost* agent_host,
const std::string& message) override;
void AgentHostClosed(content::DevToolsAgentHost* agent_host,
bool replaced) override;
// content::WebContentsObserver:
void RenderFrameHostChanged(content::RenderFrameHost* old_host,
content::RenderFrameHost* new_host) override;
void WebContentsDestroyed() override;
void OnWebContentsFocused() override;
void DidStartNavigationToPendingEntry(
const GURL& url,
content::ReloadType reload_type) override;
// content::WebContentsDelegate:
bool DidAddMessageToConsole(content::WebContents* source,
int32_t level,
const base::string16& message,
int32_t line_no,
const base::string16& source_id) override;
bool ShouldCreateWebContents(
content::WebContents* web_contents,
content::SiteInstance* source_site_instance,
int32_t route_id,
int32_t main_frame_route_id,
int32_t main_frame_widget_route_id,
content::mojom::WindowContainerType window_container_type,
const GURL& opener_url,
const std::string& frame_name,
const GURL& target_url,
const std::string& partition_id,
content::SessionStorageNamespace* session_storage_namespace) override;
void HandleKeyboardEvent(
content::WebContents*, const content::NativeWebKeyboardEvent&) override;
void CloseContents(content::WebContents* source) override;
content::ColorChooser* OpenColorChooser(
content::WebContents* source,
SkColor color,
const std::vector<content::ColorSuggestion>& suggestions) override;
void RunFileChooser(content::RenderFrameHost* render_frame_host,
const content::FileChooserParams& params) override;
void EnumerateDirectory(content::WebContents* source,
int request_id,
const base::FilePath& path) override;
// net::URLFetcherDelegate:
void OnURLFetchComplete(const net::URLFetcher* source) override;
void SendMessageAck(int request_id,
const base::Value* arg1);
bool frontend_loaded_;
scoped_refptr<content::DevToolsAgentHost> agent_host_;
std::unique_ptr<content::DevToolsFrontendHost> frontend_host_;
std::unique_ptr<DevToolsEmbedderMessageDispatcher>
embedder_message_dispatcher_;
DevToolsContentsResizingStrategy contents_resizing_strategy_;
gfx::Rect devtools_bounds_;
bool can_dock_;
std::string dock_state_;
using PendingRequestsMap = std::map<const net::URLFetcher*, DispatchCallback>;
PendingRequestsMap pending_requests_;
InspectableWebContentsDelegate* delegate_; // weak references.
PrefService* pref_service_; // weak reference.
std::unique_ptr<content::WebContents> web_contents_;
std::unique_ptr<content::WebContents> devtools_web_contents_;
std::unique_ptr<InspectableWebContentsView> view_;
base::WeakPtrFactory<InspectableWebContentsImpl> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(InspectableWebContentsImpl);
};
} // namespace brightray
#endif

View file

@ -0,0 +1,59 @@
#ifndef BRIGHTRAY_BROWSER_INSPECTABLE_WEB_CONTENTS_VIEW_H_
#define BRIGHTRAY_BROWSER_INSPECTABLE_WEB_CONTENTS_VIEW_H_
#include "base/strings/string16.h"
#include "ui/gfx/native_widget_types.h"
class DevToolsContentsResizingStrategy;
#if defined(TOOLKIT_VIEWS)
namespace views {
class View;
}
#endif
namespace brightray {
class InspectableWebContentsViewDelegate;
class InspectableWebContentsView {
public:
InspectableWebContentsView() : delegate_(nullptr) {}
virtual ~InspectableWebContentsView() {}
// The delegate manages its own life.
void SetDelegate(InspectableWebContentsViewDelegate* delegate) {
delegate_ = delegate;
}
InspectableWebContentsViewDelegate* GetDelegate() const {
return delegate_;
}
#if defined(TOOLKIT_VIEWS)
// Returns the container control, which has devtools view attached.
virtual views::View* GetView() = 0;
// Returns the web view control, which can be used by the
// GetInitiallyFocusedView() to set initial focus to web view.
virtual views::View* GetWebView() = 0;
#else
virtual gfx::NativeView GetNativeView() const = 0;
#endif
virtual void ShowDevTools() = 0;
// Hide the DevTools view.
virtual void CloseDevTools() = 0;
virtual bool IsDevToolsViewShowing() = 0;
virtual bool IsDevToolsViewFocused() = 0;
virtual void SetIsDocked(bool docked) = 0;
virtual void SetContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy) = 0;
virtual void SetTitle(const base::string16& title) = 0;
private:
InspectableWebContentsViewDelegate* delegate_; // weak references.
};
} // namespace brightray
#endif

View file

@ -0,0 +1,10 @@
#include "browser/inspectable_web_contents_view_delegate.h"
namespace brightray {
gfx::ImageSkia InspectableWebContentsViewDelegate::GetDevToolsWindowIcon() {
return gfx::ImageSkia();
}
} // namespace brightray

View file

@ -0,0 +1,28 @@
#ifndef BROWSER_INSPECTABLE_WEB_CONTENTS_VIEW_DELEGATE_H_
#define BROWSER_INSPECTABLE_WEB_CONTENTS_VIEW_DELEGATE_H_
#include "ui/gfx/image/image_skia.h"
namespace brightray {
class InspectableWebContentsViewDelegate {
public:
virtual ~InspectableWebContentsViewDelegate() {}
virtual void DevToolsFocused() {}
virtual void DevToolsOpened() {}
virtual void DevToolsClosed() {}
// Returns the icon of devtools window.
virtual gfx::ImageSkia GetDevToolsWindowIcon();
#if defined(USE_X11)
// Called when creating devtools window.
virtual void GetDevToolsWindowWMClass(
std::string* name, std::string* class_name) {}
#endif
};
} // namespace brightray
#endif // BROWSER_INSPECTABLE_WEB_CONTENTS_VIEW_DELEGATE_H_

View file

@ -0,0 +1,45 @@
#ifndef BRIGHTRAY_BROWSER_INSPECTABLE_WEB_CONTENTS_VIEW_MAC_H_
#define BRIGHTRAY_BROWSER_INSPECTABLE_WEB_CONTENTS_VIEW_MAC_H_
#include "browser/inspectable_web_contents_view.h"
#include "base/mac/scoped_nsobject.h"
@class BRYInspectableWebContentsView;
namespace brightray {
class InspectableWebContentsImpl;
class InspectableWebContentsViewMac : public InspectableWebContentsView {
public:
explicit InspectableWebContentsViewMac(
InspectableWebContentsImpl* inspectable_web_contents_impl);
virtual ~InspectableWebContentsViewMac();
gfx::NativeView GetNativeView() const override;
void ShowDevTools() override;
void CloseDevTools() override;
bool IsDevToolsViewShowing() override;
bool IsDevToolsViewFocused() override;
void SetIsDocked(bool docked) override;
void SetContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy) override;
void SetTitle(const base::string16& title) override;
InspectableWebContentsImpl* inspectable_web_contents() {
return inspectable_web_contents_;
}
private:
// Owns us.
InspectableWebContentsImpl* inspectable_web_contents_;
base::scoped_nsobject<BRYInspectableWebContentsView> view_;
DISALLOW_COPY_AND_ASSIGN(InspectableWebContentsViewMac);
};
} // namespace brightray
#endif

View file

@ -0,0 +1,59 @@
#include "browser/inspectable_web_contents_view_mac.h"
#import <AppKit/AppKit.h>
#include "base/strings/sys_string_conversions.h"
#include "browser/inspectable_web_contents.h"
#include "browser/inspectable_web_contents_view_delegate.h"
#import "browser/mac/bry_inspectable_web_contents_view.h"
namespace brightray {
InspectableWebContentsView* CreateInspectableContentsView(InspectableWebContentsImpl* inspectable_web_contents) {
return new InspectableWebContentsViewMac(inspectable_web_contents);
}
InspectableWebContentsViewMac::InspectableWebContentsViewMac(InspectableWebContentsImpl* inspectable_web_contents)
: inspectable_web_contents_(inspectable_web_contents),
view_([[BRYInspectableWebContentsView alloc] initWithInspectableWebContentsViewMac:this]) {
}
InspectableWebContentsViewMac::~InspectableWebContentsViewMac() {
[view_ removeObservers];
CloseDevTools();
}
gfx::NativeView InspectableWebContentsViewMac::GetNativeView() const {
return view_.get();
}
void InspectableWebContentsViewMac::ShowDevTools() {
[view_ setDevToolsVisible:YES];
}
void InspectableWebContentsViewMac::CloseDevTools() {
[view_ setDevToolsVisible:NO];
}
bool InspectableWebContentsViewMac::IsDevToolsViewShowing() {
return [view_ isDevToolsVisible];
}
bool InspectableWebContentsViewMac::IsDevToolsViewFocused() {
return [view_ isDevToolsFocused];
}
void InspectableWebContentsViewMac::SetIsDocked(bool docked) {
[view_ setIsDocked:docked];
}
void InspectableWebContentsViewMac::SetContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy) {
[view_ setContentsResizingStrategy:strategy];
}
void InspectableWebContentsViewMac::SetTitle(const base::string16& title) {
[view_ setTitle:base::SysUTF16ToNSString(title)];
}
} // namespace brightray

View file

@ -0,0 +1,133 @@
// This is generated file. Do not modify directly.
// Path to the code generator:
// tools/generate_library_loader/generate_library_loader.py .
#include "browser/linux/libnotify_loader.h"
#include <dlfcn.h>
LibNotifyLoader::LibNotifyLoader() : loaded_(false) {
}
LibNotifyLoader::~LibNotifyLoader() {
CleanUp(loaded_);
}
bool LibNotifyLoader::Load(const std::string& library_name) {
if (loaded_)
return false;
library_ = dlopen(library_name.c_str(), RTLD_LAZY);
if (!library_)
return false;
notify_is_initted =
reinterpret_cast<decltype(this->notify_is_initted)>(
dlsym(library_, "notify_is_initted"));
if (!notify_is_initted) {
CleanUp(true);
return false;
}
notify_init =
reinterpret_cast<decltype(this->notify_init)>(
dlsym(library_, "notify_init"));
if (!notify_init) {
CleanUp(true);
return false;
}
notify_get_server_info =
reinterpret_cast<decltype(this->notify_get_server_info)>(
dlsym(library_, "notify_get_server_info"));
if (!notify_get_server_info) {
CleanUp(true);
return false;
}
notify_get_server_caps =
reinterpret_cast<decltype(this->notify_get_server_caps)>(
dlsym(library_, "notify_get_server_caps"));
if (!notify_get_server_caps) {
CleanUp(true);
return false;
}
notify_notification_new =
reinterpret_cast<decltype(this->notify_notification_new)>(
dlsym(library_, "notify_notification_new"));
if (!notify_notification_new) {
CleanUp(true);
return false;
}
notify_notification_add_action =
reinterpret_cast<decltype(this->notify_notification_add_action)>(
dlsym(library_, "notify_notification_add_action"));
if (!notify_notification_add_action) {
CleanUp(true);
return false;
}
notify_notification_set_image_from_pixbuf = reinterpret_cast<decltype(
this->notify_notification_set_image_from_pixbuf)>(
dlsym(library_, "notify_notification_set_image_from_pixbuf"));
if (!notify_notification_set_image_from_pixbuf) {
CleanUp(true);
return false;
}
notify_notification_set_timeout =
reinterpret_cast<decltype(this->notify_notification_set_timeout)>(
dlsym(library_, "notify_notification_set_timeout"));
if (!notify_notification_set_timeout) {
CleanUp(true);
return false;
}
notify_notification_set_hint_string =
reinterpret_cast<decltype(this->notify_notification_set_hint_string)>(
dlsym(library_, "notify_notification_set_hint_string"));
if (!notify_notification_set_hint_string) {
CleanUp(true);
return false;
}
notify_notification_show =
reinterpret_cast<decltype(this->notify_notification_show)>(
dlsym(library_, "notify_notification_show"));
if (!notify_notification_show) {
CleanUp(true);
return false;
}
notify_notification_close =
reinterpret_cast<decltype(this->notify_notification_close)>(
dlsym(library_, "notify_notification_close"));
if (!notify_notification_close) {
CleanUp(true);
return false;
}
loaded_ = true;
return true;
}
void LibNotifyLoader::CleanUp(bool unload) {
if (unload) {
dlclose(library_);
library_ = NULL;
}
loaded_ = false;
notify_is_initted = NULL;
notify_init = NULL;
notify_get_server_info = NULL;
notify_get_server_caps = NULL;
notify_notification_new = NULL;
notify_notification_add_action = NULL;
notify_notification_set_image_from_pixbuf = NULL;
notify_notification_set_timeout = NULL;
notify_notification_set_hint_string = NULL;
notify_notification_show = NULL;
notify_notification_close = NULL;
}

View file

@ -0,0 +1,47 @@
// This is generated file. Do not modify directly.
// Path to the code generator:
// tools/generate_library_loader/generate_library_loader.py .
#ifndef BRIGHTRAY_BROWSER_LINUX_LIBNOTIFY_LOADER_H_
#define BRIGHTRAY_BROWSER_LINUX_LIBNOTIFY_LOADER_H_
#include <libnotify/notify.h>
#include <string>
class LibNotifyLoader {
public:
LibNotifyLoader();
~LibNotifyLoader();
bool Load(const std::string& library_name)
__attribute__((warn_unused_result));
bool loaded() const { return loaded_; }
decltype(&::notify_is_initted) notify_is_initted;
decltype(&::notify_init) notify_init;
decltype(&::notify_get_server_caps) notify_get_server_caps;
decltype(&::notify_get_server_info) notify_get_server_info;
decltype(&::notify_notification_new) notify_notification_new;
decltype(&::notify_notification_add_action) notify_notification_add_action;
decltype(&::notify_notification_set_image_from_pixbuf)
notify_notification_set_image_from_pixbuf;
decltype(&::notify_notification_set_timeout) notify_notification_set_timeout;
decltype(&::notify_notification_set_hint_string)
notify_notification_set_hint_string;
decltype(&::notify_notification_show) notify_notification_show;
decltype(&::notify_notification_close) notify_notification_close;
private:
void CleanUp(bool unload);
void* library_;
bool loaded_;
// Disallow copy constructor and assignment operator.
LibNotifyLoader(const LibNotifyLoader&);
void operator=(const LibNotifyLoader&);
};
#endif // BRIGHTRAY_BROWSER_LINUX_LIBNOTIFY_LOADER_H_

View file

@ -0,0 +1,167 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "browser/linux/libnotify_notification.h"
#include "base/files/file_enumerator.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "browser/notification_delegate.h"
#include "chrome/browser/ui/libgtkui/skia_utils_gtk.h"
#include "common/application_info.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace brightray {
namespace {
LibNotifyLoader libnotify_loader_;
bool HasCapability(const std::string& capability) {
bool result = false;
GList* capabilities = libnotify_loader_.notify_get_server_caps();
if (g_list_find_custom(capabilities, capability.c_str(),
(GCompareFunc)g_strcmp0) != NULL)
result = true;
g_list_free_full(capabilities, g_free);
return result;
}
bool NotifierSupportsActions() {
if (getenv("ELECTRON_USE_UBUNTU_NOTIFIER"))
return false;
static bool notify_has_result = false;
static bool notify_result = false;
if (notify_has_result)
return notify_result;
notify_result = HasCapability("actions");
return notify_result;
}
void log_and_clear_error(GError* error, const char* context) {
LOG(ERROR) << context
<< ": domain=" << error->domain
<< " code=" << error->code
<< " message=\"" << error->message << '"';
g_error_free(error);
}
} // namespace
// static
bool LibnotifyNotification::Initialize() {
if (!libnotify_loader_.Load("libnotify.so.4") && // most common one
!libnotify_loader_.Load("libnotify.so.5") &&
!libnotify_loader_.Load("libnotify.so.1") &&
!libnotify_loader_.Load("libnotify.so")) {
return false;
}
if (!libnotify_loader_.notify_is_initted() &&
!libnotify_loader_.notify_init(GetApplicationName().c_str())) {
return false;
}
return true;
}
LibnotifyNotification::LibnotifyNotification(NotificationDelegate* delegate,
NotificationPresenter* presenter)
: Notification(delegate, presenter),
notification_(nullptr) {
}
LibnotifyNotification::~LibnotifyNotification() {
if (notification_) {
g_signal_handlers_disconnect_by_data(notification_, this);
g_object_unref(notification_);
}
}
void LibnotifyNotification::Show(const base::string16& title,
const base::string16& body,
const std::string& tag,
const GURL& icon_url,
const SkBitmap& icon,
const bool silent) {
notification_ = libnotify_loader_.notify_notification_new(
base::UTF16ToUTF8(title).c_str(),
base::UTF16ToUTF8(body).c_str(),
nullptr);
g_signal_connect(
notification_, "closed", G_CALLBACK(OnNotificationClosedThunk), this);
// NB: On Unity and on any other DE using Notify-OSD, adding a notification
// action will cause the notification to display as a modal dialog box.
if (NotifierSupportsActions()) {
libnotify_loader_.notify_notification_add_action(
notification_, "default", "View", OnNotificationViewThunk, this,
nullptr);
}
if (!icon.drawsNothing()) {
GdkPixbuf* pixbuf = libgtkui::GdkPixbufFromSkBitmap(icon);
libnotify_loader_.notify_notification_set_image_from_pixbuf(
notification_, pixbuf);
libnotify_loader_.notify_notification_set_timeout(
notification_, NOTIFY_EXPIRES_DEFAULT);
g_object_unref(pixbuf);
}
if (!tag.empty()) {
GQuark id = g_quark_from_string(tag.c_str());
g_object_set(G_OBJECT(notification_), "id", id, NULL);
}
// Always try to append notifications.
// Unique tags can be used to prevent this.
if (HasCapability("append")) {
libnotify_loader_.notify_notification_set_hint_string(
notification_, "append", "true");
} else if (HasCapability("x-canonical-append")) {
libnotify_loader_.notify_notification_set_hint_string(
notification_, "x-canonical-append", "true");
}
GError* error = nullptr;
libnotify_loader_.notify_notification_show(notification_, &error);
if (error) {
log_and_clear_error(error, "notify_notification_show");
NotificationFailed();
return;
}
delegate()->NotificationDisplayed();
}
void LibnotifyNotification::Dismiss() {
if (!notification_) {
Destroy();
return;
}
GError* error = nullptr;
libnotify_loader_.notify_notification_close(notification_, &error);
if (error) {
log_and_clear_error(error, "notify_notification_close");
Destroy();
}
}
void LibnotifyNotification::OnNotificationClosed(
NotifyNotification* notification) {
NotificationDismissed();
}
void LibnotifyNotification::OnNotificationView(
NotifyNotification* notification, char* action) {
NotificationClicked();
}
} // namespace brightray

View file

@ -0,0 +1,44 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef BROWSER_LINUX_LIBNOTIFY_NOTIFICATION_H_
#define BROWSER_LINUX_LIBNOTIFY_NOTIFICATION_H_
#include "browser/linux/libnotify_loader.h"
#include "browser/notification.h"
#include "ui/base/glib/glib_signal.h"
namespace brightray {
class LibnotifyNotification : public Notification {
public:
LibnotifyNotification(NotificationDelegate* delegate,
NotificationPresenter* presenter);
virtual ~LibnotifyNotification();
static bool Initialize();
// Notification:
void Show(const base::string16& title,
const base::string16& msg,
const std::string& tag,
const GURL& icon_url,
const SkBitmap& icon,
const bool silent) override;
void Dismiss() override;
private:
CHROMEG_CALLBACK_0(LibnotifyNotification, void, OnNotificationClosed,
NotifyNotification*);
CHROMEG_CALLBACK_1(LibnotifyNotification, void, OnNotificationView,
NotifyNotification*, char*);
NotifyNotification* notification_;
DISALLOW_COPY_AND_ASSIGN(LibnotifyNotification);
};
} // namespace brightray
#endif // BROWSER_LINUX_LIBNOTIFY_NOTIFICATION_H_

View file

@ -0,0 +1,30 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Copyright (c) 2013 Patrick Reynolds <piki@github.com>. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file.
#include "browser/linux/notification_presenter_linux.h"
#include "browser/linux/libnotify_notification.h"
namespace brightray {
// static
NotificationPresenter* NotificationPresenter::Create() {
if (!LibnotifyNotification::Initialize())
return nullptr;
return new NotificationPresenterLinux;
}
NotificationPresenterLinux::NotificationPresenterLinux() {
}
NotificationPresenterLinux::~NotificationPresenterLinux() {
}
Notification* NotificationPresenterLinux::CreateNotificationObject(
NotificationDelegate* delegate) {
return new LibnotifyNotification(delegate, this);
}
} // namespace brightray

View file

@ -0,0 +1,27 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Copyright (c) 2013 Patrick Reynolds <piki@github.com>. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file.
#ifndef BRIGHTRAY_BROWSER_NOTIFICATION_PRESENTER_LINUX_H_
#define BRIGHTRAY_BROWSER_NOTIFICATION_PRESENTER_LINUX_H_
#include "browser/notification_presenter.h"
namespace brightray {
class NotificationPresenterLinux : public NotificationPresenter {
public:
NotificationPresenterLinux();
~NotificationPresenterLinux();
private:
Notification* CreateNotificationObject(
NotificationDelegate* delegate) override;
DISALLOW_COPY_AND_ASSIGN(NotificationPresenterLinux);
};
} // namespace brightray
#endif

View file

@ -0,0 +1,7 @@
#import "base/mac/scoped_sending_event.h"
@interface BRYApplication : NSApplication<CrAppProtocol, CrAppControlProtocol> {
BOOL _handlingSendEvent;
}
@end

View file

@ -0,0 +1,19 @@
#import "bry_application.h"
@interface BRYApplication ()
@property (nonatomic, assign, getter = isHandlingSendEvent) BOOL handlingSendEvent;
@end
@implementation BRYApplication
@synthesize handlingSendEvent = _handlingSendEvent;
- (void)sendEvent:(NSEvent *)theEvent
{
base::mac::ScopedSendingEvent scopedSendingEvent;
[super sendEvent:theEvent];
}
@end

View file

@ -0,0 +1,36 @@
#import <AppKit/AppKit.h>
#include "browser/devtools_contents_resizing_strategy.h"
#include "base/mac/scoped_nsobject.h"
#include "ui/base/cocoa/base_view.h"
namespace brightray {
class InspectableWebContentsViewMac;
}
using brightray::InspectableWebContentsViewMac;
@interface BRYInspectableWebContentsView : BaseView<NSWindowDelegate> {
@private
brightray::InspectableWebContentsViewMac* inspectableWebContentsView_;
base::scoped_nsobject<NSWindow> devtools_window_;
BOOL devtools_visible_;
BOOL devtools_docked_;
BOOL devtools_is_first_responder_;
DevToolsContentsResizingStrategy strategy_;
}
- (instancetype)initWithInspectableWebContentsViewMac:(InspectableWebContentsViewMac*)view;
- (void)removeObservers;
- (void)notifyDevToolsFocused;
- (void)setDevToolsVisible:(BOOL)visible;
- (BOOL)isDevToolsVisible;
- (BOOL)isDevToolsFocused;
- (void)setIsDocked:(BOOL)docked;
- (void)setContentsResizingStrategy:(const DevToolsContentsResizingStrategy&)strategy;
- (void)setTitle:(NSString*)title;
@end

View file

@ -0,0 +1,252 @@
#include "browser/mac/bry_inspectable_web_contents_view.h"
#include "browser/inspectable_web_contents_impl.h"
#include "browser/inspectable_web_contents_view_delegate.h"
#include "browser/inspectable_web_contents_view_mac.h"
#include "browser/mac/event_dispatching_window.h"
#include "content/public/browser/render_widget_host_view.h"
#include "ui/gfx/mac/scoped_cocoa_disable_screen_updates.h"
@implementation BRYInspectableWebContentsView
- (instancetype)initWithInspectableWebContentsViewMac:(InspectableWebContentsViewMac*)view {
self = [super init];
if (!self)
return nil;
inspectableWebContentsView_ = view;
devtools_visible_ = NO;
devtools_docked_ = NO;
devtools_is_first_responder_ = NO;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(viewDidBecomeFirstResponder:)
name:kViewDidBecomeFirstResponder
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(parentWindowBecameMain:)
name:NSWindowDidBecomeMainNotification
object:nil];
auto contents = inspectableWebContentsView_->inspectable_web_contents()->GetWebContents();
auto contentsView = contents->GetNativeView();
[contentsView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[self addSubview:contentsView];
// See https://code.google.com/p/chromium/issues/detail?id=348490.
[self setWantsLayer:YES];
return self;
}
- (void)removeObservers {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize {
[self adjustSubviews];
}
- (IBAction)showDevTools:(id)sender {
inspectableWebContentsView_->inspectable_web_contents()->ShowDevTools();
}
- (void)notifyDevToolsFocused {
if (inspectableWebContentsView_->GetDelegate())
inspectableWebContentsView_->GetDelegate()->DevToolsFocused();
}
- (void)setDevToolsVisible:(BOOL)visible {
if (visible == devtools_visible_)
return;
auto inspectable_web_contents = inspectableWebContentsView_->inspectable_web_contents();
auto webContents = inspectable_web_contents->GetWebContents();
auto devToolsWebContents = inspectable_web_contents->GetDevToolsWebContents();
auto devToolsView = devToolsWebContents->GetNativeView();
if (visible && devtools_docked_) {
webContents->SetAllowOtherViews(true);
devToolsWebContents->SetAllowOtherViews(true);
} else {
webContents->SetAllowOtherViews(false);
}
devtools_visible_ = visible;
if (devtools_docked_) {
if (visible) {
// Place the devToolsView under contentsView, notice that we didn't set
// sizes for them until the setContentsResizingStrategy message.
[self addSubview:devToolsView positioned:NSWindowBelow relativeTo:nil];
[self adjustSubviews];
// Focus on web view.
devToolsWebContents->RestoreFocus();
} else {
gfx::ScopedCocoaDisableScreenUpdates disabler;
[devToolsView removeFromSuperview];
[self adjustSubviews];
}
} else {
if (visible) {
[devtools_window_ makeKeyAndOrderFront:nil];
} else {
[[self window] makeKeyAndOrderFront:nil];
[devtools_window_ setDelegate:nil];
[devtools_window_ close];
devtools_window_.reset();
}
}
}
- (BOOL)isDevToolsVisible {
return devtools_visible_;
}
- (BOOL)isDevToolsFocused {
if (devtools_docked_) {
return [[self window] isKeyWindow] && devtools_is_first_responder_;
} else {
return [devtools_window_ isKeyWindow];
}
}
- (void)setIsDocked:(BOOL)docked {
// Revert to no-devtools state.
[self setDevToolsVisible:NO];
// Switch to new state.
devtools_docked_ = docked;
if (!docked) {
auto inspectable_web_contents = inspectableWebContentsView_->inspectable_web_contents();
auto devToolsWebContents = inspectable_web_contents->GetDevToolsWebContents();
auto devToolsView = devToolsWebContents->GetNativeView();
auto styleMask = NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask | NSResizableWindowMask |
NSTexturedBackgroundWindowMask |
NSUnifiedTitleAndToolbarWindowMask;
devtools_window_.reset([[EventDispatchingWindow alloc]
initWithContentRect:NSMakeRect(0, 0, 800, 600)
styleMask:styleMask
backing:NSBackingStoreBuffered
defer:YES]);
[devtools_window_ setDelegate:self];
[devtools_window_ setFrameAutosaveName:@"brightray.devtools"];
[devtools_window_ setTitle:@"Developer Tools"];
[devtools_window_ setReleasedWhenClosed:NO];
[devtools_window_ setAutorecalculatesContentBorderThickness:NO forEdge:NSMaxYEdge];
[devtools_window_ setContentBorderThickness:24 forEdge:NSMaxYEdge];
NSView* contentView = [devtools_window_ contentView];
devToolsView.frame = contentView.bounds;
devToolsView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[contentView addSubview:devToolsView];
}
[self setDevToolsVisible:YES];
}
- (void)setContentsResizingStrategy:(const DevToolsContentsResizingStrategy&)strategy {
strategy_.CopyFrom(strategy);
[self adjustSubviews];
}
- (void)adjustSubviews {
if (![[self subviews] count])
return;
if (![self isDevToolsVisible] || devtools_window_) {
DCHECK_EQ(1u, [[self subviews] count]);
NSView* contents = [[self subviews] objectAtIndex:0];
[contents setFrame:[self bounds]];
return;
}
NSView* devToolsView = [[self subviews] objectAtIndex:0];
NSView* contentsView = [[self subviews] objectAtIndex:1];
DCHECK_EQ(2u, [[self subviews] count]);
gfx::Rect new_devtools_bounds;
gfx::Rect new_contents_bounds;
ApplyDevToolsContentsResizingStrategy(
strategy_, gfx::Size(NSSizeToCGSize([self bounds].size)),
&new_devtools_bounds, &new_contents_bounds);
[devToolsView setFrame:[self flipRectToNSRect:new_devtools_bounds]];
[contentsView setFrame:[self flipRectToNSRect:new_contents_bounds]];
}
- (void)setTitle:(NSString*)title {
[devtools_window_ setTitle:title];
}
- (void)viewDidBecomeFirstResponder:(NSNotification*)notification {
auto inspectable_web_contents = inspectableWebContentsView_->inspectable_web_contents();
if (!inspectable_web_contents)
return;
auto webContents = inspectable_web_contents->GetWebContents();
auto webContentsView = webContents->GetNativeView();
NSView* view = [notification object];
if ([[webContentsView subviews] containsObject:view]) {
devtools_is_first_responder_ = NO;
return;
}
auto devToolsWebContents = inspectable_web_contents->GetDevToolsWebContents();
if (!devToolsWebContents)
return;
auto devToolsView = devToolsWebContents->GetNativeView();
if ([[devToolsView subviews] containsObject:view]) {
devtools_is_first_responder_ = YES;
[self notifyDevToolsFocused];
}
}
- (void)parentWindowBecameMain:(NSNotification*)notification {
NSWindow* parentWindow = [notification object];
if ([self window] == parentWindow && devtools_docked_ && devtools_is_first_responder_)
[self notifyDevToolsFocused];
}
#pragma mark - NSWindowDelegate
- (void)windowWillClose:(NSNotification*)notification {
inspectableWebContentsView_->inspectable_web_contents()->CloseDevTools();
}
- (void)windowDidBecomeMain:(NSNotification*)notification {
content::WebContents* web_contents =
inspectableWebContentsView_->inspectable_web_contents()->GetDevToolsWebContents();
if (!web_contents)
return;
web_contents->RestoreFocus();
content::RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
if (rwhv)
rwhv->SetActive(true);
[self notifyDevToolsFocused];
}
- (void)windowDidResignMain:(NSNotification*)notification {
content::WebContents* web_contents =
inspectableWebContentsView_->inspectable_web_contents()->GetDevToolsWebContents();
if (!web_contents)
return;
web_contents->StoreFocus();
content::RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
if (rwhv)
rwhv->SetActive(false);
}
@end

View file

@ -0,0 +1,42 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef BROWSER_MAC_COCOA_NOTIFICATION_H_
#define BROWSER_MAC_COCOA_NOTIFICATION_H_
#import <Foundation/Foundation.h>
#include "base/mac/scoped_nsobject.h"
#include "browser/notification.h"
namespace brightray {
class CocoaNotification : public Notification {
public:
CocoaNotification(NotificationDelegate* delegate,
NotificationPresenter* presenter);
~CocoaNotification();
// Notification:
void Show(const base::string16& title,
const base::string16& msg,
const std::string& tag,
const GURL& icon_url,
const SkBitmap& icon,
const bool silent) override;
void Dismiss() override;
void NotificationDisplayed();
NSUserNotification* notification() const { return notification_; }
private:
base::scoped_nsobject<NSUserNotification> notification_;
DISALLOW_COPY_AND_ASSIGN(CocoaNotification);
};
} // namespace brightray
#endif // BROWSER_MAC_COCOA_NOTIFICATION_H_

View file

@ -0,0 +1,64 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "browser/mac/cocoa_notification.h"
#include "base/mac/mac_util.h"
#include "base/strings/sys_string_conversions.h"
#include "browser/notification_delegate.h"
#include "browser/notification_presenter.h"
#include "skia/ext/skia_utils_mac.h"
namespace brightray {
CocoaNotification::CocoaNotification(NotificationDelegate* delegate,
NotificationPresenter* presenter)
: Notification(delegate, presenter) {
}
CocoaNotification::~CocoaNotification() {
if (notification_)
[NSUserNotificationCenter.defaultUserNotificationCenter
removeDeliveredNotification:notification_];
}
void CocoaNotification::Show(const base::string16& title,
const base::string16& body,
const std::string& tag,
const GURL& icon_url,
const SkBitmap& icon,
const bool silent) {
notification_.reset([[NSUserNotification alloc] init]);
[notification_ setTitle:base::SysUTF16ToNSString(title)];
[notification_ setInformativeText:base::SysUTF16ToNSString(body)];
if ([notification_ respondsToSelector:@selector(setContentImage:)] &&
!icon.drawsNothing()) {
NSImage* image = skia::SkBitmapToNSImageWithColorSpace(
icon, base::mac::GetGenericRGBColorSpace());
[notification_ setContentImage:image];
}
if (silent) {
[notification_ setSoundName:nil];
} else {
[notification_ setSoundName:NSUserNotificationDefaultSoundName];
}
[NSUserNotificationCenter.defaultUserNotificationCenter
deliverNotification:notification_];
}
void CocoaNotification::Dismiss() {
if (notification_)
[NSUserNotificationCenter.defaultUserNotificationCenter
removeDeliveredNotification:notification_];
NotificationDismissed();
}
void CocoaNotification::NotificationDisplayed() {
delegate()->NotificationDisplayed();
}
} // namespace brightray

View file

@ -0,0 +1,19 @@
// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef BROWSER_EVENT_DISPATCHING_WINDOW_H_
#define BROWSER_EVENT_DISPATCHING_WINDOW_H_
#import "ui/base/cocoa/underlay_opengl_hosting_window.h"
@interface EventDispatchingWindow : UnderlayOpenGLHostingWindow {
@private
BOOL redispatchingEvent_;
}
- (void)redispatchKeyEvent:(NSEvent*)event;
@end
#endif // BROWSER_EVENT_DISPATCHING_WINDOW_H_

View file

@ -0,0 +1,34 @@
// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "browser/mac/event_dispatching_window.h"
@implementation EventDispatchingWindow
- (void)sendEvent:(NSEvent*)event {
if (!redispatchingEvent_)
[super sendEvent:event];
}
- (BOOL)performKeyEquivalent:(NSEvent*)event {
if (redispatchingEvent_)
return NO;
else
return [super performKeyEquivalent:event];
}
- (void)redispatchKeyEvent:(NSEvent*)event {
NSEventType eventType = [event type];
if (eventType != NSKeyDown && eventType != NSKeyUp &&
eventType != NSFlagsChanged) {
return;
}
// Redispatch the event.
redispatchingEvent_ = YES;
[NSApp sendEvent:event];
redispatchingEvent_ = NO;
}
@end

View file

@ -0,0 +1,22 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef BROWSER_MAC_NOTIFICATION_DELEGATE_H_
#define BROWSER_MAC_NOTIFICATION_DELEGATE_H_
#import <Foundation/Foundation.h>
namespace brightray {
class NotificationPresenterMac;
}
@interface NotificationCenterDelegate :
NSObject<NSUserNotificationCenterDelegate> {
@private
brightray::NotificationPresenterMac* presenter_;
}
- (instancetype)initWithPresenter:(brightray::NotificationPresenterMac*)presenter;
@end
#endif // BROWSER_MAC_NOTIFICATION_DELEGATE_H_

View file

@ -0,0 +1,41 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "browser/mac/notification_center_delegate.h"
#include "browser/mac/cocoa_notification.h"
#include "browser/mac/notification_presenter_mac.h"
@implementation NotificationCenterDelegate
- (instancetype)initWithPresenter:(brightray::NotificationPresenterMac*)presenter {
self = [super init];
if (!self)
return nil;
presenter_ = presenter;
return self;
}
- (void)userNotificationCenter:(NSUserNotificationCenter*)center
didDeliverNotification:(NSUserNotification*)notif {
auto notification = presenter_->GetNotification(notif);
if (notification)
notification->NotificationDisplayed();
}
- (void)userNotificationCenter:(NSUserNotificationCenter*)center
didActivateNotification:(NSUserNotification *)notif {
auto notification = presenter_->GetNotification(notif);
if (notification)
notification->NotificationClicked();
}
- (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center
shouldPresentNotification:(NSUserNotification*)notification {
// Display notifications even if the app is active.
return YES;
}
@end

View file

@ -0,0 +1,36 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Copyright (c) 2013 Adam Roben <adam@roben.org>. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file.
#ifndef BRIGHTRAY_BROWSER_NOTIFICATION_PRESENTER_MAC_H_
#define BRIGHTRAY_BROWSER_NOTIFICATION_PRESENTER_MAC_H_
#include "base/mac/scoped_nsobject.h"
#include "browser/mac/notification_center_delegate.h"
#include "browser/notification_presenter.h"
namespace brightray {
class CocoaNotification;
class NotificationPresenterMac : public NotificationPresenter {
public:
CocoaNotification* GetNotification(NSUserNotification* notif);
NotificationPresenterMac();
~NotificationPresenterMac();
private:
Notification* CreateNotificationObject(
NotificationDelegate* delegate) override;
base::scoped_nsobject<NotificationCenterDelegate>
notification_center_delegate_;
DISALLOW_COPY_AND_ASSIGN(NotificationPresenterMac);
};
} // namespace brightray
#endif

View file

@ -0,0 +1,43 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "browser/mac/notification_presenter_mac.h"
#include "browser/mac/cocoa_notification.h"
#include "browser/mac/notification_center_delegate.h"
namespace brightray {
// static
NotificationPresenter* NotificationPresenter::Create() {
return new NotificationPresenterMac;
}
CocoaNotification* NotificationPresenterMac::GetNotification(
NSUserNotification* ns_notification) {
for (Notification* notification : notifications()) {
auto native_notification = static_cast<CocoaNotification*>(notification);
if ([native_notification->notification() isEqual:ns_notification])
return native_notification;
}
return nullptr;
}
NotificationPresenterMac::NotificationPresenterMac()
: notification_center_delegate_(
[[NotificationCenterDelegate alloc] initWithPresenter:this]) {
NSUserNotificationCenter.defaultUserNotificationCenter.delegate =
notification_center_delegate_;
}
NotificationPresenterMac::~NotificationPresenterMac() {
NSUserNotificationCenter.defaultUserNotificationCenter.delegate = nil;
}
Notification* NotificationPresenterMac::CreateNotificationObject(
NotificationDelegate* delegate) {
return new CocoaNotification(delegate, this);
}
} // namespace brightray

View file

@ -0,0 +1,158 @@
// 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-CHROMIUM file.
#include "browser/media/media_capture_devices_dispatcher.h"
#include "base/logging.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/media_capture_devices.h"
#include "content/public/common/media_stream_request.h"
namespace brightray {
using content::BrowserThread;
using content::MediaStreamDevices;
namespace {
// Finds a device in |devices| that has |device_id|, or NULL if not found.
const content::MediaStreamDevice* FindDeviceWithId(
const content::MediaStreamDevices& devices,
const std::string& device_id) {
auto iter = devices.begin();
for (; iter != devices.end(); ++iter) {
if (iter->id == device_id) {
return &(*iter);
}
}
return nullptr;
}
const MediaStreamDevices& EmptyDevices() {
static MediaStreamDevices* devices = new MediaStreamDevices;
return *devices;
}
} // namespace
MediaCaptureDevicesDispatcher* MediaCaptureDevicesDispatcher::GetInstance() {
return base::Singleton<MediaCaptureDevicesDispatcher>::get();
}
MediaCaptureDevicesDispatcher::MediaCaptureDevicesDispatcher()
: is_device_enumeration_disabled_(false) {
// MediaCaptureDevicesDispatcher is a singleton. It should be created on
// UI thread.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
MediaCaptureDevicesDispatcher::~MediaCaptureDevicesDispatcher() {}
const MediaStreamDevices&
MediaCaptureDevicesDispatcher::GetAudioCaptureDevices() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (is_device_enumeration_disabled_)
return EmptyDevices();
return content::MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices();
}
const MediaStreamDevices&
MediaCaptureDevicesDispatcher::GetVideoCaptureDevices() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (is_device_enumeration_disabled_)
return EmptyDevices();
return content::MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices();
}
void MediaCaptureDevicesDispatcher::GetDefaultDevices(
bool audio,
bool video,
content::MediaStreamDevices* devices) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(audio || video);
if (audio) {
const content::MediaStreamDevice* device = GetFirstAvailableAudioDevice();
if (device)
devices->push_back(*device);
}
if (video) {
const content::MediaStreamDevice* device = GetFirstAvailableVideoDevice();
if (device)
devices->push_back(*device);
}
}
const content::MediaStreamDevice*
MediaCaptureDevicesDispatcher::GetRequestedAudioDevice(
const std::string& requested_audio_device_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const content::MediaStreamDevices& audio_devices = GetAudioCaptureDevices();
const content::MediaStreamDevice* const device =
FindDeviceWithId(audio_devices, requested_audio_device_id);
return device;
}
const content::MediaStreamDevice*
MediaCaptureDevicesDispatcher::GetFirstAvailableAudioDevice() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const content::MediaStreamDevices& audio_devices = GetAudioCaptureDevices();
if (audio_devices.empty())
return nullptr;
return &(*audio_devices.begin());
}
const content::MediaStreamDevice*
MediaCaptureDevicesDispatcher::GetRequestedVideoDevice(
const std::string& requested_video_device_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const content::MediaStreamDevices& video_devices = GetVideoCaptureDevices();
const content::MediaStreamDevice* const device =
FindDeviceWithId(video_devices, requested_video_device_id);
return device;
}
const content::MediaStreamDevice*
MediaCaptureDevicesDispatcher::GetFirstAvailableVideoDevice() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const content::MediaStreamDevices& video_devices = GetVideoCaptureDevices();
if (video_devices.empty())
return nullptr;
return &(*video_devices.begin());
}
void MediaCaptureDevicesDispatcher::DisableDeviceEnumerationForTesting() {
is_device_enumeration_disabled_ = true;
}
void MediaCaptureDevicesDispatcher::OnAudioCaptureDevicesChanged() {
}
void MediaCaptureDevicesDispatcher::OnVideoCaptureDevicesChanged() {
}
void MediaCaptureDevicesDispatcher::OnMediaRequestStateChanged(
int render_process_id,
int render_view_id,
int page_request_id,
const GURL& security_origin,
content::MediaStreamType stream_type,
content::MediaRequestState state) {
}
void MediaCaptureDevicesDispatcher::OnCreatingAudioStream(
int render_process_id,
int render_view_id) {
}
void MediaCaptureDevicesDispatcher::OnSetCapturingLinkSecured(
int render_process_id,
int render_frame_id,
int page_request_id,
content::MediaStreamType stream_type,
bool is_secure) {
}
} // namespace brightray

View file

@ -0,0 +1,84 @@
// 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-CHROMIUM file.
#ifndef BRIGHTRAY_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_
#define BRIGHTRAY_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_
#include "base/callback.h"
#include "base/memory/singleton.h"
#include "content/public/browser/media_observer.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/common/media_stream_request.h"
namespace brightray {
// This singleton is used to receive updates about media events from the content
// layer.
class MediaCaptureDevicesDispatcher : public content::MediaObserver {
public:
static MediaCaptureDevicesDispatcher* GetInstance();
// Methods for observers. Called on UI thread.
const content::MediaStreamDevices& GetAudioCaptureDevices();
const content::MediaStreamDevices& GetVideoCaptureDevices();
// Helper to get the default devices which can be used by the media request.
// Uses the first available devices if the default devices are not available.
// If the return list is empty, it means there is no available device on the
// OS.
// Called on the UI thread.
void GetDefaultDevices(bool audio,
bool video,
content::MediaStreamDevices* devices);
// Helpers for picking particular requested devices, identified by raw id.
// If the device requested is not available it will return NULL.
const content::MediaStreamDevice*
GetRequestedAudioDevice(const std::string& requested_audio_device_id);
const content::MediaStreamDevice*
GetRequestedVideoDevice(const std::string& requested_video_device_id);
// Returns the first available audio or video device, or NULL if no devices
// are available.
const content::MediaStreamDevice* GetFirstAvailableAudioDevice();
const content::MediaStreamDevice* GetFirstAvailableVideoDevice();
// Unittests that do not require actual device enumeration should call this
// API on the singleton. It is safe to call this multiple times on the
// signleton.
void DisableDeviceEnumerationForTesting();
// Overridden from content::MediaObserver:
void OnAudioCaptureDevicesChanged() override;
void OnVideoCaptureDevicesChanged() override;
void OnMediaRequestStateChanged(
int render_process_id,
int render_view_id,
int page_request_id,
const GURL& security_origin,
content::MediaStreamType stream_type,
content::MediaRequestState state) override;
void OnCreatingAudioStream(int render_process_id,
int render_view_id) override;
void OnSetCapturingLinkSecured(int render_process_id,
int render_frame_id,
int page_request_id,
content::MediaStreamType stream_type,
bool is_secure) override;
private:
friend struct base::DefaultSingletonTraits<MediaCaptureDevicesDispatcher>;
MediaCaptureDevicesDispatcher();
virtual ~MediaCaptureDevicesDispatcher();
// Flag used by unittests to disable device enumeration.
bool is_device_enumeration_disabled_;
DISALLOW_COPY_AND_ASSIGN(MediaCaptureDevicesDispatcher);
};
} // namespace brightray
#endif // BRIGHTRAY_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_

View file

@ -0,0 +1,53 @@
// Copyright 2013 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 "browser/media/media_device_id_salt.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_context.h"
using content::BrowserThread;
namespace brightray {
namespace {
const char kMediaDeviceIdSalt[] = "brightray.media.device_id_salt";
} // namespace
MediaDeviceIDSalt::MediaDeviceIDSalt(PrefService* pref_service) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
media_device_id_salt_.Init(kMediaDeviceIdSalt, pref_service);
if (media_device_id_salt_.GetValue().empty()) {
media_device_id_salt_.SetValue(
content::ResourceContext::CreateRandomMediaDeviceIDSalt());
}
}
MediaDeviceIDSalt::~MediaDeviceIDSalt() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
media_device_id_salt_.Destroy();
}
std::string MediaDeviceIDSalt::GetSalt() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
return media_device_id_salt_.GetValue();
}
// static
void MediaDeviceIDSalt::RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterStringPref(kMediaDeviceIdSalt, std::string());
}
// static
void MediaDeviceIDSalt::Reset(PrefService* pref_service) {
pref_service->SetString(
kMediaDeviceIdSalt,
content::ResourceContext::CreateRandomMediaDeviceIDSalt());
}
} // namespace brightray

View file

@ -0,0 +1,40 @@
// Copyright 2013 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.
#ifndef BRIGHTRAY_BROWSER_MEDIA_MEDIA_DEVICE_ID_SALT_H_
#define BRIGHTRAY_BROWSER_MEDIA_MEDIA_DEVICE_ID_SALT_H_
#include <string>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "components/prefs/pref_member.h"
class PrefRegistrySimple;
class PrefService;
namespace brightray {
// MediaDeviceIDSalt is responsible for creating and retrieving a salt string
// that is used for creating MediaSource IDs that can be cached by a web
// service. If the cache is cleared, the MediaSourceIds are invalidated.
class MediaDeviceIDSalt {
public:
explicit MediaDeviceIDSalt(PrefService* pref_service);
~MediaDeviceIDSalt();
std::string GetSalt();
static void RegisterPrefs(PrefRegistrySimple* pref_registry);
static void Reset(PrefService* pref_service);
private:
StringPrefMember media_device_id_salt_;
DISALLOW_COPY_AND_ASSIGN(MediaDeviceIDSalt);
};
} // namespace brightray
#endif // BRIGHTRAY_BROWSER_MEDIA_MEDIA_DEVICE_ID_SALT_H_

View file

@ -0,0 +1,198 @@
// 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-CHROMIUM file.
#include "browser/media/media_stream_devices_controller.h"
#include "browser/media/media_capture_devices_dispatcher.h"
#include "content/public/browser/desktop_media_id.h"
#include "content/public/common/media_stream_request.h"
namespace brightray {
namespace {
bool HasAnyAvailableDevice() {
const content::MediaStreamDevices& audio_devices =
MediaCaptureDevicesDispatcher::GetInstance()->GetAudioCaptureDevices();
const content::MediaStreamDevices& video_devices =
MediaCaptureDevicesDispatcher::GetInstance()->GetVideoCaptureDevices();
return !audio_devices.empty() || !video_devices.empty();
}
} // namespace
MediaStreamDevicesController::MediaStreamDevicesController(
const content::MediaStreamRequest& request,
const content::MediaResponseCallback& callback)
: request_(request),
callback_(callback),
// For MEDIA_OPEN_DEVICE requests (Pepper) we always request both webcam
// and microphone to avoid popping two infobars.
microphone_requested_(
request.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE ||
request.request_type == content::MEDIA_OPEN_DEVICE_PEPPER_ONLY),
webcam_requested_(
request.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE ||
request.request_type == content::MEDIA_OPEN_DEVICE_PEPPER_ONLY) {
}
MediaStreamDevicesController::~MediaStreamDevicesController() {
if (!callback_.is_null()) {
callback_.Run(content::MediaStreamDevices(),
content::MEDIA_DEVICE_INVALID_STATE,
std::unique_ptr<content::MediaStreamUI>());
}
}
bool MediaStreamDevicesController::TakeAction() {
// Do special handling of desktop screen cast.
if (request_.audio_type == content::MEDIA_TAB_AUDIO_CAPTURE ||
request_.video_type == content::MEDIA_TAB_VIDEO_CAPTURE ||
request_.audio_type == content::MEDIA_DESKTOP_AUDIO_CAPTURE ||
request_.video_type == content::MEDIA_DESKTOP_VIDEO_CAPTURE) {
HandleUserMediaRequest();
return true;
}
// Deny the request if there is no device attached to the OS.
if (!HasAnyAvailableDevice()) {
Deny(content::MEDIA_DEVICE_NO_HARDWARE);
return true;
}
Accept();
return true;
}
void MediaStreamDevicesController::Accept() {
// Get the default devices for the request.
content::MediaStreamDevices devices;
if (microphone_requested_ || webcam_requested_) {
switch (request_.request_type) {
case content::MEDIA_OPEN_DEVICE_PEPPER_ONLY: {
const content::MediaStreamDevice* device = nullptr;
// For open device request pick the desired device or fall back to the
// first available of the given type.
if (request_.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE) {
device = MediaCaptureDevicesDispatcher::GetInstance()->
GetRequestedAudioDevice(request_.requested_audio_device_id);
// TODO(wjia): Confirm this is the intended behavior.
if (!device) {
device = MediaCaptureDevicesDispatcher::GetInstance()->
GetFirstAvailableAudioDevice();
}
} else if (request_.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE) {
// Pepper API opens only one device at a time.
device = MediaCaptureDevicesDispatcher::GetInstance()->
GetRequestedVideoDevice(request_.requested_video_device_id);
// TODO(wjia): Confirm this is the intended behavior.
if (!device) {
device = MediaCaptureDevicesDispatcher::GetInstance()->
GetFirstAvailableVideoDevice();
}
}
if (device)
devices.push_back(*device);
break;
} case content::MEDIA_GENERATE_STREAM: {
bool needs_audio_device = microphone_requested_;
bool needs_video_device = webcam_requested_;
// Get the exact audio or video device if an id is specified.
if (!request_.requested_audio_device_id.empty()) {
const content::MediaStreamDevice* audio_device =
MediaCaptureDevicesDispatcher::GetInstance()->
GetRequestedAudioDevice(request_.requested_audio_device_id);
if (audio_device) {
devices.push_back(*audio_device);
needs_audio_device = false;
}
}
if (!request_.requested_video_device_id.empty()) {
const content::MediaStreamDevice* video_device =
MediaCaptureDevicesDispatcher::GetInstance()->
GetRequestedVideoDevice(request_.requested_video_device_id);
if (video_device) {
devices.push_back(*video_device);
needs_video_device = false;
}
}
// If either or both audio and video devices were requested but not
// specified by id, get the default devices.
if (needs_audio_device || needs_video_device) {
MediaCaptureDevicesDispatcher::GetInstance()->
GetDefaultDevices(needs_audio_device,
needs_video_device,
&devices);
}
break;
} case content::MEDIA_DEVICE_ACCESS:
// Get the default devices for the request.
MediaCaptureDevicesDispatcher::GetInstance()->
GetDefaultDevices(microphone_requested_,
webcam_requested_,
&devices);
break;
}
}
content::MediaResponseCallback cb = callback_;
callback_.Reset();
cb.Run(devices, content::MEDIA_DEVICE_OK,
std::unique_ptr<content::MediaStreamUI>());
}
void MediaStreamDevicesController::Deny(
content::MediaStreamRequestResult result) {
content::MediaResponseCallback cb = callback_;
callback_.Reset();
cb.Run(content::MediaStreamDevices(),
result,
std::unique_ptr<content::MediaStreamUI>());
}
void MediaStreamDevicesController::HandleUserMediaRequest() {
content::MediaStreamDevices devices;
if (request_.audio_type == content::MEDIA_TAB_AUDIO_CAPTURE) {
devices.push_back(content::MediaStreamDevice(
content::MEDIA_TAB_AUDIO_CAPTURE, "", ""));
}
if (request_.video_type == content::MEDIA_TAB_VIDEO_CAPTURE) {
devices.push_back(content::MediaStreamDevice(
content::MEDIA_TAB_VIDEO_CAPTURE, "", ""));
}
if (request_.audio_type == content::MEDIA_DESKTOP_AUDIO_CAPTURE) {
devices.push_back(content::MediaStreamDevice(
content::MEDIA_DESKTOP_AUDIO_CAPTURE, "loopback", "System Audio"));
}
if (request_.video_type == content::MEDIA_DESKTOP_VIDEO_CAPTURE) {
content::DesktopMediaID screen_id;
// If the device id wasn't specified then this is a screen capture request
// (i.e. chooseDesktopMedia() API wasn't used to generate device id).
if (request_.requested_video_device_id.empty()) {
screen_id = content::DesktopMediaID(content::DesktopMediaID::TYPE_SCREEN,
-1 /* kFullDesktopScreenId */);
} else {
screen_id =
content::DesktopMediaID::Parse(request_.requested_video_device_id);
}
devices.push_back(
content::MediaStreamDevice(content::MEDIA_DESKTOP_VIDEO_CAPTURE,
screen_id.ToString(), "Screen"));
}
content::MediaResponseCallback cb = callback_;
callback_.Reset();
cb.Run(devices,
devices.empty() ? content::MEDIA_DEVICE_INVALID_STATE :
content::MEDIA_DEVICE_OK,
std::unique_ptr<content::MediaStreamUI>());
}
} // namespace brightray

View file

@ -0,0 +1,47 @@
// 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-CHROMIUM file.
#ifndef BRIGHTRAY_BROWSER_MEDIA_MEDIA_STREAM_DEVICES_CONTROLLER_H_
#define BRIGHTRAY_BROWSER_MEDIA_MEDIA_STREAM_DEVICES_CONTROLLER_H_
#include <string>
#include "content/public/browser/web_contents_delegate.h"
namespace brightray {
class MediaStreamDevicesController {
public:
MediaStreamDevicesController(const content::MediaStreamRequest& request,
const content::MediaResponseCallback& callback);
virtual ~MediaStreamDevicesController();
// Accept or deny the request based on the default policy.
bool TakeAction();
// Explicitly accept or deny the request.
void Accept();
void Deny(content::MediaStreamRequestResult result);
private:
// Handle the request of desktop or tab screen cast.
void HandleUserMediaRequest();
// The original request for access to devices.
const content::MediaStreamRequest request_;
// The callback that needs to be Run to notify WebRTC of whether access to
// audio/video devices was granted or not.
content::MediaResponseCallback callback_;
bool microphone_requested_;
bool webcam_requested_;
DISALLOW_COPY_AND_ASSIGN(MediaStreamDevicesController);
};
} // namespace brightray
#endif // BRIGHTRAY_BROWSER_MEDIA_MEDIA_STREAM_DEVICES_CONTROLLER_H_

View file

@ -0,0 +1,35 @@
// Copyright 2014 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 "browser/net/devtools_network_conditions.h"
namespace brightray {
DevToolsNetworkConditions::DevToolsNetworkConditions(bool offline)
: offline_(offline),
latency_(0),
download_throughput_(0),
upload_throughput_(0) {
}
DevToolsNetworkConditions::DevToolsNetworkConditions(
bool offline,
double latency,
double download_throughput,
double upload_throughput)
: offline_(offline),
latency_(latency),
download_throughput_(download_throughput),
upload_throughput_(upload_throughput) {
}
DevToolsNetworkConditions::~DevToolsNetworkConditions() {
}
bool DevToolsNetworkConditions::IsThrottling() const {
return !offline_ && ((latency_ != 0.0) || (download_throughput_ != 0.0) ||
(upload_throughput_ != 0.0));
}
} // namespace brightray

View file

@ -0,0 +1,43 @@
// Copyright 2014 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.
#ifndef BROWSER_DEVTOOLS_NETWORK_CONDITIONS_H_
#define BROWSER_DEVTOOLS_NETWORK_CONDITIONS_H_
#include <string>
#include <vector>
#include "base/macros.h"
#include "url/gurl.h"
namespace brightray {
class DevToolsNetworkConditions {
public:
explicit DevToolsNetworkConditions(bool offline);
DevToolsNetworkConditions(bool offline,
double latency,
double download_throughput,
double upload_throughput);
~DevToolsNetworkConditions();
bool IsThrottling() const;
bool offline() const { return offline_; }
double latency() const { return latency_; }
double download_throughput() const { return download_throughput_; }
double upload_throughput() const { return upload_throughput_; }
private:
const bool offline_;
const double latency_;
const double download_throughput_;
const double upload_throughput_;
DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkConditions);
};
} // namespace brightray
#endif // BROWSER_DEVTOOLS_NETWORK_CONDITIONS_H_

View file

@ -0,0 +1,79 @@
// Copyright (c) 2015 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-CHROMIUM file.
#include "browser/net/devtools_network_controller.h"
#include "browser/net/devtools_network_conditions.h"
#include "browser/net/devtools_network_interceptor.h"
#include "browser/net/devtools_network_transaction.h"
#include "base/bind.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace brightray {
DevToolsNetworkController::DevToolsNetworkController()
: appcache_interceptor_(new DevToolsNetworkInterceptor) {
}
DevToolsNetworkController::~DevToolsNetworkController() {
}
void DevToolsNetworkController::SetNetworkState(
const std::string& client_id,
std::unique_ptr<DevToolsNetworkConditions> conditions) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
auto it = interceptors_.find(client_id);
if (it == interceptors_.end()) {
if (!conditions)
return;
std::unique_ptr<DevToolsNetworkInterceptor> new_interceptor(
new DevToolsNetworkInterceptor);
new_interceptor->UpdateConditions(std::move(conditions));
interceptors_[client_id] = std::move(new_interceptor);
} else {
if (!conditions) {
std::unique_ptr<DevToolsNetworkConditions> online_conditions(
new DevToolsNetworkConditions(false));
it->second->UpdateConditions(std::move(online_conditions));
interceptors_.erase(client_id);
} else {
it->second->UpdateConditions(std::move(conditions));
}
}
bool has_offline_interceptors = false;
for (const auto& interceptor : interceptors_) {
if (interceptor.second->IsOffline()) {
has_offline_interceptors = true;
break;
}
}
bool is_appcache_offline = appcache_interceptor_->IsOffline();
if (is_appcache_offline != has_offline_interceptors) {
std::unique_ptr<DevToolsNetworkConditions> appcache_conditions(
new DevToolsNetworkConditions(has_offline_interceptors));
appcache_interceptor_->UpdateConditions(std::move(appcache_conditions));
}
}
DevToolsNetworkInterceptor*
DevToolsNetworkController::GetInterceptor(const std::string& client_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (interceptors_.empty() || client_id.empty())
return nullptr;
auto it = interceptors_.find(client_id);
if (it == interceptors_.end())
return nullptr;
return it->second.get();
}
} // namespace brightray

View file

@ -0,0 +1,44 @@
// Copyright (c) 2015 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-CHROMIUM file.
#ifndef BROWSER_DEVTOOLS_NETWORK_CONTROLLER_H_
#define BROWSER_DEVTOOLS_NETWORK_CONTROLLER_H_
#include <unordered_map>
#include <memory>
#include <string>
#include "base/macros.h"
#include "base/threading/thread_checker.h"
namespace brightray {
class DevToolsNetworkConditions;
class DevToolsNetworkInterceptor;
class DevToolsNetworkTransaction;
class DevToolsNetworkController {
public:
DevToolsNetworkController();
virtual ~DevToolsNetworkController();
void SetNetworkState(const std::string& client_id,
std::unique_ptr<DevToolsNetworkConditions> conditions);
DevToolsNetworkInterceptor* GetInterceptor(const std::string& client_id);
private:
using InterceptorMap =
std::unordered_map<std::string,
std::unique_ptr<DevToolsNetworkInterceptor>>;
std::unique_ptr<DevToolsNetworkInterceptor> appcache_interceptor_;
InterceptorMap interceptors_;
DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkController);
};
} // namespace brightray
#endif // BROWSER_DEVTOOLS_NETWORK_CONTROLLER_H_

View file

@ -0,0 +1,60 @@
// Copyright (c) 2015 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-CHROMIUM file.
#include "browser/net/devtools_network_controller_handle.h"
#include "base/bind.h"
#include "browser/net/devtools_network_conditions.h"
#include "browser/net/devtools_network_controller.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace brightray {
DevToolsNetworkControllerHandle::DevToolsNetworkControllerHandle() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
}
DevToolsNetworkControllerHandle::~DevToolsNetworkControllerHandle() {
BrowserThread::DeleteSoon(BrowserThread::IO,
FROM_HERE,
controller_.release());
}
void DevToolsNetworkControllerHandle::SetNetworkState(
const std::string& client_id,
std::unique_ptr<DevToolsNetworkConditions> conditions) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&DevToolsNetworkControllerHandle::SetNetworkStateOnIO,
base::Unretained(this), client_id, base::Passed(&conditions)));
}
DevToolsNetworkController* DevToolsNetworkControllerHandle::GetController() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
LazyInitialize();
return controller_.get();
}
void DevToolsNetworkControllerHandle::LazyInitialize() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (!controller_)
controller_.reset(new DevToolsNetworkController);
}
void DevToolsNetworkControllerHandle::SetNetworkStateOnIO(
const std::string& client_id,
std::unique_ptr<DevToolsNetworkConditions> conditions) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
LazyInitialize();
controller_->SetNetworkState(client_id, std::move(conditions));
}
} // namespace brightray

View file

@ -0,0 +1,45 @@
// Copyright (c) 2015 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-CHROMIUM file.
#ifndef BROWSER_DEVTOOLS_NETWORK_CONTROLLER_HANDLE_H_
#define BROWSER_DEVTOOLS_NETWORK_CONTROLLER_HANDLE_H_
#include <memory>
#include <string>
#include "base/macros.h"
namespace brightray {
class DevToolsNetworkConditions;
class DevToolsNetworkController;
// A handle to manage an IO-thread DevToolsNetworkController on the IO thread
// while allowing SetNetworkState to be called from the UI thread.
class DevToolsNetworkControllerHandle {
public:
DevToolsNetworkControllerHandle();
~DevToolsNetworkControllerHandle();
// Called on the UI thread.
void SetNetworkState(const std::string& client_id,
std::unique_ptr<DevToolsNetworkConditions> conditions);
// Called on the IO thread.
DevToolsNetworkController* GetController();
private:
void LazyInitialize();
void SetNetworkStateOnIO(
const std::string& client_id,
std::unique_ptr<DevToolsNetworkConditions> conditions);
std::unique_ptr<DevToolsNetworkController> controller_;
DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkControllerHandle);
};
} // namespace brightray
#endif // BROWSER_DEVTOOLS_NETWORK_CONTROLLER_HANDLE_H_

View file

@ -0,0 +1,293 @@
// Copyright 2014 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 "browser/net/devtools_network_interceptor.h"
#include <algorithm>
#include <limits>
#include "base/time/time.h"
#include "browser/net/devtools_network_conditions.h"
#include "net/base/net_errors.h"
namespace brightray {
namespace {
int64_t kPacketSize = 1500;
base::TimeDelta CalculateTickLength(double throughput) {
if (!throughput)
return base::TimeDelta();
int64_t us_tick_length = (1000000L * kPacketSize) / throughput;
if (us_tick_length == 0)
us_tick_length = 1;
return base::TimeDelta::FromMicroseconds(us_tick_length);
}
} // namespace
DevToolsNetworkInterceptor::ThrottleRecord::ThrottleRecord() {
}
DevToolsNetworkInterceptor::ThrottleRecord::ThrottleRecord(
const ThrottleRecord& other) = default;
DevToolsNetworkInterceptor::ThrottleRecord::~ThrottleRecord() {
}
DevToolsNetworkInterceptor::DevToolsNetworkInterceptor()
: conditions_(new DevToolsNetworkConditions(false)),
download_last_tick_(0),
upload_last_tick_(0),
weak_ptr_factory_(this) {
}
DevToolsNetworkInterceptor::~DevToolsNetworkInterceptor() {
}
base::WeakPtr<DevToolsNetworkInterceptor>
DevToolsNetworkInterceptor::GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
void DevToolsNetworkInterceptor::FinishRecords(
ThrottleRecords* records, bool offline) {
ThrottleRecords temp;
temp.swap(*records);
for (const ThrottleRecord& record : temp) {
bool failed = offline && !record.is_upload;
record.callback.Run(
failed ? net::ERR_INTERNET_DISCONNECTED : record.result,
record.bytes);
}
}
void DevToolsNetworkInterceptor::UpdateConditions(
std::unique_ptr<DevToolsNetworkConditions> conditions) {
DCHECK(conditions);
base::TimeTicks now = base::TimeTicks::Now();
if (conditions_->IsThrottling())
UpdateThrottled(now);
conditions_ = std::move(conditions);
bool offline = conditions_->offline();
if (offline || !conditions_->IsThrottling()) {
timer_.Stop();
FinishRecords(&download_, offline);
FinishRecords(&upload_, offline);
FinishRecords(&suspended_, offline);
return;
}
// Throttling.
DCHECK(conditions_->download_throughput() != 0 ||
conditions_->upload_throughput() != 0);
offset_ = now;
download_last_tick_ = 0;
download_tick_length_ = CalculateTickLength(
conditions_->download_throughput());
upload_last_tick_ = 0;
upload_tick_length_ = CalculateTickLength(conditions_->upload_throughput());
latency_length_ = base::TimeDelta();
double latency = conditions_->latency();
if (latency > 0)
latency_length_ = base::TimeDelta::FromMillisecondsD(latency);
ArmTimer(now);
}
uint64_t DevToolsNetworkInterceptor::UpdateThrottledRecords(
base::TimeTicks now,
ThrottleRecords* records,
uint64_t last_tick,
base::TimeDelta tick_length) {
if (tick_length.is_zero()) {
DCHECK(records->empty());
return last_tick;
}
int64_t new_tick = (now - offset_) / tick_length;
int64_t ticks = new_tick - last_tick;
int64_t length = records->size();
if (!length)
return new_tick;
int64_t shift = ticks % length;
for (int64_t i = 0; i < length; ++i) {
(*records)[i].bytes -=
(ticks / length) * kPacketSize + (i < shift ? kPacketSize : 0);
}
std::rotate(records->begin(), records->begin() + shift, records->end());
return new_tick;
}
void DevToolsNetworkInterceptor::UpdateThrottled(base::TimeTicks now) {
download_last_tick_ = UpdateThrottledRecords(
now, &download_, download_last_tick_, download_tick_length_);
upload_last_tick_ = UpdateThrottledRecords(
now, &upload_, upload_last_tick_, upload_tick_length_);
UpdateSuspended(now);
}
void DevToolsNetworkInterceptor::UpdateSuspended(base::TimeTicks now) {
int64_t activation_baseline =
(now - latency_length_ - base::TimeTicks()).InMicroseconds();
ThrottleRecords suspended;
for (const ThrottleRecord& record : suspended_) {
if (record.send_end <= activation_baseline) {
if (record.is_upload)
upload_.push_back(record);
else
download_.push_back(record);
} else {
suspended.push_back(record);
}
}
suspended_.swap(suspended);
}
void DevToolsNetworkInterceptor::CollectFinished(
ThrottleRecords* records, ThrottleRecords* finished) {
ThrottleRecords active;
for (const ThrottleRecord& record : *records) {
if (record.bytes < 0)
finished->push_back(record);
else
active.push_back(record);
}
records->swap(active);
}
void DevToolsNetworkInterceptor::OnTimer() {
base::TimeTicks now = base::TimeTicks::Now();
UpdateThrottled(now);
ThrottleRecords finished;
CollectFinished(&download_, &finished);
CollectFinished(&upload_, &finished);
for (const ThrottleRecord& record : finished)
record.callback.Run(record.result, record.bytes);
ArmTimer(now);
}
base::TimeTicks DevToolsNetworkInterceptor::CalculateDesiredTime(
const ThrottleRecords& records,
uint64_t last_tick,
base::TimeDelta tick_length) {
int64_t min_ticks_left = 0x10000L;
size_t count = records.size();
for (size_t i = 0; i < count; ++i) {
int64_t packets_left = (records[i].bytes + kPacketSize - 1) / kPacketSize;
int64_t ticks_left = (i + 1) + count * (packets_left - 1);
if (i == 0 || ticks_left < min_ticks_left)
min_ticks_left = ticks_left;
}
return offset_ + tick_length * (last_tick + min_ticks_left);
}
void DevToolsNetworkInterceptor::ArmTimer(base::TimeTicks now) {
size_t suspend_count = suspended_.size();
if (download_.empty() && upload_.empty() && !suspend_count) {
timer_.Stop();
return;
}
base::TimeTicks desired_time = CalculateDesiredTime(
download_, download_last_tick_, download_tick_length_);
if (desired_time == offset_) {
FinishRecords(&download_, false);
}
base::TimeTicks upload_time = CalculateDesiredTime(
upload_, upload_last_tick_, upload_tick_length_);
if (upload_time != offset_ && upload_time < desired_time)
desired_time = upload_time;
int64_t min_baseline = std::numeric_limits<int64_t>::max();
for (size_t i = 0; i < suspend_count; ++i) {
if (suspended_[i].send_end < min_baseline)
min_baseline = suspended_[i].send_end;
}
if (suspend_count) {
base::TimeTicks activation_time = base::TimeTicks() +
base::TimeDelta::FromMicroseconds(min_baseline) + latency_length_;
if (activation_time < desired_time)
desired_time = activation_time;
}
timer_.Start(
FROM_HERE, (desired_time - now).magnitude(),
base::Bind(&DevToolsNetworkInterceptor::OnTimer, base::Unretained(this)));
}
int DevToolsNetworkInterceptor::StartThrottle(
int result,
int64_t bytes,
base::TimeTicks send_end,
bool start,
bool is_upload,
const ThrottleCallback& callback) {
if (result < 0)
return result;
if (conditions_->offline())
return is_upload ? result : net::ERR_INTERNET_DISCONNECTED;
if ((is_upload && !conditions_->upload_throughput()) ||
(!is_upload && !conditions_->download_throughput())) {
return result;
}
ThrottleRecord record;
record.result = result;
record.bytes = bytes;
record.callback = callback;
record.is_upload = is_upload;
base::TimeTicks now = base::TimeTicks::Now();
UpdateThrottled(now);
if (start && latency_length_ != base::TimeDelta()) {
record.send_end = (send_end - base::TimeTicks()).InMicroseconds();
suspended_.push_back(record);
UpdateSuspended(now);
} else {
if (is_upload)
upload_.push_back(record);
else
download_.push_back(record);
}
ArmTimer(now);
return net::ERR_IO_PENDING;
}
void DevToolsNetworkInterceptor::StopThrottle(
const ThrottleCallback& callback) {
RemoveRecord(&download_, callback);
RemoveRecord(&upload_, callback);
RemoveRecord(&suspended_, callback);
}
void DevToolsNetworkInterceptor::RemoveRecord(
ThrottleRecords* records, const ThrottleCallback& callback) {
records->erase(
std::remove_if(records->begin(), records->end(),
[&callback](const ThrottleRecord& record){
return record.callback.Equals(callback);
}),
records->end());
}
bool DevToolsNetworkInterceptor::IsOffline() {
return conditions_->offline();
}
} // namespace brightray

View file

@ -0,0 +1,107 @@
// Copyright 2014 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.
#ifndef BROWSER_DEVTOOLS_NETWORK_INTERCEPTOR_H_
#define BROWSER_DEVTOOLS_NETWORK_INTERCEPTOR_H_
#include <string>
#include <utility>
#include <vector>
#include "base/macros.h"
#include "base/timer/timer.h"
namespace base {
class TimeDelta;
class TimeTicks;
}
namespace brightray {
class DevToolsNetworkConditions;
class DevToolsNetworkTransaction;
class DevToolsNetworkInterceptor {
public:
using ThrottleCallback = base::Callback<void(int, int64_t)>;
DevToolsNetworkInterceptor();
virtual ~DevToolsNetworkInterceptor();
base::WeakPtr<DevToolsNetworkInterceptor> GetWeakPtr();
// Applies network emulation configuration.
void UpdateConditions(std::unique_ptr<DevToolsNetworkConditions> conditions);
// Throttles with |is_upload == true| always succeed, even in offline mode.
int StartThrottle(int result,
int64_t bytes,
base::TimeTicks send_end,
bool start,
bool is_upload,
const ThrottleCallback& callback);
void StopThrottle(const ThrottleCallback& callback);
bool IsOffline();
private:
struct ThrottleRecord {
public:
ThrottleRecord();
ThrottleRecord(const ThrottleRecord& other);
~ThrottleRecord();
int result;
int64_t bytes;
int64_t send_end;
bool is_upload;
ThrottleCallback callback;
};
using ThrottleRecords = std::vector<ThrottleRecord>;
void FinishRecords(ThrottleRecords* records, bool offline);
uint64_t UpdateThrottledRecords(base::TimeTicks now,
ThrottleRecords* records,
uint64_t last_tick,
base::TimeDelta tick_length);
void UpdateThrottled(base::TimeTicks now);
void UpdateSuspended(base::TimeTicks now);
void CollectFinished(ThrottleRecords* records, ThrottleRecords* finished);
void OnTimer();
base::TimeTicks CalculateDesiredTime(const ThrottleRecords& records,
uint64_t last_tick,
base::TimeDelta tick_length);
void ArmTimer(base::TimeTicks now);
void RemoveRecord(ThrottleRecords* records, const ThrottleCallback& callback);
std::unique_ptr<DevToolsNetworkConditions> conditions_;
// Throttables suspended for a "latency" period.
ThrottleRecords suspended_;
// Throttables waiting for certain amount of transfer to be "accounted".
ThrottleRecords download_;
ThrottleRecords upload_;
base::OneShotTimer timer_;
base::TimeTicks offset_;
base::TimeDelta download_tick_length_;
base::TimeDelta upload_tick_length_;
base::TimeDelta latency_length_;
uint64_t download_last_tick_;
uint64_t upload_last_tick_;
base::WeakPtrFactory<DevToolsNetworkInterceptor> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkInterceptor);
};
} // namespace brightray
#endif // BROWSER_DEVTOOLS_NETWORK_INTERCEPTOR_H_

View file

@ -0,0 +1,172 @@
// Copyright (c) 2014 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-CHROMIUM file.
#include "browser/net/devtools_network_protocol_handler.h"
#include "browser/browser_context.h"
#include "browser/net/devtools_network_conditions.h"
#include "browser/net/devtools_network_controller.h"
#include "base/strings/stringprintf.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/devtools_agent_host.h"
namespace brightray {
namespace {
namespace params {
const char kDownloadThroughput[] = "downloadThroughput";
const char kLatency[] = "latency";
const char kOffline[] = "offline";
const char kUploadThroughput[] = "uploadThroughput";
const char kResult[] = "result";
const char kErrorCode[] = "code";
const char kErrorMessage[] = "message";
} // namespace params
const char kEmulateNetworkConditions[] = "Network.emulateNetworkConditions";
const char kCanEmulateNetworkConditions[] =
"Network.canEmulateNetworkConditions";
const char kId[] = "id";
const char kMethod[] = "method";
const char kParams[] = "params";
const char kError[] = "error";
// JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object
const int kErrorInvalidParams = -32602;
bool ParseCommand(const base::DictionaryValue* command,
int* id,
std::string* method,
const base::DictionaryValue** params) {
if (!command)
return false;
if (!command->GetInteger(kId, id) || *id < 0)
return false;
if (!command->GetString(kMethod, method))
return false;
if (!command->GetDictionary(kParams, params))
*params = nullptr;
return true;
}
std::unique_ptr<base::DictionaryValue>
CreateSuccessResponse(int id, std::unique_ptr<base::DictionaryValue> result) {
std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue);
response->SetInteger(kId, id);
response->Set(params::kResult, result.release());
return response;
}
std::unique_ptr<base::DictionaryValue>
CreateFailureResponse(int id, const std::string& param) {
std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue);
auto error_object = new base::DictionaryValue;
response->Set(kError, error_object);
error_object->SetInteger(params::kErrorCode, kErrorInvalidParams);
error_object->SetString(params::kErrorMessage,
base::StringPrintf("Missing or Invalid '%s' parameter", param.c_str()));
return response;
}
} // namespace
DevToolsNetworkProtocolHandler::DevToolsNetworkProtocolHandler() {
}
DevToolsNetworkProtocolHandler::~DevToolsNetworkProtocolHandler() {
}
base::DictionaryValue* DevToolsNetworkProtocolHandler::HandleCommand(
content::DevToolsAgentHost* agent_host,
base::DictionaryValue* command) {
int id = 0;
std::string method;
const base::DictionaryValue* params = nullptr;
if (!ParseCommand(command, &id, &method, &params))
return nullptr;
if (method == kEmulateNetworkConditions)
return EmulateNetworkConditions(agent_host, id, params).release();
if (method == kCanEmulateNetworkConditions)
return CanEmulateNetworkConditions(agent_host, id, params).release();
return nullptr;
}
void DevToolsNetworkProtocolHandler::DevToolsAgentStateChanged(
content::DevToolsAgentHost* agent_host,
bool attached) {
std::unique_ptr<DevToolsNetworkConditions> conditions;
if (attached)
conditions.reset(new DevToolsNetworkConditions(false));
UpdateNetworkState(agent_host, std::move(conditions));
}
std::unique_ptr<base::DictionaryValue>
DevToolsNetworkProtocolHandler::CanEmulateNetworkConditions(
content::DevToolsAgentHost* agent_host,
int id,
const base::DictionaryValue* params) {
std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue);
result->SetBoolean(params::kResult, true);
return CreateSuccessResponse(id, std::move(result));
}
std::unique_ptr<base::DictionaryValue>
DevToolsNetworkProtocolHandler::EmulateNetworkConditions(
content::DevToolsAgentHost* agent_host,
int id,
const base::DictionaryValue* params) {
bool offline = false;
if (!params || !params->GetBoolean(params::kOffline, &offline))
return CreateFailureResponse(id, params::kOffline);
double latency = 0.0;
if (!params->GetDouble(params::kLatency, &latency))
return CreateFailureResponse(id, params::kLatency);
if (latency < 0.0)
latency = 0.0;
double download_throughput = 0.0;
if (!params->GetDouble(params::kDownloadThroughput, &download_throughput))
return CreateFailureResponse(id, params::kDownloadThroughput);
if (download_throughput < 0.0)
download_throughput = 0.0;
double upload_throughput = 0.0;
if (!params->GetDouble(params::kUploadThroughput, &upload_throughput))
return CreateFailureResponse(id, params::kUploadThroughput);
if (upload_throughput < 0.0)
upload_throughput = 0.0;
std::unique_ptr<DevToolsNetworkConditions> conditions(
new DevToolsNetworkConditions(offline,
latency,
download_throughput,
upload_throughput));
UpdateNetworkState(agent_host, std::move(conditions));
return std::unique_ptr<base::DictionaryValue>();
}
void DevToolsNetworkProtocolHandler::UpdateNetworkState(
content::DevToolsAgentHost* agent_host,
std::unique_ptr<DevToolsNetworkConditions> conditions) {
auto browser_context =
static_cast<brightray::BrowserContext*>(agent_host->GetBrowserContext());
browser_context->network_controller_handle()->SetNetworkState(
agent_host->GetId(), std::move(conditions));
}
} // namespace brightray

View file

@ -0,0 +1,48 @@
// Copyright 2014 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.
#ifndef BROWSER_DEVTOOLS_NETWORK_PROTOCOL_HANDLER_H_
#define BROWSER_DEVTOOLS_NETWORK_PROTOCOL_HANDLER_H_
#include "base/macros.h"
#include "base/values.h"
namespace content {
class DevToolsAgentHost;
}
namespace brightray {
class DevToolsNetworkConditions;
class DevToolsNetworkProtocolHandler {
public:
DevToolsNetworkProtocolHandler();
~DevToolsNetworkProtocolHandler();
base::DictionaryValue* HandleCommand(
content::DevToolsAgentHost* agent_host,
base::DictionaryValue* command);
void DevToolsAgentStateChanged(content::DevToolsAgentHost* agent_host,
bool attached);
private:
std::unique_ptr<base::DictionaryValue> CanEmulateNetworkConditions(
content::DevToolsAgentHost* agent_host,
int command_id,
const base::DictionaryValue* params);
std::unique_ptr<base::DictionaryValue> EmulateNetworkConditions(
content::DevToolsAgentHost* agent_host,
int command_id,
const base::DictionaryValue* params);
void UpdateNetworkState(
content::DevToolsAgentHost* agent_host,
std::unique_ptr<DevToolsNetworkConditions> conditions);
DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkProtocolHandler);
};
} // namespace brightray
#endif // BROWSER_DEVTOOLS_NETWORK_PROTOCOL_HANDLER_H_

View file

@ -0,0 +1,299 @@
// Copyright (c) 2015 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-CHROMIUM file.
#include "browser/net/devtools_network_transaction.h"
#include "browser/net/devtools_network_controller.h"
#include "browser/net/devtools_network_upload_data_stream.h"
#include "net/base/load_timing_info.h"
#include "net/base/net_errors.h"
#include "net/base/upload_progress.h"
#include "net/http/http_network_transaction.h"
#include "net/http/http_request_info.h"
#include "net/socket/connection_attempts.h"
namespace brightray {
// static
const char
DevToolsNetworkTransaction::kDevToolsEmulateNetworkConditionsClientId[] =
"X-DevTools-Emulate-Network-Conditions-Client-Id";
DevToolsNetworkTransaction::DevToolsNetworkTransaction(
DevToolsNetworkController* controller,
std::unique_ptr<net::HttpTransaction> transaction)
: throttled_byte_count_(0),
controller_(controller),
transaction_(std::move(transaction)),
request_(nullptr),
failed_(false) {
DCHECK(controller);
}
DevToolsNetworkTransaction::~DevToolsNetworkTransaction() {
if (interceptor_ && !throttle_callback_.is_null())
interceptor_->StopThrottle(throttle_callback_);
}
void DevToolsNetworkTransaction::IOCallback(
const net::CompletionCallback& callback, bool start, int result) {
result = Throttle(callback, start, result);
if (result != net::ERR_IO_PENDING)
callback.Run(result);
}
int DevToolsNetworkTransaction::Throttle(
const net::CompletionCallback& callback, bool start, int result) {
if (failed_)
return net::ERR_INTERNET_DISCONNECTED;
if (!interceptor_ || result < 0)
return result;
base::TimeTicks send_end;
if (start) {
throttled_byte_count_ += transaction_->GetTotalReceivedBytes();
net::LoadTimingInfo load_timing_info;
if (GetLoadTimingInfo(&load_timing_info))
send_end = load_timing_info.send_end;
if (send_end.is_null())
send_end = base::TimeTicks::Now();
}
if (result > 0)
throttled_byte_count_ += result;
throttle_callback_ = base::Bind(&DevToolsNetworkTransaction::ThrottleCallback,
base::Unretained(this),
callback);
int rv = interceptor_->StartThrottle(result, throttled_byte_count_, send_end,
start, false, throttle_callback_);
if (rv != net::ERR_IO_PENDING)
throttle_callback_.Reset();
if (rv == net::ERR_INTERNET_DISCONNECTED)
Fail();
return rv;
}
void DevToolsNetworkTransaction::ThrottleCallback(
const net::CompletionCallback& callback, int result, int64_t bytes) {
DCHECK(!throttle_callback_.is_null());
throttle_callback_.Reset();
if (result == net::ERR_INTERNET_DISCONNECTED)
Fail();
throttled_byte_count_ = bytes;
callback.Run(result);
}
void DevToolsNetworkTransaction::Fail() {
DCHECK(request_);
DCHECK(!failed_);
failed_ = true;
transaction_->SetBeforeNetworkStartCallback(BeforeNetworkStartCallback());
if (interceptor_)
interceptor_.reset();
}
bool DevToolsNetworkTransaction::CheckFailed() {
if (failed_)
return true;
if (interceptor_ && interceptor_->IsOffline()) {
Fail();
return true;
}
return false;
}
int DevToolsNetworkTransaction::Start(const net::HttpRequestInfo* request,
const net::CompletionCallback& callback,
const net::NetLogWithSource& net_log) {
DCHECK(request);
request_ = request;
std::string client_id;
bool has_devtools_client_id = request_->extra_headers.HasHeader(
kDevToolsEmulateNetworkConditionsClientId);
if (has_devtools_client_id) {
custom_request_.reset(new net::HttpRequestInfo(*request_));
custom_request_->extra_headers.GetHeader(
kDevToolsEmulateNetworkConditionsClientId, &client_id);
custom_request_->extra_headers.RemoveHeader(
kDevToolsEmulateNetworkConditionsClientId);
if (request_->upload_data_stream) {
custom_upload_data_stream_.reset(
new DevToolsNetworkUploadDataStream(request_->upload_data_stream));
custom_request_->upload_data_stream = custom_upload_data_stream_.get();
}
request_ = custom_request_.get();
}
DevToolsNetworkInterceptor* interceptor =
controller_->GetInterceptor(client_id);
if (interceptor) {
interceptor_ = interceptor->GetWeakPtr();
if (custom_upload_data_stream_)
custom_upload_data_stream_->SetInterceptor(interceptor);
}
if (CheckFailed())
return net::ERR_INTERNET_DISCONNECTED;
if (!interceptor_)
return transaction_->Start(request_, callback, net_log);
int result = transaction_->Start(request_,
base::Bind(&DevToolsNetworkTransaction::IOCallback,
base::Unretained(this), callback, true),
net_log);
return Throttle(callback, true, result);
}
int DevToolsNetworkTransaction::RestartIgnoringLastError(
const net::CompletionCallback& callback) {
if (CheckFailed())
return net::ERR_INTERNET_DISCONNECTED;
if (!interceptor_)
return transaction_->RestartIgnoringLastError(callback);
int result = transaction_->RestartIgnoringLastError(
base::Bind(&DevToolsNetworkTransaction::IOCallback,
base::Unretained(this), callback, true));
return Throttle(callback, true, result);
}
int DevToolsNetworkTransaction::RestartWithCertificate(
net::X509Certificate* client_cert,
net::SSLPrivateKey* client_private_key,
const net::CompletionCallback& callback) {
if (CheckFailed())
return net::ERR_INTERNET_DISCONNECTED;
if (!interceptor_) {
return transaction_->RestartWithCertificate(
client_cert, client_private_key, callback);
}
int result = transaction_->RestartWithCertificate(
client_cert, client_private_key,
base::Bind(&DevToolsNetworkTransaction::IOCallback,
base::Unretained(this), callback, true));
return Throttle(callback, true, result);
}
int DevToolsNetworkTransaction::RestartWithAuth(
const net::AuthCredentials& credentials,
const net::CompletionCallback& callback) {
if (CheckFailed())
return net::ERR_INTERNET_DISCONNECTED;
if (!interceptor_)
return transaction_->RestartWithAuth(credentials, callback);
int result = transaction_->RestartWithAuth(credentials,
base::Bind(&DevToolsNetworkTransaction::IOCallback,
base::Unretained(this), callback, true));
return Throttle(callback, true, result);
}
bool DevToolsNetworkTransaction::IsReadyToRestartForAuth() {
return transaction_->IsReadyToRestartForAuth();
}
int DevToolsNetworkTransaction::Read(
net::IOBuffer* buf,
int buf_len,
const net::CompletionCallback& callback) {
if (CheckFailed())
return net::ERR_INTERNET_DISCONNECTED;
if (!interceptor_)
return transaction_->Read(buf, buf_len, callback);
int result = transaction_->Read(buf, buf_len,
base::Bind(&DevToolsNetworkTransaction::IOCallback,
base::Unretained(this), callback, false));
// URLRequestJob relies on synchronous end-of-stream notification.
if (result == 0)
return result;
return Throttle(callback, false, result);
}
void DevToolsNetworkTransaction::StopCaching() {
transaction_->StopCaching();
}
bool DevToolsNetworkTransaction::GetFullRequestHeaders(
net::HttpRequestHeaders* headers) const {
return transaction_->GetFullRequestHeaders(headers);
}
int64_t DevToolsNetworkTransaction::GetTotalReceivedBytes() const {
return transaction_->GetTotalReceivedBytes();
}
int64_t DevToolsNetworkTransaction::GetTotalSentBytes() const {
return transaction_->GetTotalSentBytes();
}
void DevToolsNetworkTransaction::DoneReading() {
transaction_->DoneReading();
}
const net::HttpResponseInfo*
DevToolsNetworkTransaction::GetResponseInfo() const {
return transaction_->GetResponseInfo();
}
net::LoadState DevToolsNetworkTransaction::GetLoadState() const {
return transaction_->GetLoadState();
}
void DevToolsNetworkTransaction::SetQuicServerInfo(
net::QuicServerInfo* info) {
transaction_->SetQuicServerInfo(info);
}
bool DevToolsNetworkTransaction::GetLoadTimingInfo(
net::LoadTimingInfo* info) const {
return transaction_->GetLoadTimingInfo(info);
}
bool DevToolsNetworkTransaction::GetRemoteEndpoint(
net::IPEndPoint* endpoint) const {
return transaction_->GetRemoteEndpoint(endpoint);
}
void DevToolsNetworkTransaction::PopulateNetErrorDetails(
net::NetErrorDetails* details) const {
return transaction_->PopulateNetErrorDetails(details);
}
void DevToolsNetworkTransaction::SetPriority(net::RequestPriority priority) {
transaction_->SetPriority(priority);
}
void DevToolsNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
net::WebSocketHandshakeStreamBase::CreateHelper* helper) {
transaction_->SetWebSocketHandshakeStreamCreateHelper(helper);
}
void DevToolsNetworkTransaction::SetBeforeNetworkStartCallback(
const BeforeNetworkStartCallback& callback) {
transaction_->SetBeforeNetworkStartCallback(callback);
}
void DevToolsNetworkTransaction::SetBeforeHeadersSentCallback(
const BeforeHeadersSentCallback& callback) {
transaction_->SetBeforeHeadersSentCallback(callback);
}
int DevToolsNetworkTransaction::ResumeNetworkStart() {
if (CheckFailed())
return net::ERR_INTERNET_DISCONNECTED;
return transaction_->ResumeNetworkStart();
}
void DevToolsNetworkTransaction::GetConnectionAttempts(
net::ConnectionAttempts* out) const {
transaction_->GetConnectionAttempts(out);
}
} // namespace brightray

View file

@ -0,0 +1,108 @@
// Copyright 2014 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.
#ifndef BROWSER_DEVTOOLS_NETWORK_TRANSACTION_H_
#define BROWSER_DEVTOOLS_NETWORK_TRANSACTION_H_
#include <stdint.h>
#include "base/memory/weak_ptr.h"
#include "browser/net/devtools_network_interceptor.h"
#include "net/base/completion_callback.h"
#include "net/base/load_states.h"
#include "net/base/request_priority.h"
#include "net/http/http_transaction.h"
#include "net/websockets/websocket_handshake_stream_base.h"
namespace brightray {
class DevToolsNetworkController;
class DevToolsNetworkUploadDataStream;
class DevToolsNetworkTransaction : public net::HttpTransaction {
public:
static const char kDevToolsEmulateNetworkConditionsClientId[];
DevToolsNetworkTransaction(
DevToolsNetworkController* controller,
std::unique_ptr<net::HttpTransaction> network_transaction);
~DevToolsNetworkTransaction() override;
// HttpTransaction methods:
int Start(const net::HttpRequestInfo* request,
const net::CompletionCallback& callback,
const net::NetLogWithSource& net_log) override;
int RestartIgnoringLastError(
const net::CompletionCallback& callback) override;
int RestartWithCertificate(net::X509Certificate* client_cert,
net::SSLPrivateKey* client_private_key,
const net::CompletionCallback& callback) override;
int RestartWithAuth(const net::AuthCredentials& credentials,
const net::CompletionCallback& callback) override;
bool IsReadyToRestartForAuth() override;
int Read(net::IOBuffer* buf,
int buf_len,
const net::CompletionCallback& callback) override;
void StopCaching() override;
bool GetFullRequestHeaders(net::HttpRequestHeaders* headers) const override;
int64_t GetTotalReceivedBytes() const override;
int64_t GetTotalSentBytes() const override;
void DoneReading() override;
const net::HttpResponseInfo* GetResponseInfo() const override;
net::LoadState GetLoadState() const override;
void SetQuicServerInfo(net::QuicServerInfo* quic_server_info) override;
bool GetLoadTimingInfo(net::LoadTimingInfo* load_timing_info) const override;
bool GetRemoteEndpoint(net::IPEndPoint* endpoint) const override;
void PopulateNetErrorDetails(net::NetErrorDetails* details) const override;
void SetPriority(net::RequestPriority priority) override;
void SetWebSocketHandshakeStreamCreateHelper(
net::WebSocketHandshakeStreamBase::CreateHelper* create_helper) override;
void SetBeforeNetworkStartCallback(
const BeforeNetworkStartCallback& callback) override;
void SetBeforeHeadersSentCallback(
const BeforeHeadersSentCallback& callback) override;
int ResumeNetworkStart() override;
void GetConnectionAttempts(net::ConnectionAttempts* out) const override;
private:
void Fail();
bool CheckFailed();
void IOCallback(const net::CompletionCallback& callback,
bool start,
int result);
int Throttle(const net::CompletionCallback& callback,
bool start,
int result);
void ThrottleCallback(const net::CompletionCallback& callback,
int result,
int64_t bytes);
DevToolsNetworkInterceptor::ThrottleCallback throttle_callback_;
int64_t throttled_byte_count_;
DevToolsNetworkController* controller_;
base::WeakPtr<DevToolsNetworkInterceptor> interceptor_;
// Modified upload data stream. Should be destructed after |custom_request_|.
std::unique_ptr<DevToolsNetworkUploadDataStream> custom_upload_data_stream_;
// Modified request. Should be destructed after |transaction_|.
std::unique_ptr<net::HttpRequestInfo> custom_request_;
// Original network transaction.
std::unique_ptr<net::HttpTransaction> transaction_;
const net::HttpRequestInfo* request_;
// True if Fail was already invoked.
bool failed_;
DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkTransaction);
};
} // namespace brightray
#endif // BROWSER_DEVTOOLS_NETWORK_TRANSACTION_H_

View file

@ -0,0 +1,51 @@
// Copyright (c) 2015 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-CHROMIUM file.
#include "browser/net/devtools_network_transaction_factory.h"
#include "browser/net/devtools_network_controller.h"
#include "browser/net/devtools_network_transaction.h"
#include "content/public/browser/service_worker_context.h"
#include "net/base/net_errors.h"
#include "net/http/http_network_layer.h"
#include "net/http/http_network_transaction.h"
namespace brightray {
DevToolsNetworkTransactionFactory::DevToolsNetworkTransactionFactory(
DevToolsNetworkController* controller,
net::HttpNetworkSession* session)
: controller_(controller),
network_layer_(new net::HttpNetworkLayer(session)) {
std::set<std::string> headers;
headers.insert(
DevToolsNetworkTransaction::kDevToolsEmulateNetworkConditionsClientId);
content::ServiceWorkerContext::AddExcludedHeadersForFetchEvent(headers);
}
DevToolsNetworkTransactionFactory::~DevToolsNetworkTransactionFactory() {
}
int DevToolsNetworkTransactionFactory::CreateTransaction(
net::RequestPriority priority,
std::unique_ptr<net::HttpTransaction>* transaction) {
std::unique_ptr<net::HttpTransaction> new_transaction;
int rv = network_layer_->CreateTransaction(priority, &new_transaction);
if (rv != net::OK)
return rv;
transaction->reset(
new DevToolsNetworkTransaction(controller_, std::move(new_transaction)));
return net::OK;
}
net::HttpCache* DevToolsNetworkTransactionFactory::GetCache() {
return network_layer_->GetCache();
}
net::HttpNetworkSession* DevToolsNetworkTransactionFactory::GetSession() {
return network_layer_->GetSession();
}
} // namespace brightray

View file

@ -0,0 +1,39 @@
// Copyright (c) 2015 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-CHROMIUM file.
#ifndef BROWSER_DEVTOOLS_NETWORK_TRANSACTION_FACTORY_H_
#define BROWSER_DEVTOOLS_NETWORK_TRANSACTION_FACTORY_H_
#include "base/macros.h"
#include "net/base/request_priority.h"
#include "net/http/http_transaction_factory.h"
namespace brightray {
class DevToolsNetworkController;
class DevToolsNetworkTransactionFactory : public net::HttpTransactionFactory {
public:
explicit DevToolsNetworkTransactionFactory(
DevToolsNetworkController* controller,
net::HttpNetworkSession* session);
~DevToolsNetworkTransactionFactory() override;
// net::HttpTransactionFactory:
int CreateTransaction(
net::RequestPriority priority,
std::unique_ptr<net::HttpTransaction>* transaction) override;
net::HttpCache* GetCache() override;
net::HttpNetworkSession* GetSession() override;
private:
DevToolsNetworkController* controller_;
std::unique_ptr<net::HttpTransactionFactory> network_layer_;
DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkTransactionFactory);
};
} // namespace brightray
#endif // BROWSER_DEVTOOLS_NETWORK_TRANSACTION_FACTORY_H_

View file

@ -0,0 +1,96 @@
// Copyright 2015 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 "browser/net/devtools_network_upload_data_stream.h"
#include "net/base/net_errors.h"
namespace brightray {
DevToolsNetworkUploadDataStream::DevToolsNetworkUploadDataStream(
net::UploadDataStream* upload_data_stream)
: net::UploadDataStream(upload_data_stream->is_chunked(),
upload_data_stream->identifier()),
throttle_callback_(
base::Bind(&DevToolsNetworkUploadDataStream::ThrottleCallback,
base::Unretained(this))),
throttled_byte_count_(0),
upload_data_stream_(upload_data_stream) {
}
DevToolsNetworkUploadDataStream::~DevToolsNetworkUploadDataStream() {
if (interceptor_)
interceptor_->StopThrottle(throttle_callback_);
}
void DevToolsNetworkUploadDataStream::SetInterceptor(
DevToolsNetworkInterceptor* interceptor) {
DCHECK(!interceptor_);
if (interceptor)
interceptor_ = interceptor->GetWeakPtr();
}
bool DevToolsNetworkUploadDataStream::IsInMemory() const {
return false;
}
int DevToolsNetworkUploadDataStream::InitInternal(
const net::NetLogWithSource& net_log) {
throttled_byte_count_ = 0;
int result = upload_data_stream_->Init(
base::Bind(&DevToolsNetworkUploadDataStream::StreamInitCallback,
base::Unretained(this)),
net_log);
if (result == net::OK && !is_chunked())
SetSize(upload_data_stream_->size());
return result;
}
void DevToolsNetworkUploadDataStream::StreamInitCallback(int result) {
if (!is_chunked())
SetSize(upload_data_stream_->size());
OnInitCompleted(result);
}
int DevToolsNetworkUploadDataStream::ReadInternal(
net::IOBuffer* buf, int buf_len) {
int result = upload_data_stream_->Read(buf, buf_len,
base::Bind(&DevToolsNetworkUploadDataStream::StreamReadCallback,
base::Unretained(this)));
return ThrottleRead(result);
}
void DevToolsNetworkUploadDataStream::StreamReadCallback(int result) {
result = ThrottleRead(result);
if (result != net::ERR_IO_PENDING)
OnReadCompleted(result);
}
int DevToolsNetworkUploadDataStream::ThrottleRead(int result) {
if (is_chunked() && upload_data_stream_->IsEOF())
SetIsFinalChunk();
if (!interceptor_ || result < 0)
return result;
if (result > 0)
throttled_byte_count_ += result;
return interceptor_->StartThrottle(result, throttled_byte_count_,
base::TimeTicks(), false, true, throttle_callback_);
}
void DevToolsNetworkUploadDataStream::ThrottleCallback(
int result, int64_t bytes) {
throttled_byte_count_ = bytes;
OnReadCompleted(result);
}
void DevToolsNetworkUploadDataStream::ResetInternal() {
upload_data_stream_->Reset();
throttled_byte_count_ = 0;
if (interceptor_)
interceptor_->StopThrottle(throttle_callback_);
}
} // namespace brightray

View file

@ -0,0 +1,51 @@
// Copyright 2015 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.
#ifndef BROWSER_DEVTOOLS_NETWORK_UPLOAD_DATA_STREAM_H_
#define BROWSER_DEVTOOLS_NETWORK_UPLOAD_DATA_STREAM_H_
#include <stdint.h>
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "browser/net/devtools_network_interceptor.h"
#include "net/base/completion_callback.h"
#include "net/base/upload_data_stream.h"
namespace brightray {
class DevToolsNetworkUploadDataStream : public net::UploadDataStream {
public:
// Supplied |upload_data_stream| must outlive this object.
explicit DevToolsNetworkUploadDataStream(
net::UploadDataStream* upload_data_stream);
~DevToolsNetworkUploadDataStream() override;
void SetInterceptor(DevToolsNetworkInterceptor* interceptor);
private:
// net::UploadDataStream implementation.
bool IsInMemory() const override;
int InitInternal(const net::NetLogWithSource& net_log) override;
int ReadInternal(net::IOBuffer* buf, int buf_len) override;
void ResetInternal() override;
void StreamInitCallback(int result);
void StreamReadCallback(int result);
int ThrottleRead(int result);
void ThrottleCallback(int result, int64_t bytes);
DevToolsNetworkInterceptor::ThrottleCallback throttle_callback_;
int64_t throttled_byte_count_;
net::UploadDataStream* upload_data_stream_;
base::WeakPtr<DevToolsNetworkInterceptor> interceptor_;
DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkUploadDataStream);
};
} // namespace brightray
#endif // BROWSER_DEVTOOLS_NETWORK_UPLOAD_DATA_STREAM_H_

View file

@ -0,0 +1,64 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "browser/net_log.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/values.h"
#include "content/public/common/content_switches.h"
#include "net/log/net_log_util.h"
namespace brightray {
namespace {
std::unique_ptr<base::DictionaryValue> GetConstants() {
std::unique_ptr<base::DictionaryValue> constants = net::GetNetConstants();
// Adding client information to constants dictionary.
auto* client_info = new base::DictionaryValue();
client_info->SetString(
"command_line",
base::CommandLine::ForCurrentProcess()->GetCommandLineString());
constants->Set("clientInfo", client_info);
return constants;
}
} // namespace
NetLog::NetLog() {
}
NetLog::~NetLog() {
}
void NetLog::StartLogging(net::URLRequestContext* url_request_context) {
auto command_line = base::CommandLine::ForCurrentProcess();
if (!command_line->HasSwitch(switches::kLogNetLog))
return;
base::FilePath log_path =
command_line->GetSwitchValuePath(switches::kLogNetLog);
#if defined(OS_WIN)
log_file_.reset(_wfopen(log_path.value().c_str(), L"w"));
#elif defined(OS_POSIX)
log_file_.reset(fopen(log_path.value().c_str(), "w"));
#endif
if (!log_file_) {
LOG(ERROR) << "Could not open file: " << log_path.value()
<< "for net logging";
return;
}
std::unique_ptr<base::Value> constants(GetConstants());
write_to_file_observer_.StartObserving(this,
std::move(log_file_),
constants.get(),
url_request_context);
}
} // namespace brightray

View file

@ -0,0 +1,30 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef BROWSER_NET_LOG_H_
#define BROWSER_NET_LOG_H_
#include "base/files/scoped_file.h"
#include "net/log/net_log.h"
#include "net/log/write_to_file_net_log_observer.h"
namespace brightray {
class NetLog : public net::NetLog {
public:
NetLog();
~NetLog() override;
void StartLogging(net::URLRequestContext* url_request_context);
private:
base::ScopedFILE log_file_;
net::WriteToFileNetLogObserver write_to_file_observer_;
DISALLOW_COPY_AND_ASSIGN(NetLog);
};
} // namespace brightray
#endif // BROWSER_NET_LOG_H_

View file

@ -0,0 +1,148 @@
// 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-CHROMIUM file.
#include "browser/network_delegate.h"
#include <string>
#include <vector>
#include "base/command_line.h"
#include "base/strings/string_split.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/url_request/url_request.h"
namespace brightray {
namespace {
// Ignore the limit of 6 connections per host.
const char kIgnoreConnectionsLimit[] = "ignore-connections-limit";
} // namespace
NetworkDelegate::NetworkDelegate() {
auto command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(kIgnoreConnectionsLimit)) {
std::string value =
command_line->GetSwitchValueASCII(kIgnoreConnectionsLimit);
ignore_connections_limit_domains_ = base::SplitString(
value, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
}
}
NetworkDelegate::~NetworkDelegate() {
}
int NetworkDelegate::OnBeforeURLRequest(
net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url) {
for (const auto& domain : ignore_connections_limit_domains_) {
if (request->url().DomainIs(domain)) {
// Allow unlimited concurrent connections.
request->SetPriority(net::MAXIMUM_PRIORITY);
request->SetLoadFlags(request->load_flags() | net::LOAD_IGNORE_LIMITS);
break;
}
}
return net::OK;
}
int NetworkDelegate::OnBeforeStartTransaction(
net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) {
return net::OK;
}
void NetworkDelegate::OnStartTransaction(
net::URLRequest* request,
const net::HttpRequestHeaders& headers) {
}
void NetworkDelegate::OnBeforeSendHeaders(
net::URLRequest* request,
const net::ProxyInfo& proxy_info,
const net::ProxyRetryInfoMap& proxy_retry_info,
net::HttpRequestHeaders* headers) {
}
int NetworkDelegate::OnHeadersReceived(
net::URLRequest* request,
const net::CompletionCallback& callback,
const net::HttpResponseHeaders* original_response_headers,
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
GURL* allowed_unsafe_redirect_url) {
return net::OK;
}
void NetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
const GURL& new_location) {
}
void NetworkDelegate::OnResponseStarted(net::URLRequest* request) {
}
void NetworkDelegate::OnNetworkBytesReceived(net::URLRequest* request,
int64_t bytes_read) {
}
void NetworkDelegate::OnNetworkBytesSent(net::URLRequest* request,
int64_t bytes_sent) {
}
void NetworkDelegate::OnCompleted(net::URLRequest* request, bool started) {
}
void NetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
}
void NetworkDelegate::OnPACScriptError(int line_number,
const base::string16& error) {
}
NetworkDelegate::AuthRequiredResponse NetworkDelegate::OnAuthRequired(
net::URLRequest* request,
const net::AuthChallengeInfo& auth_info,
const AuthCallback& callback,
net::AuthCredentials* credentials) {
return AUTH_REQUIRED_RESPONSE_NO_ACTION;
}
bool NetworkDelegate::OnCanGetCookies(const net::URLRequest& request,
const net::CookieList& cookie_list) {
return true;
}
bool NetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
const std::string& cookie_line,
net::CookieOptions* options) {
return true;
}
bool NetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
const base::FilePath& path) const {
return true;
}
bool NetworkDelegate::OnCanEnablePrivacyMode(
const GURL& url,
const GURL& first_party_for_cookies) const {
return false;
}
bool NetworkDelegate::OnAreExperimentalCookieFeaturesEnabled() const {
return true;
}
bool NetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
const net::URLRequest& request,
const GURL& target_url,
const GURL& referrer_url) const {
return false;
}
} // namespace brightray

View file

@ -0,0 +1,80 @@
// 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-CHROMIUM file.
#ifndef BRIGHTRAY_BROWSER_NETWORK_DELEGATE_H_
#define BRIGHTRAY_BROWSER_NETWORK_DELEGATE_H_
#include <string>
#include <vector>
#include "net/base/network_delegate.h"
#include "net/proxy/proxy_server.h"
namespace brightray {
class NetworkDelegate : public net::NetworkDelegate {
public:
NetworkDelegate();
virtual ~NetworkDelegate();
protected:
int OnBeforeURLRequest(net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url) override;
int OnBeforeStartTransaction(net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) override;
void OnBeforeSendHeaders(net::URLRequest* request,
const net::ProxyInfo& proxy_info,
const net::ProxyRetryInfoMap& proxy_retry_info,
net::HttpRequestHeaders* headers) override;
void OnStartTransaction(net::URLRequest* request,
const net::HttpRequestHeaders& headers) override;
int OnHeadersReceived(
net::URLRequest* request,
const net::CompletionCallback& callback,
const net::HttpResponseHeaders* original_response_headers,
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
GURL* allowed_unsafe_redirect_url) override;
void OnBeforeRedirect(net::URLRequest* request,
const GURL& new_location) override;
void OnResponseStarted(net::URLRequest* request) override;
void OnNetworkBytesReceived(net::URLRequest* request,
int64_t bytes_read) override;
void OnNetworkBytesSent(net::URLRequest* request,
int64_t bytes_sent) override;
void OnCompleted(net::URLRequest* request, bool started) override;
void OnURLRequestDestroyed(net::URLRequest* request) override;
void OnPACScriptError(int line_number,
const base::string16& error) override;
AuthRequiredResponse OnAuthRequired(
net::URLRequest* request,
const net::AuthChallengeInfo& auth_info,
const AuthCallback& callback,
net::AuthCredentials* credentials) override;
bool OnCanGetCookies(const net::URLRequest& request,
const net::CookieList& cookie_list) override;
bool OnCanSetCookie(const net::URLRequest& request,
const std::string& cookie_line,
net::CookieOptions* options) override;
bool OnCanAccessFile(const net::URLRequest& request,
const base::FilePath& path) const override;
bool OnCanEnablePrivacyMode(
const GURL& url,
const GURL& first_party_for_cookies) const override;
bool OnAreExperimentalCookieFeaturesEnabled() const override;
bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
const net::URLRequest& request,
const GURL& target_url,
const GURL& referrer_url) const override;
private:
std::vector<std::string> ignore_connections_limit_domains_;
DISALLOW_COPY_AND_ASSIGN(NetworkDelegate);
};
} // namespace brightray
#endif

View file

@ -0,0 +1,42 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "browser/notification.h"
#include "browser/notification_delegate.h"
#include "browser/notification_presenter.h"
namespace brightray {
Notification::Notification(NotificationDelegate* delegate,
NotificationPresenter* presenter)
: delegate_(delegate),
presenter_(presenter),
weak_factory_(this) {
}
Notification::~Notification() {
delegate()->NotificationDestroyed();
}
void Notification::NotificationClicked() {
delegate()->NotificationClick();
Destroy();
}
void Notification::NotificationDismissed() {
delegate()->NotificationClosed();
Destroy();
}
void Notification::NotificationFailed() {
delegate()->NotificationFailed();
Destroy();
}
void Notification::Destroy() {
presenter()->RemoveNotification(this);
}
} // namespace brightray

View file

@ -0,0 +1,65 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef BROWSER_NOTIFICATION_H_
#define BROWSER_NOTIFICATION_H_
#include "base/memory/weak_ptr.h"
#include "base/strings/string16.h"
class GURL;
class SkBitmap;
namespace brightray {
class NotificationDelegate;
class NotificationPresenter;
class Notification {
public:
// Shows the notification.
virtual void Show(const base::string16& title,
const base::string16& msg,
const std::string& tag,
const GURL& icon_url,
const SkBitmap& icon,
const bool silent) = 0;
// Closes the notification, this instance will be destroyed after the
// notification gets closed.
virtual void Dismiss() = 0;
// Should be called by derived classes.
void NotificationClicked();
void NotificationDismissed();
void NotificationFailed();
// delete this.
void Destroy();
base::WeakPtr<Notification> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
NotificationDelegate* delegate() const { return delegate_; }
NotificationPresenter* presenter() const { return presenter_; }
protected:
Notification(NotificationDelegate* delegate,
NotificationPresenter* presenter);
public:
virtual ~Notification();
private:
NotificationDelegate* delegate_;
NotificationPresenter* presenter_;
base::WeakPtrFactory<Notification> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(Notification);
};
} // namespace brightray
#endif // BROWSER_NOTIFICATION_H_

View file

@ -0,0 +1,23 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef BROWSER_NOTIFICATION_DELEGATE_H_
#define BROWSER_NOTIFICATION_DELEGATE_H_
#include "content/public/browser/desktop_notification_delegate.h"
namespace brightray {
class NotificationDelegate : public content::DesktopNotificationDelegate {
public:
// The native Notification object is destroyed.
virtual void NotificationDestroyed() {}
// Failed to send the notification.
virtual void NotificationFailed() {}
};
} // namespace brightray
#endif // BROWSER_NOTIFICATION_DELEGATE_H_

View file

@ -0,0 +1,33 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "browser/notification_delegate_adapter.h"
namespace brightray {
NotificationDelegateAdapter::NotificationDelegateAdapter(
std::unique_ptr<content::DesktopNotificationDelegate> delegate)
: delegate_(std::move(delegate)) {
}
NotificationDelegateAdapter::~NotificationDelegateAdapter() {
}
void NotificationDelegateAdapter::NotificationDestroyed() {
delete this;
}
void NotificationDelegateAdapter::NotificationDisplayed() {
delegate_->NotificationDisplayed();
}
void NotificationDelegateAdapter::NotificationClosed() {
delegate_->NotificationClosed();
}
void NotificationDelegateAdapter::NotificationClick() {
delegate_->NotificationClick();
}
} // namespace brightray

View file

@ -0,0 +1,38 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef BROWSER_NOTIFICATION_DELEGATE_ADAPTER_H_
#define BROWSER_NOTIFICATION_DELEGATE_ADAPTER_H_
#include <memory>
#include "base/macros.h"
#include "browser/notification_delegate.h"
namespace brightray {
// Adapt the content::DesktopNotificationDelegate to NotificationDelegate.
class NotificationDelegateAdapter : public NotificationDelegate {
public:
explicit NotificationDelegateAdapter(
std::unique_ptr<content::DesktopNotificationDelegate> delegate);
~NotificationDelegateAdapter() override;
// NotificationDelegate:
void NotificationDestroyed() override;
// content::DesktopNotificationDelegate:
void NotificationDisplayed() override;
void NotificationClosed() override;
void NotificationClick() override;
private:
std::unique_ptr<content::DesktopNotificationDelegate> delegate_;
DISALLOW_COPY_AND_ASSIGN(NotificationDelegateAdapter);
};
} // namespace brightray
#endif // BROWSER_NOTIFICATION_DELEGATE_ADAPTER_H_

View file

@ -0,0 +1,31 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "browser/notification_presenter.h"
#include "browser/notification.h"
namespace brightray {
NotificationPresenter::NotificationPresenter() {
}
NotificationPresenter::~NotificationPresenter() {
for (Notification* notification : notifications_)
delete notification;
}
base::WeakPtr<Notification> NotificationPresenter::CreateNotification(
NotificationDelegate* delegate) {
Notification* notification = CreateNotificationObject(delegate);
notifications_.insert(notification);
return notification->GetWeakPtr();
}
void NotificationPresenter::RemoveNotification(Notification* notification) {
notifications_.erase(notification);
delete notification;
}
} // namespace brightray

View file

@ -0,0 +1,45 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef BRIGHTRAY_BROWSER_NOTIFICATION_PRESENTER_H_
#define BRIGHTRAY_BROWSER_NOTIFICATION_PRESENTER_H_
#include <set>
#include "base/memory/weak_ptr.h"
namespace brightray {
class Notification;
class NotificationDelegate;
class NotificationPresenter {
public:
static NotificationPresenter* Create();
virtual ~NotificationPresenter();
base::WeakPtr<Notification> CreateNotification(
NotificationDelegate* delegate);
std::set<Notification*> notifications() const { return notifications_; }
protected:
NotificationPresenter();
virtual Notification* CreateNotificationObject(
NotificationDelegate* delegate) = 0;
private:
friend class Notification;
void RemoveNotification(Notification* notification);
std::set<Notification*> notifications_;
DISALLOW_COPY_AND_ASSIGN(NotificationPresenter);
};
} // namespace brightray
#endif

View file

@ -0,0 +1,84 @@
// Copyright (c) 2015 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-CHROMIUM file.
#include "browser/permission_manager.h"
#include "base/callback.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/permission_type.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
namespace brightray {
PermissionManager::PermissionManager() {
}
PermissionManager::~PermissionManager() {
}
int PermissionManager::RequestPermission(
content::PermissionType permission,
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
bool user_gesture,
const base::Callback<void(blink::mojom::PermissionStatus)>& callback) {
if (permission == content::PermissionType::MIDI_SYSEX) {
content::ChildProcessSecurityPolicy::GetInstance()->
GrantSendMidiSysExMessage(render_frame_host->GetProcess()->GetID());
}
callback.Run(blink::mojom::PermissionStatus::GRANTED);
return kNoPendingOperation;
}
int PermissionManager::RequestPermissions(
const std::vector<content::PermissionType>& permissions,
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
bool user_gesture,
const base::Callback<void(
const std::vector<blink::mojom::PermissionStatus>&)>& callback) {
std::vector<blink::mojom::PermissionStatus> permissionStatuses;
for (auto permission : permissions) {
if (permission == content::PermissionType::MIDI_SYSEX) {
content::ChildProcessSecurityPolicy::GetInstance()->
GrantSendMidiSysExMessage(render_frame_host->GetProcess()->GetID());
}
permissionStatuses.push_back(blink::mojom::PermissionStatus::GRANTED);
}
callback.Run(permissionStatuses);
return kNoPendingOperation;
}
void PermissionManager::CancelPermissionRequest(int request_id) {
}
void PermissionManager::ResetPermission(
content::PermissionType permission,
const GURL& requesting_origin,
const GURL& embedding_origin) {
}
blink::mojom::PermissionStatus PermissionManager::GetPermissionStatus(
content::PermissionType permission,
const GURL& requesting_origin,
const GURL& embedding_origin) {
return blink::mojom::PermissionStatus::GRANTED;
}
int PermissionManager::SubscribePermissionStatusChange(
content::PermissionType permission,
const GURL& requesting_origin,
const GURL& embedding_origin,
const base::Callback<void(blink::mojom::PermissionStatus)>& callback) {
return -1;
}
void PermissionManager::UnsubscribePermissionStatusChange(int subscription_id) {
}
} // namespace brightray

View file

@ -0,0 +1,57 @@
// Copyright (c) 2015 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-CHROMIUM file.
#ifndef BROWSER_PERMISSION_MANAGER_H_
#define BROWSER_PERMISSION_MANAGER_H_
#include "base/callback_forward.h"
#include "base/macros.h"
#include "content/public/browser/permission_manager.h"
namespace brightray {
class PermissionManager : public content::PermissionManager {
public:
PermissionManager();
~PermissionManager() override;
// content::PermissionManager:
int RequestPermission(
content::PermissionType permission,
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
bool user_gesture,
const base::Callback<void(blink::mojom::PermissionStatus)>& callback)
override;
int RequestPermissions(
const std::vector<content::PermissionType>& permissions,
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
bool user_gesture,
const base::Callback<
void(const std::vector<blink::mojom::PermissionStatus>&)>& callback)
override;
void CancelPermissionRequest(int request_id) override;
void ResetPermission(content::PermissionType permission,
const GURL& requesting_origin,
const GURL& embedding_origin) override;
blink::mojom::PermissionStatus GetPermissionStatus(
content::PermissionType permission,
const GURL& requesting_origin,
const GURL& embedding_origin) override;
int SubscribePermissionStatusChange(
content::PermissionType permission,
const GURL& requesting_origin,
const GURL& embedding_origin,
const base::Callback<void(blink::mojom::PermissionStatus)>& callback)
override;
void UnsubscribePermissionStatusChange(int subscription_id) override;
private:
DISALLOW_COPY_AND_ASSIGN(PermissionManager);
};
} // namespace brightray
#endif // BROWSER_PERMISSION_MANAGER_H_

View file

@ -0,0 +1,109 @@
// Copyright (c) 2015 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-CHROMIUM file.
#include "browser/platform_notification_service.h"
#include "browser/browser_client.h"
#include "browser/notification.h"
#include "browser/notification_delegate_adapter.h"
#include "browser/notification_presenter.h"
#include "content/public/common/platform_notification_data.h"
#include "content/public/common/notification_resources.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace brightray {
namespace {
void RemoveNotification(base::WeakPtr<Notification> notification) {
if (notification)
notification->Dismiss();
}
void OnWebNotificationAllowed(base::WeakPtr<Notification> notification,
const SkBitmap& icon,
const content::PlatformNotificationData& data,
bool audio_muted,
bool allowed) {
if (!notification)
return;
if (allowed)
notification->Show(data.title, data.body, data.tag, data.icon, icon,
audio_muted ? true : data.silent);
else
notification->Destroy();
}
} // namespace
PlatformNotificationService::PlatformNotificationService(
BrowserClient* browser_client)
: browser_client_(browser_client),
render_process_id_(-1) {
}
PlatformNotificationService::~PlatformNotificationService() {}
blink::mojom::PermissionStatus
PlatformNotificationService::CheckPermissionOnUIThread(
content::BrowserContext* browser_context,
const GURL& origin,
int render_process_id) {
render_process_id_ = render_process_id;
return blink::mojom::PermissionStatus::GRANTED;
}
blink::mojom::PermissionStatus
PlatformNotificationService::CheckPermissionOnIOThread(
content::ResourceContext* resource_context,
const GURL& origin,
int render_process_id) {
return blink::mojom::PermissionStatus::GRANTED;
}
void PlatformNotificationService::DisplayNotification(
content::BrowserContext* browser_context,
const std::string& notification_id,
const GURL& origin,
const content::PlatformNotificationData& notification_data,
const content::NotificationResources& notification_resources,
std::unique_ptr<content::DesktopNotificationDelegate> delegate,
base::Closure* cancel_callback) {
auto presenter = browser_client_->GetNotificationPresenter();
if (!presenter)
return;
std::unique_ptr<NotificationDelegateAdapter> adapter(
new NotificationDelegateAdapter(std::move(delegate)));
auto notification = presenter->CreateNotification(adapter.get());
if (notification) {
ignore_result(adapter.release()); // it will release itself automatically.
*cancel_callback = base::Bind(&RemoveNotification, notification);
browser_client_->WebNotificationAllowed(
render_process_id_, base::Bind(&OnWebNotificationAllowed, notification,
notification_resources.notification_icon,
notification_data));
}
}
void PlatformNotificationService::DisplayPersistentNotification(
content::BrowserContext* browser_context,
const std::string& notification_id,
const GURL& service_worker_scope,
const GURL& origin,
const content::PlatformNotificationData& notification_data,
const content::NotificationResources& notification_resources) {
}
void PlatformNotificationService::ClosePersistentNotification(
content::BrowserContext* browser_context,
const std::string& notification_id) {
}
bool PlatformNotificationService::GetDisplayedNotifications(
content::BrowserContext* browser_context,
std::set<std::string>* displayed_notifications) {
return false;
}
} // namespace brightray

View file

@ -0,0 +1,61 @@
// Copyright (c) 2015 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-CHROMIUM file.
#ifndef BROWSER_PLATFORM_NOTIFICATION_SERVICE_H_
#define BROWSER_PLATFORM_NOTIFICATION_SERVICE_H_
#include "content/public/browser/browser_context.h"
#include "content/public/browser/platform_notification_service.h"
namespace brightray {
class BrowserClient;
class PlatformNotificationService
: public content::PlatformNotificationService {
public:
explicit PlatformNotificationService(BrowserClient* browser_client);
~PlatformNotificationService() override;
protected:
// content::PlatformNotificationService:
blink::mojom::PermissionStatus CheckPermissionOnUIThread(
content::BrowserContext* browser_context,
const GURL& origin,
int render_process_id) override;
blink::mojom::PermissionStatus CheckPermissionOnIOThread(
content::ResourceContext* resource_context,
const GURL& origin,
int render_process_id) override;
void DisplayNotification(
content::BrowserContext* browser_context,
const std::string& notification_id,
const GURL& origin,
const content::PlatformNotificationData& notification_data,
const content::NotificationResources& notification_resources,
std::unique_ptr<content::DesktopNotificationDelegate> delegate,
base::Closure* cancel_callback) override;
void DisplayPersistentNotification(
content::BrowserContext* browser_context,
const std::string& notification_id,
const GURL& service_worker_scope,
const GURL& origin,
const content::PlatformNotificationData& notification_data,
const content::NotificationResources& notification_resources) override;
void ClosePersistentNotification(content::BrowserContext* browser_context,
const std::string& notification_id) override;
bool GetDisplayedNotifications(
content::BrowserContext* browser_context,
std::set<std::string>* displayed_notifications) override;
private:
BrowserClient* browser_client_;
int render_process_id_;
DISALLOW_COPY_AND_ASSIGN(PlatformNotificationService);
};
} // namespace brightray
#endif // BROWSER_PLATFORM_NOTIFICATION_SERVICE_H_

View file

@ -0,0 +1,39 @@
// Copyright 2014 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 "browser/special_storage_policy.h"
namespace brightray {
SpecialStoragePolicy::SpecialStoragePolicy() {
}
SpecialStoragePolicy::~SpecialStoragePolicy() {
}
bool SpecialStoragePolicy::IsStorageProtected(const GURL& origin) {
return true;
}
bool SpecialStoragePolicy::IsStorageUnlimited(const GURL& origin) {
return true;
}
bool SpecialStoragePolicy::IsStorageDurable(const GURL& origin) {
return true;
}
bool SpecialStoragePolicy::HasIsolatedStorage(const GURL& origin) {
return false;
}
bool SpecialStoragePolicy::IsStorageSessionOnly(const GURL& origin) {
return false;
}
bool SpecialStoragePolicy::HasSessionOnlyOrigins() {
return false;
}
} // namespace brightray

View file

@ -0,0 +1,30 @@
// Copyright 2014 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.
#ifndef BRIGHTRAY_BROWSER_SPECIAL_STORAGE_POLICY_H_
#define BRIGHTRAY_BROWSER_SPECIAL_STORAGE_POLICY_H_
#include "storage/browser/quota/special_storage_policy.h"
namespace brightray {
class SpecialStoragePolicy : public storage::SpecialStoragePolicy {
public:
SpecialStoragePolicy();
// storage::SpecialStoragePolicy implementation.
bool IsStorageProtected(const GURL& origin) override;
bool IsStorageUnlimited(const GURL& origin) override;
bool IsStorageDurable(const GURL& origin) override;
bool HasIsolatedStorage(const GURL& origin) override;
bool IsStorageSessionOnly(const GURL& origin) override;
bool HasSessionOnlyOrigins() override;
protected:
~SpecialStoragePolicy() override;
};
} // namespace brightray
#endif // BRIGHTRAY_BROWSER_SPECIAL_STORAGE_POLICY_H_

View file

@ -0,0 +1,371 @@
// 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-CHROMIUM file.
#include "browser/url_request_context_getter.h"
#include <algorithm>
#include "browser/net/devtools_network_controller_handle.h"
#include "browser/net/devtools_network_transaction_factory.h"
#include "browser/net_log.h"
#include "browser/network_delegate.h"
#include "common/switches.h"
#include "base/command_line.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_util.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/threading/worker_pool.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/cookie_store_factory.h"
#include "content/public/common/content_switches.h"
#include "net/base/host_mapping_rules.h"
#include "net/cert/cert_verifier.h"
#include "net/cert/ct_known_logs.h"
#include "net/cert/ct_log_verifier.h"
#include "net/cert/ct_policy_enforcer.h"
#include "net/cert/multi_log_ct_verifier.h"
#include "net/cookies/cookie_monster.h"
#include "net/dns/mapped_host_resolver.h"
#include "net/http/http_auth_filter.h"
#include "net/http/http_auth_handler_factory.h"
#include "net/http/http_auth_preferences.h"
#include "net/http/http_server_properties_impl.h"
#include "net/log/net_log.h"
#include "net/proxy/dhcp_proxy_script_fetcher_factory.h"
#include "net/proxy/proxy_config.h"
#include "net/proxy/proxy_config_service.h"
#include "net/proxy/proxy_script_fetcher_impl.h"
#include "net/proxy/proxy_service.h"
#include "net/proxy/proxy_service_v8.h"
#include "net/ssl/channel_id_service.h"
#include "net/ssl/default_channel_id_store.h"
#include "net/ssl/ssl_config_service_defaults.h"
#include "net/url_request/data_protocol_handler.h"
#include "net/url_request/file_protocol_handler.h"
#include "net/url_request/static_http_user_agent_settings.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h"
#include "net/url_request/url_request_context_storage.h"
#include "net/url_request/url_request_intercepting_job_factory.h"
#include "net/url_request/url_request_job_factory_impl.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/url_constants.h"
#include "storage/browser/quota/special_storage_policy.h"
#if defined(USE_NSS_CERTS)
#include "net/cert_net/nss_ocsp.h"
#endif
using content::BrowserThread;
namespace brightray {
std::string URLRequestContextGetter::Delegate::GetUserAgent() {
return base::EmptyString();
}
std::unique_ptr<net::URLRequestJobFactory>
URLRequestContextGetter::Delegate::CreateURLRequestJobFactory(
content::ProtocolHandlerMap* protocol_handlers) {
std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory(
new net::URLRequestJobFactoryImpl);
for (auto& it : *protocol_handlers) {
job_factory->SetProtocolHandler(
it.first, base::WrapUnique(it.second.release()));
}
protocol_handlers->clear();
job_factory->SetProtocolHandler(
url::kDataScheme, base::WrapUnique(new net::DataProtocolHandler));
job_factory->SetProtocolHandler(
url::kFileScheme,
base::WrapUnique(new net::FileProtocolHandler(
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))));
return std::move(job_factory);
}
net::HttpCache::BackendFactory*
URLRequestContextGetter::Delegate::CreateHttpCacheBackendFactory(
const base::FilePath& base_path) {
base::FilePath cache_path = base_path.Append(FILE_PATH_LITERAL("Cache"));
return new net::HttpCache::DefaultBackend(
net::DISK_CACHE,
net::CACHE_BACKEND_DEFAULT,
cache_path,
0,
BrowserThread::GetTaskRunnerForThread(BrowserThread::CACHE));
}
std::unique_ptr<net::CertVerifier>
URLRequestContextGetter::Delegate::CreateCertVerifier() {
return net::CertVerifier::CreateDefault();
}
net::SSLConfigService*
URLRequestContextGetter::Delegate::CreateSSLConfigService() {
return new net::SSLConfigServiceDefaults;
}
std::vector<std::string>
URLRequestContextGetter::Delegate::GetCookieableSchemes() {
return { "http", "https", "ws", "wss" };
}
URLRequestContextGetter::URLRequestContextGetter(
Delegate* delegate,
DevToolsNetworkControllerHandle* handle,
NetLog* net_log,
const base::FilePath& base_path,
bool in_memory,
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector protocol_interceptors)
: delegate_(delegate),
network_controller_handle_(handle),
net_log_(net_log),
base_path_(base_path),
in_memory_(in_memory),
io_task_runner_(io_task_runner),
file_task_runner_(file_task_runner),
protocol_interceptors_(std::move(protocol_interceptors)),
job_factory_(nullptr) {
// Must first be created on the UI thread.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (protocol_handlers)
std::swap(protocol_handlers_, *protocol_handlers);
if (delegate_)
user_agent_ = delegate_->GetUserAgent();
// We must create the proxy config service on the UI loop on Linux because it
// must synchronously run on the glib message loop. This will be passed to
// the URLRequestContextStorage on the IO thread in GetURLRequestContext().
proxy_config_service_ = net::ProxyService::CreateSystemProxyConfigService(
io_task_runner_, file_task_runner_);
}
URLRequestContextGetter::~URLRequestContextGetter() {
#if defined(USE_NSS_CERTS)
net::SetURLRequestContextForNSSHttpIO(NULL);
#endif
}
net::HostResolver* URLRequestContextGetter::host_resolver() {
return url_request_context_->host_resolver();
}
net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!url_request_context_.get()) {
auto& command_line = *base::CommandLine::ForCurrentProcess();
url_request_context_.reset(new net::URLRequestContext);
#if defined(USE_NSS_CERTS)
net::SetURLRequestContextForNSSHttpIO(url_request_context_.get());
#endif
// --log-net-log
if (net_log_) {
net_log_->StartLogging(url_request_context_.get());
url_request_context_->set_net_log(net_log_);
}
network_delegate_.reset(delegate_->CreateNetworkDelegate());
url_request_context_->set_network_delegate(network_delegate_.get());
storage_.reset(
new net::URLRequestContextStorage(url_request_context_.get()));
auto cookie_path = in_memory_ ?
base::FilePath() : base_path_.Append(FILE_PATH_LITERAL("Cookies"));
auto cookie_config = content::CookieStoreConfig(
cookie_path,
content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
nullptr,
delegate_->CreateCookieDelegate());
cookie_config.cookieable_schemes = delegate_->GetCookieableSchemes();
std::unique_ptr<net::CookieStore> cookie_store =
content::CreateCookieStore(cookie_config);
storage_->set_cookie_store(std::move(cookie_store));
storage_->set_channel_id_service(base::MakeUnique<net::ChannelIDService>(
new net::DefaultChannelIDStore(nullptr)));
std::string accept_lang = l10n_util::GetApplicationLocale("");
storage_->set_http_user_agent_settings(base::WrapUnique(
new net::StaticHttpUserAgentSettings(
net::HttpUtil::GenerateAcceptLanguageHeader(accept_lang),
user_agent_)));
std::unique_ptr<net::HostResolver> host_resolver(
net::HostResolver::CreateDefaultResolver(nullptr));
// --host-resolver-rules
if (command_line.HasSwitch(::switches::kHostResolverRules)) {
std::unique_ptr<net::MappedHostResolver> remapped_resolver(
new net::MappedHostResolver(std::move(host_resolver)));
remapped_resolver->SetRulesFromString(
command_line.GetSwitchValueASCII(::switches::kHostResolverRules));
host_resolver = std::move(remapped_resolver);
}
// --proxy-server
net::DhcpProxyScriptFetcherFactory dhcp_factory;
if (command_line.HasSwitch(switches::kNoProxyServer)) {
storage_->set_proxy_service(net::ProxyService::CreateDirect());
} else if (command_line.HasSwitch(switches::kProxyServer)) {
net::ProxyConfig proxy_config;
proxy_config.proxy_rules().ParseFromString(
command_line.GetSwitchValueASCII(switches::kProxyServer));
proxy_config.proxy_rules().bypass_rules.ParseFromString(
command_line.GetSwitchValueASCII(switches::kProxyBypassList));
storage_->set_proxy_service(net::ProxyService::CreateFixed(proxy_config));
} else if (command_line.HasSwitch(switches::kProxyPacUrl)) {
auto proxy_config = net::ProxyConfig::CreateFromCustomPacURL(
GURL(command_line.GetSwitchValueASCII(switches::kProxyPacUrl)));
proxy_config.set_pac_mandatory(true);
storage_->set_proxy_service(net::ProxyService::CreateFixed(
proxy_config));
} else {
storage_->set_proxy_service(
net::CreateProxyServiceUsingV8ProxyResolver(
std::move(proxy_config_service_),
new net::ProxyScriptFetcherImpl(url_request_context_.get()),
dhcp_factory.Create(url_request_context_.get()),
host_resolver.get(),
nullptr,
url_request_context_->network_delegate()));
}
std::vector<std::string> schemes;
schemes.push_back(std::string("basic"));
schemes.push_back(std::string("digest"));
schemes.push_back(std::string("ntlm"));
schemes.push_back(std::string("negotiate"));
#if defined(OS_POSIX)
http_auth_preferences_.reset(new net::HttpAuthPreferences(schemes,
std::string()));
#else
http_auth_preferences_.reset(new net::HttpAuthPreferences(schemes));
#endif
// --auth-server-whitelist
if (command_line.HasSwitch(switches::kAuthServerWhitelist)) {
http_auth_preferences_->set_server_whitelist(
command_line.GetSwitchValueASCII(switches::kAuthServerWhitelist));
}
// --auth-negotiate-delegate-whitelist
if (command_line.HasSwitch(switches::kAuthNegotiateDelegateWhitelist)) {
http_auth_preferences_->set_delegate_whitelist(
command_line.GetSwitchValueASCII(
switches::kAuthNegotiateDelegateWhitelist));
}
auto auth_handler_factory =
net::HttpAuthHandlerRegistryFactory::Create(
http_auth_preferences_.get(), host_resolver.get());
std::unique_ptr<net::TransportSecurityState> transport_security_state =
base::WrapUnique(new net::TransportSecurityState);
transport_security_state->SetRequireCTDelegate(
delegate_->GetRequireCTDelegate());
storage_->set_transport_security_state(std::move(transport_security_state));
storage_->set_cert_verifier(delegate_->CreateCertVerifier());
storage_->set_ssl_config_service(delegate_->CreateSSLConfigService());
storage_->set_http_auth_handler_factory(std::move(auth_handler_factory));
std::unique_ptr<net::HttpServerProperties> server_properties(
new net::HttpServerPropertiesImpl);
storage_->set_http_server_properties(std::move(server_properties));
std::unique_ptr<net::MultiLogCTVerifier> ct_verifier =
base::MakeUnique<net::MultiLogCTVerifier>();
ct_verifier->AddLogs(net::ct::CreateLogVerifiersForKnownLogs());
storage_->set_cert_transparency_verifier(std::move(ct_verifier));
storage_->set_ct_policy_enforcer(base::MakeUnique<net::CTPolicyEnforcer>());
net::HttpNetworkSession::Params network_session_params;
net::URLRequestContextBuilder::SetHttpNetworkSessionComponents(
url_request_context_.get(), &network_session_params);
network_session_params.ignore_certificate_errors = false;
// --disable-http2
if (command_line.HasSwitch(switches::kDisableHttp2))
network_session_params.enable_http2 = false;
// --ignore-certificate-errors
if (command_line.HasSwitch(switches::kIgnoreCertificateErrors))
network_session_params.ignore_certificate_errors = true;
// --host-rules
if (command_line.HasSwitch(switches::kHostRules)) {
host_mapping_rules_.reset(new net::HostMappingRules);
host_mapping_rules_->SetRulesFromString(
command_line.GetSwitchValueASCII(switches::kHostRules));
network_session_params.host_mapping_rules = host_mapping_rules_.get();
}
// Give |storage_| ownership at the end in case it's |mapped_host_resolver|.
storage_->set_host_resolver(std::move(host_resolver));
network_session_params.host_resolver =
url_request_context_->host_resolver();
http_network_session_.reset(
new net::HttpNetworkSession(network_session_params));
std::unique_ptr<net::HttpCache::BackendFactory> backend;
if (in_memory_) {
backend = net::HttpCache::DefaultBackend::InMemory(0);
} else {
backend.reset(delegate_->CreateHttpCacheBackendFactory(base_path_));
}
if (network_controller_handle_) {
storage_->set_http_transaction_factory(base::WrapUnique(
new net::HttpCache(
base::WrapUnique(new DevToolsNetworkTransactionFactory(
network_controller_handle_->GetController(),
http_network_session_.get())),
std::move(backend),
false)));
} else {
storage_->set_http_transaction_factory(base::WrapUnique(
new net::HttpCache(http_network_session_.get(),
std::move(backend),
false)));
}
std::unique_ptr<net::URLRequestJobFactory> job_factory =
delegate_->CreateURLRequestJobFactory(&protocol_handlers_);
job_factory_ = job_factory.get();
// Set up interceptors in the reverse order.
std::unique_ptr<net::URLRequestJobFactory> top_job_factory =
std::move(job_factory);
content::URLRequestInterceptorScopedVector::reverse_iterator it;
for (it = protocol_interceptors_.rbegin();
it != protocol_interceptors_.rend();
++it) {
top_job_factory.reset(new net::URLRequestInterceptingJobFactory(
std::move(top_job_factory), std::move(*it)));
}
protocol_interceptors_.clear();
storage_->set_job_factory(std::move(top_job_factory));
}
return url_request_context_.get();
}
scoped_refptr<base::SingleThreadTaskRunner>
URLRequestContextGetter::GetNetworkTaskRunner() const {
return BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
}
} // namespace brightray

View file

@ -0,0 +1,115 @@
// 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-CHROMIUM file.
#ifndef BRIGHTRAY_BROWSER_URL_REQUEST_CONTEXT_GETTER_H_
#define BRIGHTRAY_BROWSER_URL_REQUEST_CONTEXT_GETTER_H_
#include "base/files/file_path.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/content_browser_client.h"
#include "net/cookies/cookie_monster.h"
#include "net/http/http_cache.h"
#include "net/http/transport_security_state.h"
#include "net/http/url_security_manager.h"
#include "net/url_request/url_request_context_getter.h"
namespace base {
class MessageLoop;
}
namespace net {
class HostMappingRules;
class HostResolver;
class HttpAuthPreferences;
class NetworkDelegate;
class ProxyConfigService;
class URLRequestContextStorage;
class URLRequestJobFactory;
}
namespace brightray {
class DevToolsNetworkControllerHandle;
class MediaDeviceIDSalt;
class NetLog;
class URLRequestContextGetter : public net::URLRequestContextGetter {
public:
class Delegate {
public:
Delegate() {}
virtual ~Delegate() {}
virtual net::NetworkDelegate* CreateNetworkDelegate() { return nullptr; }
virtual net::CookieMonsterDelegate* CreateCookieDelegate() {
return nullptr;
}
virtual std::string GetUserAgent();
virtual std::unique_ptr<net::URLRequestJobFactory>
CreateURLRequestJobFactory(content::ProtocolHandlerMap* protocol_handlers);
virtual net::HttpCache::BackendFactory* CreateHttpCacheBackendFactory(
const base::FilePath& base_path);
virtual std::unique_ptr<net::CertVerifier> CreateCertVerifier();
virtual net::SSLConfigService* CreateSSLConfigService();
virtual std::vector<std::string> GetCookieableSchemes();
virtual net::TransportSecurityState::RequireCTDelegate*
GetRequireCTDelegate() {
return nullptr;
}
virtual MediaDeviceIDSalt* GetMediaDeviceIDSalt() { return nullptr; }
};
URLRequestContextGetter(
Delegate* delegate,
DevToolsNetworkControllerHandle* handle,
NetLog* net_log,
const base::FilePath& base_path,
bool in_memory,
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector protocol_interceptors);
virtual ~URLRequestContextGetter();
// net::URLRequestContextGetter:
net::URLRequestContext* GetURLRequestContext() override;
scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
const override;
net::HostResolver* host_resolver();
net::URLRequestJobFactory* job_factory() const { return job_factory_; }
MediaDeviceIDSalt* GetMediaDeviceIDSalt() const {
return delegate_->GetMediaDeviceIDSalt();
}
private:
Delegate* delegate_;
DevToolsNetworkControllerHandle* network_controller_handle_;
NetLog* net_log_;
base::FilePath base_path_;
bool in_memory_;
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
std::string user_agent_;
std::unique_ptr<net::ProxyConfigService> proxy_config_service_;
std::unique_ptr<net::NetworkDelegate> network_delegate_;
std::unique_ptr<net::URLRequestContextStorage> storage_;
std::unique_ptr<net::URLRequestContext> url_request_context_;
std::unique_ptr<net::HostMappingRules> host_mapping_rules_;
std::unique_ptr<net::HttpAuthPreferences> http_auth_preferences_;
std::unique_ptr<net::HttpNetworkSession> http_network_session_;
content::ProtocolHandlerMap protocol_handlers_;
content::URLRequestInterceptorScopedVector protocol_interceptors_;
net::URLRequestJobFactory* job_factory_; // weak ref
DISALLOW_COPY_AND_ASSIGN(URLRequestContextGetter);
};
} // namespace brightray
#endif

View file

@ -0,0 +1,229 @@
#include "browser/views/inspectable_web_contents_view_views.h"
#include "browser/inspectable_web_contents_delegate.h"
#include "browser/inspectable_web_contents_impl.h"
#include "browser/inspectable_web_contents_view_delegate.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/window/client_view.h"
namespace brightray {
namespace {
class DevToolsWindowDelegate : public views::ClientView,
public views::WidgetDelegate {
public:
DevToolsWindowDelegate(InspectableWebContentsViewViews* shell,
views::View* view,
views::Widget* widget)
: views::ClientView(widget, view),
shell_(shell),
view_(view),
widget_(widget) {
// A WidgetDelegate should be deleted on DeleteDelegate.
set_owned_by_client();
if (shell->GetDelegate())
icon_ = shell->GetDelegate()->GetDevToolsWindowIcon();
}
virtual ~DevToolsWindowDelegate() {}
// views::WidgetDelegate:
void DeleteDelegate() override { delete this; }
views::View* GetInitiallyFocusedView() override { return view_; }
bool CanResize() const override { return true; }
bool CanMaximize() const override { return true; }
bool CanMinimize() const override { return true; }
base::string16 GetWindowTitle() const override { return shell_->GetTitle(); }
gfx::ImageSkia GetWindowAppIcon() override { return GetWindowIcon(); }
gfx::ImageSkia GetWindowIcon() override { return icon_; }
views::Widget* GetWidget() override { return widget_; }
const views::Widget* GetWidget() const override { return widget_; }
views::View* GetContentsView() override { return view_; }
views::ClientView* CreateClientView(views::Widget* widget) override {
return this;
}
// views::ClientView:
bool CanClose() override {
shell_->inspectable_web_contents()->CloseDevTools();
return false;
}
private:
InspectableWebContentsViewViews* shell_;
views::View* view_;
views::Widget* widget_;
gfx::ImageSkia icon_;
DISALLOW_COPY_AND_ASSIGN(DevToolsWindowDelegate);
};
} // namespace
InspectableWebContentsView* CreateInspectableContentsView(
InspectableWebContentsImpl* inspectable_web_contents) {
return new InspectableWebContentsViewViews(inspectable_web_contents);
}
InspectableWebContentsViewViews::InspectableWebContentsViewViews(
InspectableWebContentsImpl* inspectable_web_contents)
: inspectable_web_contents_(inspectable_web_contents),
devtools_window_web_view_(nullptr),
contents_web_view_(nullptr),
devtools_web_view_(new views::WebView(nullptr)),
devtools_visible_(false),
devtools_window_delegate_(nullptr),
title_(base::ASCIIToUTF16("Developer Tools")) {
set_owned_by_client();
if (inspectable_web_contents_->GetWebContents()->GetNativeView()) {
views::WebView* contents_web_view = new views::WebView(nullptr);
contents_web_view->SetWebContents(
inspectable_web_contents_->GetWebContents());
contents_web_view_ = contents_web_view;
} else {
contents_web_view_ = new views::Label(
base::ASCIIToUTF16("No content under offscreen mode"));
}
devtools_web_view_->SetVisible(false);
AddChildView(devtools_web_view_);
AddChildView(contents_web_view_);
}
InspectableWebContentsViewViews::~InspectableWebContentsViewViews() {
if (devtools_window_)
inspectable_web_contents()->SaveDevToolsBounds(
devtools_window_->GetWindowBoundsInScreen());
}
views::View* InspectableWebContentsViewViews::GetView() {
return this;
}
views::View* InspectableWebContentsViewViews::GetWebView() {
return contents_web_view_;
}
void InspectableWebContentsViewViews::ShowDevTools() {
if (devtools_visible_)
return;
devtools_visible_ = true;
if (devtools_window_) {
devtools_window_web_view_->SetWebContents(
inspectable_web_contents_->GetDevToolsWebContents());
devtools_window_->SetBounds(
inspectable_web_contents()->GetDevToolsBounds());
devtools_window_->Show();
} else {
devtools_web_view_->SetVisible(true);
devtools_web_view_->SetWebContents(
inspectable_web_contents_->GetDevToolsWebContents());
devtools_web_view_->RequestFocus();
Layout();
}
}
void InspectableWebContentsViewViews::CloseDevTools() {
if (!devtools_visible_)
return;
devtools_visible_ = false;
if (devtools_window_) {
inspectable_web_contents()->SaveDevToolsBounds(
devtools_window_->GetWindowBoundsInScreen());
devtools_window_.reset();
devtools_window_web_view_ = nullptr;
devtools_window_delegate_ = nullptr;
} else {
devtools_web_view_->SetVisible(false);
devtools_web_view_->SetWebContents(NULL);
Layout();
}
}
bool InspectableWebContentsViewViews::IsDevToolsViewShowing() {
return devtools_visible_;
}
bool InspectableWebContentsViewViews::IsDevToolsViewFocused() {
if (devtools_window_web_view_)
return devtools_window_web_view_->HasFocus();
else if (devtools_web_view_)
return devtools_web_view_->HasFocus();
else
return false;
}
void InspectableWebContentsViewViews::SetIsDocked(bool docked) {
CloseDevTools();
if (!docked) {
devtools_window_.reset(new views::Widget);
devtools_window_web_view_ = new views::WebView(NULL);
devtools_window_delegate_ = new DevToolsWindowDelegate(
this,
devtools_window_web_view_,
devtools_window_.get());
views::Widget::InitParams params;
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.delegate = devtools_window_delegate_;
params.bounds = inspectable_web_contents()->GetDevToolsBounds();
#if defined(USE_X11)
params.wm_role_name = "devtools";
if (GetDelegate())
GetDelegate()->GetDevToolsWindowWMClass(&params.wm_class_name,
&params.wm_class_class);
#endif
devtools_window_->Init(params);
devtools_window_->UpdateWindowIcon();
}
ShowDevTools();
}
void InspectableWebContentsViewViews::SetContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy) {
strategy_.CopyFrom(strategy);
Layout();
}
void InspectableWebContentsViewViews::SetTitle(const base::string16& title) {
if (devtools_window_) {
title_ = title;
devtools_window_->UpdateWindowTitle();
}
}
void InspectableWebContentsViewViews::Layout() {
if (!devtools_web_view_->visible()) {
contents_web_view_->SetBoundsRect(GetContentsBounds());
return;
}
gfx::Size container_size(width(), height());
gfx::Rect new_devtools_bounds;
gfx::Rect new_contents_bounds;
ApplyDevToolsContentsResizingStrategy(strategy_, container_size,
&new_devtools_bounds, &new_contents_bounds);
// DevTools cares about the specific position, so we have to compensate RTL
// layout here.
new_devtools_bounds.set_x(GetMirroredXForRect(new_devtools_bounds));
new_contents_bounds.set_x(GetMirroredXForRect(new_contents_bounds));
devtools_web_view_->SetBoundsRect(new_devtools_bounds);
contents_web_view_->SetBoundsRect(new_contents_bounds);
}
} // namespace brightray

View file

@ -0,0 +1,67 @@
#ifndef BROWSER_VIEWS_INSPECTABLE_WEB_CONTENTS_VIEW_VIEWS_H_
#define BROWSER_VIEWS_INSPECTABLE_WEB_CONTENTS_VIEW_VIEWS_H_
#include "browser/devtools_contents_resizing_strategy.h"
#include "browser/inspectable_web_contents_view.h"
#include "base/compiler_specific.h"
#include "ui/views/view.h"
namespace views {
class WebView;
class Widget;
class WidgetDelegate;
}
namespace brightray {
class InspectableWebContentsImpl;
class InspectableWebContentsViewViews : public InspectableWebContentsView,
public views::View {
public:
explicit InspectableWebContentsViewViews(
InspectableWebContentsImpl* inspectable_web_contents_impl);
~InspectableWebContentsViewViews();
// InspectableWebContentsView:
views::View* GetView() override;
views::View* GetWebView() override;
void ShowDevTools() override;
void CloseDevTools() override;
bool IsDevToolsViewShowing() override;
bool IsDevToolsViewFocused() override;
void SetIsDocked(bool docked) override;
void SetContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy) override;
void SetTitle(const base::string16& title) override;
InspectableWebContentsImpl* inspectable_web_contents() {
return inspectable_web_contents_;
}
const base::string16& GetTitle() const { return title_; }
private:
// views::View:
void Layout() override;
// Owns us.
InspectableWebContentsImpl* inspectable_web_contents_;
std::unique_ptr<views::Widget> devtools_window_;
views::WebView* devtools_window_web_view_;
views::View* contents_web_view_;
views::WebView* devtools_web_view_;
DevToolsContentsResizingStrategy strategy_;
bool devtools_visible_;
views::WidgetDelegate* devtools_window_delegate_;
base::string16 title_;
DISALLOW_COPY_AND_ASSIGN(InspectableWebContentsViewViews);
};
} // namespace brightray
#endif // BROWSER_VIEWS_INSPECTABLE_WEB_CONTENTS_VIEW_VIEWS_H_

View file

@ -0,0 +1,115 @@
// Copyright (c) 2014 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-CHROMIUM file.
#include "browser/views/views_delegate.h"
#include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
#include "ui/views/widget/native_widget_aura.h"
#if defined(OS_LINUX)
#include "ui/views/linux_ui/linux_ui.h"
#endif
namespace brightray {
ViewsDelegate::ViewsDelegate() {
}
ViewsDelegate::~ViewsDelegate() {
}
void ViewsDelegate::SaveWindowPlacement(const views::Widget* window,
const std::string& window_name,
const gfx::Rect& bounds,
ui::WindowShowState show_state) {
}
bool ViewsDelegate::GetSavedWindowPlacement(
const views::Widget* widget,
const std::string& window_name,
gfx::Rect* bounds,
ui::WindowShowState* show_state) const {
return false;
}
void ViewsDelegate::NotifyAccessibilityEvent(
views::View* view, ui::AXEvent event_type) {
}
void ViewsDelegate::NotifyMenuItemFocused(
const base::string16& menu_name,
const base::string16& menu_item_name,
int item_index,
int item_count,
bool has_submenu) {
}
#if defined(OS_WIN)
HICON ViewsDelegate::GetDefaultWindowIcon() const {
// Use current exe's icon as default window icon.
return LoadIcon(GetModuleHandle(NULL),
MAKEINTRESOURCE(1 /* IDR_MAINFRAME */));
}
HICON ViewsDelegate::GetSmallWindowIcon() const {
return GetDefaultWindowIcon();
}
bool ViewsDelegate::IsWindowInMetro(gfx::NativeWindow window) const {
return false;
}
#elif defined(OS_LINUX) && !defined(OS_CHROMEOS)
gfx::ImageSkia* ViewsDelegate::GetDefaultWindowIcon() const {
return NULL;
}
#endif
views::NonClientFrameView* ViewsDelegate::CreateDefaultNonClientFrameView(
views::Widget* widget) {
return NULL;
}
void ViewsDelegate::AddRef() {
}
void ViewsDelegate::ReleaseRef() {
}
content::WebContents* ViewsDelegate::CreateWebContents(
content::BrowserContext* browser_context,
content::SiteInstance* site_instance) {
return NULL;
}
void ViewsDelegate::OnBeforeWidgetInit(
views::Widget::InitParams* params,
views::internal::NativeWidgetDelegate* delegate) {
// If we already have a native_widget, we don't have to try to come
// up with one.
if (params->native_widget)
return;
if (params->parent &&
params->type != views::Widget::InitParams::TYPE_MENU &&
params->type != views::Widget::InitParams::TYPE_TOOLTIP) {
params->native_widget = new views::NativeWidgetAura(delegate);
} else {
params->native_widget = new views::DesktopNativeWidgetAura(delegate);
}
}
bool ViewsDelegate::WindowManagerProvidesTitleBar(bool maximized) {
#if defined(OS_LINUX)
// On Ubuntu Unity, the system always provides a title bar for maximized
// windows.
views::LinuxUI* ui = views::LinuxUI::instance();
return maximized && ui && ui->UnityIsRunning();
#else
return false;
#endif
}
} // namespace brightray

View file

@ -0,0 +1,64 @@
// Copyright (c) 2014 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-CHROMIUM file.
#ifndef BRIGHTRAY_BROWSER_VIEWS_VIEWS_DELEGATE_H_
#define BRIGHTRAY_BROWSER_VIEWS_VIEWS_DELEGATE_H_
#include "base/compiler_specific.h"
#include "ui/views/views_delegate.h"
namespace brightray {
class ViewsDelegate : public views::ViewsDelegate {
public:
ViewsDelegate();
virtual ~ViewsDelegate();
protected:
// views::ViewsDelegate:
void SaveWindowPlacement(const views::Widget* window,
const std::string& window_name,
const gfx::Rect& bounds,
ui::WindowShowState show_state) override;
bool GetSavedWindowPlacement(
const views::Widget* widget,
const std::string& window_name,
gfx::Rect* bounds,
ui::WindowShowState* show_state) const override;
void NotifyAccessibilityEvent(
views::View* view, ui::AXEvent event_type) override;
void NotifyMenuItemFocused(const base::string16& menu_name,
const base::string16& menu_item_name,
int item_index,
int item_count,
bool has_submenu) override;
#if defined(OS_WIN)
HICON GetDefaultWindowIcon() const override;
HICON GetSmallWindowIcon() const override;
bool IsWindowInMetro(gfx::NativeWindow window) const override;
#elif defined(OS_LINUX) && !defined(OS_CHROMEOS)
gfx::ImageSkia* GetDefaultWindowIcon() const override;
#endif
views::NonClientFrameView* CreateDefaultNonClientFrameView(
views::Widget* widget) override;
void AddRef() override;
void ReleaseRef() override;
content::WebContents* CreateWebContents(
content::BrowserContext* browser_context,
content::SiteInstance* site_instance) override;
void OnBeforeWidgetInit(
views::Widget::InitParams* params,
views::internal::NativeWidgetDelegate* delegate) override;
bool WindowManagerProvidesTitleBar(bool maximized) override;
private:
DISALLOW_COPY_AND_ASSIGN(ViewsDelegate);
};
} // namespace brightray
#endif // BRIGHTRAY_BROWSER_VIEWS_VIEWS_DELEGATE_H_

View file

@ -0,0 +1,60 @@
// Copyright (c) 2013 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-CHROMIUM file.
#include "browser/web_ui_controller_factory.h"
#include "browser/devtools_ui.h"
#include "base/memory/singleton.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/common/url_constants.h"
namespace brightray {
namespace {
const char kChromeUIDevToolsBundledHost[] = "devtools";
} // namespace
// static
WebUIControllerFactory* WebUIControllerFactory::GetInstance() {
return base::Singleton<WebUIControllerFactory>::get();
}
WebUIControllerFactory::WebUIControllerFactory() {
}
WebUIControllerFactory::~WebUIControllerFactory() {
}
content::WebUI::TypeID WebUIControllerFactory::GetWebUIType(
content::BrowserContext* browser_context, const GURL& url) const {
if (url.host() == kChromeUIDevToolsBundledHost) {
return const_cast<WebUIControllerFactory*>(this);
}
return content::WebUI::kNoWebUI;
}
bool WebUIControllerFactory::UseWebUIForURL(
content::BrowserContext* browser_context, const GURL& url) const {
return GetWebUIType(browser_context, url) != content::WebUI::kNoWebUI;
}
bool WebUIControllerFactory::UseWebUIBindingsForURL(
content::BrowserContext* browser_context, const GURL& url) const {
return UseWebUIForURL(browser_context, url);
}
content::WebUIController* WebUIControllerFactory::CreateWebUIControllerForURL(
content::WebUI* web_ui, const GURL& url) const {
if (url.host() == kChromeUIDevToolsBundledHost) {
auto browser_context = web_ui->GetWebContents()->GetBrowserContext();
return new DevToolsUI(browser_context, web_ui);
}
return nullptr;
}
} // namespace brightray

View file

@ -0,0 +1,45 @@
// Copyright (c) 2013 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-CHROMIUM file.
#ifndef BRIGHTRAY_BROWSER_WEB_UI_CONTROLLER_FACTORY_H_
#define BRIGHTRAY_BROWSER_WEB_UI_CONTROLLER_FACTORY_H_
#include "base/macros.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_controller_factory.h"
namespace base {
template <typename T> struct DefaultSingletonTraits;
}
namespace brightray {
class BrowserContext;
class WebUIControllerFactory : public content::WebUIControllerFactory {
public:
static WebUIControllerFactory* GetInstance();
WebUIControllerFactory();
virtual ~WebUIControllerFactory();
content::WebUI::TypeID GetWebUIType(
content::BrowserContext* browser_context, const GURL& url) const override;
bool UseWebUIForURL(content::BrowserContext* browser_context,
const GURL& url) const override;
bool UseWebUIBindingsForURL(content::BrowserContext* browser_context,
const GURL& url) const override;
content::WebUIController* CreateWebUIControllerForURL(
content::WebUI* web_ui,
const GURL& url) const override;
private:
friend struct base::DefaultSingletonTraits<WebUIControllerFactory>;
DISALLOW_COPY_AND_ASSIGN(WebUIControllerFactory);
};
} // namespace brightray
#endif

Some files were not shown because too many files have changed in this diff Show more