Pending renderer process no longer has render view attached

This commit is contained in:
Cheng Zhao 2016-03-09 21:34:51 +09:00
parent 6de9c4332f
commit bfc6d77bb3
3 changed files with 33 additions and 7 deletions

View file

@ -203,13 +203,8 @@ void AtomBrowserClient::AppendExtraCommandLineSwitches(
// Certain render process will be created with no associated render view, // Certain render process will be created with no associated render view,
// for example: ServiceWorker. // for example: ServiceWorker.
auto rvh = content::RenderViewHost::FromID(process_id, kDefaultRoutingID);
if (!rvh)
return;
// Get the WebContents of the render process.
content::WebContents* web_contents = content::WebContents* web_contents =
content::WebContents::FromRenderViewHost(rvh); WebContentsPreferences::GetWebContentsFromProcessID(process_id);
if (!web_contents) if (!web_contents)
return; return;

View file

@ -4,12 +4,15 @@
#include "atom/browser/web_contents_preferences.h" #include "atom/browser/web_contents_preferences.h"
#include <algorithm>
#include <string> #include <string>
#include <vector>
#include "atom/common/native_mate_converters/value_converter.h" #include "atom/common/native_mate_converters/value_converter.h"
#include "atom/common/options_switches.h" #include "atom/common/options_switches.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/content_switches.h" #include "content/public/common/content_switches.h"
#include "content/public/common/web_preferences.h" #include "content/public/common/web_preferences.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
@ -23,9 +26,13 @@ DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::WebContentsPreferences);
namespace atom { namespace atom {
// static
std::vector<WebContentsPreferences*> WebContentsPreferences::instances_;
WebContentsPreferences::WebContentsPreferences( WebContentsPreferences::WebContentsPreferences(
content::WebContents* web_contents, content::WebContents* web_contents,
const mate::Dictionary& web_preferences) { const mate::Dictionary& web_preferences)
: web_contents_(web_contents) {
v8::Isolate* isolate = web_preferences.isolate(); v8::Isolate* isolate = web_preferences.isolate();
mate::Dictionary copied(isolate, web_preferences.GetHandle()->Clone()); mate::Dictionary copied(isolate, web_preferences.GetHandle()->Clone());
// Following fields should not be stored. // Following fields should not be stored.
@ -35,15 +42,31 @@ WebContentsPreferences::WebContentsPreferences(
mate::ConvertFromV8(isolate, copied.GetHandle(), &web_preferences_); mate::ConvertFromV8(isolate, copied.GetHandle(), &web_preferences_);
web_contents->SetUserData(UserDataKey(), this); web_contents->SetUserData(UserDataKey(), this);
instances_.push_back(this);
} }
WebContentsPreferences::~WebContentsPreferences() { WebContentsPreferences::~WebContentsPreferences() {
instances_.erase(
std::remove(instances_.begin(), instances_.end(), this),
instances_.end());
} }
void WebContentsPreferences::Merge(const base::DictionaryValue& extend) { void WebContentsPreferences::Merge(const base::DictionaryValue& extend) {
web_preferences_.MergeDictionary(&extend); web_preferences_.MergeDictionary(&extend);
} }
// static
content::WebContents* WebContentsPreferences::GetWebContentsFromProcessID(
int process_id) {
for (WebContentsPreferences* preferences : instances_) {
content::WebContents* web_contents = preferences->web_contents_;
if (web_contents->GetRenderProcessHost()->GetID() == process_id)
return web_contents;
}
return nullptr;
}
// static // static
void WebContentsPreferences::AppendExtraCommandLineSwitches( void WebContentsPreferences::AppendExtraCommandLineSwitches(
content::WebContents* web_contents, base::CommandLine* command_line) { content::WebContents* web_contents, base::CommandLine* command_line) {

View file

@ -5,6 +5,8 @@
#ifndef ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_ #ifndef ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_
#define ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_ #define ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_
#include <vector>
#include "base/values.h" #include "base/values.h"
#include "content/public/browser/web_contents_user_data.h" #include "content/public/browser/web_contents_user_data.h"
@ -26,6 +28,9 @@ namespace atom {
class WebContentsPreferences class WebContentsPreferences
: public content::WebContentsUserData<WebContentsPreferences> { : public content::WebContentsUserData<WebContentsPreferences> {
public: public:
// Get WebContents according to process ID.
static content::WebContents* GetWebContentsFromProcessID(int process_id);
// Append command paramters according to |web_contents|'s preferences. // Append command paramters according to |web_contents|'s preferences.
static void AppendExtraCommandLineSwitches( static void AppendExtraCommandLineSwitches(
content::WebContents* web_contents, base::CommandLine* command_line); content::WebContents* web_contents, base::CommandLine* command_line);
@ -47,6 +52,9 @@ class WebContentsPreferences
private: private:
friend class content::WebContentsUserData<WebContentsPreferences>; friend class content::WebContentsUserData<WebContentsPreferences>;
static std::vector<WebContentsPreferences*> instances_;
content::WebContents* web_contents_;
base::DictionaryValue web_preferences_; base::DictionaryValue web_preferences_;
DISALLOW_COPY_AND_ASSIGN(WebContentsPreferences); DISALLOW_COPY_AND_ASSIGN(WebContentsPreferences);