use browser context from webcontents for registering datasource

This commit is contained in:
Robo 2015-08-28 01:24:05 +05:30
parent fa5c8fc943
commit 0e956c497d
8 changed files with 27 additions and 28 deletions

View file

@ -69,7 +69,7 @@ class BrowserContext::ResourceContext : public content::ResourceContext {
BrowserContext::BrowserContext() : resource_context_(new ResourceContext) { BrowserContext::BrowserContext() : resource_context_(new ResourceContext) {
} }
void BrowserContext::Initialize(const base::FilePath& partition_path, bool in_memory) { void BrowserContext::Initialize(const std::string& partition_path, bool in_memory) {
if (!PathService::Get(DIR_USER_DATA, &path_)) { if (!PathService::Get(DIR_USER_DATA, &path_)) {
PathService::Get(DIR_APP_DATA, &path_); PathService::Get(DIR_APP_DATA, &path_);
path_ = path_.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName())); path_ = path_.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName()));
@ -77,7 +77,7 @@ void BrowserContext::Initialize(const base::FilePath& partition_path, bool in_me
} }
if (!partition_path.empty()) if (!partition_path.empty())
path_ = path_.Append(partition_path); path_ = path_.Append(base::FilePath::FromUTF8Unsafe(partition_path));
in_memory_ = in_memory; in_memory_ = in_memory;
auto prefs_path = GetPath().Append(FILE_PATH_LITERAL("Preferences")); auto prefs_path = GetPath().Append(FILE_PATH_LITERAL("Preferences"));

View file

@ -23,7 +23,7 @@ class BrowserContext : public content::BrowserContext,
BrowserContext(); BrowserContext();
~BrowserContext(); ~BrowserContext();
virtual void Initialize(const base::FilePath& partition_path, virtual void Initialize(const std::string& partition_path,
bool in_memory = false); bool in_memory = false);
// content::BrowserContext: // content::BrowserContext:

View file

@ -121,12 +121,10 @@ void BrowserMainParts::PreMainMessageLoopStart() {
void BrowserMainParts::PreMainMessageLoopRun() { void BrowserMainParts::PreMainMessageLoopRun() {
browser_context_.reset(CreateBrowserContext()); browser_context_.reset(CreateBrowserContext());
browser_context_->Initialize(base::FilePath()); browser_context_->Initialize(std::string());
web_ui_controller_factory_.reset(
new WebUIControllerFactory(browser_context_.get()));
content::WebUIControllerFactory::RegisterFactory( content::WebUIControllerFactory::RegisterFactory(
web_ui_controller_factory_.get()); WebUIControllerFactory::GetInstance());
// --remote-debugging-port // --remote-debugging-port
auto command_line = base::CommandLine::ForCurrentProcess(); auto command_line = base::CommandLine::ForCurrentProcess();

View file

@ -28,7 +28,6 @@ class WMState;
namespace brightray { namespace brightray {
class BrowserContext; class BrowserContext;
class WebUIControllerFactory;
class BrowserMainParts : public content::BrowserMainParts { class BrowserMainParts : public content::BrowserMainParts {
public: public:
@ -57,7 +56,6 @@ class BrowserMainParts : public content::BrowserMainParts {
#endif #endif
scoped_ptr<BrowserContext> browser_context_; scoped_ptr<BrowserContext> browser_context_;
scoped_ptr<WebUIControllerFactory> web_ui_controller_factory_;
scoped_ptr<devtools_http_handler::DevToolsHttpHandler> devtools_http_handler_; scoped_ptr<devtools_http_handler::DevToolsHttpHandler> devtools_http_handler_;
#if defined(TOOLKIT_VIEWS) #if defined(TOOLKIT_VIEWS)

View file

@ -6,8 +6,6 @@
#include <string> #include <string>
#include "browser/browser_context.h"
#include "base/memory/ref_counted_memory.h" #include "base/memory/ref_counted_memory.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
@ -93,7 +91,8 @@ class BundledDataSource : public content::URLDataSource {
} // namespace } // namespace
DevToolsUI::DevToolsUI(BrowserContext* browser_context, content::WebUI* web_ui) DevToolsUI::DevToolsUI(content::BrowserContext* browser_context,
content::WebUI* web_ui)
: WebUIController(web_ui) { : WebUIController(web_ui) {
web_ui->SetBindings(0); web_ui->SetBindings(0);
content::URLDataSource::Add(browser_context, new BundledDataSource()); content::URLDataSource::Add(browser_context, new BundledDataSource());

View file

@ -6,6 +6,7 @@
#define BRIGHTRAY_BROWSER_DEVTOOLS_UI_H_ #define BRIGHTRAY_BROWSER_DEVTOOLS_UI_H_
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_ui_controller.h" #include "content/public/browser/web_ui_controller.h"
namespace brightray { namespace brightray {
@ -14,7 +15,8 @@ class BrowserContext;
class DevToolsUI : public content::WebUIController { class DevToolsUI : public content::WebUIController {
public: public:
explicit DevToolsUI(BrowserContext* browser_context, content::WebUI* web_ui); explicit DevToolsUI(content::BrowserContext* browser_context,
content::WebUI* web_ui);
private: private:
DISALLOW_COPY_AND_ASSIGN(DevToolsUI); DISALLOW_COPY_AND_ASSIGN(DevToolsUI);

View file

@ -4,9 +4,8 @@
#include "browser/web_ui_controller_factory.h" #include "browser/web_ui_controller_factory.h"
#include "browser/browser_context.h"
#include "browser/devtools_ui.h" #include "browser/devtools_ui.h"
#include "base/memory/singleton.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h" #include "content/public/browser/web_ui.h"
#include "content/public/common/url_constants.h" #include "content/public/common/url_constants.h"
@ -19,9 +18,12 @@ const char kChromeUIDevToolsBundledHost[] = "devtools";
} }
WebUIControllerFactory::WebUIControllerFactory(BrowserContext* browser_context) // static
: browser_context_(browser_context) { WebUIControllerFactory* WebUIControllerFactory::GetInstance() {
DCHECK(browser_context_); return Singleton<WebUIControllerFactory>::get();
}
WebUIControllerFactory::WebUIControllerFactory() {
} }
WebUIControllerFactory::~WebUIControllerFactory() { WebUIControllerFactory::~WebUIControllerFactory() {
@ -48,11 +50,10 @@ bool WebUIControllerFactory::UseWebUIBindingsForURL(
content::WebUIController* WebUIControllerFactory::CreateWebUIControllerForURL( content::WebUIController* WebUIControllerFactory::CreateWebUIControllerForURL(
content::WebUI* web_ui, const GURL& url) const { content::WebUI* web_ui, const GURL& url) const {
DCHECK(browser_context_); if (url.host() == kChromeUIDevToolsBundledHost) {
auto browser_context = web_ui->GetWebContents()->GetBrowserContext();
if (url.host() == kChromeUIDevToolsBundledHost) return new DevToolsUI(browser_context, web_ui);
return new DevToolsUI(browser_context_, web_ui); }
return NULL; return NULL;
} }

View file

@ -9,13 +9,17 @@
#include "content/public/browser/web_ui.h" #include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_controller_factory.h" #include "content/public/browser/web_ui_controller_factory.h"
template <typename T> struct DefaultSingletonTraits;
namespace brightray { namespace brightray {
class BrowserContext; class BrowserContext;
class WebUIControllerFactory : public content::WebUIControllerFactory { class WebUIControllerFactory : public content::WebUIControllerFactory {
public: public:
explicit WebUIControllerFactory(BrowserContext* browser_context); static WebUIControllerFactory* GetInstance();
WebUIControllerFactory();
virtual ~WebUIControllerFactory(); virtual ~WebUIControllerFactory();
content::WebUI::TypeID GetWebUIType( content::WebUI::TypeID GetWebUIType(
@ -28,11 +32,8 @@ class WebUIControllerFactory : public content::WebUIControllerFactory {
content::WebUI* web_ui, content::WebUI* web_ui,
const GURL& url) const override; const GURL& url) const override;
static WebUIControllerFactory* GetInstance();
private: private:
// Weak reference to the browser context. friend struct DefaultSingletonTraits<WebUIControllerFactory>;
BrowserContext* browser_context_;
DISALLOW_COPY_AND_ASSIGN(WebUIControllerFactory); DISALLOW_COPY_AND_ASSIGN(WebUIControllerFactory);
}; };