Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
extension actions
Browse files Browse the repository at this point in the history
  • Loading branch information
bridiver committed Sep 10, 2016
1 parent bdfc4be commit 335f859
Show file tree
Hide file tree
Showing 20 changed files with 452 additions and 120 deletions.
20 changes: 16 additions & 4 deletions atom/browser/api/atom_api_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@
#include "base/strings/string_util.h"
#include "brave/browser/brave_content_browser_client.h"
#include "brightray/browser/brightray_paths.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/common/chrome_paths.h"
#include "content/public/browser/browser_accessibility_state.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/client_certificate_delegate.h"
#include "content/public/browser/gpu_data_manager.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/common/content_switches.h"
#include "native_mate/dictionary.h"
Expand All @@ -55,6 +53,8 @@

#if defined(ENABLE_EXTENSIONS)
#include "atom/browser/api/atom_api_extension.h"
#include "chrome/browser/chrome_notification_types.h"
#include "content/public/browser/notification_types.h"
#endif

using atom::Browser;
Expand Down Expand Up @@ -239,18 +239,23 @@ App::App(v8::Isolate* isolate) {
Browser::Get()->AddObserver(this);
content::GpuDataManager::GetInstance()->AddObserver(this);
Init(isolate);
#if defined(ENABLE_EXTENSIONS)
registrar_.Add(this,
content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this,
chrome::NOTIFICATION_PROFILE_CREATED,
content::NotificationService::AllBrowserContextsAndSources());
#endif
}

// TOOD(bridiver) - move this to api_extension?
void App::Observe(
int type, const content::NotificationSource& source,
const content::NotificationDetails& details) {
#if defined(ENABLE_EXTENSIONS)
switch (type) {
case content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED: {
#if defined(ENABLE_EXTENSIONS)
content::WebContents* web_contents =
content::Source<content::WebContents>(source).ptr();
auto browser_context = web_contents->GetBrowserContext();
Expand All @@ -261,10 +266,17 @@ void App::Observe(
if (Extension::IsBackgroundPageUrl(url, browser_context)) {
WebContents::CreateFrom(isolate(), web_contents);
}
#endif
break;
}
case chrome::NOTIFICATION_PROFILE_CREATED: {
AtomBrowserContext* browser_context =
content::Source<AtomBrowserContext>(source).ptr();
auto session = Session::CreateFrom(isolate(), browser_context);
Emit("browser-context-created", session);
break;
}
}
#endif
}

App::~App() {
Expand Down
40 changes: 23 additions & 17 deletions atom/browser/api/atom_api_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "atom/common/native_mate_converters/v8_value_converter.h"
#include "atom/common/node_includes.h"
#include "base/files/file_path.h"
#include "components/prefs/pref_service.h"
Expand Down Expand Up @@ -124,7 +125,7 @@ Extension* Extension::GetInstance() {
}

// static
mate::Dictionary Extension::Load(
v8::Local<v8::Value> Extension::Load(
v8::Isolate* isolate,
const base::FilePath& path,
const base::DictionaryValue& manifest,
Expand All @@ -133,32 +134,37 @@ mate::Dictionary Extension::Load(
std::string error;
scoped_refptr<extensions::Extension> extension;

if (manifest.empty()) {
extension = extensions::file_util::LoadExtension(path,
manifest_location,
flags,
&error);
} else {
std::unique_ptr<base::DictionaryValue> manifest_copy =
manifest.CreateDeepCopy();
if (manifest_copy->empty()) {
manifest_copy = extensions::file_util::LoadManifest(path, &error);
}

if (error.empty()) {
extension = LoadExtension(path,
manifest,
*manifest_copy,
manifest_location,
flags,
&error);
}

mate::Dictionary install_info = mate::Dictionary::CreateEmpty(isolate);
std::unique_ptr<base::DictionaryValue>
install_info(new base::DictionaryValue);
if (error.empty()) {
Install(extension);
install_info.Set("name", extension->non_localized_name());
install_info.Set("id", extension->id());
install_info.Set("url", extension->url().spec());
install_info.Set("path", extension->path());
install_info.Set("version", extension->VersionString());
install_info.Set("description", extension->description());
install_info->SetString("name", extension->non_localized_name());
install_info->SetString("id", extension->id());
install_info->SetString("url", extension->url().spec());
install_info->SetString("base_path", extension->path().value());
install_info->SetString("version", extension->VersionString());
install_info->SetString("description", extension->description());
install_info->Set("manifest", std::move(manifest_copy));
} else {
install_info.Set("error", error);
install_info->SetString("error", error);
}
return install_info;
std::unique_ptr<atom::V8ValueConverter>
converter(new atom::V8ValueConverter);
return converter->ToV8Value(install_info.get(), isolate->GetCurrentContext());
}

// static
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/api/atom_api_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Extension : public content::NotificationObserver {
public:
static Extension* GetInstance();

static mate::Dictionary Load(v8::Isolate* isolate,
static v8::Local<v8::Value> Load(v8::Isolate* isolate,
const base::FilePath& path,
const base::DictionaryValue& manifest,
const extensions::Manifest::Location& manifest_location,
Expand Down
19 changes: 19 additions & 0 deletions atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,11 @@ std::unique_ptr<content::BluetoothChooser> WebContents::RunBluetoothChooser(
return std::move(bluetooth_chooser);
}

void WebContents::UpdatePreferredSize(content::WebContents* web_contents,
const gfx::Size& pref_size) {
Emit("preferred-size-changed", pref_size);
}

void WebContents::BeforeUnloadFired(const base::TimeTicks& proceed_time) {
// Do nothing, we override this method just to avoid compilation error since
// there are two virtual functions named BeforeUnloadFired.
Expand Down Expand Up @@ -776,6 +781,14 @@ void WebContents::DidChangeThemeColor(SkColor theme_color) {
Emit("did-change-theme-color", hex_theme_color);
}

void WebContents::DocumentAvailableInMainFrame() {
Emit("document-available");
}

void WebContents::DocumentOnLoadCompletedInMainFrame() {
Emit("document-onload");
}

void WebContents::DocumentLoadedInFrame(
content::RenderFrameHost* render_frame_host) {
if (!render_frame_host->GetParent())
Expand Down Expand Up @@ -1692,6 +1705,10 @@ void WebContents::CapturePage(mate::Arguments* args) {
kBGRA_8888_SkColorType);
}

gfx::Size WebContents::GetPreferredSize() {
return web_contents()->GetPreferredSize();
}

void WebContents::OnCursorChange(const content::WebCursor& cursor) {
content::WebCursor::CursorInfo info;
cursor.GetCursorInfo(&info);
Expand Down Expand Up @@ -1831,6 +1848,7 @@ void WebContents::OnMemoryPressure(
web_contents()->GetController().ClearAllScreenshots();
}

// TODO(bridiver) only run once per render process
if (!web_contents()->GetRenderWidgetHostView()->HasFocus()) {
content::MemoryPressureController::SendPressureNotification(
web_contents()->GetRenderProcessHost(), memory_pressure_level);
Expand Down Expand Up @@ -1967,6 +1985,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
&WebContents::ShowDefinitionForSelection)
.SetMethod("copyImageAt", &WebContents::CopyImageAt)
.SetMethod("capturePage", &WebContents::CapturePage)
.SetMethod("getPreferredSize", &WebContents::GetPreferredSize)
.SetProperty("id", &WebContents::ID)
.SetMethod("getContentWindowId", &WebContents::GetContentWindowId)
.SetMethod("setActive", &WebContents::SetActive)
Expand Down
10 changes: 10 additions & 0 deletions atom/browser/api/atom_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ namespace brightray {
class InspectableWebContents;
}

namespace gfx {
class Size;
}

namespace mate {

template<>
Expand Down Expand Up @@ -214,6 +218,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
// done.
void CapturePage(mate::Arguments* args);

gfx::Size GetPreferredSize();

// Methods for creating <webview>.
void SetSize(const SetSizeParams& params);
bool IsGuest() const;
Expand Down Expand Up @@ -333,11 +339,15 @@ class WebContents : public mate::TrackableObject<WebContents>,
std::unique_ptr<content::BluetoothChooser> RunBluetoothChooser(
content::RenderFrameHost* frame,
const content::BluetoothChooser::EventHandler& handler) override;
void UpdatePreferredSize(content::WebContents* web_contents,
const gfx::Size& pref_size) override;

// content::WebContentsObserver:
void BeforeUnloadFired(const base::TimeTicks& proceed_time) override;
void RenderViewDeleted(content::RenderViewHost*) override;
void RenderProcessGone(base::TerminationStatus status) override;
void DocumentAvailableInMainFrame() override;
void DocumentOnLoadCompletedInMainFrame() override;
void DocumentLoadedInFrame(
content::RenderFrameHost* render_frame_host) override;
void DidStartProvisionalLoadForFrame(
Expand Down
53 changes: 48 additions & 5 deletions atom/common/api/resources/browser_action_bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,71 @@ var manifest = runtimeNatives.GetManifest();
var process = requireNative('process');
var extensionId = process.GetExtensionId();

var id = 0

var binding = {
onClicked: {
addListener: function (cb) {
ipc.send('register-chrome-browser-action', extensionId,
manifest.browser_action.default_title)
ipc.on('chrome-browser-action-clicked', function(evt, tab) {
cb(tab)
})
},
removeListener: function (cb) {
// TODO
// ipc.off('chrome-browser-action-clicked', cb)
}
},
setPopup: function (details) {
ipc.send('chrome-browser-action-set-popup', extensionId, details)
},
getPopup: function (details, cb) {
var responseId = ++id
ipc.once('chrome-browser-action-get-popup-response-' + responseId, function(evt, details) {
cb(details)
})
ipc.send('chrome-browser-action-get-popup', responseId, details)
},
setTitle: function (details) {
ipc.send('chrome-browser-action-set-title', extensionId, details)
},
getTitle: function (details, cb) {
var responseId = ++id
ipc.once('chrome-browser-action-get-title-response-' + responseId, function(evt, result) {
cb(result)
})
ipc.send('chrome-browser-action-get-title', responseId, details)
},
setIcon: function (details, cb) {
ipc.send('chrome-browser-action-set-icon', extensionId, details)
cb && cb()
if (details.imageData) {
// TODO(bridiver) - support imageData somehow
console.warn('chrome.browserAction.setIcon imageData is not supported yet')
return
}
var responseId = ++id
ipc.once('chrome-browser-action-set-icon-response-' + responseId, function(evt) {
cb && cb()
})
ipc.send('chrome-browser-action-set-icon', responseId, extensionId, details)
},
setBadgeText: function (details) {
ipc.send('chrome-browser-action-set-title', extensionId, details)
ipc.send('chrome-browser-action-set-badge-text', extensionId, details)
},
getBadgeText: function (details, cb) {
var responseId = ++id
ipc.once('chrome-browser-action-get-badge-text-response-' + responseId, function(evt, details) {
cb(details)
})
ipc.send('chrome-browser-action-get-badge-text', responseId, details)
},
setBadgeBackgroundColor: function (details) {
ipc.send('chrome-browser-action-set-badge-background-color', extensionId, details)
},
getBadgeBackgroundColor: function (details, cb) {
var responseId = ++id
ipc.once('chrome-browser-action-get-badge-text-response-' + responseId, function(evt, details) {
cb(details)
})
ipc.send('chrome-browser-action-get-badge-text', responseId, details)
}
}

Expand Down
3 changes: 1 addition & 2 deletions atom/common/api/resources/content_settings_bindings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var atom = requireNative('atom').GetBinding();
var contentSettings = requireNative('contentSettings');
var inIncognitoContext = requireNative('process').InIncognitoContext();
var contentSettings = atom.content_settings;

function getCurrent (contentType) {
return contentSettings.getCurrent(contentType, inIncognitoContext)
Expand Down
Loading

0 comments on commit 335f859

Please sign in to comment.