Add observers list for NativeWindow.
This commit is contained in:
parent
d508f711c6
commit
2d016785a7
4 changed files with 83 additions and 2 deletions
1
atom.gyp
1
atom.gyp
|
@ -33,6 +33,7 @@
|
|||
'browser/native_window.h',
|
||||
'browser/native_window_mac.h',
|
||||
'browser/native_window_mac.mm',
|
||||
'browser/native_window_observer.h',
|
||||
'common/node_bindings.cc',
|
||||
'common/node_bindings.h',
|
||||
'common/node_bindings_mac.h',
|
||||
|
|
|
@ -6,22 +6,35 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
#include "base/utf_string_conversions.h"
|
||||
#include "base/values.h"
|
||||
#include "brightray/browser/browser_context.h"
|
||||
#include "brightray/browser/inspectable_web_contents.h"
|
||||
#include "brightray/browser/inspectable_web_contents_view.h"
|
||||
#include "content/public/browser/navigation_entry.h"
|
||||
#include "content/public/browser/notification_details.h"
|
||||
#include "content/public/browser/notification_source.h"
|
||||
#include "content/public/browser/notification_types.h"
|
||||
#include "common/options_switches.h"
|
||||
#include "ui/gfx/point.h"
|
||||
#include "ui/gfx/rect.h"
|
||||
#include "ui/gfx/size.h"
|
||||
|
||||
using content::NavigationEntry;
|
||||
|
||||
namespace atom {
|
||||
|
||||
NativeWindow::NativeWindow(content::BrowserContext* browser_context,
|
||||
base::DictionaryValue* options)
|
||||
: inspectable_web_contents_(brightray::InspectableWebContents::Create(
|
||||
content::WebContents::CreateParams(browser_context))) {
|
||||
GetWebContents()->SetDelegate(this);
|
||||
content::WebContents* web_contents = GetWebContents();
|
||||
|
||||
web_contents->SetDelegate(this);
|
||||
|
||||
// Add window as an observer of the web contents.
|
||||
registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED,
|
||||
content::Source<content::WebContents>(web_contents));
|
||||
}
|
||||
|
||||
NativeWindow::~NativeWindow() {
|
||||
|
@ -89,4 +102,24 @@ content::WebContents* NativeWindow::GetWebContents() const {
|
|||
return inspectable_web_contents_->GetWebContents();
|
||||
}
|
||||
|
||||
void NativeWindow::Observe(int type,
|
||||
const content::NotificationSource& source,
|
||||
const content::NotificationDetails& details) {
|
||||
if (type == content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED) {
|
||||
std::pair<NavigationEntry*, bool>* title =
|
||||
content::Details<std::pair<NavigationEntry*, bool>>(details).ptr();
|
||||
|
||||
if (title->first) {
|
||||
bool prevent_default = false;
|
||||
std::string text = UTF16ToUTF8(title->first->GetTitle());
|
||||
FOR_EACH_OBSERVER(NativeWindowObserver,
|
||||
observers_,
|
||||
OnPageTitleUpdated(&prevent_default, text));
|
||||
|
||||
if (!prevent_default)
|
||||
SetTitle(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace atom
|
||||
|
|
|
@ -10,6 +10,10 @@
|
|||
#include "base/basictypes.h"
|
||||
#include "base/compiler_specific.h"
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
#include "base/observer_list.h"
|
||||
#include "browser/native_window_observer.h"
|
||||
#include "content/public/browser/notification_registrar.h"
|
||||
#include "content/public/browser/notification_observer.h"
|
||||
#include "content/public/browser/web_contents_delegate.h"
|
||||
|
||||
namespace base {
|
||||
|
@ -33,7 +37,8 @@ class Size;
|
|||
|
||||
namespace atom {
|
||||
|
||||
class NativeWindow : public content::WebContentsDelegate {
|
||||
class NativeWindow : public content::WebContentsDelegate,
|
||||
public content::NotificationObserver {
|
||||
public:
|
||||
virtual ~NativeWindow();
|
||||
|
||||
|
@ -76,6 +81,14 @@ class NativeWindow : public content::WebContentsDelegate {
|
|||
|
||||
content::WebContents* GetWebContents() const;
|
||||
|
||||
void AddObserver(NativeWindowObserver* obs) {
|
||||
observers_.AddObserver(obs);
|
||||
}
|
||||
|
||||
void RemoveObserver(NativeWindowObserver* obs) {
|
||||
observers_.RemoveObserver(obs);
|
||||
}
|
||||
|
||||
protected:
|
||||
explicit NativeWindow(content::BrowserContext* browser_context,
|
||||
base::DictionaryValue* options);
|
||||
|
@ -84,7 +97,18 @@ class NativeWindow : public content::WebContentsDelegate {
|
|||
return inspectable_web_contents_.get();
|
||||
}
|
||||
|
||||
// Implementations of content::NotificationObserver
|
||||
virtual void Observe(int type,
|
||||
const content::NotificationSource& source,
|
||||
const content::NotificationDetails& details) OVERRIDE;
|
||||
|
||||
private:
|
||||
// Notification manager.
|
||||
content::NotificationRegistrar registrar_;
|
||||
|
||||
// Observers of this window.
|
||||
ObserverList<NativeWindowObserver> observers_;
|
||||
|
||||
scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(NativeWindow);
|
||||
|
|
23
browser/native_window_observer.h
Normal file
23
browser/native_window_observer.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ATOM_BROWSER_NATIVE_WINDOW_OBSERVER_H_
|
||||
#define ATOM_BROWSER_NATIVE_WINDOW_OBSERVER_H_
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
namespace atom {
|
||||
|
||||
class NativeWindowObserver {
|
||||
public:
|
||||
virtual ~NativeWindowObserver() {};
|
||||
|
||||
// Called when the web page of the window has updated it's document title.
|
||||
virtual void OnPageTitleUpdated(bool* prevent_default,
|
||||
const std::string& title) = 0;
|
||||
};
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_BROWSER_NATIVE_WINDOW_OBSERVER_H_
|
Loading…
Reference in a new issue