| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "chrome/browser/dom_ui/new_tab_page_sync_handler.h" | 5 #include "chrome/browser/dom_ui/new_tab_page_sync_handler.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 case sync_ui_util::PRE_SYNCED: | 91 case sync_ui_util::PRE_SYNCED: |
| 92 case sync_ui_util::SYNCED: | 92 case sync_ui_util::SYNCED: |
| 93 default: | 93 default: |
| 94 return HIDE; | 94 return HIDE; |
| 95 } | 95 } |
| 96 } | 96 } |
| 97 | 97 |
| 98 DOMMessageHandler* NewTabPageSyncHandler::Attach(DOMUI* dom_ui) { | 98 DOMMessageHandler* NewTabPageSyncHandler::Attach(DOMUI* dom_ui) { |
| 99 sync_service_ = dom_ui->GetProfile()->GetProfileSyncService(); | 99 sync_service_ = dom_ui->GetProfile()->GetProfileSyncService(); |
| 100 DCHECK(sync_service_); // This shouldn't get called by an incognito NTP. | 100 DCHECK(sync_service_); // This shouldn't get called by an incognito NTP. |
| 101 DCHECK(!sync_service_->IsManaged()); // And neither if sync is managed. |
| 101 sync_service_->AddObserver(this); | 102 sync_service_->AddObserver(this); |
| 102 return DOMMessageHandler::Attach(dom_ui); | 103 return DOMMessageHandler::Attach(dom_ui); |
| 103 } | 104 } |
| 104 | 105 |
| 105 void NewTabPageSyncHandler::RegisterMessages() { | 106 void NewTabPageSyncHandler::RegisterMessages() { |
| 106 dom_ui_->RegisterMessageCallback("GetSyncMessage", | 107 dom_ui_->RegisterMessageCallback("GetSyncMessage", |
| 107 NewCallback(this, &NewTabPageSyncHandler::HandleGetSyncMessage)); | 108 NewCallback(this, &NewTabPageSyncHandler::HandleGetSyncMessage)); |
| 108 dom_ui_->RegisterMessageCallback("SyncLinkClicked", | 109 dom_ui_->RegisterMessageCallback("SyncLinkClicked", |
| 109 NewCallback(this, &NewTabPageSyncHandler::HandleSyncLinkClicked)); | 110 NewCallback(this, &NewTabPageSyncHandler::HandleSyncLinkClicked)); |
| 110 } | 111 } |
| 111 | 112 |
| 112 void NewTabPageSyncHandler::HandleGetSyncMessage(const Value* value) { | 113 void NewTabPageSyncHandler::HandleGetSyncMessage(const Value* value) { |
| 113 waiting_for_initial_page_load_ = false; | 114 waiting_for_initial_page_load_ = false; |
| 114 BuildAndSendSyncStatus(); | 115 BuildAndSendSyncStatus(); |
| 115 } | 116 } |
| 116 | 117 |
| 117 void NewTabPageSyncHandler::HideSyncStatusSection() { | 118 void NewTabPageSyncHandler::HideSyncStatusSection() { |
| 118 SendSyncMessageToPage(HIDE, std::string(), std::string()); | 119 SendSyncMessageToPage(HIDE, std::string(), std::string()); |
| 119 } | 120 } |
| 120 | 121 |
| 121 void NewTabPageSyncHandler::BuildAndSendSyncStatus() { | 122 void NewTabPageSyncHandler::BuildAndSendSyncStatus() { |
| 122 DCHECK(!waiting_for_initial_page_load_); | 123 DCHECK(!waiting_for_initial_page_load_); |
| 123 | 124 |
| 124 // Hide the sync status section if sync is disabled entirely. | 125 // Hide the sync status section if sync is managed or disabled entirely. |
| 125 if (!sync_service_) { | 126 if (!sync_service_ || sync_service_->IsManaged()) { |
| 126 HideSyncStatusSection(); | 127 HideSyncStatusSection(); |
| 127 return; | 128 return; |
| 128 } | 129 } |
| 129 | 130 |
| 130 // Don't show sync status if setup is not complete. | 131 // Don't show sync status if setup is not complete. |
| 131 if (!sync_service_->HasSyncSetupCompleted()) { | 132 if (!sync_service_->HasSyncSetupCompleted()) { |
| 132 return; | 133 return; |
| 133 } | 134 } |
| 134 | 135 |
| 135 // Once sync has been enabled, the supported "sync statuses" for the NNTP | 136 // Once sync has been enabled, the supported "sync statuses" for the NNTP |
| 136 // from the user's perspective are: | 137 // from the user's perspective are: |
| 137 // | 138 // |
| 138 // "Sync error", when we can't authenticate or establish a connection with | 139 // "Sync error", when we can't authenticate or establish a connection with |
| 139 // the sync server (appropriate information appended to | 140 // the sync server (appropriate information appended to |
| 140 // message). | 141 // message). |
| 141 string16 status_msg; | 142 string16 status_msg; |
| 142 string16 link_text; | 143 string16 link_text; |
| 143 sync_ui_util::MessageType type = | 144 sync_ui_util::MessageType type = |
| 144 sync_ui_util::GetStatusLabels(sync_service_, &status_msg, &link_text); | 145 sync_ui_util::GetStatusLabels(sync_service_, &status_msg, &link_text); |
| 145 SendSyncMessageToPage(FromSyncStatusMessageType(type), | 146 SendSyncMessageToPage(FromSyncStatusMessageType(type), |
| 146 UTF16ToUTF8(status_msg), UTF16ToUTF8(link_text)); | 147 UTF16ToUTF8(status_msg), UTF16ToUTF8(link_text)); |
| 147 } | 148 } |
| 148 | 149 |
| 149 void NewTabPageSyncHandler::HandleSyncLinkClicked(const Value* value) { | 150 void NewTabPageSyncHandler::HandleSyncLinkClicked(const Value* value) { |
| 150 DCHECK(!waiting_for_initial_page_load_); | 151 DCHECK(!waiting_for_initial_page_load_); |
| 151 DCHECK(sync_service_); | 152 DCHECK(sync_service_); |
| 153 if (!sync_service_->IsSyncEnabled()) |
| 154 return; |
| 152 if (sync_service_->HasSyncSetupCompleted()) { | 155 if (sync_service_->HasSyncSetupCompleted()) { |
| 153 if (sync_service_->GetAuthError().state() == | 156 if (sync_service_->GetAuthError().state() == |
| 154 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS || | 157 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS || |
| 155 sync_service_->GetAuthError().state() == | 158 sync_service_->GetAuthError().state() == |
| 156 GoogleServiceAuthError::CAPTCHA_REQUIRED) { | 159 GoogleServiceAuthError::CAPTCHA_REQUIRED) { |
| 157 sync_service_->ShowLoginDialog(); | 160 sync_service_->ShowLoginDialog(); |
| 158 return; | 161 return; |
| 159 } | 162 } |
| 160 DictionaryValue value; | 163 DictionaryValue value; |
| 161 value.SetString(L"syncEnabledMessage", | 164 value.SetString(L"syncEnabledMessage", |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 if (linkurl.empty()) { | 211 if (linkurl.empty()) { |
| 209 value.SetBoolean(L"linkurlisset", false); | 212 value.SetBoolean(L"linkurlisset", false); |
| 210 } else { | 213 } else { |
| 211 value.SetBoolean(L"linkurlisset", true); | 214 value.SetBoolean(L"linkurlisset", true); |
| 212 value.SetString(L"linkurl", linkurl); | 215 value.SetString(L"linkurl", linkurl); |
| 213 } | 216 } |
| 214 } | 217 } |
| 215 } | 218 } |
| 216 dom_ui_->CallJavascriptFunction(L"syncMessageChanged", value); | 219 dom_ui_->CallJavascriptFunction(L"syncMessageChanged", value); |
| 217 } | 220 } |
| OLD | NEW |