| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_APP_NOTIFY_CHANNEL_SETUP_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_APP_NOTIFY_CHANNEL_SETUP_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_APP_NOTIFY_CHANNEL_SETUP_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_APP_NOTIFY_CHANNEL_SETUP_H_ |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "chrome/browser/extensions/app_notify_channel_ui.h" | 11 #include "chrome/browser/extensions/app_notify_channel_ui.h" |
| 12 #include "content/public/browser/notification_observer.h" |
| 13 #include "content/public/browser/notification_registrar.h" |
| 12 #include "content/public/common/url_fetcher.h" | 14 #include "content/public/common/url_fetcher.h" |
| 13 #include "content/public/common/url_fetcher_delegate.h" | 15 #include "content/public/common/url_fetcher_delegate.h" |
| 14 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 15 | 17 |
| 16 class AppNotifyChannelSetupTest; | 18 class AppNotifyChannelSetupTest; |
| 17 class Profile; | 19 class Profile; |
| 18 | 20 |
| 21 namespace content { |
| 22 class NotificationDetails; |
| 23 } |
| 24 |
| 19 // This class uses the browser login credentials to setup app notifications | 25 // This class uses the browser login credentials to setup app notifications |
| 20 // for a given app. | 26 // for a given app. |
| 21 // | 27 // |
| 22 // Performs the following steps when Start() is called: | 28 // Performs the following steps when Start() is called: |
| 23 // 1. If the user is not logged in, prompt the user to login. | 29 // 1. If the user is not logged in, prompt the user to login. |
| 24 // 2. OAuth2: Record a notifications grant for the user and the given app. | 30 // 2. OAuth2: Record a notifications grant for the user and the given app. |
| 25 // 3. Get notifications channel id for the current user. | 31 // 3. Get notifications channel id for the current user. |
| 26 // 4. Call the delegate passed in to the constructor with the results of | 32 // 4. Call the delegate passed in to the constructor with the results of |
| 27 // the above steps. | 33 // the above steps. |
| 28 class AppNotifyChannelSetup | 34 class AppNotifyChannelSetup |
| 29 : public content::URLFetcherDelegate, | 35 : public content::URLFetcherDelegate, |
| 36 public content::NotificationObserver, |
| 30 public AppNotifyChannelUI::Delegate, | 37 public AppNotifyChannelUI::Delegate, |
| 31 public base::RefCountedThreadSafe<AppNotifyChannelSetup> { | 38 public base::RefCountedThreadSafe<AppNotifyChannelSetup> { |
| 32 public: | 39 public: |
| 33 class Delegate { | 40 class Delegate { |
| 34 public: | 41 public: |
| 35 // If successful, |channel_id| will be non-empty. On failure, |channel_id| | 42 // If successful, |channel_id| will be non-empty. On failure, |channel_id| |
| 36 // will be empty and |error| will contain an error to report to the JS | 43 // will be empty and |error| will contain an error to report to the JS |
| 37 // callback. | 44 // callback. |
| 38 virtual void AppNotifyChannelSetupComplete(const std::string& channel_id, | 45 virtual void AppNotifyChannelSetupComplete(const std::string& channel_id, |
| 39 const std::string& error, | 46 const std::string& error, |
| 40 int return_route_id, | 47 int return_route_id, |
| 41 int callback_id) = 0; | 48 int callback_id) = 0; |
| 42 }; | 49 }; |
| 43 | 50 |
| 44 // Ownership of |ui| is transferred to this object. | 51 // Ownership of |ui| is transferred to this object. |
| 45 AppNotifyChannelSetup(Profile* profile, | 52 AppNotifyChannelSetup(Profile* profile, |
| 46 const std::string& extension_id, | 53 const std::string& extension_id, |
| 47 const std::string& client_id, | 54 const std::string& client_id, |
| 48 const GURL& requestor_url, | 55 const GURL& requestor_url, |
| 49 int return_route_id, | 56 int return_route_id, |
| 50 int callback_id, | 57 int callback_id, |
| 51 AppNotifyChannelUI* ui, | 58 AppNotifyChannelUI* ui, |
| 52 base::WeakPtr<Delegate> delegate); | 59 base::WeakPtr<Delegate> delegate); |
| 53 | 60 |
| 61 AppNotifyChannelUI* ui() { return ui_.get(); } |
| 62 |
| 54 // This begins the process of fetching the channel id using the browser login | 63 // This begins the process of fetching the channel id using the browser login |
| 55 // credentials (or using |ui_| to prompt for login if needed). | 64 // credentials (or using |ui_| to prompt for login if needed). |
| 56 void Start(); | 65 void Start(); |
| 57 | 66 |
| 67 // Implementing content::NotificationObserver interface. |
| 68 virtual void Observe(int type, |
| 69 const content::NotificationSource& source, |
| 70 const content::NotificationDetails& details) OVERRIDE; |
| 58 protected: | 71 protected: |
| 59 // content::URLFetcherDelegate. | 72 // content::URLFetcherDelegate. |
| 60 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; | 73 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 61 | 74 |
| 62 // AppNotifyChannelUI::Delegate. | 75 // AppNotifyChannelUI::Delegate. |
| 63 virtual void OnSyncSetupResult(bool enabled) OVERRIDE; | 76 virtual void OnSyncSetupResult(bool enabled) OVERRIDE; |
| 64 | 77 |
| 65 private: | 78 private: |
| 66 enum State { | 79 enum State { |
| 67 INITIAL, | 80 INITIAL, |
| 68 LOGIN_STARTED, | 81 LOGIN_STARTED, |
| 69 LOGIN_DONE, | 82 LOGIN_DONE, |
| 83 FETCH_TOKEN_STARTED, |
| 84 FETCH_TOKEN_DONE, |
| 70 RECORD_GRANT_STARTED, | 85 RECORD_GRANT_STARTED, |
| 71 RECORD_GRANT_DONE, | 86 RECORD_GRANT_DONE, |
| 72 CHANNEL_ID_SETUP_STARTED, | 87 CHANNEL_ID_SETUP_STARTED, |
| 73 CHANNEL_ID_SETUP_DONE, | 88 CHANNEL_ID_SETUP_DONE, |
| 74 ERROR_STATE | 89 ERROR_STATE |
| 75 }; | 90 }; |
| 76 | 91 |
| 77 friend class base::RefCountedThreadSafe<AppNotifyChannelSetup>; | 92 friend class base::RefCountedThreadSafe<AppNotifyChannelSetup>; |
| 78 friend class AppNotifyChannelSetupTest; | 93 friend class AppNotifyChannelSetupTest; |
| 79 | 94 |
| 80 virtual ~AppNotifyChannelSetup(); | 95 virtual ~AppNotifyChannelSetup(); |
| 81 | 96 |
| 82 // Creates an instance of URLFetcher that does not send or save cookies. | 97 // Creates an instance of URLFetcher that does not send or save cookies. |
| 83 // The URLFether's method will be GET if body is empty, POST otherwise. | 98 // The URLFether's method will be GET if body is empty, POST otherwise. |
| 84 // Caller owns the returned instance. | 99 // Caller owns the returned instance. |
| 85 content::URLFetcher* CreateURLFetcher( | 100 content::URLFetcher* CreateURLFetcher( |
| 86 const GURL& url, const std::string& body, const std::string& auth_token); | 101 const GURL& url, const std::string& body, const std::string& auth_token); |
| 102 void BeginLogin(); |
| 103 void EndLogin(bool success); |
| 104 void BeginFetchTokens(); |
| 105 void EndFetchTokens(bool success); |
| 87 void BeginRecordGrant(); | 106 void BeginRecordGrant(); |
| 88 void EndRecordGrant(const content::URLFetcher* source); | 107 void EndRecordGrant(const content::URLFetcher* source); |
| 89 void BeginGetChannelId(); | 108 void BeginGetChannelId(); |
| 90 void EndGetChannelId(const content::URLFetcher* source); | 109 void EndGetChannelId(const content::URLFetcher* source); |
| 91 | 110 |
| 92 void ReportResult(const std::string& channel_id, const std::string& error); | 111 void ReportResult(const std::string& channel_id, const std::string& error); |
| 93 | 112 |
| 94 static GURL GetCWSChannelServiceURL(); | 113 static GURL GetCWSChannelServiceURL(); |
| 95 static GURL GetOAuth2IssueTokenURL(); | 114 static GURL GetOAuth2IssueTokenURL(); |
| 96 static std::string MakeOAuth2IssueTokenBody( | 115 static std::string MakeOAuth2IssueTokenBody( |
| 97 const std::string& oauth_client_id, const std::string& extension_id); | 116 const std::string& oauth_client_id, const std::string& extension_id); |
| 98 static std::string MakeAuthorizationHeader(const std::string& auth_token); | 117 static std::string MakeAuthorizationHeader(const std::string& auth_token); |
| 99 std::string GetLSOAuthToken(); | 118 std::string GetLSOAuthToken(); |
| 100 std::string GetCWSAuthToken(); | 119 std::string GetCWSAuthToken(); |
| 101 static bool ParseCWSChannelServiceResponse( | 120 static bool ParseCWSChannelServiceResponse( |
| 102 const std::string& data, std::string* result); | 121 const std::string& data, std::string* result); |
| 122 static bool IsGaiaServiceRelevant(const std::string& service); |
| 123 // Checks if the user needs to be prompted for login. |
| 124 bool ShouldPromptForLogin() const; |
| 125 // Checks if we need to fetch auth tokens for services we care about. |
| 126 virtual bool ShouldFetchServiceTokens() const; |
| 127 void RegisterForTokenServiceNotifications(); |
| 128 void UnregisterForTokenServiceNotifications(); |
| 103 | 129 |
| 104 Profile* profile_; | 130 Profile* profile_; |
| 131 content::NotificationRegistrar registrar_; |
| 105 std::string extension_id_; | 132 std::string extension_id_; |
| 106 std::string client_id_; | 133 std::string client_id_; |
| 107 GURL requestor_url_; | 134 GURL requestor_url_; |
| 108 int return_route_id_; | 135 int return_route_id_; |
| 109 int callback_id_; | 136 int callback_id_; |
| 110 base::WeakPtr<Delegate> delegate_; | 137 base::WeakPtr<Delegate> delegate_; |
| 111 scoped_ptr<content::URLFetcher> url_fetcher_; | 138 scoped_ptr<content::URLFetcher> url_fetcher_; |
| 112 scoped_ptr<AppNotifyChannelUI> ui_; | 139 scoped_ptr<AppNotifyChannelUI> ui_; |
| 113 State state_; | 140 State state_; |
| 141 int fetch_token_success_count_; |
| 142 int fetch_token_fail_count_; |
| 114 | 143 |
| 115 DISALLOW_COPY_AND_ASSIGN(AppNotifyChannelSetup); | 144 DISALLOW_COPY_AND_ASSIGN(AppNotifyChannelSetup); |
| 116 }; | 145 }; |
| 117 | 146 |
| 118 #endif // CHROME_BROWSER_EXTENSIONS_APP_NOTIFY_CHANNEL_SETUP_H_ | 147 #endif // CHROME_BROWSER_EXTENSIONS_APP_NOTIFY_CHANNEL_SETUP_H_ |
| OLD | NEW |