From 2d016785a7be69a8a486f867a05bf905fd46e00c Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 18 Apr 2013 20:50:58 +0800 Subject: [PATCH] Add observers list for NativeWindow. --- atom.gyp | 1 + browser/native_window.cc | 35 +++++++++++++++++++++++++++++++- browser/native_window.h | 26 +++++++++++++++++++++++- browser/native_window_observer.h | 23 +++++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 browser/native_window_observer.h diff --git a/atom.gyp b/atom.gyp index d191b004983c..335dbbbe3b63 100644 --- a/atom.gyp +++ b/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', diff --git a/browser/native_window.cc b/browser/native_window.cc index 8bf4433eb32e..c76910736092 100644 --- a/browser/native_window.cc +++ b/browser/native_window.cc @@ -6,22 +6,35 @@ #include +#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(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* title = + content::Details>(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 diff --git a/browser/native_window.h b/browser/native_window.h index bda6026f55a5..239406a22577 100644 --- a/browser/native_window.h +++ b/browser/native_window.h @@ -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 observers_; + scoped_ptr inspectable_web_contents_; DISALLOW_COPY_AND_ASSIGN(NativeWindow); diff --git a/browser/native_window_observer.h b/browser/native_window_observer.h new file mode 100644 index 000000000000..207ebdede431 --- /dev/null +++ b/browser/native_window_observer.h @@ -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 + +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_