| OLD | NEW | 
|     1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |     1 // Copyright (c) 2012 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/android/provider/chrome_browser_provider.h" |     5 #include "chrome/browser/android/provider/chrome_browser_provider.h" | 
|     6  |     6  | 
|     7 #include <cmath> |     7 #include <cmath> | 
|     8 #include <list> |     8 #include <list> | 
|     9 #include <utility> |     9 #include <utility> | 
|    10  |    10  | 
|    11 #include "base/android/jni_android.h" |    11 #include "base/android/jni_android.h" | 
|    12 #include "base/android/jni_array.h" |    12 #include "base/android/jni_array.h" | 
|    13 #include "base/android/jni_string.h" |    13 #include "base/android/jni_string.h" | 
|    14 #include "base/logging.h" |    14 #include "base/logging.h" | 
|    15 #include "base/memory/ref_counted_memory.h" |    15 #include "base/memory/ref_counted_memory.h" | 
|    16 #include "base/strings/utf_string_conversions.h" |    16 #include "base/strings/utf_string_conversions.h" | 
|    17 #include "base/task/cancelable_task_tracker.h" |    17 #include "base/task/cancelable_task_tracker.h" | 
|    18 #include "base/time/time.h" |    18 #include "base/time/time.h" | 
|    19 #include "chrome/browser/android/provider/blocking_ui_thread_async_request.h" |    19 #include "chrome/browser/android/provider/blocking_ui_thread_async_request.h" | 
|    20 #include "chrome/browser/android/provider/bookmark_model_observer_task.h" |    20 #include "chrome/browser/android/provider/bookmark_model_observer_task.h" | 
|    21 #include "chrome/browser/android/provider/run_on_ui_thread_blocking.h" |    21 #include "chrome/browser/android/provider/run_on_ui_thread_blocking.h" | 
|    22 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |    22 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 
|    23 #include "chrome/browser/bookmarks/chrome_bookmark_client.h" |    23 #include "chrome/browser/bookmarks/managed_bookmark_service_factory.h" | 
|    24 #include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h" |  | 
|    25 #include "chrome/browser/browser_process.h" |    24 #include "chrome/browser/browser_process.h" | 
|    26 #include "chrome/browser/favicon/favicon_service_factory.h" |    25 #include "chrome/browser/favicon/favicon_service_factory.h" | 
|    27 #include "chrome/browser/history/android/sqlite_cursor.h" |    26 #include "chrome/browser/history/android/sqlite_cursor.h" | 
|    28 #include "chrome/browser/history/history_service_factory.h" |    27 #include "chrome/browser/history/history_service_factory.h" | 
|    29 #include "chrome/browser/history/top_sites_factory.h" |    28 #include "chrome/browser/history/top_sites_factory.h" | 
|    30 #include "chrome/browser/profiles/profile.h" |    29 #include "chrome/browser/profiles/profile.h" | 
|    31 #include "chrome/browser/profiles/profile_manager.h" |    30 #include "chrome/browser/profiles/profile_manager.h" | 
|    32 #include "chrome/browser/search_engines/template_url_service_factory.h" |    31 #include "chrome/browser/search_engines/template_url_service_factory.h" | 
|    33 #include "components/bookmarks/browser/bookmark_model.h" |    32 #include "components/bookmarks/browser/bookmark_model.h" | 
|    34 #include "components/bookmarks/browser/bookmark_utils.h" |    33 #include "components/bookmarks/browser/bookmark_utils.h" | 
 |    34 #include "components/bookmarks/managed/managed_bookmark_service.h" | 
|    35 #include "components/favicon/core/favicon_service.h" |    35 #include "components/favicon/core/favicon_service.h" | 
|    36 #include "components/history/core/browser/android/android_history_types.h" |    36 #include "components/history/core/browser/android/android_history_types.h" | 
|    37 #include "components/history/core/browser/history_service.h" |    37 #include "components/history/core/browser/history_service.h" | 
|    38 #include "components/history/core/browser/top_sites.h" |    38 #include "components/history/core/browser/top_sites.h" | 
|    39 #include "components/search_engines/template_url.h" |    39 #include "components/search_engines/template_url.h" | 
|    40 #include "components/search_engines/template_url_service.h" |    40 #include "components/search_engines/template_url_service.h" | 
|    41 #include "content/public/browser/browser_thread.h" |    41 #include "content/public/browser/browser_thread.h" | 
|    42 #include "jni/ChromeBrowserProvider_jni.h" |    42 #include "jni/ChromeBrowserProvider_jni.h" | 
|    43 #include "ui/base/layout.h" |    43 #include "ui/base/layout.h" | 
|    44 #include "ui/base/resource/resource_bundle.h" |    44 #include "ui/base/resource/resource_bundle.h" | 
|    45 #include "ui/gfx/favicon_size.h" |    45 #include "ui/gfx/favicon_size.h" | 
|    46  |    46  | 
|    47 using base::android::AttachCurrentThread; |    47 using base::android::AttachCurrentThread; | 
|    48 using base::android::CheckException; |    48 using base::android::CheckException; | 
|    49 using base::android::ClearException; |    49 using base::android::ClearException; | 
|    50 using base::android::ConvertJavaStringToUTF16; |    50 using base::android::ConvertJavaStringToUTF16; | 
|    51 using base::android::ConvertJavaStringToUTF8; |    51 using base::android::ConvertJavaStringToUTF8; | 
|    52 using base::android::ConvertUTF8ToJavaString; |    52 using base::android::ConvertUTF8ToJavaString; | 
|    53 using base::android::ConvertUTF16ToJavaString; |    53 using base::android::ConvertUTF16ToJavaString; | 
|    54 using base::android::GetClass; |    54 using base::android::GetClass; | 
|    55 using base::android::MethodID; |    55 using base::android::MethodID; | 
|    56 using base::android::JavaRef; |    56 using base::android::JavaRef; | 
|    57 using base::android::ScopedJavaGlobalRef; |    57 using base::android::ScopedJavaGlobalRef; | 
|    58 using base::android::ScopedJavaLocalRef; |    58 using base::android::ScopedJavaLocalRef; | 
|    59 using bookmarks::BookmarkModel; |    59 using bookmarks::BookmarkModel; | 
|    60 using bookmarks::BookmarkNode; |    60 using bookmarks::BookmarkNode; | 
 |    61 using bookmarks::ManagedBookmarkService; | 
|    61 using content::BrowserThread; |    62 using content::BrowserThread; | 
|    62  |    63  | 
|    63 // After refactoring the following class hierarchy has been created in order |    64 // After refactoring the following class hierarchy has been created in order | 
|    64 // to avoid repeating code again for the same basic kind of tasks, to enforce |    65 // to avoid repeating code again for the same basic kind of tasks, to enforce | 
|    65 // the correct thread usage and to prevent known race conditions and deadlocks. |    66 // the correct thread usage and to prevent known race conditions and deadlocks. | 
|    66 // |    67 // | 
|    67 // - RunOnUIThreadBlocking: auxiliary class to run methods in the UI thread |    68 // - RunOnUIThreadBlocking: auxiliary class to run methods in the UI thread | 
|    68 //   blocking the current one until finished. Because of the provider threading |    69 //   blocking the current one until finished. Because of the provider threading | 
|    69 //   expectations this cannot be used from the UI thread. |    70 //   expectations this cannot be used from the UI thread. | 
|    70 // |    71 // | 
| (...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   473                                    parent->id(), result); |   474                                    parent->id(), result); | 
|   474   } |   475   } | 
|   475  |   476  | 
|   476  private: |   477  private: | 
|   477   DISALLOW_COPY_AND_ASSIGN(CreateBookmarksFolderOnceTask); |   478   DISALLOW_COPY_AND_ASSIGN(CreateBookmarksFolderOnceTask); | 
|   478 }; |   479 }; | 
|   479  |   480  | 
|   480 // Creates a Java BookmarkNode object for a node given its id. |   481 // Creates a Java BookmarkNode object for a node given its id. | 
|   481 class GetEditableBookmarkFoldersTask : public BookmarkModelTask { |   482 class GetEditableBookmarkFoldersTask : public BookmarkModelTask { | 
|   482  public: |   483  public: | 
|   483   GetEditableBookmarkFoldersTask(ChromeBookmarkClient* client, |   484   GetEditableBookmarkFoldersTask( | 
|   484                                  BookmarkModel* model) |   485       ManagedBookmarkService* managed_bookmark_service, | 
|   485       : BookmarkModelTask(model), client_(client) {} |   486       BookmarkModel* model) | 
 |   487       : BookmarkModelTask(model), | 
 |   488         managed_bookmark_service_(managed_bookmark_service) {} | 
|   486  |   489  | 
|   487   void Run(ScopedJavaGlobalRef<jobject>* jroot) { |   490   void Run(ScopedJavaGlobalRef<jobject>* jroot) { | 
|   488     RunOnUIThreadBlocking::Run( |   491     RunOnUIThreadBlocking::Run( | 
|   489         base::Bind(&GetEditableBookmarkFoldersTask::RunOnUIThread, |   492         base::Bind(&GetEditableBookmarkFoldersTask::RunOnUIThread, | 
|   490                    client_, model(), jroot)); |   493                    managed_bookmark_service_, model(), jroot)); | 
|   491   } |   494   } | 
|   492  |   495  | 
|   493   static void RunOnUIThread(ChromeBookmarkClient* client, |   496   static void RunOnUIThread(ManagedBookmarkService* managed, | 
|   494                             BookmarkModel* model, |   497                             BookmarkModel* model, | 
|   495                             ScopedJavaGlobalRef<jobject>* jroot) { |   498                             ScopedJavaGlobalRef<jobject>* jroot) { | 
|   496     DCHECK_CURRENTLY_ON(BrowserThread::UI); |   499     DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
|   497     const BookmarkNode* root = model->root_node(); |   500     const BookmarkNode* root = model->root_node(); | 
|   498     if (!root || !root->is_folder()) |   501     if (!root || !root->is_folder()) | 
|   499       return; |   502       return; | 
|   500  |   503  | 
|   501     // The iterative approach is not possible because ScopedGlobalJavaRefs |   504     // The iterative approach is not possible because ScopedGlobalJavaRefs | 
|   502     // cannot be copy-constructed, and therefore not used in STL containers. |   505     // cannot be copy-constructed, and therefore not used in STL containers. | 
|   503     ConvertFolderSubtree(client, AttachCurrentThread(), root, |   506     ConvertFolderSubtree(managed, AttachCurrentThread(), root, | 
|   504                          ScopedJavaLocalRef<jobject>(), jroot); |   507                          ScopedJavaLocalRef<jobject>(), jroot); | 
|   505   } |   508   } | 
|   506  |   509  | 
|   507  private: |   510  private: | 
|   508   static void ConvertFolderSubtree(ChromeBookmarkClient* client, |   511   static void ConvertFolderSubtree(ManagedBookmarkService* managed, | 
|   509                                    JNIEnv* env, |   512                                    JNIEnv* env, | 
|   510                                    const BookmarkNode* node, |   513                                    const BookmarkNode* node, | 
|   511                                    const JavaRef<jobject>& parent_folder, |   514                                    const JavaRef<jobject>& parent_folder, | 
|   512                                    ScopedJavaGlobalRef<jobject>* jfolder) { |   515                                    ScopedJavaGlobalRef<jobject>* jfolder) { | 
|   513     DCHECK(node); |   516     DCHECK(node); | 
|   514     DCHECK(node->is_folder()); |   517     DCHECK(node->is_folder()); | 
|   515     DCHECK(jfolder); |   518     DCHECK(jfolder); | 
|   516  |   519  | 
|   517     // Global refs should be used here for thread-safety reasons as this task |   520     // Global refs should be used here for thread-safety reasons as this task | 
|   518     // might be invoked from a thread other than UI. All refs are scoped. |   521     // might be invoked from a thread other than UI. All refs are scoped. | 
|   519     ConvertBookmarkNode(node, parent_folder, jfolder); |   522     ConvertBookmarkNode(node, parent_folder, jfolder); | 
|   520  |   523  | 
|   521     for (int i = 0; i < node->child_count(); ++i) { |   524     for (int i = 0; i < node->child_count(); ++i) { | 
|   522       const BookmarkNode* child = node->GetChild(i); |   525       const BookmarkNode* child = node->GetChild(i); | 
|   523       if (child->is_folder() && client->CanBeEditedByUser(child)) { |   526       if (child->is_folder() && managed->CanBeEditedByUser(child)) { | 
|   524         ScopedJavaGlobalRef<jobject> jchild; |   527         ScopedJavaGlobalRef<jobject> jchild; | 
|   525         ConvertFolderSubtree(client, env, child, *jfolder, &jchild); |   528         ConvertFolderSubtree(managed, env, child, *jfolder, &jchild); | 
|   526  |   529  | 
|   527         Java_BookmarkNode_addChild(env, jfolder->obj(), jchild.obj()); |   530         Java_BookmarkNode_addChild(env, jfolder->obj(), jchild.obj()); | 
|   528         if (ClearException(env)) { |   531         if (ClearException(env)) { | 
|   529           LOG(WARNING) << "Java exception while adding child node."; |   532           LOG(WARNING) << "Java exception while adding child node."; | 
|   530           return; |   533           return; | 
|   531         } |   534         } | 
|   532       } |   535       } | 
|   533     } |   536     } | 
|   534   } |   537   } | 
|   535  |   538  | 
|   536   ChromeBookmarkClient* client_; |   539   ManagedBookmarkService* managed_bookmark_service_; | 
|   537  |   540  | 
|   538   DISALLOW_COPY_AND_ASSIGN(GetEditableBookmarkFoldersTask); |   541   DISALLOW_COPY_AND_ASSIGN(GetEditableBookmarkFoldersTask); | 
|   539 }; |   542 }; | 
|   540  |   543  | 
|   541 // Creates a Java BookmarkNode object for a node given its id. |   544 // Creates a Java BookmarkNode object for a node given its id. | 
|   542 class GetBookmarkNodeTask : public BookmarkModelTask { |   545 class GetBookmarkNodeTask : public BookmarkModelTask { | 
|   543  public: |   546  public: | 
|   544   explicit GetBookmarkNodeTask(BookmarkModel* model) |   547   explicit GetBookmarkNodeTask(BookmarkModel* model) | 
|   545       : BookmarkModelTask(model) { |   548       : BookmarkModelTask(model) { | 
|   546   } |   549   } | 
| (...skipping 950 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1497     return kInvalidBookmarkId; |  1500     return kInvalidBookmarkId; | 
|  1498  |  1501  | 
|  1499   CreateBookmarksFolderOnceTask task(bookmark_model_); |  1502   CreateBookmarksFolderOnceTask task(bookmark_model_); | 
|  1500   return task.Run(title, parent_id); |  1503   return task.Run(title, parent_id); | 
|  1501 } |  1504 } | 
|  1502  |  1505  | 
|  1503 ScopedJavaLocalRef<jobject> ChromeBrowserProvider::GetEditableBookmarkFolders( |  1506 ScopedJavaLocalRef<jobject> ChromeBrowserProvider::GetEditableBookmarkFolders( | 
|  1504     JNIEnv* env, |  1507     JNIEnv* env, | 
|  1505     jobject obj) { |  1508     jobject obj) { | 
|  1506   ScopedJavaGlobalRef<jobject> jroot; |  1509   ScopedJavaGlobalRef<jobject> jroot; | 
|  1507   ChromeBookmarkClient* client = |  1510   ManagedBookmarkService* managed = | 
|  1508       ChromeBookmarkClientFactory::GetForProfile(profile_); |  1511       ManagedBookmarkServiceFactory::GetForProfile(profile_); | 
|  1509   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_); |  1512   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_); | 
|  1510   GetEditableBookmarkFoldersTask task(client, model); |  1513   GetEditableBookmarkFoldersTask task(managed, model); | 
|  1511   task.Run(&jroot); |  1514   task.Run(&jroot); | 
|  1512   return ScopedJavaLocalRef<jobject>(jroot); |  1515   return ScopedJavaLocalRef<jobject>(jroot); | 
|  1513 } |  1516 } | 
|  1514  |  1517  | 
|  1515 void ChromeBrowserProvider::RemoveAllUserBookmarks(JNIEnv* env, jobject obj) { |  1518 void ChromeBrowserProvider::RemoveAllUserBookmarks(JNIEnv* env, jobject obj) { | 
|  1516   LOG(ERROR) << "begin ChromeBrowserProvider::RemoveAllUserBookmarks"; |  1519   LOG(ERROR) << "begin ChromeBrowserProvider::RemoveAllUserBookmarks"; | 
|  1517   RemoveAllUserBookmarksTask task(bookmark_model_); |  1520   RemoveAllUserBookmarksTask task(bookmark_model_); | 
|  1518   task.Run(); |  1521   task.Run(); | 
|  1519   LOG(ERROR) << "end ChromeBrowserProvider::RemoveAllUserBookmarks"; |  1522   LOG(ERROR) << "end ChromeBrowserProvider::RemoveAllUserBookmarks"; | 
|  1520 } |  1523 } | 
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1641   ScopedJavaLocalRef<jobject> obj = weak_java_provider_.get(env); |  1644   ScopedJavaLocalRef<jobject> obj = weak_java_provider_.get(env); | 
|  1642   if (obj.is_null()) |  1645   if (obj.is_null()) | 
|  1643     return; |  1646     return; | 
|  1644   Java_ChromeBrowserProvider_onSearchTermChanged(env, obj.obj()); |  1647   Java_ChromeBrowserProvider_onSearchTermChanged(env, obj.obj()); | 
|  1645 } |  1648 } | 
|  1646  |  1649  | 
|  1647 void ChromeBrowserProvider::OnKeywordSearchTermDeleted( |  1650 void ChromeBrowserProvider::OnKeywordSearchTermDeleted( | 
|  1648     history::HistoryService* history_service, |  1651     history::HistoryService* history_service, | 
|  1649     history::URLID url_id) { |  1652     history::URLID url_id) { | 
|  1650 } |  1653 } | 
| OLD | NEW |