Skip to content

Commit

Permalink
cefclient: Move StringResourceMap to ClientHandler (see issue chromiu…
Browse files Browse the repository at this point in the history
…membedded#2586)

Fixes a DCHECK when creating multiple windows in cefclient due to the creation
of multiple StringResourceProvider objects.
  • Loading branch information
magreenblatt committed Oct 18, 2019
1 parent 329facf commit aad4bf2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 32 deletions.
13 changes: 12 additions & 1 deletion tests/cefclient/browser/client_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ ClientHandler::ClientHandler(Delegate* delegate,
#endif

resource_manager_ = new CefResourceManager();
test_runner::SetupResourceManager(resource_manager_);
test_runner::SetupResourceManager(resource_manager_, &string_resource_map_);

// Read command line settings.
CefRefPtr<CefCommandLine> command_line =
Expand Down Expand Up @@ -973,6 +973,17 @@ void ClientHandler::ShowSSLInformation(CefRefPtr<CefBrowser> browser) {
MainContext::Get()->GetRootWindowManager()->CreateRootWindow(config);
}

void ClientHandler::SetStringResource(const std::string& page,
const std::string& data) {
if (!CefCurrentlyOn(TID_IO)) {
CefPostTask(TID_IO, base::Bind(&ClientHandler::SetStringResource, this,
page, data));
return;
}

string_resource_map_[page] = data;
}

bool ClientHandler::CreatePopupWindow(CefRefPtr<CefBrowser> browser,
bool is_devtools,
const CefPopupFeatures& popupFeatures,
Expand Down
8 changes: 8 additions & 0 deletions tests/cefclient/browser/client_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "include/wrapper/cef_message_router.h"
#include "include/wrapper/cef_resource_manager.h"
#include "tests/cefclient/browser/client_types.h"
#include "tests/cefclient/browser/test_runner.h"

#if defined(OS_LINUX)
#include "tests/cefclient/browser/dialog_handler_gtk.h"
Expand Down Expand Up @@ -293,6 +294,9 @@ class ClientHandler : public CefClient,
// Show SSL information for the current site.
void ShowSSLInformation(CefRefPtr<CefBrowser> browser);

// Set a string resource for loading via StringResourceProvider.
void SetStringResource(const std::string& page, const std::string& data);

// Returns the Delegate.
Delegate* delegate() const { return delegate_; }

Expand Down Expand Up @@ -367,6 +371,10 @@ class ClientHandler : public CefClient,
// Manages the registration and delivery of resources.
CefRefPtr<CefResourceManager> resource_manager_;

// Used to manage string resources in combination with StringResourceProvider.
// Only accessed on the IO thread.
test_runner::StringResourceMap string_resource_map_;

// MAIN THREAD MEMBERS
// The following members will only be accessed on the main thread. This will
// be the same as the CEF UI thread except when using multi-threaded message
Expand Down
47 changes: 17 additions & 30 deletions tests/cefclient/browser/test_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "include/wrapper/cef_closure_task.h"
#include "include/wrapper/cef_stream_resource_handler.h"
#include "tests/cefclient/browser/binding_test.h"
#include "tests/cefclient/browser/client_handler.h"
#include "tests/cefclient/browser/dialog_test.h"
#include "tests/cefclient/browser/drm_test.h"
#include "tests/cefclient/browser/main_context.h"
Expand Down Expand Up @@ -43,27 +44,14 @@ const char kTestGetSourcePage[] = "get_source.html";
const char kTestGetTextPage[] = "get_text.html";
const char kTestPluginInfoPage[] = "plugin_info.html";

// Map of page name to data.
typedef std::map<std::string, std::string> StringResourceMap;
StringResourceMap* g_string_resource_map = NULL;

void SetStringResource(const std::string& page, const std::string& data) {
if (!CefCurrentlyOn(TID_IO)) {
CefPostTask(TID_IO, base::Bind(SetStringResource, page, data));
return;
}

if (g_string_resource_map) {
(*g_string_resource_map)[page] = data;
}
}

// Set page data and navigate the browser. Used in combination with
// StringResourceProvider.
void LoadStringResourcePage(CefRefPtr<CefBrowser> browser,
const std::string& page,
const std::string& data) {
SetStringResource(page, data);
CefRefPtr<CefClient> client = browser->GetHost()->GetClient();
ClientHandler* client_handler = static_cast<ClientHandler*>(client.get());
client_handler->SetStringResource(page, data);
browser->GetMainFrame()->LoadURL(kTestOrigin + page);
}

Expand Down Expand Up @@ -500,16 +488,12 @@ class RequestDumpResourceProvider : public CefResourceManager::Provider {
// with LoadStringResourcePage().
class StringResourceProvider : public CefResourceManager::Provider {
public:
explicit StringResourceProvider(const std::set<std::string>& pages)
: pages_(pages) {
StringResourceProvider(const std::set<std::string>& pages,
StringResourceMap* string_resource_map)
: pages_(pages), string_resource_map_(string_resource_map) {
DCHECK(!pages.empty());

DCHECK(!g_string_resource_map);
g_string_resource_map = &resource_map_;
}

virtual ~StringResourceProvider() { g_string_resource_map = NULL; }

bool OnRequest(scoped_refptr<CefResourceManager::Request> request) OVERRIDE {
CEF_REQUIRE_IO_THREAD();

Expand All @@ -526,8 +510,8 @@ class StringResourceProvider : public CefResourceManager::Provider {
}

std::string value;
StringResourceMap::const_iterator it = resource_map_.find(page);
if (it != resource_map_.end()) {
StringResourceMap::const_iterator it = string_resource_map_->find(page);
if (it != string_resource_map_->end()) {
value = it->second;
} else {
value = "<html><body>No data available</body></html>";
Expand All @@ -545,7 +529,7 @@ class StringResourceProvider : public CefResourceManager::Provider {
const std::set<std::string> pages_;

// Only accessed on the IO thread.
StringResourceMap resource_map_;
StringResourceMap* string_resource_map_;

DISALLOW_COPY_AND_ASSIGN(StringResourceProvider);
};
Expand Down Expand Up @@ -810,10 +794,12 @@ std::string GetErrorString(cef_errorcode_t code) {
}
}

void SetupResourceManager(CefRefPtr<CefResourceManager> resource_manager) {
void SetupResourceManager(CefRefPtr<CefResourceManager> resource_manager,
StringResourceMap* string_resource_map) {
if (!CefCurrentlyOn(TID_IO)) {
// Execute on the browser IO thread.
CefPostTask(TID_IO, base::Bind(SetupResourceManager, resource_manager));
CefPostTask(TID_IO, base::Bind(SetupResourceManager, resource_manager,
string_resource_map));
return;
}

Expand All @@ -834,8 +820,9 @@ void SetupResourceManager(CefRefPtr<CefResourceManager> resource_manager) {
string_pages.insert(kTestPluginInfoPage);

// Add provider for string resources.
resource_manager->AddProvider(new StringResourceProvider(string_pages), 0,
std::string());
resource_manager->AddProvider(
new StringResourceProvider(string_pages, string_resource_map), 0,
std::string());

// Add provider for bundled resource files.
#if defined(OS_WIN)
Expand Down
5 changes: 4 additions & 1 deletion tests/cefclient/browser/test_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ std::string GetDataURI(const std::string& data, const std::string& mime_type);
// Returns the string representation of the specified error code.
std::string GetErrorString(cef_errorcode_t code);

typedef std::map<std::string, std::string> StringResourceMap;

// Set up the resource manager for tests.
void SetupResourceManager(CefRefPtr<CefResourceManager> resource_manager);
void SetupResourceManager(CefRefPtr<CefResourceManager> resource_manager,
StringResourceMap* string_resource_map);

// Show a JS alert message.
void Alert(CefRefPtr<CefBrowser> browser, const std::string& message);
Expand Down

0 comments on commit aad4bf2

Please sign in to comment.