diff options
Diffstat (limited to 'chrome/browser/extensions')
7 files changed, 198 insertions, 21 deletions
diff --git a/chrome/browser/extensions/api/identity/identity_api.cc b/chrome/browser/extensions/api/identity/identity_api.cc new file mode 100644 index 0000000..1c8a8c2 --- /dev/null +++ b/chrome/browser/extensions/api/identity/identity_api.cc @@ -0,0 +1,67 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/extensions/api/identity/identity_api.h" + +#include "base/values.h" +#include "chrome/browser/extensions/extension_function_dispatcher.h" +#include "chrome/browser/signin/token_service.h" +#include "chrome/browser/signin/token_service_factory.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" +#include "chrome/common/extensions/extension.h" +#include "googleurl/src/gurl.h" + +namespace extensions { + +namespace { + +const char kInvalidClientId[] = "Invalid OAuth2 Client ID."; +const char kInvalidScopes[] = "Invalid OAuth2 scopes."; + +} // namespace + +GetAuthTokenFunction::GetAuthTokenFunction() {} +GetAuthTokenFunction::~GetAuthTokenFunction() {} + +bool GetAuthTokenFunction::RunImpl() { + const Extension* extension = GetExtension(); + Extension::OAuth2Info oauth2_info = extension->oauth2_info(); + + if (oauth2_info.client_id.empty()) { + error_ = kInvalidClientId; + return false; + } + + if (oauth2_info.scopes.size() == 0) { + error_ = kInvalidScopes; + return false; + } + + AddRef(); // Balanced in OnMintTokenSuccess|Failure. + + TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); + + flow_.reset( + new OAuth2MintTokenFlow(profile()->GetRequestContext(), this)); + flow_->Start(token_service->GetOAuth2LoginRefreshToken(), + extension->id(), oauth2_info.client_id, oauth2_info.scopes); + + return true; +} + +void GetAuthTokenFunction::OnMintTokenSuccess(const std::string& access_token) { + result_.reset(Value::CreateStringValue(access_token)); + SendResponse(true); + Release(); // Balanced in RunImpl. +} + +void GetAuthTokenFunction::OnMintTokenFailure( + const GoogleServiceAuthError& error) { + error_ = error.ToString(); + SendResponse(false); + Release(); // Balanced in RunImpl. +} + +} // namespace extensions diff --git a/chrome/browser/extensions/api/identity/identity_api.h b/chrome/browser/extensions/api/identity/identity_api.h new file mode 100644 index 0000000..636f830 --- /dev/null +++ b/chrome/browser/extensions/api/identity/identity_api.h @@ -0,0 +1,40 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ +#define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ +#pragma once + +#include <string> + +#include "base/memory/weak_ptr.h" +#include "chrome/browser/extensions/app_notify_channel_setup.h" +#include "chrome/browser/extensions/extension_function.h" +#include "chrome/common/net/gaia/oauth2_mint_token_flow.h" + +class GoogleServiceAuthError; + +namespace extensions { + +class GetAuthTokenFunction : public AsyncExtensionFunction, + public OAuth2MintTokenFlow::Delegate { + public: + GetAuthTokenFunction(); + + private: + virtual ~GetAuthTokenFunction(); + virtual bool RunImpl() OVERRIDE; + + // OAuth2MintTokenFlow::Delegate implementation: + virtual void OnMintTokenSuccess(const std::string& access_token) OVERRIDE; + virtual void OnMintTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; + + scoped_ptr<OAuth2MintTokenFlow> flow_; + + DECLARE_EXTENSION_FUNCTION_NAME("experimental.identity.getAuthToken"); +}; + +} // namespace extensions + +#endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ diff --git a/chrome/browser/extensions/api/identity/identity_apitest.cc b/chrome/browser/extensions/api/identity/identity_apitest.cc new file mode 100644 index 0000000..334c8848 --- /dev/null +++ b/chrome/browser/extensions/api/identity/identity_apitest.cc @@ -0,0 +1,45 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/extensions/api/identity/identity_api.h" +#include "chrome/browser/extensions/extension_apitest.h" +#include "chrome/common/net/gaia/google_service_auth_error.h" +#include "chrome/common/net/gaia/oauth2_mint_token_flow.h" +#include "chrome/common/chrome_switches.h" + +namespace { + +class IdentityInterceptor : public OAuth2MintTokenFlow::InterceptorForTests { + public: + virtual bool DoIntercept(const OAuth2MintTokenFlow* flow, + std::string* access_token, + GoogleServiceAuthError* error) OVERRIDE { + *access_token = "auth_token"; + get_auth_token_called_ = true; + return true; + } + + bool get_auth_token_called() const { return get_auth_token_called_; } + + private: + bool get_auth_token_called_; +}; + +} // namespace + +class ExperimentalApiTest : public ExtensionApiTest { + public: + void SetUpCommandLine(CommandLine* command_line) { + ExtensionApiTest::SetUpCommandLine(command_line); + command_line->AppendSwitch(switches::kEnablePlatformApps); + command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); + } +}; + +IN_PROC_BROWSER_TEST_F(ExperimentalApiTest, Identity) { + IdentityInterceptor interceptor; + OAuth2MintTokenFlow::SetInterceptorForTests(&interceptor); + ASSERT_TRUE(RunExtensionTest("identity")) << message_; + ASSERT_TRUE(interceptor.get_auth_token_called()); +}; diff --git a/chrome/browser/extensions/app_notify_channel_ui.cc b/chrome/browser/extensions/app_notify_channel_ui.cc index e561bcb..ba8ffbb 100644 --- a/chrome/browser/extensions/app_notify_channel_ui.cc +++ b/chrome/browser/extensions/app_notify_channel_ui.cc @@ -10,7 +10,6 @@ #include "chrome/browser/sync/profile_sync_service.h" #include "chrome/browser/sync/profile_sync_service_factory.h" #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" -#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" #include "chrome/browser/ui/webui/signin/login_ui_service.h" #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" @@ -85,11 +84,18 @@ void AppNotifyChannelUIImpl::InfoBar::InfoBarDismissed() { } -AppNotifyChannelUIImpl::AppNotifyChannelUIImpl(Browser* browser, - TabContentsWrapper* wrapper, - const std::string& app_name) - : browser_(browser), wrapper_(wrapper), app_name_(app_name), - delegate_(NULL), observing_sync_(false), wizard_shown_to_user_(false) { +AppNotifyChannelUIImpl::AppNotifyChannelUIImpl( + Profile* profile, + TabContentsWrapper* wrapper, + const std::string& app_name, + AppNotifyChannelUI::UIType ui_type) + : profile_(profile->GetOriginalProfile()), + wrapper_(wrapper), + app_name_(app_name), + ui_type_(ui_type), + delegate_(NULL), + observing_sync_(false), + wizard_shown_to_user_(false) { } AppNotifyChannelUIImpl::~AppNotifyChannelUIImpl() { @@ -104,11 +110,16 @@ void AppNotifyChannelUIImpl::PromptSyncSetup( delegate_ = delegate; if (!ProfileSyncServiceFactory::GetInstance()->HasProfileSyncService( - browser_->profile()->GetOriginalProfile())) { + profile_)) { delegate_->OnSyncSetupResult(false); return; } + if (ui_type_ == NO_INFOBAR) { + OnInfoBarResult(true); + return; + } + InfoBarTabHelper* helper = wrapper_->infobar_tab_helper(); helper->AddInfoBar(new AppNotifyChannelUIImpl::InfoBar( this, helper, app_name_)); @@ -117,8 +128,7 @@ void AppNotifyChannelUIImpl::PromptSyncSetup( void AppNotifyChannelUIImpl::OnInfoBarResult(bool accepted) { if (accepted) { StartObservingSync(); - LoginUIServiceFactory::GetForProfile( - browser_->profile()->GetOriginalProfile())->ShowLoginUI(true); + LoginUIServiceFactory::GetForProfile(profile_)->ShowLoginUI(true); } else { delegate_->OnSyncSetupResult(false); } @@ -126,10 +136,9 @@ void AppNotifyChannelUIImpl::OnInfoBarResult(bool accepted) { void AppNotifyChannelUIImpl::OnStateChanged() { ProfileSyncService* sync_service = - ProfileSyncServiceFactory::GetInstance()->GetForProfile( - browser_->profile()->GetOriginalProfile()); - LoginUIService* login_service = LoginUIServiceFactory::GetForProfile( - browser_->profile()->GetOriginalProfile()); + ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); + LoginUIService* login_service = + LoginUIServiceFactory::GetForProfile(profile_); bool wizard_visible = (login_service->current_login_ui() != NULL); // ProfileSyncService raises OnStateChanged many times. Even multiple @@ -150,12 +159,12 @@ void AppNotifyChannelUIImpl::StartObservingSync() { CHECK(!observing_sync_); observing_sync_ = true; ProfileSyncServiceFactory::GetInstance()->GetForProfile( - browser_->profile()->GetOriginalProfile())->AddObserver(this); + profile_)->AddObserver(this); } void AppNotifyChannelUIImpl::StopObservingSync() { CHECK(observing_sync_); observing_sync_ = false; ProfileSyncServiceFactory::GetInstance()->GetForProfile( - browser_->profile()->GetOriginalProfile())->RemoveObserver(this); + profile_)->RemoveObserver(this); } diff --git a/chrome/browser/extensions/app_notify_channel_ui.h b/chrome/browser/extensions/app_notify_channel_ui.h index ed92263..e345af8 100644 --- a/chrome/browser/extensions/app_notify_channel_ui.h +++ b/chrome/browser/extensions/app_notify_channel_ui.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -11,7 +11,7 @@ #include "base/compiler_specific.h" #include "chrome/browser/sync/profile_sync_service_observer.h" -class Browser; +class Profile; class TabContentsWrapper; // An interface for prompting a user to sign in to sync so that we can create @@ -20,6 +20,15 @@ class AppNotifyChannelUI { public: virtual ~AppNotifyChannelUI() {} + // Used to customize the UI we show. + enum UIType { + // Do not prompt the user with an infobar. + NO_INFOBAR, + + // Ask if the app can show notifications. + NOTIFICATION_INFOBAR, + }; + class Delegate { public: // A callback for whether the user successfully set up sync or not. @@ -36,9 +45,10 @@ class AppNotifyChannelUI { class AppNotifyChannelUIImpl : public AppNotifyChannelUI, public ProfileSyncServiceObserver { public: - AppNotifyChannelUIImpl(Browser* browser, + AppNotifyChannelUIImpl(Profile* profile, TabContentsWrapper* wrapper, - const std::string& app_name); + const std::string& app_name, + AppNotifyChannelUI::UIType ui_type); virtual ~AppNotifyChannelUIImpl(); // AppNotifyChannelUI. @@ -60,9 +70,10 @@ class AppNotifyChannelUIImpl : public AppNotifyChannelUI, void StartObservingSync(); void StopObservingSync(); - Browser* browser_; + Profile* profile_; TabContentsWrapper* wrapper_; std::string app_name_; + AppNotifyChannelUI::UIType ui_type_; AppNotifyChannelUI::Delegate* delegate_; // Have we registered ourself as a ProfileSyncServiceObserver? diff --git a/chrome/browser/extensions/extension_function_registry.cc b/chrome/browser/extensions/extension_function_registry.cc index 3b1d025..a7f13fd 100644 --- a/chrome/browser/extensions/extension_function_registry.cc +++ b/chrome/browser/extensions/extension_function_registry.cc @@ -13,6 +13,7 @@ #include "chrome/browser/extensions/api/declarative/declarative_api.h" #include "chrome/browser/extensions/api/extension_action/extension_browser_actions_api.h" #include "chrome/browser/extensions/api/extension_action/extension_page_actions_api.h" +#include "chrome/browser/extensions/api/identity/identity_api.h" #include "chrome/browser/extensions/api/offscreen_tabs/offscreen_tabs_api.h" #include "chrome/browser/extensions/api/permissions/permissions_api.h" #include "chrome/browser/extensions/api/serial/serial_api.h" @@ -481,6 +482,9 @@ void ExtensionFunctionRegistry::ResetFunctions() { RegisterFunction<ToDataUrlOffscreenTabFunction>(); RegisterFunction<UpdateOffscreenTabFunction>(); + // Identity + RegisterFunction<extensions::GetAuthTokenFunction>(); + // Generated APIs extensions::api::GeneratedFunctionRegistry::RegisterAll(this); } diff --git a/chrome/browser/extensions/extension_tab_helper.cc b/chrome/browser/extensions/extension_tab_helper.cc index 5df7f6a..b45b0ad 100644 --- a/chrome/browser/extensions/extension_tab_helper.cc +++ b/chrome/browser/extensions/extension_tab_helper.cc @@ -207,7 +207,8 @@ void ExtensionTabHelper::OnGetAppNotifyChannel( } AppNotifyChannelUI* ui = new AppNotifyChannelUIImpl( - GetBrowser(), tab_contents_wrapper(), extension->name()); + profile, tab_contents_wrapper(), extension->name(), + AppNotifyChannelUI::NOTIFICATION_INFOBAR); scoped_refptr<AppNotifyChannelSetup> channel_setup( new AppNotifyChannelSetup(profile, |