1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
// 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_PUSH_MESSAGING_PUSH_MESSAGING_API_H__
#define CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_PUSH_MESSAGING_API_H__
#include <string>
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/gtest_prod_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/scoped_observer.h"
#include "chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher.h"
#include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler_delegate.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/browser/ui/webui/signin/login_ui_service.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/extension_registry_observer.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "google_apis/gaia/oauth2_token_service.h"
namespace content {
class BrowserContext;
}
namespace extensions {
class ExtensionRegistry;
class PushMessagingInvalidationMapper;
// Observes a single InvalidationHandler and generates onMessage events.
class PushMessagingEventRouter
: public PushMessagingInvalidationHandlerDelegate {
public:
explicit PushMessagingEventRouter(content::BrowserContext* context);
~PushMessagingEventRouter() override;
// For testing purposes.
void TriggerMessageForTest(const std::string& extension_id,
int subchannel,
const std::string& payload);
private:
// InvalidationHandlerDelegate implementation.
void OnMessage(const std::string& extension_id,
int subchannel,
const std::string& payload) override;
content::BrowserContext* const browser_context_;
DISALLOW_COPY_AND_ASSIGN(PushMessagingEventRouter);
};
class PushMessagingGetChannelIdFunction
: public ChromeAsyncExtensionFunction,
public ObfuscatedGaiaIdFetcher::Delegate,
public OAuth2TokenService::Observer,
public OAuth2TokenService::Consumer {
public:
PushMessagingGetChannelIdFunction();
protected:
~PushMessagingGetChannelIdFunction() override;
// ExtensionFunction:
bool RunAsync() override;
DECLARE_EXTENSION_FUNCTION("pushMessaging.getChannelId",
PUSHMESSAGING_GETCHANNELID)
private:
void ReportResult(const std::string& gaia_id,
const std::string& error_message);
void BuildAndSendResult(const std::string& gaia_id,
const std::string& error_message);
// Begin the async fetch of the Gaia ID.
void StartGaiaIdFetch(const std::string& access_token);
// Begin the async fetch of the access token for Gaia ID fetcher.
void StartAccessTokenFetch();
// OAuth2TokenService::Observer implementation.
void OnRefreshTokenAvailable(const std::string& account_id) override;
// OAuth2TokenService::Consumer implementation.
void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
const std::string& access_token,
const base::Time& expiration_time) override;
void OnGetTokenFailure(const OAuth2TokenService::Request* request,
const GoogleServiceAuthError& error) override;
// ObfuscatedGiaiaIdFetcher::Delegate implementation.
void OnObfuscatedGaiaIdFetchSuccess(const std::string& gaia_id) override;
void OnObfuscatedGaiaIdFetchFailure(
const GoogleServiceAuthError& error) override;
scoped_ptr<ObfuscatedGaiaIdFetcher> fetcher_;
bool interactive_;
scoped_ptr<OAuth2TokenService::Request> fetcher_access_token_request_;
DISALLOW_COPY_AND_ASSIGN(PushMessagingGetChannelIdFunction);
};
class PushMessagingAPI : public BrowserContextKeyedAPI,
public ExtensionRegistryObserver {
public:
explicit PushMessagingAPI(content::BrowserContext* context);
~PushMessagingAPI() override;
// Convenience method to get the PushMessagingAPI for a BrowserContext.
static PushMessagingAPI* Get(content::BrowserContext* context);
// KeyedService implementation.
void Shutdown() override;
// BrowserContextKeyedAPI implementation.
static BrowserContextKeyedAPIFactory<PushMessagingAPI>* GetFactoryInstance();
// For testing purposes.
PushMessagingEventRouter* GetEventRouterForTest() const {
return event_router_.get();
}
PushMessagingInvalidationMapper* GetMapperForTest() const {
return handler_.get();
}
void SetMapperForTest(scoped_ptr<PushMessagingInvalidationMapper> mapper);
private:
friend class BrowserContextKeyedAPIFactory<PushMessagingAPI>;
// BrowserContextKeyedAPI implementation.
static const char* service_name() {
return "PushMessagingAPI";
}
static const bool kServiceIsNULLWhileTesting = true;
// Overridden from ExtensionRegistryObserver.
void OnExtensionLoaded(content::BrowserContext* browser_context,
const Extension* extension) override;
void OnExtensionUnloaded(content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionInfo::Reason reason) override;
void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
const Extension* extension,
bool is_update,
bool from_ephemeral,
const std::string& old_name) override;
// Initialize |event_router_| and |handler_|.
bool InitEventRouterAndHandler();
// Created lazily when an app or extension with the push messaging permission
// is loaded.
scoped_ptr<PushMessagingEventRouter> event_router_;
scoped_ptr<PushMessagingInvalidationMapper> handler_;
ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
extension_registry_observer_;
content::BrowserContext* browser_context_;
DISALLOW_COPY_AND_ASSIGN(PushMessagingAPI);
};
template <>
void BrowserContextKeyedAPIFactory<
PushMessagingAPI>::DeclareFactoryDependencies();
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_PUSH_MESSAGING_API_H__
|