-
Notifications
You must be signed in to change notification settings - Fork 898
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
011d90f
commit d909599
Showing
17 changed files
with
393 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// Copyright (c) 2019 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// you can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
|
||
#include "brave/browser/extensions/api/brave_action_api.h" | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <utility> | ||
|
||
#include "base/no_destructor.h" | ||
#include "components/keyed_service/core/dependency_manager.h" | ||
#include "components/keyed_service/core/keyed_service_factory.h" | ||
#include "chrome/browser/extensions/api/tabs/tabs_constants.h" | ||
#include "chrome/browser/extensions/api/tabs/windows_util.h" | ||
#include "chrome/browser/extensions/chrome_extension_function_details.h" | ||
#include "chrome/browser/extensions/window_controller.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "chrome/browser/ui/browser.h" | ||
#include "extensions/browser/extension_function.h" | ||
|
||
namespace { | ||
|
||
class BraveActionAPIDependencyManager : public DependencyManager { | ||
public: | ||
static BraveActionAPIDependencyManager* GetInstance() { | ||
static base::NoDestructor<BraveActionAPIDependencyManager> factory; | ||
return factory.get(); | ||
} | ||
BraveActionAPIDependencyManager() { } | ||
~BraveActionAPIDependencyManager() override; | ||
private: | ||
DISALLOW_COPY_AND_ASSIGN(BraveActionAPIDependencyManager); | ||
}; | ||
|
||
BraveActionAPIDependencyManager::~BraveActionAPIDependencyManager() { } | ||
|
||
class BraveActionAPIFactory : public KeyedServiceFactory { | ||
public: | ||
BraveActionAPIFactory() : KeyedServiceFactory("BraveActionAPI", | ||
BraveActionAPIDependencyManager::GetInstance(), SIMPLE) { } | ||
|
||
extensions::BraveActionAPI* GetBraveActionAPI(Browser* context) { | ||
return static_cast<extensions::BraveActionAPI*>( | ||
GetServiceForContext(context, true)); | ||
} | ||
|
||
private: | ||
// KeyedServiceFactory: | ||
std::unique_ptr<KeyedService> BuildServiceInstanceFor( | ||
void* context) const final { | ||
return base::WrapUnique(new extensions::BraveActionAPI()); | ||
} | ||
bool IsOffTheRecord(void* context) const final { | ||
return static_cast<Browser*>(context) | ||
->profile()->IsOffTheRecord(); | ||
} | ||
void* GetContextToUse(void* context) const final { | ||
return context; | ||
} | ||
void CreateServiceNow(void* context) final { | ||
KeyedServiceFactory::GetServiceForContext(context, true); | ||
} | ||
DISALLOW_COPY_AND_ASSIGN(BraveActionAPIFactory); | ||
}; | ||
|
||
static BraveActionAPIFactory* GetFactoryInstance() { | ||
static base::NoDestructor<BraveActionAPIFactory> instance; | ||
return instance.get(); | ||
} | ||
|
||
} // namespace | ||
|
||
namespace extensions { | ||
// | ||
// BraveActionAPI::Observer | ||
// | ||
BraveActionAPI::Observer::Observer() { } | ||
|
||
BraveActionAPI::Observer::~Observer() { } | ||
|
||
// | ||
// BraveActionAPI | ||
// | ||
// static | ||
BraveActionAPI* BraveActionAPI::Get(Browser* context) { | ||
return GetFactoryInstance()->GetBraveActionAPI(context); | ||
} | ||
|
||
// static | ||
bool BraveActionAPI::ShowActionUI( | ||
ExtensionFunction* extension_function, | ||
const std::string& extension_id, | ||
std::unique_ptr<int> window_id_param, | ||
std::unique_ptr<std::string> ui_relative_path_param, | ||
std::string* error) { | ||
// Which browser should we send the action to | ||
Browser* browser = nullptr; | ||
// If the windowId is specified, find it. Otherwise get the active | ||
// window for the profile. | ||
if (!window_id_param.get()) { | ||
browser = ChromeExtensionFunctionDetails(extension_function) | ||
.GetCurrentBrowser(); | ||
if (!browser) { | ||
*error = tabs_constants::kNoCurrentWindowError; | ||
return false; | ||
} | ||
} else { | ||
int window_id = *window_id_param; | ||
std::string get_browser_error; | ||
if (!windows_util::GetBrowserFromWindowID( | ||
extension_function, | ||
window_id, | ||
WindowController::GetAllWindowFilter(), | ||
&browser, | ||
&get_browser_error)) { | ||
*error = get_browser_error; | ||
return false; | ||
} | ||
} | ||
return ShowActionUI(browser, extension_id, std::move(ui_relative_path_param), | ||
error); | ||
} | ||
|
||
// static | ||
bool BraveActionAPI::ShowActionUI( | ||
Browser* browser, | ||
const std::string& extension_id, | ||
std::unique_ptr<std::string> ui_relative_path_param, | ||
std::string* error) { | ||
bool did_notify = BraveActionAPI::Get(browser)->NotifyObservers(extension_id, | ||
std::move(ui_relative_path_param)); | ||
if (!did_notify) { | ||
*error = "No toolbar is registered to observe BraveActionUI " | ||
"calls for this window"; | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
BraveActionAPI::BraveActionAPI() {} | ||
|
||
BraveActionAPI::~BraveActionAPI() { | ||
} | ||
|
||
void BraveActionAPI::AddObserver(Observer* observer) { | ||
observers_.AddObserver(observer); | ||
} | ||
|
||
void BraveActionAPI::RemoveObserver(Observer* observer) { | ||
observers_.RemoveObserver(observer); | ||
} | ||
|
||
bool BraveActionAPI::NotifyObservers(const std::string& extension_id, | ||
std::unique_ptr<std::string> ui_relative_path_param) { | ||
bool did_notify = false; | ||
for (auto& observer : observers_) { | ||
observer.OnBraveActionShouldTrigger(extension_id, | ||
std::move(ui_relative_path_param)); | ||
did_notify = true; | ||
} | ||
return did_notify; | ||
} | ||
} // namespace extensions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2019 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// you can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#ifndef BRAVE_BROWSER_EXTENSIONS_API_BRAVE_ACTION_API_H_ | ||
#define BRAVE_BROWSER_EXTENSIONS_API_BRAVE_ACTION_API_H_ | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "components/keyed_service/core/keyed_service.h" | ||
#include "extensions/browser/extension_function.h" | ||
#include "extensions/common/extension.h" | ||
|
||
class Browser; | ||
|
||
namespace extensions { | ||
class BraveActionAPI : public KeyedService { | ||
public: | ||
class Observer { | ||
public: | ||
Observer(); | ||
virtual void OnBraveActionShouldTrigger( | ||
const std::string& extension_id, | ||
std::unique_ptr<std::string> ui_relative_path) = 0; | ||
|
||
protected: | ||
virtual ~Observer(); | ||
}; | ||
|
||
static BraveActionAPI* Get(Browser* context); | ||
static bool ShowActionUI( | ||
ExtensionFunction* extension_function, | ||
const std::string& extension_id, | ||
std::unique_ptr<int> window_id, | ||
std::unique_ptr<std::string> ui_relative_path, | ||
std::string* error); | ||
static bool ShowActionUI( | ||
Browser* browser, | ||
const std::string& extension_id, | ||
std::unique_ptr<std::string> ui_relative_path, | ||
std::string* error); | ||
BraveActionAPI(); | ||
~BraveActionAPI() override; | ||
|
||
// Add or remove observers. | ||
void AddObserver(Observer* observer); | ||
void RemoveObserver(Observer* observer); | ||
|
||
protected: | ||
bool NotifyObservers(const std::string& extension_id, | ||
std::unique_ptr<std::string> ui_relative_path_param); | ||
|
||
private: | ||
base::ObserverList<Observer>::Unchecked observers_; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(BraveActionAPI); | ||
}; | ||
} // namespace extensions | ||
|
||
#endif // BRAVE_BROWSER_EXTENSIONS_API_BRAVE_ACTION_API_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.