]> khamidou — GitHub mirrors - electron.git/commitdiff
Add observers list for NativeWindow.
authorCheng Zhao <zcbenz@gmail.com>
Thu, 18 Apr 2013 12:50:58 +0000 (20:50 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 18 Apr 2013 12:50:58 +0000 (20:50 +0800)
atom.gyp
browser/native_window.cc
browser/native_window.h
browser/native_window_observer.h [new file with mode: 0644]

index d191b004983caae0a1c80f29e46a26d8da2190b7..335dbbbe3b63daadb5805c4ea8e95db6c4beccdc 100644 (file)
--- 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',
index 8bf4433eb32e91403a3d68baf744cf9e4bce735f..c7691073609252922617c5a23ebbb57424ca90a6 100644 (file)
@@ -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
index bda6026f55a5848767822dd71b09fe635d0cd81d..239406a22577250a3a71d728fc6a94321f87c7c6 100644 (file)
 #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);
diff --git a/browser/native_window_observer.h b/browser/native_window_observer.h
new file mode 100644 (file)
index 0000000..207ebde
--- /dev/null
@@ -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_